%%% CORRELATE TO SST%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%if 1
addpath /data/mgoes/matlab/m_map
oi=load('../wavelet/SST/SST_INTERANNUAL.mat');
%oi=load('../wavelet/SST/SST_INTERANNUAL_ANOMALY.mat');
load /data/mgoes/SA_gyre/wavelet/Felipe/mapa.mat
p1 = linspace(0,1,length(mapa));
mapa2 = interp1(p1,mapa,linspace(0,1,24));
[X,Y]=meshgrid(oi.lon,oi.lat);

ygfrac=month2yfraction(julian2month(oi.time));
%%
for nmod =1:3
[PC2,~,mo] = make_monthly_mean(real(PC(nmod,:)),month);
PC2=PC2(1:273);
CHP2 = squeeze(real(CHP(nmod,:,:)));
ADT0=oi.ADT0(1:273,:,:);
[nt0,ny0,nx0]=size(ADT0);
C=zeros(ny0,nx0);P=nan*C;

% ADT0=ADT0(1:end-2,:,:);
% PC2 = PC2(3:end);
% ygfrac=month2yfraction(julian2month(oi.time(1:end-2)));

%DJF = find(str2num(mo(:,5:6))==12 | str2num(mo(:,5:6))<3);DJF=DJF(1:end-1);
%MAM = find(str2num(mo(:,5:6))>=3 & str2num(mo(:,5:6))<6);
for ii=1:ny0
for jj=1:nx0
[c,p] = corrcoef(PC2(:),squeeze(ADT0(:,ii,jj)));
%[c,p] = corr2_AR(PC2',squeeze(ADT0(:,ii,jj)));
c=c(2,1);p=p(2,1);
C(ii,jj)=c;
if p<0.05,P(ii,jj)=1;end
end
end

nfilt = 12;
neff = nt0/nfilt;
t=abs(C).*sqrt( (neff-2)./(1-C.^2) );
PVAL=2*tcdf( -t,neff-2 );
P= double(PVAL<0.05); P(P==0)=nan;
ui = find(~isnan(P));

figure;clf;
set(gcf,'PaperPosition',[1 1 7 7]); 
set(gcf,'DefaultLineLineWidth',1)%.5)
wysiwyg;

%dois = subplot('Position',[0.1900 0.1100 0.6550 0.3412]);
dois = subplot('Position',[0.1900 0.3100 0.6412 0.2 ]);%7550 ]);
m_proj('miller','long',[-55 20],'lat',[-35 -20]);
m_contourf(lon,lat,CHP2,'edgecolor','none')
m_coast('patch',[.5 .5 .5])%,'linewidth',3);
hold on
m_contour(oi.lon,oi.lat,C,0:.1:1,'k')
m_contour(oi.lon,oi.lat,C,-1:.1:0,'k--')
m_contour(oi.lon,oi.lat,C,[0 0],'k','linewidth',3)
m_text(-53,-23,'b.','backgroundcolor',[1 1 1],'fontweight','bold')
m_grid('linest','none','tickdir','in','fontsize',12,'linewidth',2);
colormap(mapa2);%brighten(0.5)
caxis([-0.02 0.02])
axis normal 
%um = subplot('Position',[0.1900 0.5800 0.6550 0.3412]);%subplot(2,1,1)
um = subplot('Position',[0.1900 0.5800 0.6412 0.3412]);%subplot(2,1,1)
m_proj('miller','long',[-180 180],'lat',[-60 30]);
m_contourf(oi.lon,oi.lat,C,-1:.1:1,'edgecolor','none')
hold on
m_scatter(X(ui),Y(ui),1,'k.')
m_coast('color',[0 0 0],'linewidth',3);
m_grid('linest','none','tickdir','out','fontsize',12,'linewidth',2)%,'box','fancy');
colormap(mapa2);%brighten(0.5)
caxis([-.601 .601])
m_text(-175,18,'a.','backgroundcolor',[1 1 1],'fontweight','bold')
axis normal
%%m_line([28 28;15 15;15 28;28 15]',[-18 0;0 -18;0 0;-18 -18]','color',[0 0 0])
%%slp_reg1 = nanmean(nanmean((ADT0(:,oi.lat>-18&oi.lat<0,oi.lon<28&oi.lon>15)),2),3);
if nmod==1
    llon = [-5 -25];
    llat = [-40 -30];   %original
slp_reg = nanmean(nanmean((ADT0(:,oi.lat>llat(1)&oi.lat<llat(2),oi.lon<llon(1)&oi.lon>llon(2))),2),3);
m_line([llon([1 1]);llon([2 2]);llon([2 1]);llon]',[llat;llat([2 1]);llat([2 2]);llat([1 1])]','color',[0 0 0])
elseif nmod==2
    llat = [-20 -10];
%    llon = [-70 -90];   
    llon = [10 -30];   
%slp_reg = -nanmean(nanmean((ADT0(:,oi.lat>-35&oi.lat<-20,oi.lon<10&oi.lon>-10)),2),3);
%m_line([10 10;-10 -10;-10 10;10 -10]',[-35 -20;-20 -35;-20 -20;-35 -35]','color',[0 0 0])
slp_reg = nanmean(nanmean((ADT0(:,oi.lat>llat(1)&oi.lat<llat(2),oi.lon<llon(1)&oi.lon>llon(2))),2),3);
m_line([llon([1 1]);llon([2 2]);llon([2 1]);llon]',[llat;llat([2 1]);llat([2 2]);llat([1 1])]','color',[0 0 0])
elseif nmod==3
    llon = [-150 -180];
    llat = [-10 10];   %original
slp_reg = nanmean(nanmean((ADT0(:,oi.lat>llat(1)&oi.lat<llat(2),oi.lon<llon(1)&oi.lon>llon(2))),2),3);
%slp_reg = -nanmean(nanmean((ADT0(:,oi.lat>-60&oi.lat<40,oi.lon<-90&oi.lon>-150)),2),3);
%m_line([-160 -160;-180 -180;-180 -160;-160 -180]',[-20 0;0 -20;0 0;-20 -20]','color',[0 0 0])
m_line([llon([1 1]);llon([2 2]);llon([2 1]);llon]',[llat;llat([2 1]);llat([2 2]);llat([1 1])]','color',[0 0 0])

col=colorbar('position',[.85 0.58 .02 .3412 ]);
%col=colorbar('horizontal','position',[.2 .51 .6 .02]);
set(col,'fontsize',12,'xtick',[-.5:.25:.5])
end
R = corr(slp_reg,PC2');
%slp_reg=slp_reg3;%-slp_reg1;
um = subplot('Position',[0.19 0.0700 0.6412 0.18]);%figure
plot(ygfrac,(PC2(:)-mean(PC2))/std(PC2))
hold on
plot(ygfrac,sign(R)*(slp_reg(:)-mean(slp_reg))/std(slp_reg))
axis tight;ylim([-3.5 3.5])
set(gca,'linewidth',1.5)
text(1990,2.5,'c.','backgroundcolor',[1 1 1],'fontweight','bold')
leg=legend(sprintf('%s%d','PC',nmod),'SST','location','southwest');set(leg,'box','off','orientation','horizontal')%outside')
%stop
figname = sprintf('%s%d%s','CORR_SST_MODE',nmod,'_b.jpg')
%print('-dpng','-r400',figname)
end %for nmod

return