wlsettings;

dirpath  = 'f:\csm\';

%
% Get present date, determine start/enddate, create directory for computation
%

today   = datestr (now,'yyyymmdd');
away    = datestr (now-3,'yyyymmdd');

dircsm  = strcat  (dirpath,'oper_csm\',today,'\flow-waves');
dirxbc  = strcat  (dirpath,'oper_csm\',today,'\xbeach');
dirfig  = strcat  (dirpath,'oper_csm\',today,'\figs');
dirdel  = strcat  (dirpath,'oper_csm\',away);
dirmat  = pwd;

start_date_flow  = datestr(datenum(today,'yyyymmdd') - 7,'yyyymmdd');
start_date_wave  = datenum(start_date_flow,'yyyymmdd') + 5580/1440;

morfac = 10.;

try

   [success] = mkdir(dircsm);
   [success] = mkdir(dirxbc);
   [success] = mkdir(dirfig);
   [success] = rmdir(dirdel,'s')

%
% Get the hirlam data for the simulataion period
%

  [p_hirlam, u_hirlam, v_hirlam] = get_hirlam(start_date_flow);

%
% Write the Hirlam data as amp amu and amv files to dirnow
%

   write_hirlam(p_hirlam,dircsm,start_date_flow,'amp');
   write_hirlam(u_hirlam,dircsm,start_date_flow,'amu');
   write_hirlam(v_hirlam,dircsm,start_date_flow,'amv');
   cd (dirmat);

%
% Copy general csm input (without wind) to dircsm
%

   [success] = copyfile(strcat(dirpath,'general_csm\*'),dircsm);
   cd (dirmat);

%
% Replace itdate with startdate of simulation
%

   strrep =  [start_date_flow(1:4) '-' start_date_flow(5:6) '-' start_date_flow(7:8) ];
   replaceinfile('YYYY-MM-DD',strrep    ,strcat(dircsm,'\csm.mdf'));
   replaceinfile('YYYY-MM-DD',strrep    ,strcat(dircsm,'\csm.mdw'));
   replaceinfile('YYYYMMDD'  ,start_date_flow,strcat(dircsm,'\csm.mdf'));
   replaceinfile('YYYY-MM-DD',strrep    ,strcat(dircsm,'\egm.mdf'));
   replaceinfile('YYYYMMDD'  ,start_date_flow,strcat(dircsm,'\egm.mdf'));
   replaceinfile('YYYY-MM-DD',strrep    ,strcat(dircsm,'\hol.mdf'));
   replaceinfile('YYYYMMDD'  ,start_date_flow,strcat(dircsm,'\hol.mdf'));

%
% Do the combined flow/wave combination
%

   cd (dircsm)

   delete ('com-*');
   system ('run.bat');

   cd (dirmat)


%
% Do the post processing
%

   postpr_flow (dircsm,dirfig,start_date_flow);

   cd (dirmat);

   postpr_wave (dircsm,dirfig,start_date_flow);

%
% Generate boundary conditions for an xbeach simulation
%

   cd (dircsm);

   system('c:\delft3d\w32\flow\bin\nesthd2.exe < nesthd2.inp');
   cd (dirmat);

   ConvertBct2XBeach (strcat(dircsm,'\xbeach.bct'),strcat(dirxbc,'\xbeach.tim'), ...
                      1                           ,datenum(today,'yyyymmdd')  );

   MakeSpecList      (strcat(dircsm,'\'),start_date_wave,60.        ,'csm'                    , ...
                      3                 ,dirxbc         ,'\t.t'     ,datenum(today,'yyyymmdd'), ...
                      1         );

%
% Do the xbeach simulation
%

   copyfile(strcat(dirpath,'general_xbeach\*'),dirxbc);

   cd (dirxbc);

   system ('xbeach.exe');

   cd (dirmat);

%
% Postprocessing of the xbeach simulation
%

   postpr_xbeach (dirxbc,dirfig);

   cd (dirmat);

   placeFiguresOnWiki  (dirfig,dirmat);
   placeFiguresOnWiki_2(dirfig,dirmat);

   cd (dirmat);
%
% Clean up the mess
%

   cleanup (dircsm,dirdel);
   cd (dirmat)

%
% If failure, then try again
%

catch;
   cd (dirmat);
   run_csm;
end;
