%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%	MICOM DIAGNOSTICS (Matlab package)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Detelina Ivanova, detelina.ivanova@nersc.no
% 30/11/2015
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plots Volume Transports Time Series from voltran annual time series netcdf files 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all; close all;
% Grunch & Hexagon (Change the paths if local set-up)
addpath /work-common/shared/bjerknes/diagnostics/Packages/MICOM_DIAG/matlab_tools/mats
addpath /work-common/shared/bjerknes/diagnostics/Packages/MICOM_DIAG/matlab_tools/m_map1.4
addpath /work-common/shared/bjerknes/diagnostics/Packages/MICOM_DIAG/matlab_tools/m_map1.4f
addpath /work-common/shared/bjerknes/diagnostics/Packages/MICOM_DIAG/matlab_tools/matlab_netcdf_5_0
addpath /work-common/shared/bjerknes/diagnostics/Packages/MICOM_DIAG/matlab_tools/eosben07
addpath /work-common/shared/bjerknes/diagnostics/Packages/MICOM_DIAG/matlab_tools/eoslib05
addpath /work-common/shared/bjerknes/diagnostics/Packages/MICOM_DIAG/matlab_tools/evenmat

addpath /fimm/home/nersc/detivan/Matlab


% Set a structure with experiments' information:
expr(1)=struct('name','NBF1850OC_f19_tn11_01_130ka', ...
               'name_disp','130ka', ...
               'first_year',1901, ...
               'last_year',2000, ...
               'filetype','hy', ...
               'linewidth',1, ...
               'color',[0.6 0 0]);

expr(2)=struct('name','NBF1850OC_f19_tn11_01_125ka', ...
               'name_disp','125ka', ...
               'first_year',1901, ...
               'last_year',2000, ...
               'filetype','hy', ...
               'linewidth',1, ...
               'color',[1 0 1]);

expr(3)=struct('name','NBF1850OC_f19_tn11_01_120ka', ...
               'name_disp','120ka', ...
               'first_year',1901, ...
               'last_year',2000, ...
               'filetype','hy', ...
               'linewidth',1, ...
               'color',[0 1 1]);

expr(4)=struct('name','NBF1850OC_f19_tn11_01_115ka', ...
               'name_disp','115ka', ...
               'first_year',1901, ...
               'last_year',2000, ...
               'filetype','hy', ...
               'linewidth',1, ...
               'color',[0 0 1]);

expr(5)=struct('name','NBF1850OC_f19_tn11_02', ...
               'name_disp','PI', ...
               'first_year',1401, ...
               'last_year',1500, ...
               'filetype','hy', ...
               'linewidth',1.5, ...
               'color',[0 0 0]);

expr(6)=struct('name','NBF1850_f19_tn11_test_mis3b', ...
               'name_disp','MIS3', ...
               'first_year',1901, ...
               'last_year',2000, ...
               'filetype','hy', ...
               'linewidth',0.5, ...
               'color',0.7*[1 1 1]);


% Set the path to the processed results from this script. You need to create the directory in advance
workpath=['/fimm/work/cgu025/noresm/micom_diag/micom/'];

% Set the path to the plots directory. You need to create the directory in advance
picpath=['/fimm/work/cgu025/noresm/micom_diag/micom/'];

% mode 1: ANN; mode2: DJF; mode 3: JJA
plot_mode=1;


%
ff=figure;
set(ff,'paperunits','inches','paperposition',[1,1,6,4.5],'position',[100 100 1600 800]);
set(0, 'DefaultAxesFontName', 'AvantGarde');hold on;

for nexp=1:length(expr)

  fyear=expr(nexp).first_year;
  lyear=expr(nexp).last_year;
  expid=expr(nexp).name;
  ftype=expr(nexp).filetype
  
  load([workpath '/toa_mht/Data/mht_' expid '_' num2str(fyear) '-' num2str(lyear) '.mat']);
  if plot_mode==1
    mhflx_atm=mhflx_atm_ann;
  elseif plot_mode==2
    mhflx_atm=mhflx_atm_djf;
  elseif plot_mode==3
    mhflx_atm=mhflx_atm_jja;
  end

  % interpolate to 0.5 resolution
  lat_05=linspace(-90,90,361);
  tmp=mhflx_ocn(:,3);
  tmp(find(isnan(tmp)))=0;
  hf_ocn_05=interp1(lat_ocn,tmp,lat_05);
  hf_ocn_05(find(isnan(hf_ocn_05)))=0;

  hf_atm_05=interp1(lat_atm,mhflx_atm,lat_05);
  hf_tot_05=hf_ocn_05+hf_atm_05;

 


  hh(nexp)=plot(lat_05,hf_atm_05/1.e15,'linewidth',expr(nexp).linewidth,'Color',expr(nexp).color);grid on;hold on;
  ll=line([lat_05(1) lat_05(end)],[0,0],'color','k');
 
end

  h=legend(hh,expr(1:6).name_disp,'location','Best');
  legend boxoff;
  xlabel('Latitude');ylabel('Heat Transport (PW)');


print(['Figs/mhflx_Eemian_hy.1901-2000y_mhflx_avg_mode' num2str(plot_mode) '.png'],'-dpng','-r300');

xlim([50 70]);ylim([1.5 5]);
print(['Figs/mhflx_Eemian_hy.1901-2000y_mhflx_avg_mode' num2str(plot_mode) '_zoom-in.png'],'-dpng','-r300');
