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


in1='/data3/yiwen/Timcom_exp/EXP_TIMCOM/change_wind_shr_45/GTJRAv2.10/ocn/hist/TOPO.nc'
ncid=netcdf.open(in1,'NC_NOWRITE');
varid = netcdf.inqVarID(ncid,'lon'); lon=netcdf.getVar(ncid,varid,'double');
varid = netcdf.inqVarID(ncid,'lat'); lat=netcdf.getVar(ncid,varid,'double');
netcdf.close(ncid)

pathi= '/data3/yiwen/Timcom_exp/matlab_code/SIC_renew/'
load([pathi,'SIC'])
load([pathi,'SIC_V&F'])

%==============================================
%interplation [Xq,Yq,vq] = griddata(x,y,v,xq,yq)
%==============================================
AREA={AREA_control AREA_45  AREA_FWF45 }
% Uvel={Uvel_C Uvel_45 Uvel_NS}
% Vvel={Vvel_C Vvel_45 Vvel_NS}
% AREATD={AREATD_C AREATD_45 AREATD_NS}

Uvel={Uvel_mean(:,:,1) Uvel_mean(:,:,2) Uvel_mean(:,:,3) }
Vvel={Vvel_mean(:,:,1) Vvel_mean(:,:,2) Vvel_mean(:,:,3) }
AREATD={AREATD_mean(:,:,1) AREATD_mean(:,:,2) AREATD_mean(:,:,3) }


[LON,LAT]=meshgrid(lon,lat);
lat_real=linspace(-90,85,288);
lon_real=linspace(0,360,320);
[xq,yq] = meshgrid(lon_real,lat_real);

for ii=1:length(AREA)
AREA_N{ii}=griddata(LON,LAT,AREA{ii}',xq,yq);
Uvel_N{ii}=griddata(LON,LAT,Uvel{ii}',xq,yq);
Vvel_N{ii}=griddata(LON,LAT,Vvel{ii}',xq,yq);
AREATD_N{ii}=griddata(LON,LAT,AREATD{ii}',xq,yq);
end

%==============================
%let map==[-180~180]
%==============================
for i=1:length(AREA)
AREA_N2{i}=circshift(AREA_N{i},-180+20,2);
Uvel_N2{i}=circshift(Uvel_N{i},-180+20,2);
Vvel_N2{i}=circshift(Vvel_N{i},-180+20,2);
AREATD_N2{i}=circshift(AREATD_N{i},-180+20,2);
end

for i=1:length(AREA)
AREA_N2{i}(:,160:162)=[];
Uvel_N2{i}(:,160:162)=[];
Vvel_N2{i}(:,160:162)=[];
AREATD_N2{i}(:,160:162)=[];
end


lon2=linspace(-180,180,320-3);
lat2=linspace(-90,85,288);

%range%
lon_loc=lon2>=-120 & lon2<=100;
lat_loc=lat2>=30 & lat2<=85;
[xx,yy]=meshgrid(lon2(lon_loc),lat2(lat_loc));

%%
set(0,'defaultfigurecolor','w')
x=1:2;y=1:2;
vecx=[1:3:length(lon2(lon_loc))];vecy=[1:3:length(lat2(lat_loc))];
Uvel_C=Uvel_N2{1}(lat_loc,lon_loc);Vvel_C=Vvel_N2{1}(lat_loc,lon_loc);
Uvel_45=Uvel_N2{2}(lat_loc,lon_loc);Vvel_45=Vvel_N2{2}(lat_loc,lon_loc);
    m_proj('miller','lat',[30,85],'lon',[-120,100]);
    m_pcolor(xx,yy,AREATD_N2{1}(lat_loc,lon_loc));shading flat;hold on
    m_contour(xx,yy,AREA_N2{1}(lat_loc,lon_loc).*100,[15 15],'LineColor','g','LineWidth',2.0,'Fill','off');hold on
    m_contour(xx,yy,AREA_N2{2}(lat_loc,lon_loc).*100,[15 15],'LineColor','k','LineWidth',1.5,'Fill','off');hold on
    m_contour(xx,yy,AREA_N2{3}(lat_loc,lon_loc).*100,[15 15],'LineColor','r','LineWidth',1.5,'Fill','off');hold on
%     m_contour(xx,yy,AREA_N2{4}(lat_loc,lon_loc).*100,[15 15],'LineColor','b','LineWidth',1.5,'Fill','off');hold on
%     m_contour(xx,yy,AREA_N2{5}(lat_loc,lon_loc).*100,[15 15],'LineColor','y','LineWidth',1,'Fill','off');hold on
    h1=m_line(x,y,'Color','g');h2=m_line(x,y,'Color','k');h3=m_line(x,y,'Color','r');
    h4=m_line(x,y,'Color','b');h5=m_line(x,y,'Color','y');
    m_coast('patch','w','edgecolor','k');
    m_grid('box','fancy','linestyle','none');
%     legend([h1,h2,h3,h4,h5],'control','WIND0.1:45','WIND0.1:NS','HOST45','HOSTNS')
    legend([h1,h2,h3],'control','WIND0.1:45','HOST45')
    title('TIMCOM :SIC','Fontsize',18)
    colormap(flipud(brewermap(30, 'RdBu')));
    c = colorbar('location','southoutside');
    c.Title.String='area tendency dynamics [%/day]'
    caxis([-1 1])

    %================%
    %cal vector scale%
    %================%
    scale_factor=0.5
    q1=m_quiver(xx(vecy,vecx),yy(vecy,vecx),Uvel_45(vecy,vecx)*scale_factor,Vvel_45(vecy,vecx)*scale_factor,...
               'AutoScale','off')
    q2=m_quiver(50,60,0.01*scale_factor,0,'AutoScale','off')
    q1.Color = 'k';q2.Color = 'k';
    text(1.05,1.25,'1 m/s')



