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

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,:);

%1984-2014
for it = 1:372/12
SST_yr(:,:,it)=nanmean(tos2(:,:,(it-1)*12+1:(it-1)*12+12),3);
end

lon_loc=lon2>=100 &lon2<=156;
lat_loc=lat2>=10 &lat2<=43;
time=linspace(1984,2014,31);
time2=linspace(1984,2000,17);
time3=linspace(2001,2014,14);

% SST_yr_range=SST_yr(lon_loc,lat_loc,115:144);
SST_avg1=nanmean(SST_yr,3);
SST_yr_2000=SST_yr(:,:,1:17); SST_avg2=nanmean(SST_yr_2000,3);
SST_yr_2015=SST_yr(:,:,18:31); SST_avg3=nanmean(SST_yr_2015,3);

%1994-1998
% SST_avg1=nanmean(SST_yr(lon_loc,lat_loc,10:15),3);

for i=1:57
  for j=1:105
     b1 = polyfit(time,squeeze(SST_yr(i,j,:))',1);
     A1(i,j)=b1(1);
     b2 = polyfit(time2,squeeze(SST_yr_2000(i,j,:))',1);
     A2(i,j)=b2(1);
     b3 = polyfit(time3,squeeze(SST_yr_2015(i,j,:))',1);
     A3(i,j)=b3(1);
  end
end

lon_loc=lon2>=100 &lon2<=140;lat_loc=lat2>=10 &lat2<=40;
[xx,yy]=meshgrid(lon2(lon_loc),lat2(lat_loc));
%========= shift =======%
SST_annual=circshift(SST_avg1,-1,1);
SST_trend=circshift(A1,-1,1);

set(0,'defaultfigurecolor','w')
subplot(1,2,1)
m_proj('miller','lat',[10,40],'lon',[100,140]);
m_pcolor(xx,yy,SST_annual(lon_loc,lat_loc)');shading interp;hold on
m_coast('patch','w','edgecolor','k');
m_grid('linestyle','none','Fontsize',20,'Linewidth',3);
title({'SST average (1994-2014)'})
colormap(flipud(brewermap(30, 'RdYlBu')));
% colormap('jet')
c = colorbar('location','southoutside');c.Title.String = "C";
set(c,'FontSize',15);caxis([15 30])
set(gca,'Fontsize',20);set(gca,'Linewidth',3);


subplot(1,2,2)
m_proj('miller','lat',[10,40],'lon',[100,140]);
m_pcolor(xx,yy,SST_trend(lon_loc,lat_loc)');shading interp;hold on
m_coast('patch','w','edgecolor','k');
m_grid('linestyle','none','Fontsize',20,'Linewidth',3);
colormap(flipud(brewermap(30, 'RdYlBu')));
title({'SST trend (1984-2014)'})
c = colorbar('location','southoutside');c.Title.String = "yearly trend";
set(c,'FontSize',15);caxis([-0.1 0.1])
set(gca,'Fontsize',20);set(gca,'Linewidth',3);


%%


%linear trend :year mean
SST_linear=squeeze(nanmean(nanmean(SST_yr(lon_loc,lat_loc,:),1),2));
time=linspace(1984,2014,31);

b=polyfit(time,SST_linear',1);
fr=polyval(b,time);

set(0,'defaultfigurecolor','w')
plot(time,SST_linear','lineWidth',3);hold on
plot(time,fr,'lineWidth',3);
h1 = legend('Data', 'Linear trend', 'Location','NE')
[xlim ylim]
textposx = diff(xlim)*0.50+min(xlim);
textposy = diff(ylim)*0.95+min(ylim);
text(textposx,textposy, sprintf('y = %.2f*x %c %0.2f',...
     b(1), char(45-(sign(b(1))+1)), abs(b(2))),'FontSize', 20)
xlabel('year');ylabel('SST');title('SST trend')
set(gca,'Fontsize',20);set(gca,'Linewidth',3);

%Seasonal trend
time=linspace(1985,2014,30);









