% Compare all indices to cable transport
%
iplot = 1;  % plot raw time series figures
iplot = 0;  % only plot correlations

% 
% first load cable data
%
load /Molly/work/data/cable/cable_transports.mat
% cable_transport
% cable_date
cable_transport = gapfill (cable_transport);

%
% get rid of Nans (will by definition be beginning and trailing Nans)
% because we just interpolated over interior gaps
%
LL=isnan(cable_transport);
if length(LL) > 0
    cable_transport(LL) = [];
    cable_date(LL) = [];
end

d=dir('*_smooth_filtered.mat');

for II = 1: length(d)
    eval( ['load ' d(II).name] )
    L = findstr(d(II).name,'_smooth');
    L1 = find(d(II).name == '_');
    index_name = d(II).name(1:L(1)-1)
    short_name = d(II).name(1:L1(1)-1)
    
    eval(['new_' index_name ' = interp1 (' index_name '_date , ' index_name ' ,  cable_date);'])

    if iplot
        figure
            eval(['plot(cable_date, ((cable_transport-meanmiss(cable_transport)) * stdmiss(new_' index_name ')/(std(cable_transport))))'])
            hold on
            eval(['plot(' index_name '_date, ' index_name '-meanmiss(new_' index_name ') )'])
            hold on
            datetick
            eval(['plot(cable_date, new_' index_name '-meanmiss(new_' index_name '), ''k'',''linewidth'',2)'])
            title(index_name);
    end

    % get rid of beginning/trailing blanks
    % Note there are no Nans in Cable_transport
    temp_cable_transport = cable_transport;
    temp_cable_date = cable_date;
    eval(['temp = new_' index_name ';'])

    eval(['LL = find(isnan(new_' index_name '));'])

%    eval(['new_' index_name ' (LL) = [];'])
    temp_cable_transport(LL) = [];
    temp_cable_date(LL) = [];
    temp(LL) = [];
    
    eval(['[max_ccx, max_lag] = check_corr4(temp, temp_cable_transport, [''' index_name ' vs cable_transport''], ''days'', 900, 1)'])

    eval(['clear ' index_name '*'])
end

%
%  Very interesting!
%  *many* indices are related to the FC transport.
%  I should do a ships type analysis to see if the FC transport can be
%  "predicted" by using some combination of these indices.
%  Those that need to be examoned include:
%   car lead cable by 284/303 days + corr
%   nta lags cable by 674/668 days + corr
%   aao lead cable by 204 days, lags by 32 (95%) + corr
%   ammsst leads cable by 834/837 days (90%) + corr
%   amo_sm not signifigant
%   amo_us lead cable by 364/375 days (95%) + corr

%   ao  lags cable by 426 (98%) and 713 (99%) days - corr

%   atltri leads cable by 753 (95%) days + corr
%   brazilianrain lags cable by 799 days (95%)  - corr
%   censo leads cable by 632 days (90%) - corr
%   eofpac leads cable by 626 days (95%) - corr

%   eop leads cable by 623 days (98%) - corr 

%   espi leads cable by 321 days (95%) + corr
%   gmsst not significant
%   hurr leads cable by 427 (791) days (90%) + corr
%   indianmon leads cable by 221 days (90%) - corr

%   jones nao lags cable 344 days (99%) - corr

%   mei leads cable by 609/585 days (90%) - corr
%   nao lags cable by 680 (294) days (90%) - corr
%     and leads (lags) cable by 815 (865) days (98%) + corr
%   nina1 lags (leads) cable by 844 (436) days (90%) - corr
%   nina34 leads cable by 505/506 days (95%) - corr
%   nina3 leads cable by 471/482 days (>90%) - corr
%   nina4 leads (lags) cable by 561 (145) days (90%) - corr

%   noi leads cable by 400-ish days  (98%) + corr
%     and lags cable by 826 days (>98%) - corr

%   np leads cable by 179 (19) days (95%) -(+) corr
%   oni leads cable by 629 days (90%) - corr
%   pacwarm leads cable by 479 days (>95%) + corr
%   pdo leads cable by (481) 305 (and 843 days) (-) + corr (95%) 
%   pna leads cable by 787 days (90%) - corr 
%   qbo lags cable by 374/368 days (>95%) - corr
%   sahelrain lags cable by 31 days (95%) + corr
%   soi leads cable 581-631 days (90%) + corr
%   solar leads cable 900 days (max lag) (95%)  + corr

%   swmonsoon leads cable by 278/279 days (98%) - corr

%   tna insignificant
%   tni lags cable by 550/538 days (90%) + corr
%   trend insignificant

%   tsa leads cable by 259/255 days (99%) + corr

%   whwp insignificant

%   wp leads cable by 658 (99%) AND lags by about 670 (99%)
LAG = 685;
temp_cable_transport = cable_transport;
temp_cable_date = cable_date;
% shift cable forward LAG
temp_cable_transport(1:LAG)=[];
% remove end of wp time series
temp = new_wp;
temp(end-LAG+1:end) = [];
% use actual cable time
temp_cable_date(1:LAG) = [];
plot((temp-meanmiss(temp))*stdmiss(temp_cable_transport)/stdmiss(temp), temp_cable_transport-meanmiss(temp_cable_transport),'.')

check_corr4(temp,temp_cable_transport,' ' ,' ',900,1)

figure;
    plot(temp_cable_date, temp_cable_transport)
    hold on
    plot (temp_cable_date, (meanmiss(temp_cable_transport)-(temp-meanmiss(temp))*stdmiss(temp_cable_transport)/stdmiss(temp)), 'r')

