% compute AMO / PMO using North Atlantic / North Pacific Surrogates


% load NA mean of all realizations

load surrogates

load /crane/s1/smiller/CMIP5/tos/GISS-E2-R/AMO_out_2.mat model_r_mean  

NA_avg_SST=model_r_mean(:,3);

clear model_r_mean

% project CMIP5 mean using slope of 1975-2005 trend

time=[1854:2005]';
p=polyfit(time(122:152),NA_avg_SST(122:152),1);
for n=153:159
    NA_avg_SST(n)=NA_avg_SST(n-1)+p(1,1);
end


% regress NA surrogates onto CMIP5 NA avg SST compute
% residual (Atlantic) 

for i = 1:170
    for n = 1:101

    p = polyfit(NA_avg_SST,surroA(:,n,i),1);  % regress NA surrogate onto CMIP5 NA SST
    yfit = polyval(p,NA_avg_SST);  % predict NA avg using p and CMIP5 NA SST
    surroAMO_reg(:,n,i) = demean(surroA(:,n,i) - yfit);  %  compute residuals of NA avg and prediction, mean center
    surroAMO_reglp(:,n,i) = lowpassadaptive(surroAMO_reg(:,n,i),0.025);  %  low pass filter
    
    end
end

save /crane/s1/smiller/CMIP5/tos/GISS-E2-R/AMO_PMO_surrogates

% calculate detrend AMO

for i = 1:170
    for n = 1:101
    
    surroAMO_det(:,n,i) = detrend(surroA(:,n,i));
    surroAMO_detlp(:,n,i) = lowpassadaptive(surroAMO_det(:,n,i),0.025);
    
    end
end

clear surroA

save /crane/s1/smiller/CMIP5/tos/GISS-E2-R/AMO_PMO_surrogates


% load NP mean of all realizations

load surrogates

load /crane/s1/smiller/CMIP5/tos/GISS-E2-R/PDO_out.mat model_r_mean  

NP_avg_SST=model_r_mean(:,3);

clear model_r_mean

% project CMIP5 mean using slope of 1975-2005 trend

time=[1854:2005]';
p=polyfit(time(122:152),NP_avg_SST(122:152),1);
for n=153:159
    NP_avg_SST(n)=NP_avg_SST(n-1)+p(1,1);
end


% regress NP surrogates onto CMIP5 NP avg SST compute
% residual (Atlantic) 

for i = 1:170
    for n = 1:101

    p = polyfit(NP_avg_SST,surroP(:,n,i),1);  % regress NP surrogate onto CMIP5 NA SST
    yfit = polyval(p,NP_avg_SST);  % predict NP avg using p and CMIP5 NP SST
    surroPMO_reg(:,n,i) = demean(surroP(:,n,i) - yfit);  %  compute residuals of NP avg and prediction, mean center
    surroPMO_reglp(:,n,i) = lowpassadaptive(surroPMO_reg(:,n,i),0.025);  %  low pass filter
    
    end
end

save /crane/s1/smiller/CMIP5/tos/GISS-E2-R/AMO_PMO_surrogates

% calculate detrend PMO

for i = 1:170
    for n = 1:101
    
    surroPMO_det(:,n,i) = detrend(surroP(:,n,i));
    surroPMO_detlp(:,n,i) = lowpassadaptive(surroPMO_det(:,n,i),0.025);
    
    end
end

clearvars i n p surroA surroP time yfit NA_avg_SST NP_avg_SST

save /crane/s1/smiller/CMIP5/tos/GISS-E2-R/AMO_PMO_surrogates

% compute cross correlations for AMO/PMO series with 20 year max lags

for i = 1:170
    for n = 1:100
        
    det_corr(:,n,i) = crosscorr(surroAMO_detlp(:,n,i),surroPMO_detlp(:,n,i),20);
    
    end
end

for i = 1:170
    for n = 1:100
        
    reg_corr(:,n,i) = crosscorr(surroAMO_reglp(:,n,i),surroPMO_reglp(:,n,i),20);
    
    end
end

clearvars i n

save /crane/s1/smiller/CMIP5/tos/GISS-E2-R/AMO_PMO_surrogates

% find the lag of the maximum correlation

det_max=max(det_corr);

for i=1:100
    for n=1:170
    [det_row(1,i,n),det_col(1,i,n)]=find(det_corr(:,i,n)==det_max(:,i,n));
    end
end

reg_max=max(reg_corr);

for i=1:100
    for n=1:170
    [reg_row(1,i,n),reg_col(1,i,n)]=find(reg_corr(:,i,n)==reg_max(:,i,n));
    end
end

det_max=squeeze(det_max);
reg_max=squeeze(reg_max);

clear reg_col det_col i n

% histogram all correlations

binrange=[-1:0.01:1];

det_bin=histc(reshape(det_corr,size(det_corr,1)*size(det_corr,2)*size(det_corr,3),1),binrange);
reg_bin=histc(reshape(reg_corr,size(reg_corr,1)*size(reg_corr,2)*size(reg_corr,3),1),binrange);

% histogram max correlations

det_binmax=histc(reshape(det_max,size(det_max,1)*size(det_max,2),1),binrange);
reg_binmax=histc(reshape(reg_max,size(reg_max,1)*size(reg_max,2),1),binrange);

% historgram max correlation lags

binrange_row=[1:1:41];

det_binrow=histc(reshape(det_row,size(det_row,2)*size(det_row,3),1),binrange_row);

reg_binrow=histc(reshape(reg_row,size(reg_row,2)*size(reg_row,3),1),binrange_row);

save /crane/s1/smiller/CMIP5/tos/GISS-E2-R/AMO_PMO_surrogates