clear all;close all;
addpath('~/output/carbon/NOIIANOC_mr03_hist2/Cant');
pathout='~/output/carbon/NOIIANOC_mr03_hist2/Cant/';
ctrlpath='~/archive/NOIIANOC_mr03_ctr2/ocn/hist/';
histpath='~/archive/NOIIANOC_mr03_hist2/ocn/hist/';

kx=360;ky=365;kz=70;
nmask=ncread('~/tools/data/mask_tnx1v1_nordic.nc','mask_nordic');
nmask=repmat(nmask,[1,1,kz]);

depth=ncread('~/tools/data/tnx1v1_depth.nc','depth');

yr=1990:2009;

[yr depth]=meshgrid(yr,depth);

anthct=nan(kz,20);
maget=nan(kz,20);
n=1;
for T0=1990:2009
	dic01=ncread([ctrlpath 'NOIIANOC_mr03_ctr2.micom.hbgcy.' num2str(T0) '.nc'],'dissiclvl');
	dic02=ncread([histpath 'NOIIANOC_mr03_hist2.micom.hbgcy.' num2str(T0) '.nc'],'dissiclvl');
	Cant=dic02-dic01;
	Cant=Cant/1025*1e6;   %mol/m3->umol/kg
	
	mage=ncread([histpath 'NOIIANOC_mr03_hist2.micom.hy.' num2str(T0) '.nc'],'idlagelvl');

	Cant(find(nmask~=2))=NaN;
	anthcp=nanmean(Cant,1);
	anthcp=nanmean(anthcp,2);
	anthcp=squeeze(anthcp);
	anthct(:,n)=anthcp;

	mage(find(nmask~=2))=NaN;
	mage(isinf(mage))=NaN;
	magep=nanmean(mage,1);
	magep=nanmean(magep,2);
	magep=squeeze(magep);
	maget(:,n)=magep;

	n=n+1;
end

save model_decadal_change.mat anthct maget yr depth
%break

%% anthc
figure; hold on;
pcolor(yr,depth,anthct);shading flat;hc=colorbar;
set(gca,'ydir','reverse','xlim',[1990 2009],'ylim',[0 4000],'fontsize',16,'fontweight','bold');
grid on;box on;
caxis([0 50]);
xlabel('Year','fontsize',16,'fontweight','bold');
ylabel('Depth (m)','fontsize',16,'fontweight','bold');
title('Cant (umol/kg)','fontsize',16,'fontweight','bold');
%cmap=cbrewer('seq','YlOrRd',24);
cmap=cbrewer('div','RdYlBu',20);
colormap(flipud(cmap));
cbfit(hc,4);
set(gcf,'color','white');figsize(11.69,8.26);
%pathout='~/output/carbon/NOIIANOC_mr03_hist2/profile/hovmoller/';
pathout='/Users/heyc/output/carbon/NOIIANOC_mr03_hist2/profile/hovmoller/';
figname='anthc_dic_1990-2009';
mfile=[mfilename('fullpath'),'.m'];
fighist(mfile,pathout,figname)
%save2pdf([pathout,figname])
export_fig (gcf, [pathout,figname], '-png','-m2.5','-r300', '-a2', '-q101', '-painters', '-rgb');
%
%% danthc
figure; hold on;
pcolor(yr(:,2:end),depth(:,2:end),anthct(:,2:end)-anthct(:,1:end-1));shading flat;hc=colorbar;
set(gca,'ydir','reverse','xlim',[1990 2009],'ylim',[0 4000],'fontsize',16,'fontweight','bold');
grid on;box on;
caxis([-2 2]);
xlabel('Year','fontsize',16,'fontweight','bold');
ylabel('Depth (m)','fontsize',16,'fontweight','bold');
title('Cant rate (umol/kg/yr)','fontsize',16,'fontweight','bold');
%cmap=cbrewer('seq','YlOrRd',24);
cmap=cbrewer('div','RdBu',24);
colormap(flipud(cmap));cbfit(hc,4);
set(gcf,'color','white');figsize(11.69,8.26);
pathout='~/output/carbon/NOIIANOC_mr03_hist2/profile/hovmoller/';
figname='anthc_dic_rate_1990-2009';
mfile=[mfilename('fullpath'),'.m'];
fighist(mfile,pathout,figname)
%save2pdf([pathout,figname])
export_fig (gcf, [pathout,figname], '-png','-m2.5','-r300', '-a2', '-q101', '-painters', '-rgb');

%% maget
figure; hold on;
pcolor(yr,depth,maget);shading flat;hc=colorbar;
set(gca,'ydir','reverse','xlim',[1990 2009],'ylim',[0 4000],'fontsize',16,'fontweight','bold');
caxis([0 200]);
xlabel('Year','fontsize',16,'fontweight','bold');
ylabel('Depth (m)','fontsize',16,'fontweight','bold');
title('model idealized age (yr)','fontsize',16,'fontweight','bold');
cmap=cbrewer('div','RdYlBu',24);
colormap(flipud(cmap));cbfit(hc,4);
set(gcf,'color','white');figsize(11.69,8.26);
mfile=[mfilename('fullpath'),'.m'];
fighist(mfile,pathout,figname)
pathout='~/output/carbon/NOIIANOC_mr03_hist2/profile/hovmoller/';
figname='mage_dic_1990-2009';
mfile=[mfilename('fullpath'),'.m'];
fighist(mfile,pathout,figname)
%save2pdf([pathout,figname])
export_fig (gcf, [pathout,figname], '-png','-m2.5','-r300', '-a2', '-q101', '-painters', '-rgb');
