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;lon2=lon(:,1);
lat=clm.latr;lat2=lat(1,:);

for it = 1:372/12
SSH_yr(:,:,it)=nanmean(zos2(:,:,(it-1)*12+1:(it-1)*12+12),3);
end


time=linspace(1984,2014,31);
SSH_avg1=nanmean(SSH_yr,3);

for i=1:188
  for j=1:156
     b1 = polyfit(time,squeeze(SSH_yr(i,j,:))',1);
     A1(i,j)=b1(1);
  end
end


set(0,'defaultfigurecolor','w')
subplot(1,2,1)
m_proj('miller','lat',[10,40],'lon',[100,140]);
m_pcolor(lon2,lat2,SSH_avg1);shading interp;hold on
m_coast('patch','w','edgecolor','k');
m_grid('box','fancy','linestyle','none');
title({'SSH average (1984-2014)'},'Fontsize',12)
colormap(flipud(brewermap(30, 'RdYlBu')));
c = colorbar('location','southoutside');
c.Title.String = "meter";
caxis([-1.2 1.2])

subplot(1,2,2)
m_proj('miller','lat',[10,40],'lon',[100,140]);
m_pcolor(lon2,lat2,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([-3*10^-3 3*10^-3])


%%
ST=1;END=372; 

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

ssh1=zos2(:,:,month1);
ssh4=zos2(:,:,month4);
ssh7=zos2(:,:,month7);
ssh10=zos2(:,:,month10);

ssh1_avg=nanmean(zos2(:,:,month1),3);
ssh4_avg=nanmean(zos2(:,:,month4),3);
ssh7_avg=nanmean(zos2(:,:,month7),3);
ssh10_avg=nanmean(zos2(:,:,month10),3);

time1=linspace(1984,2014,31);

for i=1:57
  for j=1:105
     b1 = polyfit(time1,squeeze(ssh1(i,j,:))',1);
     A1(i,j)=b1(1);
     b4 = polyfit(time1,squeeze(ssh4(i,j,:))',1);
     A4(i,j)=b4(1);
     b7 = polyfit(time1,squeeze(ssh7(i,j,:))',1);
     A7(i,j)=b7(1);
     b10 = polyfit(time1,squeeze(ssh10(i,j,:))',1);
     A10(i,j)=b10(1);     
  end
end
%%
[ha, hfigure]= tight_subplot(4, 2, [0.04 0.0001], [0.1 0.1], [0.01 0.01]);

figure;
for ii = 1:8
    axes(ha(ii));
    switch ii
        case 1
           m_proj('miller','lat',[4,51],'lon',[95,158]);
           m_pcolor(lon,lat,ssh1_avg);shading interp;hold on
           m_coast('patch','w','edgecolor','k');
           m_grid('box','fancy','linestyle','none','Fontsize',6);
           title({'Jan. mean (1984-2014)'},'Fontsize',12)
           colormap(flipud(brewermap(30, 'RdYlBu')));
           caxis([-1.2 1.2])
           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',[4,51],'lon',[95,158]);
            m_pcolor(lon,lat,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([-3*10^-3 3*10^-3])
            cbar2 = colorbar('location','eastoutside');
            set(cbar2, 'position', [0.86 0.207 0.01 0.615]); % 记录colorbar的位置和大小
%             cbar2.Title.String = "seasonal trend";
           
        case 3
           m_proj('miller','lat',[4,51],'lon',[95,158]);
           m_pcolor(lon,lat,ssh4_avg);shading interp;hold on
           m_coast('patch','w','edgecolor','k');
           m_grid('box','fancy','linestyle','none','Fontsize',6);
           title({'Apr. mean (1984-2014)'},'Fontsize',12)
           colormap(flipud(brewermap(30, 'RdYlBu')));
           caxis([-1.2 1.2])
           colorbar('location','eastoutside', 'position', get(cbar, 'position'));


        case 4
            m_proj('miller','lat',[4,51],'lon',[95,158]);
            m_pcolor(lon,lat,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([-3*10^-3 3*10^-3])    
            colorbar('location','eastoutside', 'position', get(cbar2, 'position'));
            
        case 5
           m_proj('miller','lat',[4,51],'lon',[95,158]);
           m_pcolor(lon,lat,ssh7_avg);shading interp;hold on
           m_coast('patch','w','edgecolor','k');
           m_grid('box','fancy','linestyle','none','Fontsize',6);
           title({'Jul. mean (1984-2014)'},'Fontsize',12)
           colormap(flipud(brewermap(30, 'RdYlBu')));
           caxis([-1.2 1.2])
           colorbar('location','eastoutside', 'position', get(cbar, 'position'));
           
        case 6
            m_proj('miller','lat',[4,51],'lon',[95,158]);
            m_pcolor(lon,lat,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([-3*10^-3 3*10^-3])  
            colorbar('location','eastoutside', 'position', get(cbar2, 'position'));
            
        case 7
           m_proj('miller','lat',[4,51],'lon',[95,158]);
           m_pcolor(lon,lat,ssh10_avg);shading interp;hold on
           m_coast('patch','w','edgecolor','k');
           m_grid('box','fancy','linestyle','none','Fontsize',6);
           title({'Oct. mean (1984-2014)'},'Fontsize',12)
           colormap(flipud(brewermap(30, 'RdYlBu')));
           caxis([-1.2 1.2])
           colorbar('location','eastoutside', 'position', get(cbar, 'position'));
           
        case 8    
            m_proj('miller','lat',[4,51],'lon',[95,158]);
            m_pcolor(lon,lat,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([-3*10^-3 3*10^-3])
            colorbar('location','eastoutside', 'position', get(cbar2, 'position'));
    end

    end







