clear expr ph

expr(1)=struct('name','NAER1850CNOC_f19_g16_03', ...
               'name_disp','CMIP5', ...
               'first_year',1, ...
               'last_year',299, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('black'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NAER1850CNOC_f19_g16_03/ocn/hist');
expr(2)=struct('name','NAER1850CNOC_f19_g16_04', ...
               'name_disp','CMIP5', ...
               'first_year',300, ...
               'last_year',599, ...
               'display_first_year',300, ...
               'add_to_expr',1, ...
               'color',cmu.colors('black'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NAER1850CNOC_f19_g16_04/ocn/hist');
expr(3)=struct('name','NAER1850CNOC_f19_g16_05', ...
               'name_disp','CMIP5', ...
               'first_year',600, ...
               'last_year',699, ...
               'display_first_year',600, ...
               'add_to_expr',1, ...
               'color',cmu.colors('black'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NAER1850CNOC_f19_g16_05/ocn/hist');
expr(4)=struct('name','NAER1850CNOC_f19_g16_06', ...
               'name_disp','CMIP5', ...
               'first_year',700, ...
               'last_year',1200, ...
               'display_first_year',1850, ...
               'add_to_expr',1, ...
               'color',cmu.colors('black'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NAER1850CNOC_f19_g16_06/ocn/hist');
expr(5)=struct('name','N1850_f19_tn11_default_cguo_pecountX', ...
               'name_disp','NBF1', ...
               'first_year',1, ...
               'last_year',1200, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('red'), ...
               'print',0, ...
               'monthly_diagnostics',0,...
               'path','/fimm/work/matsbn/norstore-NS4659K/chuncheng/cases_test_vilje/N1850_f19_tn11_default_cguo_pecountX/ocn/hist');
expr(6)=struct('name','NBF1850_f19_tn11_01', ...
               'name_disp','NBF2', ...
               'first_year',1, ...
               'last_year',100, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('forest green (web)'), ...
               'print',0, ...
               'monthly_diagnostics',0,...
               'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_01/run');
%              'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_01/ocn/hist');
%              'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NBF1850_f19_tn11_01/ocn/hist');
expr(7)=struct('name','NBF1850_f19_tn11_02', ...
               'name_disp','NBF2', ...
               'first_year',101, ...
               'last_year',500, ...
               'display_first_year',101, ...
               'add_to_expr',6, ...
               'color',cmu.colors('forest green (web)'), ...
               'print',1, ...
               'monthly_diagnostics',0,...
               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_02/ocn/hist');
%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_02/run');
%              'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NBF1850_f19_tn11_02/ocn/hist');
expr(8)=struct('name','NBF1850_f19_tn11_tke_p07', ...
               'name_disp','NBF1850\_f19\_tn11\_tke\_p07', ...
               'first_year',1, ...
               'last_year',500, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('amber'), ...
               'print',0, ...
               'monthly_diagnostics',0,...
               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_tke_p07/ocn/hist');
%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_tke_p07/run');
%              'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NBF1850_f19_tn11_tke_p07/ocn/hist');
expr(9)=struct('name','NBF1850_f19_tn11_tke_p07onem', ...
               'name_disp','NBF1850\_f19\_tn11\_tke\_p07onem', ...
               'first_year',1, ...
               'last_year',500, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('lime green'), ...
               'print',0, ...
               'monthly_diagnostics',0,...
               'path','/hexagon/work/matsbn/archive/NBF1850_f19_tn11_tke_p07onem/ocn/hist');
%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_tke_p07onem/run');
%              'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NBF1850_f19_tn11_tke_p07onem/ocn/hist');
expr(10)=struct('name','NBF1850_f19_tn11_foxts_01', ...
               'name_disp','NBF1850\_f19\_tn11\_foxts\_01', ...
               'first_year',1, ...
               'last_year',500, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('gray'), ...
               'print',0, ...
               'monthly_diagnostics',0,...
               'path','/hexagon/work/matsbn/archive/NBF1850_f19_tn11_foxts_01/ocn/hist');
%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_foxts_01/run');
%              'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NBF1850_f19_tn11_foxts_01/ocn/hist');
expr(11)=struct('name','NBF1850_f19_tn11_tke_Ls_lmt', ...
               'name_disp','NBF1850\_f19\_tn11\_tke\_Ls\_lmt', ...
               'first_year',1, ...
               'last_year',500, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('blue'), ...
               'print',0, ...
               'monthly_diagnostics',0,...
               'path','/hexagon/work/matsbn/archive/NBF1850_f19_tn11_tke_Ls_lmt/ocn/hist');
%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_tke_Ls_lmt/run');
%              'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NBF1850_f19_tn11_tke_Ls_lmt/ocn/hist');
expr(12)=struct('name','NBF1850_f19_tn11_tke_p01onem', ...
               'name_disp','NBF1850\_f19\_tn11\_tke\_p01onem', ...
               'first_year',1, ...
               'last_year',200, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('cyan'), ...
               'print',0, ...
               'monthly_diagnostics',0,...
               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_tke_p01onem/ocn/hist');
%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_tke_p01onem/run');
expr(13)=struct('name','NBF1850_f19_tn11_tke_Sh_01', ...
               'name_disp','NBF1850\_f19\_tn11\_tke\_Sh\_01', ...
               'first_year',1, ...
               'last_year',200, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('yellow'), ...
               'print',0, ...
               'monthly_diagnostics',0,...
               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_tke_Sh_01/ocn/hist');
%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_tke_Sh_01/run');
expr(14)=struct('name','NBF1850_f19_tn11_tke_Sh_foxts_01', ...
               'name_disp','NBF1850\_f19\_tn11\_tke\_Sh\_foxts\_01', ...
               'first_year',1, ...
               'last_year',200, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('azure'), ...
               'print',0, ...
               'monthly_diagnostics',0,...
               'path','/hexagon/work/matsbn/archive/NBF1850_f19_tn11_tke_Sh_foxts_01/ocn/hist');
%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_tke_Sh_foxts_01/run');
expr(15)=struct('name','NBF1850_f19_tn11_SA_edsprs_02', ...
               'name_disp','NBF1850\_f19\_tn11\_SA\_edsprs\_02', ...
               'first_year',1, ...
               'last_year',200, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('magenta'), ...
               'print',0, ...
               'monthly_diagnostics',0,...
               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_SA_edsprs_02/ocn/hist');
%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_SA_edsprs_02/run');
expr(16)=struct('name','NBF1850OC_f19_tn11_01', ...
               'name_disp','NBF1850OC\_f19\_tn11\_01', ...
               'first_year',1, ...
               'last_year',500, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('black'), ...
               'print',0, ...
               'monthly_diagnostics',0,...
               'path','/fimm/work/matsbn/vilje-work/matsbn/archive/NBF1850OC_f19_tn11_01/ocn/hist');

picpath='/fimm/home/nersc/matsbn/NorClim/diag/pic/';

secno=5;

mw=ones(1,12)/12;
mw=[31 28 31 30 31 30 31 31 30 31 30 31]/365;
rmwindow=5;
fontsize=12;
linewidth=2.0;

legend_list=[];

for nexp=1:length(expr)

  % Load stored time series data if available
  if exist(['voltr_ts_' expr(nexp).name '.mat'])
    load(['voltr_ts_' expr(nexp).name '.mat'])
  else
    year_list=[];
    voltr=[];
    section=[];
  end

  % If stored time series data is continous in time and cover the
  % requested time period, skip reading data from disk
  if isempty(year_list)||any(diff(year_list)~=1)|| ...
     year_list(1)>expr(nexp).first_year||year_list(end)<expr(nexp).last_year

    % Try to complement the time series by reading and transforming data
    % from disk
    year_missing=[];
    years_added=0;
    for year=expr(nexp).first_year:expr(nexp).last_year
      cyear=sprintf('%4.4d',year);
      if isempty(find(year_list==year))
        if expr(nexp).monthly_diagnostics
          if exist([expr(nexp).path '/' expr(nexp).name '.micom.hm.' ...
                    cyear '-01.nc'])
            complete_year=1;
            disp([expr(nexp).name ' ' cyear])
            for month=1:12
              cmonth=sprintf('%2.2d',month);
              fname=[expr(nexp).path '/' expr(nexp).name '.micom.hm.' ...
                     cyear '-' cmonth '.nc'];
              if exist(fname)

                ncid=netcdf.open(fname,'NC_NOWRITE');

                if isempty(section)
                  varid=netcdf.inqVarID(ncid,'section');
                  section=netcdf.getVar(ncid,varid)';
                end

                varid=netcdf.inqVarID(ncid,'voltr');
                voltr_tmp(:,month)=netcdf.getVar(ncid,varid)';
                netcdf.close(ncid)

              else
                complete_year=0;
                break
              end
            end
            if complete_year
              years_added=1;
              year_list(end+1)=year;
              voltr=[voltr voltr_tmp];
            else
              year_missing=[year_missing year];
            end
          else
            year_missing=[year_missing year];
          end
        else
          fname=[expr(nexp).path '/' expr(nexp).name '.micom.hy.' ...
                 cyear '.nc'];
          if exist(fname)
            disp([expr(nexp).name ' ' cyear])

            ncid=netcdf.open(fname,'NC_NOWRITE');

            if isempty(section)
              varid=netcdf.inqVarID(ncid,'section');
              section=netcdf.getVar(ncid,varid)';
            end

            varid=netcdf.inqVarID(ncid,'voltr');
            voltr_tmp=netcdf.getVar(ncid,varid);
            netcdf.close(ncid)

            voltr_tmp=voltr_tmp*ones(1,12);

            years_added=1;
            year_list(end+1)=year;
            voltr=[voltr voltr_tmp];
          else
            year_missing=[year_missing year];
          end
        end
      end
    end

    if ~isempty(year_missing)
      disp([expr(nexp).name ': Could not find data for the years: ' ...
           num2str(year_missing)])
    end

    % If the time series has been extended, sort the series and store
    % the updated time series
    if years_added
      [year_list svind]=sort(year_list);
      nz=size(voltr,1);
      smind=(ones(nz*12,1)*(svind-1)*nz*12)+(1:nz*12)'*ones(1,length(svind));
      voltr=reshape(voltr(smind),nz,[]);
      save(['voltr_ts_' expr(nexp).name '.mat'], ...
           'year_list','voltr','section')
    end

  end

  expr(nexp).year_list=year_list;
  expr(nexp).voltr=voltr;
  expr(nexp).section=section;
  if ~isempty(expr(nexp).name_disp)&&isempty(expr(nexp).add_to_expr)
    legend_list(end+1)=nexp;
  end

end

% Combine time series if requested
for nexp=1:length(expr)
  nexpa=expr(nexp).add_to_expr;
  if ~isempty(nexpa) && ~isempty(expr(nexpa).year_list)
    year_list=expr(nexpa).year_list;
    voltr=expr(nexpa).voltr;
    ind=find(year_list>expr(nexpa).last_year,1,'first');
    if isempty(ind)
      year_list=[year_list expr(nexp).year_list];
      voltr=[voltr expr(nexp).voltr];
    else
      year_list=[year_list(1:ind-1) expr(nexp).year_list];
      voltr=[voltr(:,1:(ind-1)*12) expr(nexp).voltr];
    end
    [year_list svind]=sort(year_list);
    nz=size(voltr,1);
    smind=(ones(nz*12,1)*(svind-1)*nz*12)+(1:nz*12)'*ones(1,length(svind));
    voltr=reshape(voltr(smind),nz,[]);
    expr(nexpa).year_list=year_list;
    expr(nexpa).voltr=voltr;
    expr(nexpa).first_year=min(expr(nexpa).first_year,expr(nexp).first_year);
    expr(nexpa).last_year=max(expr(nexpa).last_year,expr(nexp).last_year);
  end
end

% Plot the time series

%figure('visible','off')
clf
hold on

for nexp=1:length(expr)
  if isempty(expr(nexp).add_to_expr)
    year_list=expr(nexp).year_list;
    voltr=expr(nexp).voltr(secno,:);

    voltr_yr=mw*reshape(voltr,12,[])*1e-9;

    if rmwindow>0
      voltr_yr=rm(voltr_yr,rmwindow);
    end

    iyf=find(year_list>=expr(nexp).first_year,1);
    iyl=find(year_list<=expr(nexp).last_year,1,'last');
    year_list=year_list(iyf:iyl);
    voltr_yr=voltr_yr(iyf:iyl);
    year_discont=[diff(year_list)~=1 1];
    iyl=0;
    while iyl<length(year_list)
      iyf=iyl+1;
      iyl=iyl+find(year_discont(iyf:end)==1,1);
      time=expr(nexp).display_first_year-expr(nexp).first_year+ ...
           year_list(iyf:iyl);
      ph(nexp)=plot(time,voltr_yr(iyf:iyl), ...
                    '-','LineWidth',linewidth,'Color',expr(nexp).color);
    end
  end
end

if ~isempty(legend_list)
  h=legend(ph(legend_list),expr(legend_list).name_disp,'location','Best');
  set(h,'FontSize',fontsize)
  legend boxoff
end
grid on
box on
axis tight
set(gca,'FontSize',fontsize) 
xlabel('year','FontSize',fontsize) 
ylabel('Sv','FontSize',fontsize) 
title(['Transport: ' str_name_disp(strtrim(expr(1).section(secno,:)))],'FontSize',fontsize) 

% Print the time series

for nexp=length(expr):-1:1
  if expr(nexp).print
    if ~exist([picpath '/' expr(nexp).name])
      mkdir([picpath '/' expr(nexp).name])
    end
    print (gcf,'-dpng', '-r300', [picpath '/' expr(nexp).name '/voltr_' strtrim(expr(1).section(secno,:)) '_ts.png'])
    break
  end
end
