%

clear;
lon=[-180:2.5:177.5]';
lat=[-30:2.5:30];

fid=fopen('olr.7906.fil.ER.dat','r');
%fid=fopen('olrfilernew7900jjas_1030.dat','r');
siz=144*25*10226;v=fread(fid,siz,'float');fclose(fid);
v=reshape(v,144,25,10226);
ind_olr=v(:,:,:);

% selection des mois d ete
% on selectionne les mois d ete (attention aux annees bixestiles!)

siz1=31+28+31+30+31+1;
siz1bis=31+28+31+30+31+30+31+31+30;
a=siz1bis-siz1;

olr=zeros(144,28,25,122);

siz2=siz1+366; siz2bis=siz2+a; % 1980
siz3=siz2+365; siz3bis=siz3+a; % 1981
siz4=siz3+365; siz4bis=siz4+a; % 1982
siz5=siz4+365; siz5bis=siz5+a; % 1983
siz6=siz5+366; siz6bis=siz6+a; % 1984
siz7=siz6+365; siz7bis=siz7+a; % 1985
siz8=siz7+365; siz8bis=siz8+a; % 1986
siz9=siz8+365; siz9bis=siz9+a; % 1987
siz10=siz9+366; siz10bis=siz10+a; % 1988
siz11=siz10+365; siz11bis=siz11+a; % 1989
siz12=siz11+365; siz12bis=siz12+a; % 1990
siz13=siz12+365; siz13bis=siz13+a; % 1991
siz14=siz13+366; siz14bis=siz14+a; % 1992
siz15=siz14+365; siz15bis=siz15+a; % 1993
siz16=siz15+365; siz16bis=siz16+a; % 1994
siz17=siz16+365; siz17bis=siz17+a; % 1995
siz18=siz17+366; siz18bis=siz18+a; % 1996
siz19=siz18+365; siz19bis=siz19+a; % 1997
siz20=siz19+365; siz20bis=siz20+a; % 1998
siz21=siz20+365; siz21bis=siz21+a; % 1999
siz22=siz21+366; siz22bis=siz22+a; % 2000
siz23=siz22+365; siz23bis=siz23+a; % 2001
siz24=siz23+365; siz24bis=siz24+a; % 2002
siz25=siz24+365; siz25bis=siz25+a; % 2003
siz26=siz25+366; siz26bis=siz26+a; % 2004
siz27=siz26+365; siz27bis=siz27+a; % 2005
siz28=siz27+365; siz28bis=siz28+a; % 2006

olr(:,1,:,:)=ind_olr(:,:,siz1:siz1bis); % 1979
olr(:,2,:,:)=ind_olr(:,:,siz2:siz2bis); % 1980
olr(:,3,:,:)=ind_olr(:,:,siz3:siz3bis); % 1981
olr(:,4,:,:)=ind_olr(:,:,siz4:siz4bis); % 1982
olr(:,5,:,:)=ind_olr(:,:,siz5:siz5bis); % 1983
olr(:,6,:,:)=ind_olr(:,:,siz6:siz6bis); % 1984
olr(:,7,:,:)=ind_olr(:,:,siz7:siz7bis); % 1985
olr(:,8,:,:)=ind_olr(:,:,siz8:siz8bis); % 1986
olr(:,9,:,:)=ind_olr(:,:,siz9:siz9bis); % 1987
olr(:,10,:,:)=ind_olr(:,:,siz10:siz10bis); % 1988
olr(:,11,:,:)=ind_olr(:,:,siz11:siz11bis); % 1989
olr(:,12,:,:)=ind_olr(:,:,siz12:siz12bis); % 1990
olr(:,13,:,:)=ind_olr(:,:,siz13:siz13bis); % 1991
olr(:,14,:,:)=ind_olr(:,:,siz14:siz14bis); % 1992
olr(:,15,:,:)=ind_olr(:,:,siz15:siz15bis); % 1993
olr(:,16,:,:)=ind_olr(:,:,siz16:siz16bis); % 1994
olr(:,17,:,:)=ind_olr(:,:,siz17:siz17bis); % 1995
olr(:,18,:,:)=ind_olr(:,:,siz18:siz18bis); % 1996
olr(:,19,:,:)=ind_olr(:,:,siz19:siz19bis); % 1997
olr(:,20,:,:)=ind_olr(:,:,siz20:siz20bis); % 1998
olr(:,21,:,:)=ind_olr(:,:,siz21:siz21bis); % 1999
olr(:,22,:,:)=ind_olr(:,:,siz22:siz22bis); % 2000
olr(:,23,:,:)=ind_olr(:,:,siz23:siz23bis); % 2001
olr(:,24,:,:)=ind_olr(:,:,siz24:siz24bis); % 2002
olr(:,25,:,:)=ind_olr(:,:,siz25:siz25bis); % 2003
olr(:,26,:,:)=ind_olr(:,:,siz26:siz26bis); % 2004
olr(:,27,:,:)=ind_olr(:,:,siz27:siz27bis); % 2005
olr(:,28,:,:)=ind_olr(:,:,siz28:siz28bis); % 2006
clear siz1 siz1bis;
clear siz2 siz2bis;
clear siz3 siz3bis;
clear siz4 siz4bis;
clear siz5 siz5bis;
clear siz6 siz6bis;
clear siz7 siz7bis;
clear siz8 siz8bis;
clear siz9 siz9bis;
clear siz10 siz10bis;
clear siz11 siz11bis;
clear siz12 siz12bis;
clear siz13 siz13bis;
clear siz14 siz14bis;
clear siz15 siz15bis;
clear siz16 siz16bis;
clear siz17 siz17bis;
clear siz18 siz18bis;
clear siz19 siz19bis;
clear siz20 siz20bis;
clear siz21 siz21bis;
clear siz22 siz22bis;
clear siz23 siz23bis;
clear siz24 siz24bis;
clear siz25 siz25bis;
clear siz26 siz26bis;
clear siz27 siz27bis;
clear siz28 siz28bis;

%dimensions variable olr avant permute (144,28,25,122);
olr1=permute(olr,[4 2 3 1]);
clear olr;

status=load('eof12_rossby_1030.txt');
eof12_rossby_1030=status;
clear status;

% on met sous la forme annee mois
olr_eof=reshape(eof12_rossby_1030,122,28);
clear eof12_rossby_1030;

% on calcule l ecart type sur la serie temporelle de l OLR
k=122*28;
olrbis=reshape(olr_eof,k,1);
olrbis=std(olrbis);

% intialisation des tableaux
olr_filtre_compomax=NaN*ones(50,25,144);
olr_filtre_compomin=NaN*ones(50,25,144);

compteurmax=0;
compteurmin=0;

% boucle sur les annees
for a=1:28;
   clear delta;
   delta=zeros(122,1);
   % boucle sur les jours : on prepare avant
   for b=1:122;
        if b>1;
            delta(b)=olr_eof(b,a)-olr_eof(b-1,a);
        end;
   end;
   % boucle sur les jours
   for b=1:122;
      % 1ere condition (laisser une marge de 10)
      if (b>20 && b<112);
% cas 1 : max deltas apres negetifs et deltas avant postifs
         if (delta(b+1)<0 && delta(b)<0 && delta(b-1)>0 && delta(b-2)>0 && olr_eof(b,a)>olrbis);
            compteurmax=compteurmax+1;
            olr_filtre_compomax(compteurmax,:,:)=olr1(b,a,:,:);
% cas 2 : min deltas apres positifs et deltas avant negatifs
         elseif (delta(b+1)>0 && delta(b)>0 && delta(b-1)<0 && delta(b-2)<0 && olr_eof(b,a)<(-olrbis));
            compteurmin=compteurmin+1;
            olr_filtre_compomin(compteurmin,:,:)=olr1(b,a,:,:);
         end;
      end;
   end;
end;

% on fait la somme des valeurs de olr_filtreentiel sur le nbre d occurences
% on obtent un tableau de la forme (jours-avant-apres,lon,lat)
olr_filtre_compomin1(:,:)=nanmean(olr_filtre_compomin);
clear olr_filtre_compomin;
olr_filtre_compomax1(:,:)=nanmean(olr_filtre_compomax);
clear olr_filtre_compomax;

x=[-10:1:10]';

delta_olr_filtre_min=zeros(25,144);
for b=1:25;
    for c=1:144;
        delta_olr_filtre_min(b,c)=olr_filtre_compomin1(b,c)-olr_filtre_compomax1(b,c);
        if delta_olr_filtre_min(b,c)<-25;
            delta_olr_filtre_min(b,c)=-25;
        elseif delta_olr_filtre_min(b,c)>25;
            delta_olr_filtre_min(b,c)=25;
        end;
    end;
end;
sauvegrads('composite_olr_EOF12_ER1030',delta_olr_filtre_min,[-180 2.5 -30 2.5]);
delta_olr_filtre_min(1,1)=-25.0001;
delta_olr_filtre_min(1,2)=25.0001;
fin=-30+24*2.5;

clear fin;
fin=-10+16*2.5;
[cs,h]=contour(lon,lat,delta_olr_filtre_min,[5:5:25],'k');
clabel(cs,h,[10 20 30],'color','k','rotation',0,'fontsize',5);
hold on;

clear cs h;
%[cs,h]=contour(lon,lat,delta_olr_filtre_min,[-25:5:-5],'k','Linewidth',0.3);
[cs,h]=contour(lon,lat,delta_olr_filtre_min,[-25:5:-5],'k:','Linewidth',0.3);

clabel(cs,h,[-30 -20 -10],'color','k','rotation',0,'fontsize',5);

cartemonde1;

axis equal
axis([-80 100 -10 30]);

%set (gca,'XTick',[-80:20:60],'XTickLabel',['80W';'60W';'40W';'20W';' 0 ';'20E';'40E';'60E'],'fontname','Arial','fontsize',6);
set (gca,'XTick',[-180:60:180],'XTickLabel',[' 180';'120W';' 60W';'  0 ';' 60E';'120E';'180 '],'fontname','Arial','fontsize',6);

set (gca,'YTick',[-10:10:40],'YTickLabel',['10S';' 0 ';'10N';'20N';'30N'],'fontname','Arial','fontsize',6);

set (gca,'tickDir','out');
title('Composite t=0 OLR et Pluies IRD indice ITCZ reconstruit EOF1+2','fontsize',6);
