function time_serie_eof_df(ircaam_dataset,simulation,period1, period2)
% time_serie_eof_df ++explication between period1 days and period2 days

%+
%
%  .. _time_serie_eof_df.m:
%
% ===================
% time_serie_eof_df.m
% ===================
%
% ++
%
% DESCRIPTION
% ===========
%
% From eof_arpege\_\ *simulation*\ _  \ *period1*\ -\ *period2*\ d.mat,
% ``time_serie_eof_df`` compute ++.
%
% ``time_serie_eof_df`` plot ++.
%
% ``time_serie_eof_df`` save eof[12]_arpege_\ *simulation*\ _  \ *period1*\ -\ *period2*\ d.txt.
%
% 2 Postscript files
% time_serie_arpege\_\ *simulation*\ _ \ *period1*\ -\ *period2*\ d_part[12].eps are
% written.
%
% EXAMPLES
% ========
%
% Following line read ``eof_arpege_TrNQIVIV_10-30d.mat``,
% compute ++,
% plot ++,
% write ``eof[12]_arpege_TrNQIVIV_10-30d`` and
% write ``time_serie_arpege_TrNQIVIV_10-30d_part[12].eps``
% ::
%
% >> time_serie_eof_df('arpege','TrNQIVIV',10,30)
%
% SEE ALSO
% ========
%
% :ref:`eof_df.m`
%
% :ref:`carte_eof_df.m`
%
% TODO
% ====
%
% improve description
%
% improve file pb
%
% use return function
%
% EVOLUTIONS
% ==========
%
% $Id$
%
% - fplod 2009-02-11T16:27:34Z aedon.locean-ipsl.upmc.fr (Darwin)
%
%   * add try/catch on opening file for reading
%
% - fplod 2009-02-10T14:28:00Z aedon.locean-ipsl.upmc.fr (Darwin)
%
%   * replace ndmin by period1 and ndmax by period2
%
% - fplod 2009-02-02T10:01:21Z aedon.locean-ipsl.upmc.fr (Darwin)
%
%   * created from time_serie_afr_1030.m to replace it as well as
%     time_serie_afr_30100.m, time_serie_as_1030.m, time_serie_as_30100.m,
%     time_serie_ctl_1030.m, time_serie_ctl_30100.m, time_serie_tr_1030.m and
%     time_serie_tr_30100.m
%
%     Days will be now written ``period1``\-``period2d`` (ex: 10-30d) in filenames.
%
%     Dataset ``arpege`` will be forced in output file names.
%
%     Simulation name will be add to  output file names (see parameters).
%
%     Postscript files names will be suffixe by partx instead of letter.
%
%-

global IRCAAM_ID;
global IRCAAM_OD;

if nargin==4
 % ircaam_dataset must be equal to arpege
 if ~strcmp(ircaam_dataset,'arpege')
  disp(['ircaam_dataset = ', ircaam_dataset]);
  error('eee : ircaam_dataset must be equal to arpege');
 end
 if strcmp(ircaam_dataset,'arpege')
  list_simulation={'AfNQIVIV','TrNQIVIV','AsNQIVIV','CtIV','CtCl'};
 end
 switch simulation
 case list_simulation
  % ok
 otherwise
  disp(['simulation = ', simulation]);
  disp(['list_simulation = ', mat2str(list_simulation)]);
  error('eee : simulation must be equal to one of list_simulation');
 end
 clear list_simulation;
 % period1 must be lower than period2
 if (period1 > period2)
  disp(['period1 = ', int2str(period1)]);
  disp(['period2 = ', int2str(period2)]);
  error('eee : period1 must be lower than period2');
 end
else
  usage='usage : time_serie_jjas_df(ircaam_dataset,simulation,period1, period2)';
  disp(usage);
  error('eee : wrong arguments numbers')
end

% period1 and period2 will be used in xx-yyd form in filenames
df = [int2str(period1),'-',int2str(period2),'d'];
clear period1;
clear period2;

close all;

fullfilename=[IRCAAM_ID,'eof_',ircaam_dataset,'_', simulation, '_', df, '.mat'];
try
 status=load(fullfilename,'C','olr');
catch
 error('eee : File %s not found\n', fullfilename);
end

clear fullfilename;

C=status.C;
olr=status.olr;
clear status;

olr=olr';
PC1=olr*C;

%%%%%%% cronique de la VP1
time_serie_olr1=PC1(:,360);
time_serie_olr2=PC1(:,359);

dt_olr1=std(time_serie_olr1);
dt_olr2=std(time_serie_olr2);

for a=1:3538;
   time_serie_olr1(a)=time_serie_olr1(a)/dt_olr1;
   time_serie_olr2(a)=time_serie_olr2(a)/dt_olr2;
end;

fullfilename=[IRCAAM_OD,'eof1_',ircaam_dataset,'_', simulation, '_', df,'.txt'];
save(fullfilename,'time_serie_olr1','-ASCII');
clear fullfilename;

fullfilename=[IRCAAM_OD,'eof2_',ircaam_dataset,'_', simulation, '_', df,'.txt'];
save(fullfilename','time_serie_olr2','-ASCII');
clear fullfilename;

x=[1:3538]';
for a=1:29;
   if a<12;
      figure(1);
      subplot(4,3,a);
   elseif a<23;
      figure(2);
      subplot(4,3,a-11);
   else
      figure(3);
      subplot(4,3,a-22);
   end;
   clear koro1;
   debut=(a-1)*122+1;
   fin=a*122;
   koro1=time_serie_olr1(debut:fin);
   koro2=time_serie_olr2(debut:fin);
   clear x;
   x=[1:122]';
   plot(x,koro1,'r');
   hold on
   plot(x,koro2,'k');
   y=zeros(122,1);
   plot(x,y,'k:');
   title(a+1970,'FontSize',8);
   my_title=['serie temporelle eof ', ircaam_dataset, ' ' , simulation, ' ' , df];
   if a==2;
       text(0,5.5,my_title);
   elseif a==13;
       text(0,5.5,my_title);
   elseif a==24;
       text(0,5.5,my_title);
   end;
   clear my_title;
   set (gca,'tickDir','out');
   axis([1 122 -3 3]);

   set (gca,'YTick',[-3:1:3],'YTickLabel',[-3:1:3],'fontname','Arial','fontsize',6);
   set (gca,'XTick',[0:20:120],'XTickLabel',[0:20:120],'fontname','Arial','fontsize',6);
end;

figure(1);
fullfilename=[IRCAAM_OD,'time_serie_eof_', ircaam_dataset, '_', simulation, '_', df, '_part1.eps'];
print('-depsc2',fullfilename);
clear fullfilename;

figure(2);
fullfilename=[IRCAAM_OD,'time_serie_eof_', ircaam_dataset, '_', simulation, '_', df, '_part2.eps'];
print('-depsc2',fullfilename);
clear fullfilename;

figure(3);
fullfilename=[IRCAAM_OD,'time_serie_eof_', ircaam_dataset, '_', simulation, '_', df, '_part3.eps'];
print('-depsc2',fullfilename);
clear fullfilename;

clear ircaam_dataset;
clear simulation;
clear df;
