clear
%close all

load_path
%addpath /automount1/mgoes/matlab/Hilbert_Huang
%addpath /automount1/mgoes/matlab/pcatool
%addpath /data/mgoes/matlab/mapcolor/colors/
%addpath /automount1/mgoes/matlab/m_map/
addpath /data/mgoes/SA_gyre/wavelet/Felipe
addpath /phodnet/share/mgoes/SUDIP/NINO
global lat lon

display('LOAD data...')
nino = 'nino34'
switch nino
    case 'tni'
load_tni;PC2 = weim(7,'rect',tni(1:299));
    case 'emi'
load_emi;PC2 = weim(7,'rect',emi(1:299));
    case 'nino34'
load_nino34;PC2 = weim(7,'rect',nino34(1:299));
    case 'nino4'
load_nino4;PC2 = weim(7,'rect',nino4(1:299));
end
yfrac = yfrac(1:299);

%stop
if 1
%% CORRELATE TO SLP%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
addpath /data/mgoes/matlab/m_map
oi=load('../wavelet/SLP/SLP_INTERANNUAL.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
%[PC2,~,mo] = make_monthly_mean(real(PC(nmod,:)),month);
%CHP2 = squeeze(real(CHP(nmod,:,:)));
%ADT0=oi.ADT0(1:277,:,:);
ADT0=oi.ADT0(1:299,:,:);
[nt0,ny0,nx0]=size(ADT0);
C=zeros(ny0,nx0);P=nan*C;
%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
ui = find(~isnan(P));


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

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','linewidth',2)%,'box','fancy');
colormap(mapa2);%brighten(0.5)
caxis([-.801 .801])
m_text(-175,18,'a.','backgroundcolor',[1 1 1],'fontweight','bold')
col=colorbar('position',[.85 0.58 .02 .3412 ]);
%col=colorbar('horizontal','position',[.2 .51 .6 .02]);
set(col,'fontsize',12,'xtick',[-.75:.25:.75])
axis normal
if nmod==1
    %DARWIN
    llon = [160 120];
    llat = [-20 5];
slp_reg1 = 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])
    llon = [-100 -150];
    llat = [-5 10];
slp_reg2 = 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])
end
slp_reg = slp_reg1-slp_reg2;
R = corr(slp_reg(:),PC2(:));
um = subplot('Position',[0.1900 0.3100 0.6412 0.2 ])%7550 ]);
plot(yfrac,(PC2(:)-mean(PC2))/std(PC2))
%hold on
%plot(yfrac,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(upper(nino),'SLP','location','northwest');set(leg,'box','off','orientation','horizontal')%outside')
%stop
%figname = sprintf('%s%d%s','CORR_SLP_MODE',nmod,'_b.jpg')
%print('-dpng','-r400',figname)
end %for nmod
end %if
%print('-djpeg','-r300',[nino,'_SLP.jpg'])
stop
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%HOVMOLLER
if 0
   [~,i22] = min(abs(lat+30));%22.5));
for ii = 1:N
    oo = zeros(nt,size(EOFs,2));
    for jj=ii:-1:1%:ii
   oo = oo + transpose(PC(jj,:))*conj(EOFs(jj,:)); 
    end
   reconstruction = mat2map(mask,real(oo));
   figure(10+ii)
   pcolor(lon,ydec,squeeze(reconstruction(:,i22,:)));shading interp
   caxis([-5 5])
end
end


if 0
[area,dx,dy]=sw_area(lon,lat);
clear u v
for ii=1:N
    %taux  = squeeze(theta(ii,:,:))*pi/180;
    taux = squeeze(theta(ii,:,:));taux(taux<0)=taux(taux<0)+360;
[dthetax,dthetay] = gradient(taux);
%[dthetax2,dthetay2] = gradient(taux2);
%dthetax = min(dthetax,dthetax2)
dt = 7*24*60*60;
dd = diff(phi(ii,:));
idd = find(abs(dd)>180);
phi2 = phi(ii,:);
for jj=1:length(idd)
    phi2(idd(jj)+1:end)=phi2(idd(jj)+1:end)+360;
end
dphidt = gradient(phi2*pi/180,dt);
for jj=1:nt
u(:,:,ii,jj) = -1*dphidt(jj)./(dthetax./dx);
v(:,:,ii,jj) = -1*dphidt(jj)./(dthetay./dy);
end
end
end %0

%CHP = mat2map(mask,EOFs);
NLAG = 5        %6;%4;%DT = 2;%7;Dt = 7;
if 0
PHI = imag(PC(:,1:NLAG));
else
%FIND PHI'S for each 45degree (veci):
PHI = zeros(N,NLAG);
for ii = 1:N
    dd = diff(phi(ii,:));
    %idd = find(abs(dd)>180); %dd<-180
    idd = find(abs(dd)>200); %dd<-180
 %  veci = -1*[180 90  0 -90 -180];
    veci = -1*[180 135 90 45 0];
    odd = find(diff(idd)>5);%odd=odd(1)
    vec = interp1(phi(ii,idd(odd(1))+1:idd(odd(2))),idd(odd(1))+1:idd(odd(2)),veci,'nearest','extrap');
    if sum(abs(phi(ii,vec)-veci))>100
     vec = interp1(phi(ii,idd(odd(2))+1:idd(odd(3))),idd(odd(2))+1:idd(odd(3)),veci,'nearest','extrap');
    end
   PHI(ii,1:NLAG) = PC(ii,vec(1:NLAG));
end
end
%
%PHI = (PC(:,1:NLAG));
dispCEOF_aviso(lon,lat,ydec,CHP,EXPVAR,PHI,1:N,PC)  %PC timeseries to implement still
%lon,lat,CHP,PC,EXPVAR,Dt,NLAG,1:N)
%dispEEOF_aviso_figures(lon,lat,ydec,CHP,PC,EXPVAR,Dt*DT,NLAG,1:N)
if 0
    for ii=1:N
     save2eps(sprintf('%s%d%s%d%s%d%s','CEOF',ii,'_LAG',NLAG,region,wave,'.eps'),ii,300)
    end
end

wave_global(real(PC(1,:)),time,7,1);
wave_global(real(PC(2,:)),time,7,0);
wave_global(real(PC(3,:)),time,7,0);
wave_global(real(PC(4,:)),time,7,0);

return

IMF = cell(nl,6);
%V   = nan*ones(1,nl);
B   = nan*ones(nl,6);

for ii=vec %1:nl
ii
   x = ADT(:,ii);
   imf = emd(x);
   ni = length(imf);
   b=[];d=[];
   for k = 1:ni
     b(k) = sum(imf{k}.*imf{k});
     th   = angle(hilbert(imf{k}));
     d{k} = diff(th)/Ts/(2*pi);
   end
 %   D(1:ni,ii) = d;
%[~,v] = sort(-b);
%b     = 1-b/max(b);
      B(ii,1:length(b)) = b;
   IMF(ii,1:length(imf)) = imf;

end

