%run_tidegui_loop run tidegui analysis & prediction for multiple stations and years
%
%See also: tide, tidegui
clear all

station = {'Honolulu','HVH'};
lims    = {[-.5:.1:.6],[-2:.2:2]};
yr0     = [2007 2013];
time{1} = {'01-jan-2007','01-jan-2008'};
time{2} = {'01-jan-2013','01-jan-2014'};
package = 'tidegui';
timer.analysis   = [];
timer.prediction = [];

for ist=1:2
    for iyr=1:2
    
%% load & analyse & store: <station>_noos_<yearfrom>_<package>_analysis.xml
    
        tide = tidegui.initializetide;
        year = num2str(yr0(iyr));
        label             = [station{ist},'_',year]; % e.g. 'hvh_2007'
        tide              = tidegui.importseries(tide,['../measurements/',station{ist},'_noos_',year,'_measurements.txt'],'','MATROOS',label);
        complabel         = [label,'_comp'];
        syn2007_label     = [label,'_syn2007'];
        syn2013_label     = [label,'_syn2013'];
        nodefac           = 1; % USE correction for nodal factors and shift phase of arguments
        tic;
        tide              = tidegui.series2components(tide, label, 'year', complabel, time{iyr}, nodefac);
        timer.analysis(ist) = toc;
        
        tidegui.exportcomponents(tide,complabel,['components_',label,'.csv'],'CSV');
        comp_index        = tidegui.findindexlabel(tide.Comp, complabel);

        write_xml_components([station{ist},'_noos_',year,'_',package,'_analysis.xml'],...
            tide.Comp(comp_index).names,...
            tide.Comp(comp_index).freqs*180*60/pi,...
            tide.Comp(comp_index).amplitudes,...
            tide.Comp(comp_index).phases,...
            station{ist},datenum(time{iyr}{1}),datenum(time{iyr}{2}));
            
%% predict & store: <station>_noos_<yearfrom>_<yearto>_<package>_prediction.txt

        tic;
        tide = tidegui.component2series(tide, complabel,time{1}{1} , time{1}, 10, syn2007_label);
        timer.prediction(ist) = toc;
        tidegui.exportseries   (tide, syn2007_label,['HVH_noos_',year,'_2007_',package,'_prediction.txt'], 'MATROOS');

        tide = tidegui.component2series(tide, complabel,time{2}{1} , time{2}, 10, syn2013_label);
        tidegui.exportseries   (tide, syn2013_label,['HVH_noos_',year,'_2013_',package,'_prediction.txt'], 'MATROOS');
    end
    
%% plot    
    
    close
    lim = lims{ist}([1 end]);
    t0 = tide.Series(1).Timedef.refdate;
%     subplot(2,1,1)
	plot(tide.Series(1).data.times./60./24 + t0,tide.Series(1).data.zvalues,'Displayname','data')
    hold on
    plot(tide.Series(3).data.times./60./24 + t0,tide.Series(3).data.zvalues,'r','Displayname','prediction')
    datetick('x')
    grid on
    title(station{ist})
    ylim(lim)
    
%     subplot(2,1,2)
%     scatterbin(tide.Series(1).data.zvalues,tide.Series(3).data.zvalues,lims{ist},lims{ist})
%     hold on
%     %plot(D(iyr).data,P(iyr,iyr).data,'k.') % takes too long to print
%     plot(lim,lim,'k--')
%     colorbar
%     axis equal
%     axis([lim lim])
%     grid on
    print2a4([station{ist},'_noos_',num2str(yr0(iyr)),'_',package,'_prediction.png'],'v','t',200,'o');    
    
end 
%% write performance log

fid = fopen([package,'.log'],'w');
fprintf(fid,'station    %s %s\n',station{1},station{2});
fprintf(fid,'analysis   %f %f sec.\n',timer.analysis(1),timer.analysis(2));
fprintf(fid,'prediction %f %f sec.\n',timer.prediction(1),timer.prediction(2));
fclose(fid);