% calculate North Atlantic average tos

antos_NAavg_dm=antos_dm.*cos(rlatnw/360*2*pi);
antos_NAavg_dm=sum(antos_NAavg_dm,2)./sum(cos(rlatnw/360*2*pi),2);

% calculate North Atlantic average of detrended annual tos

antos_det_NAavg=antos_det.*cos(rlatnw/360*2*pi);
antos_det_NAavg=sum(antos_det_NAavg,2)./sum(cos(rlatnw/360*2*pi),2);

% calculate average of North Atlantic tos minus global average tos 

antos_dglo_NAavg=antos_dglo.*cos(rlatnw/360*2*pi);
antos_dglo_NAavg=sum(antos_dglo_NAavg,2)./sum(cos(rlatnw/360*2*pi),2);

% calculate average of North Atlantic tos minus NH average tos 

antos_dNHavg_NAavg=antos_dNHavg.*cos(rlatnw/360*2*pi);
antos_dNHavg_NAavg=sum(antos_dNHavg_NAavg,2)./sum(cos(rlatnw/360*2*pi),2);

% create output variable

out(:,1)=antos_gloavg_dm(:,1);
out(:,2)=antos_NHavg_dm(:,1);
out(:,3)=antos_NAavg_dm;
out(:,4)=antos_det_NAavg;
out(:,5)=lowpassadaptive(out(:,4),0.025);
out(:,6)=antos_dglo_NAavg;
out(:,7)=antos_dNHavg_NAavg;

load /crane/s1/smiller/CMIP5/tos/GISS-E2-R/AMO_out_2_GISS.mat model_r_mean
model_r_mean_GISS=model_r_mean;
load /crane/s1/smiller/CMIP5/tos/GISS-E2-R/AMO_out_2.mat model_r_mean

if size(out,1) == 146
    model_r_mean(1:10,:)=[];
    model_r_mean_GISS(1:10,:)=[];
elseif size(out,1) == 155
    model_r_mean(1,:)=[];
    model_r_mean_GISS(1,:)=[];
elseif size(out,1) == 145
    model_r_mean(1:11,:)=[];
    model_r_mean_GISS(1:11,:)=[];
end

% regress NA avg onto CMIP5-all ensemble mean global SST and 40 yr lowpass
% residuals...works for runs of various length

p = polyfit(model_r_mean(:,1),out(:,3),1);  % regress NA avg onto ensemble global SST
yfit = polyval(p,model_r_mean(:,1));  % predict NA avg using p and ensemble global SST
out(:,8) = out(:,3) - yfit;  %  compute residuals of NA avg and prediction
out(:,9) = lowpassadaptive(out(:,8),0.025);  %  low pass filter

% regress NA avg onto CMIP5-all ensemble mean AMO region SST and 40 yr lowpass
% residuals

p = polyfit(model_r_mean(:,3),out(:,3),1);  % regress NA avg onto ensemble NA SST
yfit = polyval(p,model_r_mean(:,3));  % predict NA avg using p and ensemble NA SST
out(:,10) = out(:,3) - yfit;  %  compute residuals of NA avg and prediction
out(:,11) = lowpassadaptive(out(:,10),0.025);  %  low pass filter

% compute simple difference between NA average and CMIP5-all ensemble mean
% AMO region SST and 40 yr lowpass residuals

out(:,12) = out(:,3) - model_r_mean(:,3);
out(:,13) = lowpassadaptive(out(:,12),0.025);

% regress NA avg onto CMIP5-All ensemble mean
% AMO region SST (reduced climate sensitivity) and 40 yr lowpass residuals

sens = 0.78;
p = polyfit(model_r_mean(:,3)*sens,out(:,3),1);  % regress NA avg onto NA SST
yfit = polyval(p,model_r_mean(:,3)*sens);  % predict NA avg using p and NA SST
out(:,14) = out(:,3) - yfit;  %  compute residuals of NA avg and prediction
out(:,15) = lowpassadaptive(out(:,14),0.025);  %  low pass filter

% compute simple difference between NA average and CMIP5 ensemble mean
% AMO region SST (reduced climate sensitivity) and 40 yr lowpass residuals

out(:,16) = out(:,3) - model_r_mean(:,3)*sens;
out(:,17) = lowpassadaptive(out(:,16),0.025);


% create output variable for models that use aerosol indirect effects
% these results are only saved (using the batch scripts) if the model
% includes aerosol indirect effects

% create output variable

out_ae(:,1)=antos_gloavg_dm(:,1);
out_ae(:,2)=antos_NHavg_dm(:,1);
out_ae(:,3)=antos_NAavg_dm;
out_ae(:,4)=antos_det_NAavg;
out_ae(:,5)=lowpassadaptive(out(:,4),0.025);
out_ae(:,6)=antos_dglo_NAavg;
out_ae(:,7)=antos_dNHavg_NAavg;

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

if size(out,1) == 146
    model_r_mean_ae(1:10,:)=[];
elseif size(out,1) == 155
    model_r_mean_ae(1,:)=[];
elseif size(out,1) == 145
    model_r_mean_ae(1:11,:)=[];
end

% regress NA avg onto CMIP5-all ensemble mean global SST and 40 yr lowpass
% residuals...works for runs of various length

p = polyfit(model_r_mean_ae(:,1),out(:,3),1);  % regress NA avg onto ensemble global SST
yfit = polyval(p,model_r_mean_ae(:,1));  % predict NA avg using p and ensemble global SST
out_ae(:,8) = out(:,3) - yfit;  %  compute residuals of NA avg and prediction
out_ae(:,9) = lowpassadaptive(out_ae(:,8),0.025);  %  low pass filter

% regress NA avg onto CMIP5-all ensemble mean AMO region SST and 40 yr lowpass
% residuals

p = polyfit(model_r_mean_ae(:,3),out(:,3),1);  % regress NA avg onto ensemble NA SST
yfit = polyval(p,model_r_mean_ae(:,3));  % predict NA avg using p and ensemble NA SST
out_ae(:,10) = out(:,3) - yfit;  %  compute residuals of NA avg and prediction
out_ae(:,11) = lowpassadaptive(out_ae(:,10),0.025);  %  low pass filter

% compute simple difference between NA average and CMIP5-all ensemble mean
% AMO region SST and 40 yr lowpass residuals

out_ae(:,12) = out(:,3) - model_r_mean_ae(:,3);
out_ae(:,13) = lowpassadaptive(out_ae(:,12),0.025);

% regress NA avg onto CMIP5-All ensemble mean
% AMO region SST (reduced climate sensitivity) and 40 yr lowpass residuals

sens = 0.78;
p = polyfit(model_r_mean_ae(:,3)*sens,out(:,3),1);  % regress NA avg onto NA SST
yfit = polyval(p,model_r_mean_ae(:,3)*sens);  % predict NA avg using p and NA SST
out_ae(:,14) = out(:,3) - yfit;  %  compute residuals of NA avg and prediction
out_ae(:,15) = lowpassadaptive(out_ae(:,14),0.025);  %  low pass filter

% compute simple difference between NA average and CMIP5 ensemble mean
% AMO region SST (reduced climate sensitivity) and 40 yr lowpass residuals

out_ae(:,16) = out(:,3) - model_r_mean_ae(:,3)*sens;
out_ae(:,17) = lowpassadaptive(out_ae(:,16),0.025);

