function prepare_olr_filtre_simulation(ircaam_dataset,simulation)
% prepare_olr_filtre_simulation ++explication ircaam_dataset,simulation

%+
%
%  .. _prepare_olr_filtre_simulation.m:
%
% ===============================
% prepare_olr_filtre_simulation.m
% ===============================
%
% ce programme prepare les fichiers d'OLR de simulations IRCAAM CNRM
% pour le programme de filtrage
%
% DESCRIPTION
% ===========
%
% From :file:`rlut_d.{simulation}.197106-09.nc`,
% ``prepare_olr_filtre_simulation`` compute ++.
%
% ``prepare_olr_filtre_simulation`` write 
% :file:`olr_nofiltre_arpege_{simulation}.dat`.
%
% CAUTIONS
% ========
%
% can't work with octave because of ncload.
%
% EXAMPLES
% ========
%
% Following line read from :file:`rlut_d.AfNQIVIV.197106-09.nc` to 
% :file:`rlut_d.AfNQIVIV.200006-09.nc`,
% compute ++,
% write :file:`olr_nofiltre_arpege_AfNQIVIV.dat`::
%
% >> prepare_olr_filtre_simulation('arpege','AfNQIVIV')
%
% SEE ALSO
% ========
%
% :ref:`carte_eof_df.m`
%
% TODO
% ====
%
% improve description
%
% improve file pb
%
% use return function
%
% EVOLUTIONS
% ==========
%
% $Id$
%
% - fplod 2009-02-10T15:54:57Z aedon.locean-ipsl.upmc.fr (Darwin)
%
%   * force output to be written using IEEE floating point 32 bits
%
% - fplod 2009-02-04T09:40:31Z aedon.locean-ipsl.upmc.fr (Darwin)
%
%   * force output to be written using IEEE floating point with little-endian 
%     byte order
%
% - fplod 2009-02-02T13:45:57Z aedon.locean-ipsl.upmc.fr (Darwin)
%
%   * created from prepare_olr_filtre_AFR.m to replace it as well as
%     prepare_olr_filtre_AS.m, prepare_olr_filtre_CTL.m, 
%     prepare_olr_filtre_CTL_clim.m and prepare_olr_filtre_TR.m
%
%     Dataset ``arpege`` will be forced in output file names.
%
%     Simulation name will be add to output file names (see parameters).
%
%     1990 is added to years set.
%-

global IRCAAM_ID;
global IRCAAM_OD;

% force output to be written using IEEE floating point with little-endian byte 
% order
machineformat='l';

if nargin==2
 % 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;
else
  usage='usage : prepare_olr_filtre_simulation(ircaam_dataset,simulation)';
  disp(usage);
  error('eee : wrong arguments numbers')
end

close all;

iyearmin=1971;
iyearmax=2000;
nbyear=iyearmax-iyearmin+1;

olr=zeros(122,nbyear,64,128);

iiyear=0;
for iyear=iyearmin:iyearmax
 cyear=num2str(iyear);
 fullfilename=[IRCAAM_ID,'rlut_d.',simulation,'.',cyear,'06-09.nc'];
 ncload(fullfilename,'rlut');
 clear fullfilename;
 iiyear=iiyear+1;
 olr(:,iiyear,:,:)=rlut; 
 clear rlut;
end;

% selection du domaine geographique
olr1=zeros(122,nbyear,64,128);
olr1(:,:,:,1:64)=olr(:,:,:,65:128);
olr1(:,:,:,65:128)=olr(:,:,:,1:64);
clear olr;
olr=olr1;
clear olr1;
olr=olr(:,:,29:43,54:77);
tps=size(olr,1)*size(olr,2);
olr=reshape(olr,tps,15,24);
olr=permute(olr,[3 2 1]);
olr=reshape(olr,360,tps);

switch simulation
 case {'CtIV','CtCl'}
  % nothing
 otherwise
  for a=1:360;
   clear vaude;
   vaude=olr(a,:);
   vaude=vaude';
   vaude=mean(vaude);
   olr(a,:)=olr(a,:)-vaude;
  end;
end

fullfilename=[IRCAAM_OD,'olr_nofiltre_',ircaam_dataset,'_', simulation, '.dat'];
fid=fopen(fullfilename,'w',machineformat);
clear fullfilename;
fwrite(fid,olr,'float32');
fclose(fid);
