clear expr ph

%expr(1)=struct('name','N1850C5OL45OCL32_18mar2016_f19_tn11', ...
%               'name_disp','N1850C5OL45OCL32\_18mar2016\_f19\_tn11', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('forest green (web)'), ...
%               'print',1, ...
%               'monthly_diagnostics',1,...
%               'path','/fimm/work/matsbn/hexagon-work/oyvinds/archive/N1850C5OL45OCL32_18mar2016_f19_tn11/ocn/hist');
%expr(2)=struct('name','N1850C5OL45OCL32_18mar2016_f09_tn11', ...
%               'name_disp','N1850C5OL45OCL32\_18mar2016\_f09\_tn11', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('red'), ...
%               'print',0, ...
%               'monthly_diagnostics',1,...
%               'path','/fimm/work/matsbn/hexagon-work/oyvinds/archive/N1850C5OL45OCL32_18mar2016_f09_tn11/ocn/hist');
%expr(3)=struct('name','N1850C5OL45OCL32_f19_tn11_EnergyFix_01', ...
%               'name_disp','N1850C5OL45OCL32\_f19\_tn11\_EnergyFix\_01', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('black'), ...
%               'print',0, ...
%               'monthly_diagnostics',1,...
%               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/N1850C5OL45OCL32_f19_tn11_EnergyFix_01/ocn/hist');
%expr(4)=struct('name','N1850C5OL45OCL32_21mar2016_f19_tn11', ...
%               'name_disp','N1850C5OL45OCL32\_21mar2016\_f19\_tn11', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('magenta'), ...
%               'print',0, ...
%               'monthly_diagnostics',1,...
%               'path','/fimm/work/matsbn/hexagon-work/alfgr/archive/N1850C5OL45OCL32_21mar2016_f19_tn11/ocn/hist');
%expr(1)=struct('name','NAER1850CNOC_f19_g16_03', ...
%               'name_disp','CMIP5', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               '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','NBF1850_f19_tn11_SA_edsprs_02', ...
%               'name_disp','BCCR fast', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('pumpkin'), ...
%               'print',0, ...
%               'monthly_diagnostics',0,...
%               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NBF1850_f19_tn11_SA_edsprs_02/ocn/hist');
%expr(3)=struct('name','N1850C5OL45OCL32_30mar2016_f19_tn11', ...
%               'name_disp','NorESM\_c1.2-LM', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('forest green (web)'), ...
%               'print',1, ...
%               'monthly_diagnostics',1,...
%               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/N1850C5OL45OCL32_30mar2016_f19_tn11/ocn/hist');
%%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/N1850C5OL45OCL32_30mar2016_f19_tn11/run');
%expr(4)=struct('name','N1850C5OL45OCL32_01apr2016_f09_tn11', ...
%               'name_disp','NorESM\_c1.2-MM', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('red'), ...
%               'print',0, ...
%               'monthly_diagnostics',1,...
%               'path','/fimm/work/matsbn/hexagon-work/oyvinds/archive/N1850C5OL45OCL32_01apr2016_f09_tn11/ocn/hist');
%%              'path','/fimm/work/matsbn/hexagon-work/oyvinds/noresm/N1850C5OL45OCL32_01apr2016_f09_tn11/run');
%expr(5)=struct('name','N1850C5OL45L32_f09_tn0251_T02', ...
%               'name_disp','NorESM\_c1.2-MH', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('blue'), ...
%               'print',0, ...
%               'monthly_diagnostics',1,...
%               'path','/fimm/work/matsbn/hexagon-work/agu002/archive/N1850C5OL45L32_f09_tn0251_T02/ocn/hist');
%%              'path','/fimm/work/matsbn/hexagon-work/agu002/noresm/N1850C5OL45L32_f09_tn0251_T02/run');
%expr(1)=struct('name','NAER1850CNOC_f19_g16_06', ...
%               'name_disp','piControl', ...
%               'first_year',700, ...
%               'last_year',750, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('blue'), ...
%               'print',0, ...
%               'monthly_diagnostics',1,...
%               'path','/work/matsbn/archive/NAER1850CNOC_f19_g16_06/ocn/hist');
%expr(2)=struct('name','N18_f19_tn11_160617', ...
%               'name_disp','N18\_f19\_tn11\_160617', ...
%               'first_year',1, ...
%               'last_year',50, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('red'), ...
%               'print',0, ...
%               'monthly_diagnostics',1,...
%               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/N18_f19_tn11_160617/ocn/hist');
%expr(1)=struct('name','NAER1850CNOC_f19_g16_03', ...
%               'name_disp','CMIP5', ...
%               'first_year',1, ...
%               'last_year',100, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('black'), ...
%               'print',0, ...
%               'monthly_diagnostics',1,...
%               'path','/hexagon/work/agu002/archive/N1850_f09_tn11_NorESM_15082017/ocn/hist');
%expr(2)=struct('name','N1850_f09_tn11_NorESM_15082017', ...
%               'name_disp','N1850\_f09\_tn11\_NorESM\_15082017', ...
%               'first_year',1, ...
%               'last_year',45, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('blue'), ...
%               'print',1, ...
%               'monthly_diagnostics',1,...
%               'path','/hexagon/work/agu002/archive/N1850_f09_tn11_NorESM_15082017/ocn/hist');
%expr(3)=struct('name','N1850_f09_tn11_NorESM_NorESM_15082017_20years_AM', ...
%               'name_disp','N1850\_f09\_tn11\_NorESM\_NorESM\_15082017\_20years\_AM', ...
%               'first_year',1, ...
%               'last_year',100, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('red'), ...
%               'print',0, ...
%               'monthly_diagnostics',1,...
%               'path','/fimm/work/matsbn/vilje-work/agu002/archive/N1850_f09_tn11_NorESM_NorESM_15082017_20years_AM/ocn/hist');
%expr(4)=struct('name','N1850_f09_tn14_NorESM_15082017_20years', ...
%               'name_disp','N1850\_f09\_tn14\_NorESM\_15082017\_20years', ...
%               'first_year',1, ...
%               'last_year',100, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('forest green (web)'), ...
%               'print',0, ...
%               'monthly_diagnostics',1,...
%               'path','/fimm/work/matsbn/vilje-work/agu002/archive/N1850_f09_tn14_NorESM_15082017_20years/ocn/hist');
%expr(5)=struct('name','N1850_f19_tn11_230815', ...
%               'name_disp','N1850\_f19\_tn11\_230815', ...
%               'first_year',1, ...
%               'last_year',100, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('pumpkin'), ...
%               'print',0, ...
%               'monthly_diagnostics',1,...
%               'path','/fimm/work/matsbn/vilje-work/oyvinds/archive/N1850_f19_tn11_230815/ocn/hist');
%%expr(5)=struct('name','N1850OC_f09_tn11_NorESM_NorESM_15082017_20years_AM', ...
%%               'name_disp','N1850OC\_f09\_tn11\_NorESM\_NorESM\_15082017\_20years\_AM', ...
%%               'first_year',1, ...
%%               'last_year',50, ...
%%               'display_first_year',1, ...
%%               'add_to_expr',[], ...
%%               'color',cmu.colors('pumpkin'), ...
%%               'print',0, ...
%%               'monthly_diagnostics',1,...
%%               'path','/fimm/work/matsbn/vilje-work/agu002/archive/N1850OC_f09_tn11_NorESM_NorESM_15082017_20years_AM/ocn/hist');
%expr(1)=struct('name','NOJRA_T62_tn14_JRA_02', ...
%               'name_disp','JRA55', ...
%               'first_year',1, ...
%               'last_year',100, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'color',cmu.colors('black'), ...
%               'print',0, ...
%               'monthly_diagnostics',1,...
%               'path','/fimm/work/matsbn/vilje-work/milicak/archive/NOJRA_T62_tn14_JRA_02/ocn/hist');
expr(1)=struct('name','NOIIA_T62_tn14_micomdev3_ref', ...
               'name_disp','NOIIA\_T62\_tn14\_micomdev3\_ref', ...
               'first_year',1, ...
               'last_year',62, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('black'), ...
               'print',1, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/nird-cases/NOIIA_T62_tn14_micomdev3_ref/ocn/hist');
expr(2)=struct('name','NOIIA_T62_tn14_micomdev3_CE05', ...
               'name_disp','NOIIA\_T62\_tn14\_micomdev3\_CE05', ...
               'first_year',1, ...
               'last_year',62, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('red'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/nird-cases/NOIIA_T62_tn14_micomdev3_CE05/ocn/hist');
expr(3)=struct('name','NOIIA_T62_tn14_micomdev3_CE1', ...
               'name_disp','NOIIA\_T62\_tn14\_micomdev3\_CE1', ...
               'first_year',1, ...
               'last_year',62, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('blue'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/nird-cases/NOIIA_T62_tn14_micomdev3_CE1/ocn/hist');
expr(4)=struct('name','NOIIA_T62_tn14_micomdev3_CE1_moment', ...
               'name_disp','NOIIA\_T62\_tn14\_micomdev3\_CE1\_moment', ...
               'first_year',1, ...
               'last_year',62, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('light blue'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/nird-cases/NOIIA_T62_tn14_micomdev3_CE1_moment/ocn/hist');
expr(5)=struct('name','NOIIA_T62_tn14_micomdev3_ncpl24_01', ...
               'name_disp','NOIIA\_T62\_tn14\_micomdev3\_ncpl24\_01', ...
               'first_year',1, ...
               'last_year',62, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('forest green (web)'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/vilje-work/matsbn/archive/NOIIA_T62_tn14_micomdev3_ncpl24_01/ocn/hist');
expr(6)=struct('name','NOIIA_T62_tn14_micomdev3_ncpl24_02', ...
               'name_disp','NOIIA\_T62\_tn14\_micomdev3\_ncpl24\_02', ...
               'first_year',1, ...
               'last_year',62, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('magenta'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/vilje-work/matsbn/archive/NOIIA_T62_tn14_micomdev3_ncpl24_02/ocn/hist');
expr(7)=struct('name','NOIIA_T62_tn14_micomdev3_ncpl24_03', ...
               'name_disp','NOIIA\_T62\_tn14\_micomdev3\_ncpl24\_03', ...
               'first_year',1, ...
               'last_year',62, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('yellow'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/vilje-work/matsbn/archive/NOIIA_T62_tn14_micomdev3_ncpl24_03/ocn/hist');
expr(8)=struct('name','NOIIA_T62_tn14_micomdev3_ncpl24_04', ...
               'name_disp','NOIIA\_T62\_tn14\_micomdev3\_ncpl24\_04', ...
               'first_year',1, ...
               'last_year',62, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('pumpkin'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/vilje-work/matsbn/archive/NOIIA_T62_tn14_micomdev3_ncpl24_04/ocn/hist');
expr(9)=struct('name','NOIIA_T62_tn14_micomdev3_ncpl12_01', ...
               'name_disp','NOIIA\_T62\_tn14\_micomdev3\_ncpl12\_01', ...
               'first_year',1, ...
               'last_year',62, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('azure'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/vilje-work/matsbn/archive/NOIIA_T62_tn14_micomdev3_ncpl12_01/ocn/hist');
expr(10)=struct('name','NOIIA_T62_tn14_micomdev3_ncpl24_05', ...
               'name_disp','NOIIA\_T62\_tn14\_micomdev3\_ncpl24\_05', ...
               'first_year',1, ...
               'last_year',62, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('boysenberry'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/fram-work/matsbn/archive/NOIIA_T62_tn14_micomdev3_ncpl24_05/ocn/hist');
expr(11)=struct('name','NOIIA_T62_tn14_micomdev3_ncpl24_06', ...
               'name_disp','NOIIA\_T62\_tn14\_micomdev3\_ncpl24\_06', ...
               'first_year',1, ...
               'last_year',62, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'color',cmu.colors('light apricot'), ...
               'print',0, ...
               'monthly_diagnostics',1,...
               'path','/fimm/work/matsbn/fram-work/matsbn/archive/NOIIA_T62_tn14_micomdev3_ncpl24_06/ocn/hist');
%              'path','/fimm/work/matsbn/fram-work/matsbn/noresm/NOIIA_T62_tn14_micomdev3_ncpl24_06/run');

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

% plot_mode=0: plot max. AMOC between 20N-60N, at 26.5N, and at 45.0N together
% plot_mode=1: plot max. AMOC between 20N-60N.
% plot_mode=2: plot max. AMOC at 26.5N.
% plot_mode=3: plot max. AMOC at 45.0N.
plot_mode=2;

rmwindow=0;
figure_height_scale=0.5;
fontsize=12;
linewidth=2.0;

% Latitude interval for maximum AMOC search
lat1=20;
lat2=60;

legend_list=[];

for nexp=1:length(expr)

  % Load stored time series data if available
  if exist(['amoc_ts_' expr(nexp).name '.mat'])
    load(['amoc_ts_' expr(nexp).name '.mat'])
  else
    year_list=[];
    amocmax=[];
    amoc265=[];
    amoc450=[];
  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;
    grid_info_read=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 ~grid_info_read
                  varid=netcdf.inqVarID(ncid,'lat');
                  lat=netcdf.getVar(ncid,varid);

                  ind1=min(find(lat>=lat1));
                  ind2=max(find(lat<=lat2));
                  ind265=find(abs(lat-26.5)==min(abs(lat-26.5)),1);
                  ind450=find(abs(lat-45.0)==min(abs(lat-45.0)),1);

                  grid_info_read=1;
                end

                varid=netcdf.inqVarID(ncid,'mmflxd');
                mmflxd=netcdf.getVar(ncid,varid);
                fill_value=netcdf.getAtt(ncid,varid,'_FillValue');
                netcdf.close(ncid)
                mmflxd(find(mmflxd==fill_value))=nan;

                amocmax_tmp(month)=nanmax(reshape(mmflxd(ind1:ind2,:,1),1,[]))*1e-9;
                amoc265_tmp(month)=nanmax(reshape(mmflxd(ind265,:,1),1,[]))*1e-9;
                amoc450_tmp(month)=nanmax(reshape(mmflxd(ind450,:,1),1,[]))*1e-9;

              else
                complete_year=0;
                break
              end
            end
            if complete_year
              years_added=1;
              year_list(end+1)=year;
              amocmax=[amocmax amocmax_tmp];
              amoc265=[amoc265 amoc265_tmp];
              amoc450=[amoc450 amoc450_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 ~grid_info_read
              varid=netcdf.inqVarID(ncid,'lat');
              lat=netcdf.getVar(ncid,varid);

              ind1=min(find(lat>=lat1));
              ind2=max(find(lat<=lat2));
              ind265=find(abs(lat-26.5)==min(abs(lat-26.5)),1);
              ind450=find(abs(lat-45.0)==min(abs(lat-45.0)),1);

              grid_info_read=1;
            end

            varid=netcdf.inqVarID(ncid,'mmflxd');
            mmflxd=netcdf.getVar(ncid,varid);
            fill_value=netcdf.getAtt(ncid,varid,'_FillValue');
            netcdf.close(ncid)
            mmflxd(find(mmflxd==fill_value))=nan;

            amocmax_tmp=nanmax(reshape(mmflxd(ind1:ind2,:,1),1,[]))*1e-9;
            amoc265_tmp=nanmax(reshape(mmflxd(ind265,:,1),1,[]))*1e-9;
            amoc450_tmp=nanmax(reshape(mmflxd(ind450,:,1),1,[]))*1e-9;

            amocmax_tmp=amocmax_tmp*ones(1,12);
            amoc265_tmp=amoc265_tmp*ones(1,12);
            amoc450_tmp=amoc450_tmp*ones(1,12);

            years_added=1;
            year_list(end+1)=year;
            amocmax=[amocmax amocmax_tmp];
            amoc265=[amoc265 amoc265_tmp];
            amoc450=[amoc450 amoc450_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);
      smind=(ones(12,1)*(svind-1)*12)+(1:12)'*ones(1,length(svind));
      amocmax=reshape(amocmax(smind),1,[]);
      amoc265=reshape(amoc265(smind),1,[]);
      amoc450=reshape(amoc450(smind),1,[]);
      save(['amoc_ts_' expr(nexp).name '.mat'], ...
           'year_list','amocmax','amoc265','amoc450')
    end

  end

  expr(nexp).year_list=year_list;
  expr(nexp).amocmax=amocmax;
  expr(nexp).amoc265=amoc265;
  expr(nexp).amoc450=amoc450;
  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;
    amocmax=expr(nexpa).amocmax;
    amoc265=expr(nexpa).amoc265;
    amoc450=expr(nexpa).amoc450;
    ind=find(year_list>expr(nexpa).last_year,1,'first');
    if isempty(ind)
      year_list=[year_list expr(nexp).year_list];
      amocmax=[amocmax expr(nexp).amocmax];
      amoc265=[amoc265 expr(nexp).amoc265];
      amoc450=[amoc450 expr(nexp).amoc450];
    else
      year_list=[year_list(1:ind-1) expr(nexp).year_list];
      amocmax=[amocmax(1:(ind-1)*12) expr(nexp).amocmax];
      amoc265=[amoc265(1:(ind-1)*12) expr(nexp).amoc265];
      amoc450=[amoc450(1:(ind-1)*12) expr(nexp).amoc450];
    end
    [year_list svind]=sort(year_list);
    smind=(ones(12,1)*(svind-1)*12)+(1:12)'*ones(1,length(svind));
    amocmax=reshape(amocmax(smind),1,[]);
    amoc265=reshape(amoc265(smind),1,[]);
    amoc450=reshape(amoc450(smind),1,[]);
    expr(nexpa).year_list=year_list;
    expr(nexpa).amocmax=amocmax;
    expr(nexpa).amoc265=amoc265;
    expr(nexpa).amoc450=amoc450;
    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
mw=[31 28 31 30 31 30 31 31 30 31 30 31]/365;

%figure('visible','off')
clf;hold on
%set(gcf,'paperunits','inches','papersize',[8 6*figure_height_scale], ...
%        'paperposition',[0 0 8 6*figure_height_scale],'color',[1 1 1], ...
%        'renderer','painters','inverthardcopy','off')
%set(gca,'outerposition',[0 0 1 1],'position',[0.1 0.2 0.8 0.75], ...
%        'color',[1 1 1])

for nexp=1:length(expr)
  if isempty(expr(nexp).add_to_expr)
    year_list=expr(nexp).year_list;
    amocmax=expr(nexp).amocmax;
    amoc265=expr(nexp).amoc265;
    amoc450=expr(nexp).amoc450;

    amocmax_yr=mw*reshape(amocmax,12,[]);
    amoc265_yr=mw*reshape(amoc265,12,[]);
    amoc450_yr=mw*reshape(amoc450,12,[]);

    if rmwindow>0
      amocmax_yr=rm(amocmax_yr,rmwindow);
      amoc265_yr=rm(amoc265_yr,rmwindow);
      amoc450_yr=rm(amoc450_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);
    amocmax_yr=amocmax_yr(iyf:iyl);
    amoc265_yr=amoc265_yr(iyf:iyl);
    amoc450_yr=amoc450_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);
      if     plot_mode==0
        ph(nexp)=plot(time,amocmax_yr(iyf:iyl), ...
                      '-','LineWidth',linewidth,'Color',expr(nexp).color);
                 plot(time,amoc265_yr(iyf:iyl), ...
                      '--','LineWidth',linewidth,'Color',expr(nexp).color);
                 plot(time,amoc450_yr(iyf:iyl), ...
                      ':','LineWidth',linewidth,'Color',expr(nexp).color);
      elseif plot_mode==1
        ph(nexp)=plot(time,amocmax_yr(iyf:iyl), ...
                      '-','LineWidth',linewidth,'Color',expr(nexp).color);
      elseif plot_mode==2
        ph(nexp)=plot(time,amoc265_yr(iyf:iyl), ...
                      '-','LineWidth',linewidth,'Color',expr(nexp).color);
      elseif plot_mode==3
        ph(nexp)=plot(time,amoc450_yr(iyf:iyl), ...
                      '-','LineWidth',linewidth,'Color',expr(nexp).color);
      end
    end
  end
end

if ~isempty(legend_list)
  h=legend(ph(legend_list),expr(legend_list).name_disp,'location','Best');
% h=legend(ph(legend_list),expr(legend_list).name_disp,'location','southwest');
  set(h,'FontSize',fontsize)
  legend boxoff
end
grid on
box on
axis tight
set(gca,'FontSize',fontsize) 
xlabel('year','FontSize',fontsize) 
if     plot_mode==0
  title('Maximum Atlantic Meridional Overturning Circulation between\newline20N and 60N (solid line), at 26.5N (dashed line), and 45N (dotted line)','FontSize',12) 
elseif plot_mode==1
  ylabel('Max. AMOC 20^{\circ}-60^{\circ}N (Sv)','fontsize',fontsize)
elseif plot_mode==2
  ylabel('Max. AMOC 26.5^{\circ}N (Sv)','fontsize',fontsize)
elseif plot_mode==3
  ylabel('Max. AMOC 45^{\circ}N (Sv)','fontsize',fontsize)
end

% 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,'-depsc', '-r300', [picpath '/' expr(nexp).name '/amoc_ts.eps'])
    print (gcf,'-dpng', '-r300', [picpath '/' expr(nexp).name '/amoc_ts.png'])
    break
  end
end
