clear
load_path
addpath /automount1/mgoes/matlab/m_map/

    filename = '../wavelet/ADT_0_5periods.mat';
    varId = {'ADT3','ADT4'}%'ADT0'
    varName = {'ADT3ADT4'}%'ADT0'
   %varId = {'ADT0','ADT1'}%'ADT0'
   % varName = {'ADT0ADT1'}%'ADT0'
    ADT = [];
    for ii=1:length(varId)
        load(filename,varId{ii});
        if ii==1,
    func = strcat('ADT = ',varId{ii},';')%,'; clear(',varId{ii},')')
        else
    func =strcat('ADT = ADT + ',varId{ii},';')%,'; clear(',varId{ii},')')
        end
        eval(func)
        clear(varId{ii})
    end


   nc=netcdf('../../AVISO/adt_week_SA.cdf');
LON = nc{'NBLONGITUDESN179_61'}(:);
LAT = nc{'NBLATITUDES307_413'}(:); 
TIME = nc{'TIME'}(:)/24 + julian(1950,1,1,0);
month = julian2month(TIME);
time = month2yfraction(month);
close(nc)

weekly = 0;
if ~weekly
%Make Monthly Data
 [ADT,std_mon,year_mon] = make_monthly_mean(ADT,month);
 ADT = ADT(10:end,:,:);year_mon = year_mon(10:end,:);
 time = month2yfraction(year_mon);
end

%Define Boundary Region as Index
 jj=83;ii=53:70; %Close to the AX97
display(sprintf('%s%4.2f%s%4.2f','lat = ',LAT(jj),' lon = ',LON(ii)))
    P1_AGU = nanmean(ADT(:,jj,ii),3);
   % P1_AGU = nanmean(ADT(:,jj,ii(1:end/2)),3)- nanmean(ADT(:,jj,ii(end/2+1:end)),3);


%%
nlon = length(LON);
nlat = length(LAT);

%c35 = nan*ones(1,nlon);
l35 = nan*ones(nlat,nlon);
c35_max = nan*ones(nlat,nlon);
%c35_agu = nan*ones(1,nlon);
l35_agu = nan*ones(nlat,nlon);
c35_max_agu = nan*ones(nlat,nlon);
%!stop

for ii=1:nlon
    for jj=1:nlat;
p=P1_AGU;q=ADT(:,jj,ii);
acha=find(~isnan(p+q));
  if ~isempty(acha)
  p=p(acha)-mean(p(acha));
  q=q(acha)-mean(q(acha));
if ~weekly
   [C,LAGS] = xcorr(q,p,300,'coeff');%monthly
else
   [C,LAGS] = xcorr(q,p,52,'coeff');%weekly
end
  lc = length(C);
  %c35(jj,ii) = C(ceil(lc/2));
  [~,ai] = max((C));
  l35(jj,ii) = LAGS(ai);
  c35_max(jj,ii) = C(ai);
  end


%end
    end %jj
end %ii

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%PLOTA O INDICE A SER USADO
figure(3),clf
set(gcf,'PaperPosition',[1 1 7 3]);
set(gcf,'DefaultLineLineWidth',1.5)
wysiwyg
plot(time,P1_AGU,'k')%,'linewidth',3)
%xlim([1958 2009])
grid on
set(gca,'fontsize',14)%,'ytick',0.1:0.025:0.2)
xlabel('Year')
ylabel('BC ADT (cm)')
%save2eps('WSstrength.eps',3,300)
%stop

%% PLOTTING SECTION
 figure(2),clf
set(gcf,'PaperPosition',[1 1 7 8]); 
set(gcf,'DefaultLineLineWidth',1.5)
wysiwyg;

%subplot('position',[.1 .53 .75 .35])%2,1,1), 
subplot('position',[.1 .65 .75 .25])%2,1,1), 
m_proj('mercator','lat',[-36.75 -15.25],'long',[-55 20]);
[~,h]=m_contourf(LON,LAT,l35)%,[-50:10:250]);%20);
%caxis([-20 250])
set(h,'edgecolor','none')
m_grid
m_coast('patch',[1 .85 .7]);
axis image,axis normal
m_text(-54,-18,'(a)','fontsize',14,'fontweight','bold')
% col=colorbar('position',[.86 .53 .02 .35]);
 col=colorbar('position',[.86 .65 .02 .25]);
if ~weekly
 set(get(col,'ylabel'),'String','LAG (months)')
else
    set(get(col,'ylabel'),'String','LAG (weeks)')
end
subplot('position',[.1 .35 .75 .25])%2,1,1), 
% subplot('position',[.1 .1 .75 .35])%subplot(2,1,2), 
 m_proj('mercator','lat',[-36.75 -15.25],'long',[-55 20]);
[~,h]=m_contourf(LON,LAT,c35_max,20);%[0:.05:0.8])%,xlim([-55 20])
 set(h,'edgecolor','none')
m_coast('patch',[1 .85 .7]);
m_grid
axis image,axis normal
m_text(-54,-18,'(b)','fontsize',14,'fontweight','bold')
%  col=colorbar('position',[.86 .1 .02 .35]);
  col=colorbar('position',[.86 .35 .02 .25]);
set(get(col,'ylabel'),'String','R')

subplot('position',[.1 .1 .75 .2])%2,1,1), 
plot(time,P1_AGU,'k')%,'linewidth',3)
xlim([1992.5 2014])
grid on
set(gca,'fontsize',14)%,'ytick',0.1:0.025:0.2)
xlabel('Year')
ylabel('BC ADT (cm)')
text(1993,4,varId)
figname = sprintf('%s%s%s%d%s','corr_map_ind_BC_',varName{:},'w',weekly,'.eps')
%figname = char(strcat('corr_map_ind_BC_',varName,'.eps'));
% save2eps(figname,2,300)
%%  
 return

%   figure(3),clf
% set(gcf,'PaperPosition',[1 1 7 5]); 
% set(gcf,'DefaultLineLineWidth',1.5)
% wysiwyg;subplot('position',[.1 .53 .75 .35])%2,1,1), 
% m_proj('mercator','lat',[-36.75 -25.25],'long',[-55 20]);
% [~,h]=m_contourf(LON,LAT,l35_agu,20);
% set(h,'edgecolor','none')
% m_grid
% m_coast('patch',[1 .85 .7]);
% axis image,axis normal
%  title('LAG (months)')
%  col=colorbar('position',[.86 .53 .02 .35]);
% set(get(col,'ylabel'),'String','LAG (months)')
% 
%  subplot('position',[.1 .1 .75 .35])%subplot(2,1,2), 
%  m_proj('mercator','lat',[-36.75 -25.25],'long',[-55 20]);
% [~,h]=m_contourf(LON,LAT,c35_max_agu,20);%[0:.05:0.8])%,xlim([-55 20])
%  set(h,'edgecolor','none')
% m_grid
% m_coast('patch',[1 .85 .7]);
% axis image,axis normal
%  title('R')
%   col=colorbar('position',[.86 .1 .02 .35]);
% set(get(col,'ylabel'),'String','R')

