%

% initialisation
clear;
close all;

% etape 1 :on selectionne les mois d ete (attention aux annees bixestiles!)
%          on compile sous forme d'un seul fichier

uwnd700=zeros(122,49,144,28);

siz=31+28+31+30+31+1;
sizbis=31+28+31+30+31+30+31+31+30;
ncload('uwnd700.1979.nc'); uwnd700(:,:,:,1)=uwnd(siz:sizbis,1,:,:); clear uwnd;
ncload('uwnd700.1980.nc'); uwnd700(:,:,:,2)=uwnd(siz+1:sizbis+1,1,:,:); clear uwnd;
ncload('uwnd700.1981.nc'); uwnd700(:,:,:,3)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1982.nc'); uwnd700(:,:,:,4)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1983.nc'); uwnd700(:,:,:,5)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1984.nc'); uwnd700(:,:,:,6)=uwnd(siz+1:sizbis+1,1,:,:);clear uwnd;
ncload('uwnd700.1985.nc'); uwnd700(:,:,:,7)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1986.nc'); uwnd700(:,:,:,8)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1987.nc'); uwnd700(:,:,:,9)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1988.nc'); uwnd700(:,:,:,10)=uwnd(siz+1:sizbis+1,1,:,:);clear uwnd;
ncload('uwnd700.1989.nc'); uwnd700(:,:,:,11)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1990.nc'); uwnd700(:,:,:,12)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1991.nc'); uwnd700(:,:,:,13)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1992.nc'); uwnd700(:,:,:,14)=uwnd(siz+1:sizbis+1,1,:,:);clear uwnd;
ncload('uwnd700.1993.nc'); uwnd700(:,:,:,15)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1994.nc'); uwnd700(:,:,:,16)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1995.nc'); uwnd700(:,:,:,17)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1996.nc'); uwnd700(:,:,:,18)=uwnd(siz+1:sizbis+1,1,:,:);clear uwnd;
ncload('uwnd700.1997.nc'); uwnd700(:,:,:,19)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1998.nc'); uwnd700(:,:,:,20)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.1999.nc'); uwnd700(:,:,:,21)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.2000.nc'); uwnd700(:,:,:,22)=uwnd(siz+1:sizbis+1,1,:,:);clear uwnd;
ncload('uwnd700.2001.nc'); uwnd700(:,:,:,23)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.2002.nc'); uwnd700(:,:,:,24)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.2003.nc'); uwnd700(:,:,:,25)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.2004.nc'); uwnd700(:,:,:,26)=uwnd(siz+1:sizbis+1,1,:,:);clear uwnd;
ncload('uwnd700.2005.nc'); uwnd700(:,:,:,27)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd700.2006.nc'); uwnd700(:,:,:,28)=uwnd(siz:sizbis,1,:,:);clear uwnd;

uwnd700=uwnd700*0.01;
uwnd700=uwnd700-187.65;
clear siz1 siz1bis;

uwnd=permute(uwnd700,[1 4 2 3]);
clear uwnd700;

%%%%%%%%%%%%%%% FICHIER OLR
% ouverture / lecture du fichier
status=load('eof12_rossby_30100.txt');
eof12_rossby_30100=status;
clear status;
ind_olr=reshape(eof12_rossby_30100,122,28);
k=122*28;
clear eof12_rossby_30100
% on calcule l ecart type sur la serie entiere de l OLR
ind_olr1=reshape(ind_olr, k,1);
ind_olr2=std(ind_olr1);
clear ind_olr1;

% GEOPOTENTIEL
% intialisation des tableaux
uwnd700_filtre_compomax=NaN*ones(60,31,49,144);
uwnd700_filtre_compomin=NaN*ones(60,31,49,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)=ind_olr(b,a)-ind_olr(b-1,a);
        end;
   end;
   % boucle sur les jours
   for b=1:122;
      % 1ere condition (laisser une marge de 10)
      if (b>22 && 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 && ind_olr(b,a)>ind_olr2);
            compteurmax=compteurmax+1;
            uwnd700_filtre_compomax(compteurmax,1,:,:)=uwnd(b-21,a,:,:);
            uwnd700_filtre_compomax(compteurmax,2,:,:)=uwnd(b-20,a,:,:);
            uwnd700_filtre_compomax(compteurmax,3,:,:)=uwnd(b-19,a,:,:);
            uwnd700_filtre_compomax(compteurmax,4,:,:)=uwnd(b-18,a,:,:);
            uwnd700_filtre_compomax(compteurmax,5,:,:)=uwnd(b-17,a,:,:);
            uwnd700_filtre_compomax(compteurmax,6,:,:)=uwnd(b-16,a,:,:);
            uwnd700_filtre_compomax(compteurmax,7,:,:)=uwnd(b-15,a,:,:);
            uwnd700_filtre_compomax(compteurmax,8,:,:)=uwnd(b-14,a,:,:);
            uwnd700_filtre_compomax(compteurmax,9,:,:)=uwnd(b-13,a,:,:);
            uwnd700_filtre_compomax(compteurmax,10,:,:)=uwnd(b-12,a,:,:);
            uwnd700_filtre_compomax(compteurmax,11,:,:)=uwnd(b-11,a,:,:);

            uwnd700_filtre_compomax(compteurmax,12,:,:)=uwnd(b-10,a,:,:);
            uwnd700_filtre_compomax(compteurmax,13,:,:)=uwnd(b-9,a,:,:);
            uwnd700_filtre_compomax(compteurmax,14,:,:)=uwnd(b-8,a,:,:);
            uwnd700_filtre_compomax(compteurmax,15,:,:)=uwnd(b-7,a,:,:);
            uwnd700_filtre_compomax(compteurmax,16,:,:)=uwnd(b-6,a,:,:);
            uwnd700_filtre_compomax(compteurmax,17,:,:)=uwnd(b-5,a,:,:);
            uwnd700_filtre_compomax(compteurmax,18,:,:)=uwnd(b-4,a,:,:);
            uwnd700_filtre_compomax(compteurmax,19,:,:)=uwnd(b-3,a,:,:);
            uwnd700_filtre_compomax(compteurmax,20,:,:)=uwnd(b-2,a,:,:);
            uwnd700_filtre_compomax(compteurmax,21,:,:)=uwnd(b-1,a,:,:);

            uwnd700_filtre_compomax(compteurmax,22,:,:)=uwnd(b,a,:,:);
            uwnd700_filtre_compomax(compteurmax,23,:,:)=uwnd(b+1,a,:,:);
            uwnd700_filtre_compomax(compteurmax,24,:,:)=uwnd(b+2,a,:,:);
            uwnd700_filtre_compomax(compteurmax,25,:,:)=uwnd(b+3,a,:,:);
            uwnd700_filtre_compomax(compteurmax,26,:,:)=uwnd(b+4,a,:,:);
            uwnd700_filtre_compomax(compteurmax,27,:,:)=uwnd(b+5,a,:,:);
            uwnd700_filtre_compomax(compteurmax,28,:,:)=uwnd(b+6,a,:,:);
            uwnd700_filtre_compomax(compteurmax,29,:,:)=uwnd(b+7,a,:,:);
            uwnd700_filtre_compomax(compteurmax,30,:,:)=uwnd(b+8,a,:,:);
            uwnd700_filtre_compomax(compteurmax,31,:,:)=uwnd(b+9,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 && ind_olr(b,a)<(-ind_olr2));
            compteurmin=compteurmin+1;
            uwnd700_filtre_compomin(compteurmin,1,:,:)=uwnd(b-21,a,:,:);
            uwnd700_filtre_compomin(compteurmin,2,:,:)=uwnd(b-20,a,:,:);
            uwnd700_filtre_compomin(compteurmin,3,:,:)=uwnd(b-19,a,:,:);
            uwnd700_filtre_compomin(compteurmin,4,:,:)=uwnd(b-18,a,:,:);
            uwnd700_filtre_compomin(compteurmin,5,:,:)=uwnd(b-17,a,:,:);
            uwnd700_filtre_compomin(compteurmin,6,:,:)=uwnd(b-16,a,:,:);
            uwnd700_filtre_compomin(compteurmin,7,:,:)=uwnd(b-15,a,:,:);
            uwnd700_filtre_compomin(compteurmin,8,:,:)=uwnd(b-14,a,:,:);
            uwnd700_filtre_compomin(compteurmin,9,:,:)=uwnd(b-13,a,:,:);
            uwnd700_filtre_compomin(compteurmin,10,:,:)=uwnd(b-12,a,:,:);
            uwnd700_filtre_compomin(compteurmin,11,:,:)=uwnd(b-11,a,:,:);

            uwnd700_filtre_compomin(compteurmin,12,:,:)=uwnd(b-10,a,:,:);
            uwnd700_filtre_compomin(compteurmin,13,:,:)=uwnd(b-9,a,:,:);
            uwnd700_filtre_compomin(compteurmin,14,:,:)=uwnd(b-8,a,:,:);
            uwnd700_filtre_compomin(compteurmin,15,:,:)=uwnd(b-7,a,:,:);
            uwnd700_filtre_compomin(compteurmin,16,:,:)=uwnd(b-6,a,:,:);
            uwnd700_filtre_compomin(compteurmin,17,:,:)=uwnd(b-5,a,:,:);
            uwnd700_filtre_compomin(compteurmin,18,:,:)=uwnd(b-4,a,:,:);
            uwnd700_filtre_compomin(compteurmin,19,:,:)=uwnd(b-3,a,:,:);
            uwnd700_filtre_compomin(compteurmin,20,:,:)=uwnd(b-2,a,:,:);
            uwnd700_filtre_compomin(compteurmin,21,:,:)=uwnd(b-1,a,:,:);

            uwnd700_filtre_compomin(compteurmin,22,:,:)=uwnd(b,a,:,:);
            uwnd700_filtre_compomin(compteurmin,23,:,:)=uwnd(b+1,a,:,:);
            uwnd700_filtre_compomin(compteurmin,24,:,:)=uwnd(b+2,a,:,:);
            uwnd700_filtre_compomin(compteurmin,25,:,:)=uwnd(b+3,a,:,:);
            uwnd700_filtre_compomin(compteurmin,26,:,:)=uwnd(b+4,a,:,:);
            uwnd700_filtre_compomin(compteurmin,27,:,:)=uwnd(b+5,a,:,:);
            uwnd700_filtre_compomin(compteurmin,28,:,:)=uwnd(b+6,a,:,:);
            uwnd700_filtre_compomin(compteurmin,29,:,:)=uwnd(b+7,a,:,:);
            uwnd700_filtre_compomin(compteurmin,30,:,:)=uwnd(b+8,a,:,:);
            uwnd700_filtre_compomin(compteurmin,31,:,:)=uwnd(b+9,a,:,:);

         end;
      end;
   end;
end;

% on fait la somme des valeurs de uwnd700_filtreentiel sur le nbre d occurences
% on obtent un tableau de la forme (jours-avant-apres,lon,lat)
uwnd700_filtre_compomin1(:,:,:)=nanmean(uwnd700_filtre_compomin);
clear uwnd700_filtre_compomin;
uwnd700_filtre_compomax1(:,:,:)=nanmean(uwnd700_filtre_compomax);
clear uwnd700_filtre_compomax;

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

figure(1);
orient('landscape');
lat=lat';

for e=1:31;
       clear delta_uwnd700_filtre_min;
       delta_uwnd700_filtre_min=zeros(49,144);
       for b=1:49;
          for c=1:144;
             delta_uwnd700_filtre_min(b,c)=uwnd700_filtre_compomin1(e,b,c)-uwnd700_filtre_compomax1(e,b,c);
          end;
       end;
       delta_uwnd700=zeros(49,144);
       delta_uwnd700(:,1:72)=delta_uwnd700_filtre_min(:,73:144);
       delta_uwnd700(:,73:144)=delta_uwnd700_filtre_min(:,1:72);
       delta_uwnd700_filtre_min=flipud(delta_uwnd700);

       if e==1;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins21',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==2;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins20',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==3;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins19',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==4;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins18',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==5;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins17',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==6;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins16',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==7;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins15',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==8;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins14',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==9;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins13',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==10;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins12',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==11;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins11',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==12;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins10',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==13;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins9',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==14;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins8',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==15;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins7',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==16;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins6',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==17;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins5',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==18;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins4',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==19;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins3',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==20;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins2',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==21;
            sauvegrads('composite_uwnd700_mode_rossby_30100_moins1',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==22;
            sauvegrads('composite_uwnd700_mode_rossby_30100_0',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==23;
            sauvegrads('composite_uwnd700_mode_rossby_30100_plus1',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==24;
            sauvegrads('composite_uwnd700_mode_rossby_30100_plus2',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==25;
            sauvegrads('composite_uwnd700_mode_rossby_30100_plus3',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==26;
            sauvegrads('composite_uwnd700_mode_rossby_30100_plus4',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==27;
            sauvegrads('composite_uwnd700_mode_rossby_30100_plus5',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==28;
            sauvegrads('composite_uwnd700_mode_rossby_30100_plus6',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==29;
            sauvegrads('composite_uwnd700_mode_rossby_30100_plus7',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==30;
            sauvegrads('composite_uwnd700_mode_rossby_30100_plus8',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==31;
            sauvegrads('composite_uwnd700_mode_rossby_30100_plus9',delta_uwnd700_filtre_min,[-180 2.5 -10 2.5]);
       end;

end;
