%

clear;
close all;

status=load('eof_rossby_jjas.mat','C','olr_centre');
C=status.C;
olr_centre=status.olr_centre;
clear status;

olr_centre=olr_centre';
PC1=olr_centre*C;

cosa=zeros(3,2684,425);
% boucle sur les VP
for a=1:3;
      for b=1:2684 % boucle sur le temps
          for c=1:425 % boucle sur l espace
             cosa(a,b,c)=PC1(b,426-a)*C(c,426-a);
          end;
      end;
end;

cosi3(:,:)=sum(cosa);
cosi3=reshape(cosi3,2684,17,25);

cosabis=cosa(1:2,:,:);
cosi2(:,:)=sum(cosabis);
clear cosabis;
cosi2=reshape(cosi2,2684,17,25);

cosa1(:,:)=cosa(1,:,:);
clear cosa;
cosi1=reshape(cosa1,2684,17,25);
clear cosa1;

figure(1);
orient('landscape')
figure(2);
orient('landscape')
figure(3);
orient('landscape')

% On selectionne la demaine geographique

taille=(12-7+1)*(17-9+1);
cosa1=cosi1(:,7:12,9:17);
clear cosi1;
cosa1=reshape(cosa1,2684,taille);
cosa1=cosa1';
cosa1=mean(cosa1);
cosa1=cosa1';
cosa1=reshape(cosa1,122,22);

cosa2=cosi2(:,7:12,9:17);
clear cosi2;
cosa2=reshape(cosa2,2684,taille);
cosa2=cosa2';
cosa2=mean(cosa2);
cosa2=cosa2';
save('mode_rossby_jjas.txt','cosa2','-ASCII');

cosa2=reshape(cosa2,122,22);

cosa3=cosi3(:,7:12,9:17);
clear cosi3;
cosa3=reshape(cosa3,2684,taille);
cosa3=cosa3';
cosa3=mean(cosa3);
cosa3=cosa3';
cosa3=reshape(cosa3,122,22);

% On selectionne la demaine geographique
olr_centre=reshape(olr_centre,2684,17,25);
cosa4=olr_centre(:,7:12,9:17);
clear olr_centre;
cosa4=reshape(cosa4,2684,taille);
clear taille;
cosa4=cosa4';
cosa4=mean(cosa4);
cosa4=cosa4';
cosa4=reshape(cosa4,122,22);

for a=1:22;
     if a<12;
        figure(1);
        subplot(4,3,a);
     else
        figure(2);
        subplot(4,3,a-11);
     end;
     y1=cosa1(:,a);
     y2=cosa2(:,a);
     y3=cosa3(:,a);
     y4=cosa4(:,a);
     x=[1:122]';
     plot(x,y1,'r');
     hold on;
     plot(x,y2);
     plot(x,y3,'g');
     plot(x,y4,'k');
     y5=zeros(122,1);
     plot(x,y5,'k:');

     title(a+1978,'Fontsize',8);
     if a==2;
         text(0,65,'OLR 5 -17.5N et 10W - 10E');
     elseif a==13;
         text(0,65,'OLR 5 - 17.5N et 10W - 10E');
     end;
     set (gca,'tickDir','out');
     if a==11;
        aa=legend('OLR VP1','OLR VP1-2','OLR VP1-3','OLR NOAA');
        set (aa,'fontsize',8,'Position',[0.6800 0.1039 0.1270 0.1642])
        legend boxoff;
     elseif a==22;
        clear aa;
        aa=legend('OLR VP1','OLR VP1-2','OLR VP1-3','OLR NOAA');
        set (aa,'fontsize',8,'Position',[0.6800 0.1039 0.1270 0.1642])
        legend boxoff;
     elseif a==22;
        clear aa;
        aa=legend('OLR VP1','OLR VP1-2','OLR VP1-3','OLR NOAA');
        set (aa,'fontsize',8,'Position',[0.6800 0.1039 0.1270 0.1642])
        legend boxoff;
     end;
     axis([1 122 -40 40]);
     set (gca,'YTick',[-40:20:40],'YTickLabel',[-40:20:40],'fontname','Arial','fontsize',6);
   set (gca,'XTick',[10:10:90],'XTickLabel',[10:10:90],'fontname','Arial','fontsize',6);

end;

figure(1);
print('-depsc2','reconstitution_OLR_JJAS_part1.eps');
figure(2);
print('-depsc2','reconstitution_OLR_JJAS_part2.eps');
figure(3);
print('-depsc2','reconstitution_OLR_JJAS_part3.eps');
