clc;clear all;close all
addpath('/data3/yiwen/cesm_final_exp/matlab_toolbox/m_map')
addpath('/data3/yiwen/cesm_final_exp/matlab_toolbox/colorbrewer')
pathi='/data3/yiwen/cesm_final_exp/plot_test/OBS_data/HadISST_sst.nc'

sst=ncread(pathi,'sst');
lon=ncread(pathi,'longitude');lat=ncread(pathi,'latitude');
time=ncread(pathi,'time'); %1870-2021(151*12)

lon_loc=lon>=100 &lon<=156;
lat_loc=lat>=10 &lat<=43;
% SST_1984_2014=sst(lon_loc,lat_loc,1441:end);
SST_1994_1998=sst(lon_loc,lat_loc,1489:1548); %(i,j,60[12*5])

% ST=1;END=372; 
% ST=1;END=204; %1984-2000
% ST=205;END=372;

%1994-1998%
ST=1;END=60;

% 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,:)]

sst12=SST_1994_1998(:,:,month12);
sst35=SST_1994_1998(:,:,month35);
sst68=SST_1994_1998(:,:,month68);
sst911=SST_1994_1998(:,:,month911);


sst1_avg=nanmean(SST_1994_1998(:,:,month12),3);
sst4_avg=nanmean(SST_1994_1998(:,:,month35),3);
sst7_avg=nanmean(SST_1994_1998(:,:,month68),3);
sst10_avg=nanmean(SST_1994_1998(:,:,month911),3);

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

for i=1:40
  for j=1:30
     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

%%
[ha, hfigure]= tight_subplot(4, 1, [0.04 0.01], [0.1 0.1], [0.01 0.01]);
[xx,yy]=meshgrid(lon(lon_loc),lat(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({'DJF. 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({'MAM. 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({'JJA. mean (1994-1998)'},'Fontsize',12)
           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({'SON. mean (1994-1998)'},'Fontsize',12)
           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