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('/home/yckuo/MATLAB/ROMS/hycom_1994to2004_mon_zeta.mat')
A=z1;Z1 = reshape(A,188,156,[]);
load('/home/yckuo/MATLAB/ROMS/hycom_2005to2014_mon_zeta.mat')
B=z1;Z2 = reshape(B,188,156,[]);

SSH=cat(3,Z1,Z2); %1994-2014

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

time=linspace(1994,2014,21);
% choose 1994-1999
SSH_avg1=nanmean(SSH_yr(:,:,1:5),3);

%[10-35],[110-140]
for i=1:188
  for j=1:156
     b1 = polyfit(time,squeeze(SSH_yr(i,j,:))',1);
     A1(i,j)=b1(1);
  end
end

[xx,yy]=meshgrid(lon1,lat1);


set(0,'defaultfigurecolor','w')
subplot(1,2,1)
m_proj('miller','lat',[10,35],'lon',[110,140]);
m_pcolor(xx,yy,SSH_avg1');shading interp;hold on
m_coast('patch','w','edgecolor','k');
m_grid('box','fancy','linestyle','none');
title({'Hycom SSH average (1984-1999)'},'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,35],'lon',[110,140]);
m_pcolor(xx,yy,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([-6*10^-3 6*10^-3])

%%
ST=1;END=252; 

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

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

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

time1=linspace(1994,2014,21);

for i=1:188
  for j=1:156
     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',[10,35],'lon',[110,140]);
           m_pcolor(xx,yy,ssh1_avg');shading interp;hold on
           m_coast('patch','w','edgecolor','k');
           m_grid('box','fancy','linestyle','none','Fontsize',6);
           title({'Hycom Jan. mean (1994-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',[10,35],'lon',[110,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([-6*10^-3 6*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',[10,35],'lon',[110,140]);
           m_pcolor(xx,yy,ssh4_avg');shading interp;hold on
           m_coast('patch','w','edgecolor','k');
           m_grid('box','fancy','linestyle','none','Fontsize',6);
           title({'Apr. mean (1994-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',[10,35],'lon',[110,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([-6*10^-3 6*10^-3])    
            colorbar('location','eastoutside', 'position', get(cbar2, 'position'));
            
        case 5
           m_proj('miller','lat',[10,35],'lon',[110,140]);
           m_pcolor(xx,yy,ssh7_avg');shading interp;hold on
           m_coast('patch','w','edgecolor','k');
           m_grid('box','fancy','linestyle','none','Fontsize',6);
           title({'Jul. mean (1994-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',[10,35],'lon',[110,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([-6*10^-3 6*10^-3]) 
            colorbar('location','eastoutside', 'position', get(cbar2, 'position'));
            
        case 7
           m_proj('miller','lat',[10,35],'lon',[110,140]);
           m_pcolor(xx,yy,ssh10_avg');shading interp;hold on
           m_coast('patch','w','edgecolor','k');
           m_grid('box','fancy','linestyle','none','Fontsize',6);
           title({'Oct. mean (1994-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',[10,35],'lon',[110,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([-6*10^-3 6*10^-3])
            colorbar('location','eastoutside', 'position', get(cbar2, 'position'));
    end

    end














