clear


load('matres_fgco2_decomposition_trend_nino_nina.mat'); load('matres_fgco2_trend_nino_nina.mat'); ft='La Nina trend Nino3 index';

inds=find(lon>=140 & lon<=285 & lat>=-15 & lat<=15);
inds_nan=find(lon>=276 & lon<=285 & lat>=9 & lat<=15);
area=grid_area(lon,lat);
trend_unit=100; %per century
idx=[2:8]; %[2 3 4 6 7 8 10 11 12]; %model with decomposition terms

fgco2_trend_model_nino=fgco2_trend_model_nino*trend_unit;
fgco2_trend_ste_model_nino=fgco2_trend_ste_model_nino*trend_unit;%*2;
%fgco2_trend_ste_model_nino(end-1:end)=fgco2_trend_ste_model_nino(end-1:end)/2;

fgco2_trend_model_nina=fgco2_trend_model_nina*trend_unit;
fgco2_trend_ste_model_nina=fgco2_trend_ste_model_nina*trend_unit;%*2;
%fgco2_trend_ste_model_nina(end-1:end)=fgco2_trend_ste_model_nina(end-1:end)/2;

data_varall={'fgco2_dpco2_trend_model_kw_m_nino','fgco2_kw_trend_model_kw_ano_nino','fgco2_kw_trend_model_dpco2_m_nino','fgco2_dpco2_trend_model_dpco2_ano_nino',...
             'fgco2_dpco2_trend_model_kw_m_nina','fgco2_kw_trend_model_kw_ano_nina','fgco2_kw_trend_model_dpco2_m_nina','fgco2_dpco2_trend_model_dpco2_ano_nina'};

for v=1:length(data_varall);
    eval(['data=',char(data_varall(v)),';']);
    [mm,jm,im]=size(data);
    for i=1:mm;
        datax=squeeze(data(i,:,:))*trend_unit;
        datax=datax.*area; datax(inds_nan)=nan; 
        fgco2_trend_comp(v,i)=nansum(datax(inds))/1e15;
    end
end
data=fgco2_trend_model_nino;     clear fgco2_trend_model_nino;     fgco2_trend_model_nino(1)=data(9);      fgco2_trend_model_nino(2:9)=data(1:8);
data=fgco2_trend_ste_model_nino; clear fgco2_trend_ste_model_nino; fgco2_trend_ste_model_nino(1)=data(9);  fgco2_trend_ste_model_nino(2:9)=data(1:8);
data=fgco2_trend_model_nina;     clear fgco2_trend_model_nina;     fgco2_trend_model_nina(1)=data(9);      fgco2_trend_model_nina(2:9)=data(1:8);
data=fgco2_trend_ste_model_nina; clear fgco2_trend_ste_model_nina; fgco2_trend_ste_model_nina(1)=data(9);  fgco2_trend_ste_model_nina(2:9)=data(1:8);
data=fgco2_trend_comp;           clear fgco2_trend_comp;           fgco2_trend_comp(:,1)=data(:,8);        fgco2_trend_comp(:,2:8)=data(:,1:7);

mk= ['+ooxsshdvv^>'];mks=1;mks2=120;
mkf=['nfnnnfnnnfnf'];
col=[0.9922    0.6667    0.2824];
C = linspecer(7);
trend_unit=100;
leg_text={'F''_{\deltat}','F''_{\deltatK^o}','F''_{\deltatK''}','F''_{\deltat\Deltap^o}','F''_{\deltat\Deltap^''}'};


fid=figure(1);clf
set(gcf,'paperOrientation','landscape');
set_figure_size(gcf, '', [0.1 0.1 0.1 0.1],1,0.7);
subaxis(2,1,1,'ml',0.13,'mr',0.04,'mb',0.1,'sv',0.04,'sh',0.12,'mt',0.07); hold on
datax=[1:length(fgco2_trend_model_nino)]; 
b1=bar(datax-0.15,fgco2_trend_model_nino,0.12,'FaceAlpha',0.1); b1.BarWidth=0.2;
b2=errorbar(datax-0.15,fgco2_trend_model_nino,fgco2_trend_ste_model_nino,'o','MarkerSize',1,'Color','k','linewidth',0.5);
datax2=[1 idx+1]+0.15;
b3=bar(datax2,fgco2_trend_comp(1:4,:),0.12,'stacked','FaceAlpha',0.8); b3(1).BarWidth=0.2;
b3(1).FaceColor=[85 164 242]/255; %light blue
b3(2).FaceColor=[12 2 96]/255; %dark blue
b3(3).FaceColor=[247 136 1]/255; %[0    0.0118    0.3569]; %dark blue
b3(4).FaceColor=[221 45 45]/255; %[0.5843    0.8157    0.9882]; %light blue
leg_d=legend([b1 b3],leg_text,'Orientation','horizontal','location','northeast');
leg_d.FontSize=10; leg_d.NumColumns=5; leg_d.ItemTokenSize=[20 10];
labels={{'Model';'mean'} {'ACCESS';'ESM1-5'} {'CESM2'} {'CESM2';'WACCM'} {'CNRM';'ESM2-1'} {'GFDL';'CM4'} {'GFDL';'ESM4'} {'IPSL';'CM6A-LR'} {'Nor';'ESM-2MM'}};
xlim([0.5 9.5]); ylim([-0.07 0.09]); set(gca,'xticklabels',''); %h=my_xticklabels(gca,datax,labels);
ylabel({'CO_2 flux El Nino anomaly'; 'trend F''_{\deltat} (PgC/yr/century)'},'fontsize',12.5)
text(0.55,0.08,'a','fontsize',22);
set(gca,'fontsize',12);

subaxis(2,1,2); hold on
datax=[1:length(fgco2_trend_model_nina)];
b1=bar(datax-0.15,fgco2_trend_model_nina,0.12,'FaceAlpha',0.1); b1.BarWidth=0.2;
b2=errorbar(datax-0.15,fgco2_trend_model_nina,fgco2_trend_ste_model_nina,'o','MarkerSize',1,'Color','k','linewidth',0.5);
datax2=[1 idx+1]+0.15;
b3=bar(datax2,fgco2_trend_comp(5:8,:),0.12,'stacked','FaceAlpha',0.8); b3(1).BarWidth=0.2;
b3(1).FaceColor=[85 164 242]/255; %light blue
b3(2).FaceColor=[12 2 96]/255; %dark blue
b3(3).FaceColor=[247 136 1]/255; %[0    0.0118    0.3569]; %dark blue
b3(4).FaceColor=[221 45 45]/255; %[0.5843    0.8157    0.9882]; %light blue
leg_d=legend([b1 b3],leg_text,'Orientation','horizontal','location','northeast');
leg_d.FontSize=10; leg_d.NumColumns=5; leg_d.ItemTokenSize=[20 10];
labels={{'Multi-Model';'mean'} {'ACCESS';'ESM1-5'} {'CESM2'} {'CESM2';'WACCM'} {'CNRM';'ESM2-1'} {'GFDL';'CM4'} {'GFDL';'ESM4'} {'IPSL';'CM6A-LR'} {'Nor';'ESM-2MM'}};
xlim([0.5 9.5]);ylim([-0.07 0.09]); h=my_xticklabels(gca,datax,labels);
set(gca,'fontsize',12);
ylabel({'CO_2 flux La Nina anomaly'; 'trend F''_{\deltat} (PgC/yr/century)'},'fontsize',12.5)
text(0.55,0.08,'b','fontsize',22);

print_pdf_liao(fid,['res_figs/figs03_bar_fCO2_trend_decomposition.pdf'])
print('-dpng','-r500', ['res_figs/figs03_bar_fCO2_trend_decomposition.png']);



