clear expr ph

%%%
%expr(1)=struct('name','NAER1850CNOC_f19_g16_01', ...
%               'first_year',1, ...
%               'last_year',99, ...
%               'display_first_year',1, ...
%               'print',0, ...
%               'ref_exp',1, ...
%               'grid_file','/work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'path','/work/matsbn/archive/NAER1850CNOC_f19_g16_01/ocn/hist');
%expr(2)=struct('name','NAER1850CNOC_f19_g16_02', ...
%               'first_year',100, ...
%               'last_year',300, ...
%               'display_first_year',100, ...
%               'print',0, ...
%               'ref_exp',1, ...
%               'grid_file','/work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'path','/work/matsbn/archive/NAER1850CNOC_f19_g16_02/ocn/hist');
%%%
%expr(1)=struct('name','NAER1850_aclc', ...
%               'first_year',1, ...
%               'last_year',60, ...
%               'display_first_year',1, ...
%               'print',0, ...
%               'ref_exp',1, ...
%               'grid_file','/work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'path','/work/matsbn/archive/NAER1850_aclc/ocn/hist');
%expr(1)=struct('name','NAER1850CNOC_f19_g16_01', ...
%               'first_year',1, ...
%               'last_year',60, ...
%               'display_first_year',1, ...
%               'print',0, ...
%               'ref_exp',1, ...
%               'grid_file','/work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'path','/work/matsbn/archive/NAER1850CNOC_f19_g16_01/ocn/hist');
%%%
%expr(1)=struct('name','NAER1850CNOC_f19_g16_01', ...
%               'first_year',1, ...
%               'last_year',50, ...
%               'display_first_year',1, ...
%               'print',0, ...
%               'ref_exp',1, ...
%               'grid_file','/work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'path','/work/matsbn/archive/NAER1850CNOC_f19_g16_01/ocn/hist');
%expr(2)=struct('name','NAER1850_aclc', ...
%               'first_year',1, ...
%               'last_year',50, ...
%               'display_first_year',50, ...
%               'print',0, ...
%               'ref_exp',2, ...
%               'grid_file','/work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'path','/work/matsbn/archive/NAER185_aclc/ocn/hist');
%expr(3)=struct('name','NAER1850CN_nobrnpd0', ...
%               'first_year',1, ...
%               'last_year',50, ...
%               'display_first_year',100, ...
%               'print',0, ...
%               'ref_exp',3, ...
%               'grid_file','/work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'path','/work/matsbn/archive/NAER1850CN_nobrnpd0/ocn/hist');
%expr(4)=struct('name','NAER1850CN_newbpd', ...
%               'first_year',1, ...
%               'last_year',50, ...
%               'display_first_year',150, ...
%               'print',0, ...
%               'ref_exp',4, ...
%               'grid_file','/work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'path','/work/matsbn/archive/NAER1850CN_newbpd/ocn/hist');
%expr(5)=struct('name','NAER1850CN_newbpd2', ...
%               'first_year',1, ...
%               'last_year',50, ...
%               'display_first_year',200, ...
%               'print',0, ...
%               'ref_exp',5, ...
%               'grid_file','/work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'path','/work/matsbn/archive/NAER1850CN_newbpd2/ocn/hist');
%%%
%expr(1)=struct('name','NAER1850CNOC_f19_g16_03', ...
%               'first_year',1, ...
%               'last_year',299, ...
%               'display_first_year',1, ...
%               'print',0, ...
%               'ref_exp',1, ...
%               'grid_file','/work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'path','/work/matsbn/archive/NAER1850CNOC_f19_g16_03/ocn/hist');
%expr(2)=struct('name','NAER1850CNOC_f19_g16_04', ...
%               'first_year',300, ...
%               'last_year',599, ...
%               'display_first_year',300, ...
%               'print',0, ...
%               'ref_exp',1, ...
%               'grid_file','/work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'path','/work/matsbn/archive/NAER1850CNOC_f19_g16_04/ocn/hist');
%expr(3)=struct('name','NAER1850CNOC_f19_g16_05', ...
%               'first_year',600, ...
%               'last_year',699, ...
%               'display_first_year',600, ...
%               'print',0, ...
%               'ref_exp',1, ...
%               'grid_file','/work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'path','/work/matsbn/archive/NAER1850CNOC_f19_g16_05/ocn/hist');
%expr(4)=struct('name','NAER1850CNOC_f19_g16_06', ...
%               'first_year',700, ...
%               'last_year',1149, ...
%               'display_first_year',700, ...
%               'print',0, ...
%               'ref_exp',1, ...
%               'grid_file','/work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'path','/work/matsbn/archive/NAER1850CNOC_f19_g16_06/ocn/hist');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%expr(1)=struct('name','CAM5O1MICOM.100Y', ...
%               'name_disp','CAM5O1MICOM.100Y', ...
%               'first_year',1, ...
%               'last_year',30, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',0, ...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx0.25v1/20130930/grid.nc', ...
%               'path','/hexagon/work/agu002/archive/CAM5O1MICOM.100Y/ocn/hist');
%expr(2)=struct('name','CAM5O1MICOM.100Y.cont', ...
%               'name_disp',[], ...
%               'first_year',31, ...
%               'last_year',50, ...
%               'display_first_year',31, ...
%               'add_to_expr',1, ...
%               'ref_exp',0, ...
%               'print',0, ...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx0.25v1/20130930/grid.nc', ...
%               'path','/hexagon/work/agu002/archive/CAM5O1MICOM.100Y.cont/ocn/hist');
%%%
%expr(1)=struct('name','CAM5O1MICOMFINAL2', ...
%               'name_disp','CAM5O1MICOMFINAL2', ...
%               'first_year',1, ...
%               'last_year',50, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',0, ...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx0.25v1/20130930/grid.nc', ...
%               'path','/fimm/work/matsbn/vilje-work/agu002/archive/CAM5O1MICOMFINAL2/ocn/hist');
%%%
%expr(1)=struct('name','ATM_f09_MICOM_tnx025', ...
%               'name_disp','ATM\_f09\_MICOM\_tnx025', ...
%               'first_year',1, ...
%               'last_year',50, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',0, ...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx0.25v1/20130930/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS9998K/noresm/cases/ATM_f09_MICOM_tnx025/ocn/hist');
%%%
%expr(1)=struct('name','N1850C5OL45_f09_tn11', ...
%               'name_disp','N1850C5OL45\_f09\_tn11', ...
%               'first_year',1, ...
%               'last_year',50, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',0, ...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS2345K/migrate_NS9998K/noresm/cases/N1850C5OL45_f09_tn11/ocn/hist');
%%%%
%expr(1)=struct('name','N1850_f19_tn11_01_default', ...
%               'name_disp','N1850\_f19\_tn11\_01\_default', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/N1850_f19_tn11_01_default/ocn/hist');
%%%
%expr(1)=struct('name','N1850_f19_tn11_MB01', ...
%               'name_disp','N1850\_f19\_tn11\_MB01', ...
%               'first_year',1, ...
%               'last_year',400, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',0, ...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/hexagon/work/detivan/archive/N1850_f19_tn11_MB01/ocn/hist');
%%%
%expr(1)=struct('name','N1850_f19_tn11_MB03', ...
%               'name_disp','N1850\_f19\_tn11\_MB03', ...
%               'first_year',1, ...
%               'last_year',400, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',0, ...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/vilje-work/detivan/archive/N1850_f19_tn11_MB03/ocn/hist');
%%%
%expr(1)=struct('name','N1850C5OL45_f09_tn11_171115', ...
%               'name_disp','N1850C5OL45\_f09\_tn11\_171115', ...
%               'first_year',1, ...
%               'last_year',100, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',0, ...
%               'monthly_diagnostics',1,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/N1850C5OL45_f09_tn11_171115/ocn/hist');
%expr(1)=struct('name','N1850_f19_tn11_default_cguo_pecountX', ...
%               'name_disp','N1850_f19\_tn11\_default\_cguo\_pecountX', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS4659K/chuncheng/cases_test_vilje/N1850_f19_tn11_default_cguo_pecountX/ocn/hist');
%expr(1)=struct('name','NBF1850_f19_tn11_Xmas', ...
%               'name_disp','NBF1850\_f19\_tn11\_Xmas', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS4659K/chuncheng/cases_test_Xmas2015/NBF1850_f19_tn11_Xmas/ocn/hist');
%expr(1)=struct('name','NBF1850_f19_tn11_Xmas_enecon', ...
%               'name_disp','NBF1850\_f19\_tn11\_Xmas\_enecon', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS4659K/chuncheng/cases_test_Xmas2015/NBF1850_f19_tn11_Xmas_enecon/ocn/hist');
%expr(1)=struct('name','NBF1850_f19_tn11_Xmas_SolarAngle', ...
%               'name_disp','NBF1850\_f19\_tn11\_Xmas\_SolarAngle', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS4659K/chuncheng/cases_test_Xmas2015/NBF1850_f19_tn11_Xmas_SolarAngle/ocn/hist');
%expr(1)=struct('name','NBF1850_f19_tn11_Xmas_SolarAngle_enecon', ...
%               'name_disp','NBF1850\_f19\_tn11\_Xmas\_SolarAngle\_enecon', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS4659K/chuncheng/cases_test_Xmas2015/NBF1850_f19_tn11_Xmas_SolarAngle_enecon/ocn/hist');
%expr(1)=struct('name','NBF1850_f19_tn11_Xmas_rm018_01', ...
%               'name_disp','NBF1850\_f19\_tn11\_Xmas\_rm018\_01', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_Xmas_rm018_01/ocn/hist');
%%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_Xmas_rm018_01/run');
%expr(1)=struct('name','NBF1850_f19_tn11_Xmas_nec_01', ...
%               'name_disp','NBF1850\_f19\_tn11\_Xmas\_nec\_01', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_Xmas_nec_01/ocn/hist');
%expr(1)=struct('name','NBF1850_f19_tn11_SA_edsprs_01', ...
%               'name_disp','NBF1850\_f19\_tn11\_SA\_edsprs\_01', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_SA_edsprs_01/ocn/hist');
%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_SA_edsprs_01/run');
%expr(1)=struct('name','NBF1850_f19_tn11_SA_ri07_01', ...
%               'name_disp','NBF1850\_f19\_tn11\_SA\_ri07\_01', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_SA_ri07_01/ocn/hist');
%%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_SA_ri07_01/run');
%expr(1)=struct('name','NBF1850_f19_tn11_SA_dluc_01', ...
%               'name_disp','NBF1850\_f19\_tn11\_SA\_dluc\_01', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_SA_dluc_01/ocn/hist');
%%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_SA_dluc_01/run');
%expr(1)=struct('name','NBF1850_f19_tn11_SA_bdmc017_01', ...
%               'name_disp','NBF1850\_f19\_tn11\_SA\_bdmc017\_01', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_SA_bdmc017_01/ocn/hist');
%%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_SA_bdmc017_01/run');
%expr(1)=struct('name','NBF1850_f19_tn11_Xmas_dpvold_01', ...
%               'name_disp','NBF1850\_f19\_tn11\_Xmas\_dpvold\_01', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_Xmas_dpvold_01/ocn/hist');
%%              'path','/fimm/work/matsbn/hexagon-work/matsbn/noresm/NBF1850_f19_tn11_Xmas_dpvold_01/run');
%expr(1)=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',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_SA_edsprs_02/ocn/hist');
%expr(1)=struct('name','NBF1850_f19_tn11_SA_tke_01', ...
%               'name_disp','NBF1850\_f19\_tn11\_SA\_tke\_01', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_SA_tke_01/ocn/hist');
%expr(1)=struct('name','NBF1850_f19_tn11_01', ...
%               'name_disp','NBF1850\_f19\_tn11\_01', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_01/ocn/hist');
%expr(1)=struct('name','NBF1850_f19_tn11_01', ...
%               'name_disp','NBF1850\_f19\_tn11\_0[12]', ...
%               'first_year',1, ...
%               'last_year',100, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',0, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/NBF1850_f19_tn11_01/ocn/hist');
%expr(2)=struct('name','NBF1850_f19_tn11_02', ...
%               'name_disp','NBF1850\_f19\_tn11\_0[12]', ...
%               'first_year',101, ...
%               'last_year',500, ...
%               'display_first_year',101, ...
%               'add_to_expr',1, ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NBF1850_f19_tn11_02/ocn/hist');
%expr(1)=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',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',1,...
%               'grid_file','/fimm/work/matsbn/vilje-work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/N1850C5OL45OCL32_30mar2016_f19_tn11/ocn/hist');
%%              'path','/fimm/work/matsbn/hexagon-work/matsbn/archive/N1850C5OL45OCL32_30mar2016_f19_tn11/ocn/hist');
%expr(1)=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',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',1,...
%               'grid_file','/fimm/work/matsbn/vilje-work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/N1850C5OL45OCL32_01apr2016_f09_tn11/ocn/hist');
%%              'path','/fimm/work/matsbn/hexagon-work/oyvinds/archive/N1850C5OL45OCL32_01apr2016_f09_tn11/ocn/hist');
%expr(1)=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',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',1,...
%               'grid_file','/fimm/work/matsbn/vilje-work/shared/noresm/inputdata/ocn/micom/tnx0.25v1/20130930/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS2345K/N1850C5OL45L32_f09_tn0251_T02/ocn/hist');
%%              'path','/fimm/work/matsbn/hexagon-work/agu002/archive/N1850C5OL45L32_f09_tn0251_T02/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',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'grid_file','/fimm/work/matsbn/vilje-work/shared/noresm/inputdata/ocn/micom/gx1v6/20100629/grid.nc', ...
%               'monthly_diagnostics',1,...
%               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NAER1850CNOC_f19_g16_03/ocn/hist');
%expr(1)=struct('name','NBF1850_f19_tn11_foxts_01', ...
%               'name_disp','NBF1850\_f19\_tn11\_foxts\_01', ...
%               'first_year',1, ...
%               'last_year',200, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NBF1850_f19_tn11_foxts_01/ocn/hist');
%%               'path','/hexagon/work/matsbn/archive/NBF1850_f19_tn11_foxts_01/ocn/hist');
%expr(1)=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',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/hexagon/work/matsbn/archive/NBF1850_f19_tn11_tke_p01onem/ocn/hist');
%expr(1)=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',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/hexagon/work/matsbn/archive/NBF1850_f19_tn11_tke_Sh_01/ocn/hist');
%expr(1)=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',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'monthly_diagnostics',0,...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'path','/hexagon/work/matsbn/archive/NBF1850_f19_tn11_tke_Sh_foxts_01/ocn/hist');
%expr(1)=struct('name','NBF1850OC_f19_tn11_01', ...
%               'name_disp','NBF1850OC\_f19\_tn11\_01', ...
%               'first_year',1, ...
%               'last_year',100, ...
%               'display_first_year',1, ...
%               'add_to_expr',[], ...
%               'ref_exp',0, ...
%               'print',1, ...
%               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
%               'monthly_diagnostics',0,...
%               'path','/fimm/work/matsbn/norstore-NS2345K/noresm/cases/NBF1850OC_f19_tn11_01/ocn/hist');
expr(1)=struct('name','NBF1850_f19_tn11_jerlov3_01', ...
               'name_disp','NBF1850\_f19\_tn11\_jerlov3\_01', ...
               'first_year',1, ...
               'last_year',100, ...
               'display_first_year',1, ...
               'add_to_expr',[], ...
               'ref_exp',0, ...
               'print',1, ...
               'grid_file','/hexagon/work/shared/noresm/inputdata/ocn/micom/tnx1v1/20120120/grid.nc', ...
               'monthly_diagnostics',0,...
               'path','/fimm/work/matsbn/vilje-work/matsbn/archive/NBF1850_f19_tn11_jerlov3_01/ocn/hist');
%              'path','/fimm/work/matsbn/vilje-work/matsbn/noresm/NBF1850_f19_tn11_jerlov3_01/run');


mw=ones(1,12)/12;
mw=[31 28 31 30 31 30 31 31 30 31 30 31]/365;
picpath='/fimm/home/nersc/matsbn/NorClim/diag/pic/';
color_lim=[-.1 .1];
color_lim=[-.25 .25];
color_lim=[-.3 .3];
color_lim=[-.5 .5];

fontsize=12;

plot_depth_mapped=1;
%depth_half=1500;
%depth_max=5500;
%depth_tick=[0 500 1000 1500 2500 3500 4500 5500];
depth_half=1000;
depth_max=5500;
depth_tick=[0 250 500 750 1000 2000 3000 4000 5000];

woa09_grid_file='/fimm/home/nersc/matsbn/matlab/regrid/grids/remap_grid_woa09_20120501.nc';
t_woa09_file='/hexagon//work/shared/noresm/WOA09/netcdf/t00an1.nc';
s_woa09_file='/hexagon//work/shared/noresm/WOA09/netcdf/s00an1.nc';
phc30_grid_file='/fimm/home/nersc/matsbn/matlab/regrid/grids/remap_grid_woa09_20120501.nc';
t_phc30_file='/hexagon/work/shared/noresm/PHC3.0/netcdf/Temp00_p3.obj.nc';
s_phc30_file='/hexagon/work/shared/noresm/PHC3.0/netcdf/Salt00_p3.obj.nc';

if ~isempty(find([expr.ref_exp]==0))
  if exist(['woa09.mat'])
    load(['woa09.mat'])
  else
    depth_woa09=ncgetvar(t_woa09_file,'depth');
    t=ncgetvar(t_woa09_file,'t');
    s=ncgetvar(s_woa09_file,'s');
    area=ncgetvar(woa09_grid_file,'grid_area');
    [nx ny nz]=size(t);
    p=reshape(ones(nx*ny,1)*depth_woa09',nx,ny,nz);
    pt=theta_from_t(s,t,p,zeros(nx,ny,nz));
    area_z=area*ones(1,nz);
    area_z(isnan(pt))=0;
    pt_z_woa09=nansum(reshape(pt,[],nz).*area_z)./sum(area_z);
    s_z_woa09=nansum(reshape(s,[],nz).*area_z)./sum(area_z);
    save('woa09.mat','depth_woa09','pt_z_woa09','s_z_woa09');
  end
end

if ~isempty(find([expr.ref_exp]==-1))
  if exist(['phc30.mat'])
    load(['phc30.mat'])
  else
    depth_phc30=ncgetvar(t_phc30_file,'depth');
    t=ncgetvar(t_phc30_file,'Temp');
    s=ncgetvar(s_phc30_file,'Salt');
    area=ncgetvar(phc30_grid_file,'grid_area');
    [nx ny nz]=size(t);
    p=reshape(ones(nx*ny,1)*depth_phc30',nx,ny,nz);
    pt=theta_from_t(s,t,p,zeros(nx,ny,nz));
    area_z=area*ones(1,nz);
    area_z(isnan(pt))=0;
    pt_z_phc30=nansum(reshape(pt,[],nz).*area_z)./sum(area_z);
    s_z_phc30=nansum(reshape(s,[],nz).*area_z)./sum(area_z);
    save('phc30.mat','depth_phc30','pt_z_phc30','s_z_phc30');
  end
end

for nexp=1:length(expr)

  % Load stored time series data if available
  if exist(['saln_z_ts_' expr(nexp).name '.mat'])
    load(['saln_z_ts_' expr(nexp).name '.mat'])
  else
    year_list=[];
    saln_z=[];
  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)

                if ~grid_info_read
                  ncid=netcdf.open(expr(nexp).grid_file,'NC_NOWRITE');
                  varid=netcdf.inqVarID(ncid,'parea');
                  area=netcdf.getVar(ncid,varid);
                  varid=netcdf.inqVarID(ncid,'pmask');
                  mask=netcdf.getVar(ncid,varid);
                  netcdf.close(ncid)

                  area(find(mask==0))=0.;
                  area_sum=nansum(area(:));

                  ncid=netcdf.open(fname,'NC_NOWRITE');
                  varid=netcdf.inqVarID(ncid,'salnlvl');
                  tmp=netcdf.getVar(ncid,varid);
                  scale_factor=netcdf.getAtt(ncid,varid,'scale_factor');
                  add_offset=netcdf.getAtt(ncid,varid,'add_offset');
                  fill_value=netcdf.getAtt(ncid,varid,'_FillValue');
                  saln3d=ones(size(tmp))*nan;
                  ind=find(tmp~=fill_value);
                  saln3d(ind)=double(tmp(ind))*scale_factor+add_offset;
%%%
                  varid=netcdf.inqVarID(ncid,'depth');
                  depth=netcdf.getVar(ncid,varid);
%%%
%                 load saln_z_ts_NAER1850CNOC_f19_g16_01 depth
%%%
                  netcdf.close(ncid)

                  nz=length(depth);
                  area_z=area(:)*ones(1,nz);
                  area_z(isnan(saln3d))=0;
                  area_z_sum=sum(area_z);

                  grid_info_read=1;
                end

                ncid=netcdf.open(fname,'NC_NOWRITE');
                varid=netcdf.inqVarID(ncid,'salnlvl');
                tmp=netcdf.getVar(ncid,varid);
                scale_factor=netcdf.getAtt(ncid,varid,'scale_factor');
                add_offset=netcdf.getAtt(ncid,varid,'add_offset');
                fill_value=netcdf.getAtt(ncid,varid,'_FillValue');
                saln3d=ones(size(tmp))*nan;
                ind=find(tmp~=fill_value);
                saln3d(ind)=double(tmp(ind))*scale_factor+add_offset;
                netcdf.close(ncid)

                saln_z_tmp(:,month)=nansum(reshape(saln3d,[],nz).*area_z)./area_z_sum;

              else
                complete_year=0;
                break
              end
            end
            if complete_year
              years_added=1;
              year_list(end+1)=year;
              saln_z=[saln_z saln_z_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])

            if ~grid_info_read
              ncid=netcdf.open(expr(nexp).grid_file,'NC_NOWRITE');
              varid=netcdf.inqVarID(ncid,'parea');
              area=netcdf.getVar(ncid,varid);
              varid=netcdf.inqVarID(ncid,'pmask');
              mask=netcdf.getVar(ncid,varid);
              netcdf.close(ncid)

              area(find(mask==0))=0.;
              area_sum=nansum(area(:));

              ncid=netcdf.open(fname,'NC_NOWRITE');
              varid=netcdf.inqVarID(ncid,'salnlvl');
              tmp=netcdf.getVar(ncid,varid);
              scale_factor=netcdf.getAtt(ncid,varid,'scale_factor');
              add_offset=netcdf.getAtt(ncid,varid,'add_offset');
              fill_value=netcdf.getAtt(ncid,varid,'_FillValue');
              saln3d=ones(size(tmp))*nan;
              ind=find(tmp~=fill_value);
              saln3d(ind)=double(tmp(ind))*scale_factor+add_offset;
%%%
              varid=netcdf.inqVarID(ncid,'depth');
              depth=netcdf.getVar(ncid,varid);
%%%
%             load saln_z_ts_NAER1850CNOC_f19_g16_01 depth
%%%
              netcdf.close(ncid)

              nz=length(depth);
              area_z=area(:)*ones(1,nz);
              area_z(isnan(saln3d))=0;
              area_z_sum=sum(area_z);

              grid_info_read=1;
            end

            ncid=netcdf.open(fname,'NC_NOWRITE');
            varid=netcdf.inqVarID(ncid,'salnlvl');
            tmp=netcdf.getVar(ncid,varid);
            scale_factor=netcdf.getAtt(ncid,varid,'scale_factor');
            add_offset=netcdf.getAtt(ncid,varid,'add_offset');
            fill_value=netcdf.getAtt(ncid,varid,'_FillValue');
            saln3d=ones(size(tmp))*nan;
            ind=find(tmp~=fill_value);
            saln3d(ind)=double(tmp(ind))*scale_factor+add_offset;
            netcdf.close(ncid)

            saln_z_tmp=(nansum(reshape(saln3d,[],nz).*area_z)./area_z_sum)';

            saln_z_tmp=saln_z_tmp*ones(1,12);

            years_added=1;
            year_list(end+1)=year;
            saln_z=[saln_z saln_z_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(nz*12,1)*(svind-1)*nz*12)+(1:nz*12)'*ones(1,length(svind));
      saln_z=reshape(saln_z(smind),nz,[]);
      save(['saln_z_ts_' expr(nexp).name '.mat'], ...
           'year_list','saln_z','depth')
    end

  end

  expr(nexp).year_list=year_list;
  expr(nexp).saln_z=saln_z;
  expr(nexp).depth=depth;
  expr(nexp).saln_z_first_year=[];

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;
    saln_z=expr(nexpa).saln_z;
    ind=find(year_list>expr(nexpa).last_year,1,'first');
    if isempty(ind)
      year_list=[year_list expr(nexp).year_list];
      saln_z=[saln_z expr(nexp).saln_z];
    else
      year_list=[year_list(1:ind-1) expr(nexp).year_list];
      saln_z=[saln_z(:,1:(ind-1)*12) expr(nexp).saln_z];
    end
    [year_list svind]=sort(year_list);
    nz=size(saln_z,1);
    smind=(ones(nz*12,1)*(svind-1)*nz*12)+(1:nz*12)'*ones(1,length(svind));
    saln_z=reshape(saln_z(smind),nz,[]);
    expr(nexpa).year_list=year_list;
    expr(nexpa).saln_z=saln_z;
    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;
    saln_z=expr(nexp).saln_z;
    depth=expr(nexp).depth;

    depth_mapped=0.5*(min(depth_half,depth)/depth_half ...
                     +(max(depth_half,depth)-depth_half) ...
                      /(depth_max-depth_half));
    depth_tick_mapped=0.5*(min(depth_half,depth_tick)/depth_half ...
                          +(max(depth_half,depth_tick)-depth_half) ...
                           /(depth_max-depth_half));

%   saln_z_yr=mon2ann(saln_z')';
    saln_z_yr=reshape(mw*reshape(saln_z',12,[]),[],size(saln_z,1))';

    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);
      saln_z_yr=saln_z_yr(:,iyf:iyl);
    expr(nexp).saln_z_first_year=saln_z_yr(:,1);
    year_discont=[diff(year_list)~=1 1];
    iyl=0;
    if     expr(nexp).ref_exp==0
      saln_z_ref=interp1(depth_woa09,s_z_woa09,depth);
    elseif expr(nexp).ref_exp==-1
      saln_z_ref=interp1(depth_phc30,s_z_phc30,depth);
    else
      if isempty(expr(expr(nexp).ref_exp).saln_z_first_year)|| ...
         length(expr(expr(nexp).ref_exp).depth)~=length(depth)
        error(['The reference experiment for ' expr(nexp).name  ...
               ' is not appropriate!'])
      end
      saln_z_ref=expr(expr(nexp).ref_exp).saln_z_first_year;
    end
    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_depth_mapped
        pcolor(time,depth_mapped, ...
               saln_z_yr(:,iyf:iyl)-saln_z_ref*ones(1,iyl-iyf+1));
      else
        pcolor(time,depth, ...
               saln_z_yr(:,iyf:iyl)-saln_z_ref*ones(1,iyl-iyf+1));
      end
    end
  end
end

shading flat
%colormap anom
colormap(cbsafemap(511,'rdbu'))
caxis(color_lim)
axis tight
if plot_depth_mapped
  set(gca,'FontSize',fontsize,'ydir','reverse', ...
          'ytick',depth_tick_mapped,'yticklabel',depth_tick,'ylim',[0 1]) 
else
  set(gca,'FontSize',fontsize,'ydir','reverse')
end
set(gca,'xlim',[expr(1).first_year expr(1).last_year])
xlabel('year','FontSize',fontsize) 
ylabel('depth (m)','FontSize',fontsize) 
title([expr(1).name_disp ' - Global mean salinity difference'], ...
      'FontSize',fontsize) 
h=colorbar;
ylabel(h,'g/kg')

box on

% 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 '/saln_z_ts.png'])
    break
  end
end
