%

% initialisation
clear;
close all;

% etape 1 :on selectionne les mois d ete (attention aux annees bixestiles!)
%          on compile sous forme d'un seul fichier

uwnd925=zeros(122,49,144,28);

siz=31+28+31+30+31+1;
sizbis=31+28+31+30+31+30+31+31+30;
ncload('uwnd925.1979.nc'); uwnd925(:,:,:,1)=uwnd(siz:sizbis,1,:,:); clear uwnd;
ncload('uwnd925.1980.nc'); uwnd925(:,:,:,2)=uwnd(siz+1:sizbis+1,1,:,:); clear uwnd;
ncload('uwnd925.1981.nc'); uwnd925(:,:,:,3)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1982.nc'); uwnd925(:,:,:,4)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1983.nc'); uwnd925(:,:,:,5)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1984.nc'); uwnd925(:,:,:,6)=uwnd(siz+1:sizbis+1,1,:,:);clear uwnd;
ncload('uwnd925.1985.nc'); uwnd925(:,:,:,7)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1986.nc'); uwnd925(:,:,:,8)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1987.nc'); uwnd925(:,:,:,9)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1988.nc'); uwnd925(:,:,:,10)=uwnd(siz+1:sizbis+1,1,:,:);clear uwnd;
ncload('uwnd925.1989.nc'); uwnd925(:,:,:,11)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1990.nc'); uwnd925(:,:,:,12)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1991.nc'); uwnd925(:,:,:,13)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1992.nc'); uwnd925(:,:,:,14)=uwnd(siz+1:sizbis+1,1,:,:);clear uwnd;
ncload('uwnd925.1993.nc'); uwnd925(:,:,:,15)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1994.nc'); uwnd925(:,:,:,16)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1995.nc'); uwnd925(:,:,:,17)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1996.nc'); uwnd925(:,:,:,18)=uwnd(siz+1:sizbis+1,1,:,:);clear uwnd;
ncload('uwnd925.1997.nc'); uwnd925(:,:,:,19)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1998.nc'); uwnd925(:,:,:,20)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.1999.nc'); uwnd925(:,:,:,21)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.2000.nc'); uwnd925(:,:,:,22)=uwnd(siz+1:sizbis+1,1,:,:);clear uwnd;
ncload('uwnd925.2001.nc'); uwnd925(:,:,:,23)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.2002.nc'); uwnd925(:,:,:,24)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.2003.nc'); uwnd925(:,:,:,25)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.2004.nc'); uwnd925(:,:,:,26)=uwnd(siz+1:sizbis+1,1,:,:);clear uwnd;
ncload('uwnd925.2005.nc'); uwnd925(:,:,:,27)=uwnd(siz:sizbis,1,:,:);clear uwnd;
ncload('uwnd925.2006.nc'); uwnd925(:,:,:,28)=uwnd(siz:sizbis,1,:,:);clear uwnd;

uwnd925=uwnd925*0.01;
uwnd925=uwnd925-187.65;

clear siz1 siz1bis;

uwnd=permute(uwnd925,[1 4 2 3]);
clear uwnd925;


%%%%%%%%%%%%%%% FICHIER OLR
% ouverture / lecture du fichier
load eof12_rossby_30100.txt
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
uwnd925_filtre_compomax=NaN*ones(60,31,49,144);
uwnd925_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;
            uwnd925_filtre_compomax(compteurmax,1,:,:)=uwnd(b-21,a,:,:);
            uwnd925_filtre_compomax(compteurmax,2,:,:)=uwnd(b-20,a,:,:);
            uwnd925_filtre_compomax(compteurmax,3,:,:)=uwnd(b-19,a,:,:);
            uwnd925_filtre_compomax(compteurmax,4,:,:)=uwnd(b-18,a,:,:);
            uwnd925_filtre_compomax(compteurmax,5,:,:)=uwnd(b-17,a,:,:);
            uwnd925_filtre_compomax(compteurmax,6,:,:)=uwnd(b-16,a,:,:);
            uwnd925_filtre_compomax(compteurmax,7,:,:)=uwnd(b-15,a,:,:);
            uwnd925_filtre_compomax(compteurmax,8,:,:)=uwnd(b-14,a,:,:);
            uwnd925_filtre_compomax(compteurmax,9,:,:)=uwnd(b-13,a,:,:);
            uwnd925_filtre_compomax(compteurmax,10,:,:)=uwnd(b-12,a,:,:);
            uwnd925_filtre_compomax(compteurmax,11,:,:)=uwnd(b-11,a,:,:);

            uwnd925_filtre_compomax(compteurmax,12,:,:)=uwnd(b-10,a,:,:);
            uwnd925_filtre_compomax(compteurmax,13,:,:)=uwnd(b-9,a,:,:);
            uwnd925_filtre_compomax(compteurmax,14,:,:)=uwnd(b-8,a,:,:);
            uwnd925_filtre_compomax(compteurmax,15,:,:)=uwnd(b-7,a,:,:);
            uwnd925_filtre_compomax(compteurmax,16,:,:)=uwnd(b-6,a,:,:);
            uwnd925_filtre_compomax(compteurmax,17,:,:)=uwnd(b-5,a,:,:);
            uwnd925_filtre_compomax(compteurmax,18,:,:)=uwnd(b-4,a,:,:);
            uwnd925_filtre_compomax(compteurmax,19,:,:)=uwnd(b-3,a,:,:);
            uwnd925_filtre_compomax(compteurmax,20,:,:)=uwnd(b-2,a,:,:);
            uwnd925_filtre_compomax(compteurmax,21,:,:)=uwnd(b-1,a,:,:);

            uwnd925_filtre_compomax(compteurmax,22,:,:)=uwnd(b,a,:,:);
            uwnd925_filtre_compomax(compteurmax,23,:,:)=uwnd(b+1,a,:,:);
            uwnd925_filtre_compomax(compteurmax,24,:,:)=uwnd(b+2,a,:,:);
            uwnd925_filtre_compomax(compteurmax,25,:,:)=uwnd(b+3,a,:,:);
            uwnd925_filtre_compomax(compteurmax,26,:,:)=uwnd(b+4,a,:,:);
            uwnd925_filtre_compomax(compteurmax,27,:,:)=uwnd(b+5,a,:,:);
            uwnd925_filtre_compomax(compteurmax,28,:,:)=uwnd(b+6,a,:,:);
            uwnd925_filtre_compomax(compteurmax,29,:,:)=uwnd(b+7,a,:,:);
            uwnd925_filtre_compomax(compteurmax,30,:,:)=uwnd(b+8,a,:,:);
            uwnd925_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;
            uwnd925_filtre_compomin(compteurmin,1,:,:)=uwnd(b-21,a,:,:);
            uwnd925_filtre_compomin(compteurmin,2,:,:)=uwnd(b-20,a,:,:);
            uwnd925_filtre_compomin(compteurmin,3,:,:)=uwnd(b-19,a,:,:);
            uwnd925_filtre_compomin(compteurmin,4,:,:)=uwnd(b-18,a,:,:);
            uwnd925_filtre_compomin(compteurmin,5,:,:)=uwnd(b-17,a,:,:);
            uwnd925_filtre_compomin(compteurmin,6,:,:)=uwnd(b-16,a,:,:);
            uwnd925_filtre_compomin(compteurmin,7,:,:)=uwnd(b-15,a,:,:);
            uwnd925_filtre_compomin(compteurmin,8,:,:)=uwnd(b-14,a,:,:);
            uwnd925_filtre_compomin(compteurmin,9,:,:)=uwnd(b-13,a,:,:);
            uwnd925_filtre_compomin(compteurmin,10,:,:)=uwnd(b-12,a,:,:);
            uwnd925_filtre_compomin(compteurmin,11,:,:)=uwnd(b-11,a,:,:);

            uwnd925_filtre_compomin(compteurmin,12,:,:)=uwnd(b-10,a,:,:);
            uwnd925_filtre_compomin(compteurmin,13,:,:)=uwnd(b-9,a,:,:);
            uwnd925_filtre_compomin(compteurmin,14,:,:)=uwnd(b-8,a,:,:);
            uwnd925_filtre_compomin(compteurmin,15,:,:)=uwnd(b-7,a,:,:);
            uwnd925_filtre_compomin(compteurmin,16,:,:)=uwnd(b-6,a,:,:);
            uwnd925_filtre_compomin(compteurmin,17,:,:)=uwnd(b-5,a,:,:);
            uwnd925_filtre_compomin(compteurmin,18,:,:)=uwnd(b-4,a,:,:);
            uwnd925_filtre_compomin(compteurmin,19,:,:)=uwnd(b-3,a,:,:);
            uwnd925_filtre_compomin(compteurmin,20,:,:)=uwnd(b-2,a,:,:);
            uwnd925_filtre_compomin(compteurmin,21,:,:)=uwnd(b-1,a,:,:);

            uwnd925_filtre_compomin(compteurmin,22,:,:)=uwnd(b,a,:,:);
            uwnd925_filtre_compomin(compteurmin,23,:,:)=uwnd(b+1,a,:,:);
            uwnd925_filtre_compomin(compteurmin,24,:,:)=uwnd(b+2,a,:,:);
            uwnd925_filtre_compomin(compteurmin,25,:,:)=uwnd(b+3,a,:,:);
            uwnd925_filtre_compomin(compteurmin,26,:,:)=uwnd(b+4,a,:,:);
            uwnd925_filtre_compomin(compteurmin,27,:,:)=uwnd(b+5,a,:,:);
            uwnd925_filtre_compomin(compteurmin,28,:,:)=uwnd(b+6,a,:,:);
            uwnd925_filtre_compomin(compteurmin,29,:,:)=uwnd(b+7,a,:,:);
            uwnd925_filtre_compomin(compteurmin,30,:,:)=uwnd(b+8,a,:,:);
            uwnd925_filtre_compomin(compteurmin,31,:,:)=uwnd(b+9,a,:,:);

         end;
      end;
   end;
end;


% on fait la somme des valeurs de uwnd925_filtreentiel sur le nbre d occurences
% on obtent un tableau de la forme (jours-avant-apres,lon,lat)
uwnd925_filtre_compomin1(:,:,:)=nanmean(uwnd925_filtre_compomin);
clear uwnd925_filtre_compomin;
uwnd925_filtre_compomax1(:,:,:)=nanmean(uwnd925_filtre_compomax);
clear uwnd925_filtre_compomax;

x=[-10:1:10]';

figure(1);
orient('landscape');
lat=lat';

for e=1:31;
       clear delta_uwnd925_filtre_min;
       delta_uwnd925_filtre_min=zeros(49,144);
       for b=1:49;
          for c=1:144;
             delta_uwnd925_filtre_min(b,c)=uwnd925_filtre_compomin1(e,b,c)-uwnd925_filtre_compomax1(e,b,c);
          end;
       end;
       delta_uwnd925=zeros(49,144);
       delta_uwnd925(:,1:72)=delta_uwnd925_filtre_min(:,73:144);
       delta_uwnd925(:,73:144)=delta_uwnd925_filtre_min(:,1:72);
       delta_uwnd925_filtre_min=flipud(delta_uwnd925);
       if e==1;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins21',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==2;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins20',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==3;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins19',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==4;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins18',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==5;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins17',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==6;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins16',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==7;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins15',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==8;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins14',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==9;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins13',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==10;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins12',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==11;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins11',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==12;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins10',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==13;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins9',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==14;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins8',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==15;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins7',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==16;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins6',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==17;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins5',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==18;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins4',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==19;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins3',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==20;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins2',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==21;
            sauvegrads('composite_uwnd925_mode_rossby_30100_moins1',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==22;
            sauvegrads('composite_uwnd925_mode_rossby_30100_0',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==23;
            sauvegrads('composite_uwnd925_mode_rossby_30100_plus1',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==24;
            sauvegrads('composite_uwnd925_mode_rossby_30100_plus2',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==25;
            sauvegrads('composite_uwnd925_mode_rossby_30100_plus3',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==26;
            sauvegrads('composite_uwnd925_mode_rossby_30100_plus4',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==27;
            sauvegrads('composite_uwnd925_mode_rossby_30100_plus5',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==28;
            sauvegrads('composite_uwnd925_mode_rossby_30100_plus6',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==29;
            sauvegrads('composite_uwnd925_mode_rossby_30100_plus7',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==30;
            sauvegrads('composite_uwnd925_mode_rossby_30100_plus8',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       elseif e==31;
            sauvegrads('composite_uwnd925_mode_rossby_30100_plus9',delta_uwnd925_filtre_min,[-180 2.5 -10 2.5]);
       end
       delta_uwnd925_filtre_min=flipud(delta_uwnd925_filtre_min);
end
