clc;clear all;close all
addpath('/data3/yiwen/cesm_final_exp/matlab_toolbox/m_map')
addpath('/data3/yiwen/cesm_final_exp/matlab_toolbox/colorbrewer')
addpath('/home/yiwen/matlab_test/tight_subplot')

load('/data/yckuo/ccipc2/yckuo/matlab_roms/forcing/tos_his.mat') %SST
load('/data/yckuo/ccipc2/yckuo/matlab_roms/forcing/zos_his.mat') %SSH
lon=clm.lonr;lat=clm.latr;

lon2=lon(:,1);lat2=lat(1,:);
lon_loc=lon2>=100&lon2<=156;
lat_loc=lat2>=10&lat2<=43;
tos3=tos2(lon_loc,lat_loc,:);

%total month == 372
%1984(1,4,7,10)
%1985(13,16,19,21)

% From 1984-2014 %
% ST=1;END=372; 
% ST=1;END=204; %1984-2000
% ST=205;END=372;
 ST=13;END=132;  %1994-1998

month1=ST:12:END;
month4=ST+3:12:END;
month7=ST+6:12:END;
month10=ST+9:12:END;

% % MAM %
% month3=ST+2:12:END;
% month35=[(ST+2:12:END)' (ST+3:12:END)' (ST+4:12:END)']
% month35=[month35(1,:) month35(2,:) month35(3,:) month35(4,:) month35(5,:) ]
% 
% % JJA %
% month6=ST+5:12:END;
% month68=[(ST+5:12:END)' (ST+6:12:END)' (ST+7:12:END)']
% month68=[month68(1,:) month68(2,:) month68(3,:) month68(4,:) month68(5,:) ]
% 
% % SON %
% month9=ST+8:12:END;
% month911=[(ST+8:12:END)' (ST+9:12:END)' (ST+10:12:END)']
% month911=[month911(1,:) month911(2,:) month911(3,:) month911(4,:) month911(5,:)]
% 
% % DJF %
% month12=ST+11:12:END;
% month12=[(ST+0:12:END)' (ST+1:12:END)' (ST+11:12:END)']
% month12=[month12(1,:) month12(2,:) month12(3,:) month12(4,:) month12(5,:)]


sst1=tos3(:,:,month1);
sst4=tos3(:,:,month4);
sst7=tos3(:,:,month7);
sst10=tos3(:,:,month10);

sst1_avg=nanmean(tos3(:,:,month1),3);
sst4_avg=nanmean(tos3(:,:,month4),3);
sst7_avg=nanmean(tos3(:,:,month7),3);
sst10_avg=nanmean(tos3(:,:,month10),3);

time1=linspace(1984,2014,31);
time2=linspace(1984,2000,17);
time3=linspace(2001,2014,14);

for i=1:size(sst1_avg,1)
  for j=1:size(sst1_avg,2)
     b1 = polyfit(time3,squeeze(sst1(i,j,:))',1);
     A1(i,j)=b1(1);
     b4 = polyfit(time3,squeeze(sst4(i,j,:))',1);
     A4(i,j)=b4(1);
     b7 = polyfit(time3,squeeze(sst7(i,j,:))',1);
     A7(i,j)=b7(1);
     b10 = polyfit(time3,squeeze(sst10(i,j,:))',1);
     A10(i,j)=b10(1);     
  end
end


% set(0,'defaultfigurecolor','w')
% subplot(1,2,1)
% m_proj('miller','lat',[4,51],'lon',[95,158]);
% m_pcolor(lon,lat,sst1);shading interp;hold on
% m_coast('patch','w','edgecolor','k');
% m_grid('box','fancy','linestyle','none');
% title({'January mean (1984-2014)'},'Fontsize',12)
% colormap(flipud(brewermap(30, 'RdYlBu')));
% c = colorbar('location','southoutside');
% c.Title.String = "C";
% caxis([-5 30])
% 
% subplot(1,2,2)
% m_proj('miller','lat',[10,40],'lon',[100,140]);
% m_pcolor(lon,lat,A1);shading interp;hold on
% m_coast('patch','w','edgecolor','k');
% m_grid('box','fancy','linestyle','none');
% colormap(flipud(brewermap(30, 'RdYlBu')));
% c = colorbar('location','southoutside');
% c.Title.String = "yearly trend";
% caxis([-0.1 0.1])


%%
[ha, hfigure]= tight_subplot(4, 1, [0.04 0.01], [0.1 0.1], [0.01 0.01]);
[xx,yy]=meshgrid(lon2(lon_loc),lat2(lat_loc));

figure;
for ii = 1:4
    axes(ha(ii));
    switch ii
        case 1
           m_proj('miller','lat',[10,43],'lon',[100,156]);
           m_pcolor(xx,yy,sst1_avg');shading interp;hold on
           m_coast('patch','w','edgecolor','k');
           m_grid('box','fancy','linestyle','none','Fontsize',6);
           title({'Jan. mean (1994-1998)'},'Fontsize',12)
%            colormap(flipud(brewermap(30, 'RdYlBu')));
           colormap('jet') 
           caxis([15 35])
           cbar = colorbar('location','eastoutside');
           cbar.Title.String = "C";            
           set(cbar, 'position', [0.38 0.207 0.01 0.615]); % 记录colorbar的位置和大小
                     
%         case 2
%             m_proj('miller','lat',[10,40],'lon',[100,140]);
%             m_pcolor(xx,yy,A1');shading interp;hold on
%             m_coast('patch','w','edgecolor','k');
%             m_grid('box','fancy','linestyle','none','Fontsize',6);
%             colormap(flipud(brewermap(30, 'RdYlBu')));
%             caxis([-0.1 0.1])
%             cbar2 = colorbar('location','eastoutside');
%             set(cbar2, 'position', [0.86 0.207 0.01 0.615]); % 记录colorbar的位置和大小
%             cbar2.Title.String = "seasonal trend";
           
        case 2
           m_proj('miller','lat',[10,43],'lon',[100,156]);
           m_pcolor(xx,yy,sst4_avg');shading interp;hold on
           m_coast('patch','w','edgecolor','k');
           m_grid('box','fancy','linestyle','none','Fontsize',6);
           title({'Apr. mean (1994-1998)'},'Fontsize',12)
%            colormap(flipud(brewermap(30, 'RdYlBu')));
           colormap('jet') 
           caxis([15 35])
           colorbar('location','eastoutside', 'position', get(cbar, 'position'));


%         case 4
%             m_proj('miller','lat',[10,40],'lon',[100,140]);
%             m_pcolor(xx,yy,A4');shading interp;hold on
%             m_coast('patch','w','edgecolor','k');
%             m_grid('box','fancy','linestyle','none','Fontsize',6);
%             colormap(flipud(brewermap(30, 'RdYlBu')));
%             caxis([-0.1 0.1])     
%             colorbar('location','eastoutside', 'position', get(cbar2, 'position'));
            
        case 3
           m_proj('miller','lat',[10,43],'lon',[100,156]);
           m_pcolor(xx,yy,sst7_avg');shading interp;hold on
           m_coast('patch','w','edgecolor','k');
           m_grid('box','fancy','linestyle','none','Fontsize',6);
           title({'Jul. mean (1994-1998)'},'Fontsize',12)
%            colormap(flipud(brewermap(30, 'RdYlBu')));
           colormap('jet') 
           caxis([15 35])
           colorbar('location','eastoutside', 'position', get(cbar, 'position'));
           
%         case 6
%            m_proj('miller','lat',[10,40],'lon',[100,140]);
%             m_pcolor(xx,yy,A7');shading interp;hold on
%             m_coast('patch','w','edgecolor','k');
%             m_grid('box','fancy','linestyle','none','Fontsize',6);
%             colormap(flipud(brewermap(30, 'RdYlBu')));
%             caxis([-0.1 0.1])   
%             colorbar('location','eastoutside', 'position', get(cbar2, 'position'));
            
        case 4
           m_proj('miller','lat',[10,43],'lon',[100,156]);
           m_pcolor(xx,yy,sst10_avg');shading interp;hold on
           m_coast('patch','w','edgecolor','k');
           m_grid('box','fancy','linestyle','none','Fontsize',6);
           title({'Oct. mean (1994-1998)'},'Fontsize',12)
%            colormap(flipud(brewermap(30, 'RdYlBu')));
          colormap('jet')  
          caxis([15 35])
           colorbar('location','eastoutside', 'position', get(cbar, 'position'));
           
%         case 8    
%           m_proj('miller','lat',[10,40],'lon',[100,140]);
%             m_pcolor(xx,yy,A10');shading interp;hold on
%             m_coast('patch','w','edgecolor','k');
%             m_grid('box','fancy','linestyle','none','Fontsize',6);
%             colormap(flipud(brewermap(30, 'RdYlBu')));
%             caxis([-0.1 0.1])  
%             colorbar('location','eastoutside', 'position', get(cbar2, 'position'));
    end

    end
