% calculate using full CMIP5 ensemble

% add NaN columns to make model series equal length

nan50=nan(1,17);

nan50_59=repmat(nan50,10,1);

nan50_60=repmat(nan50,11,1);

% GFDL_CM3 add nan to 1850-1859

out_GFDL_CM3_r1i1p1 = cat(1,nan50_59,out_GFDL_CM3_r1i1p1);

out_GFDL_CM3_r2i1p1 = cat(1,nan50_59,out_GFDL_CM3_r2i1p1);

out_GFDL_CM3_r3i1p1 = cat(1,nan50_59,out_GFDL_CM3_r3i1p1);

out_GFDL_CM3_r4i1p1 = cat(1,nan50_59,out_GFDL_CM3_r4i1p1);

out_GFDL_CM3_r5i1p1 = cat(1,nan50_59,out_GFDL_CM3_r5i1p1);

% GFDL_CM2p1 add nan to 1850-1860

out_GFDL_CM2p1_r1i1p1 = cat(1,nan50_60,out_GFDL_CM2p1_r1i1p1);

out_GFDL_CM2p1_r2i1p1 = cat(1,nan50_60,out_GFDL_CM2p1_r2i1p1);

out_GFDL_CM2p1_r3i1p1 = cat(1,nan50_60,out_GFDL_CM2p1_r3i1p1);

out_GFDL_CM2p1_r4i1p1 = cat(1,nan50_60,out_GFDL_CM2p1_r4i1p1);

out_GFDL_CM2p1_r5i1p1 = cat(1,nan50_60,out_GFDL_CM2p1_r5i1p1);

out_GFDL_CM2p1_r6i1p1 = cat(1,nan50_60,out_GFDL_CM2p1_r6i1p1);

out_GFDL_CM2p1_r7i1p1 = cat(1,nan50_60,out_GFDL_CM2p1_r7i1p1);

out_GFDL_CM2p1_r8i1p1 = cat(1,nan50_60,out_GFDL_CM2p1_r8i1p1);

out_GFDL_CM2p1_r9i1p1 = cat(1,nan50_60,out_GFDL_CM2p1_r9i1p1);

out_GFDL_CM2p1_r10i1p1 = cat(1,nan50_60,out_GFDL_CM2p1_r10i1p1);

% HadCM3 add nan to 1850-1859

out_HadCM3_r1i1p1 = cat(1,nan50_59,out_HadCM3_r1i1p1);

out_HadCM3_r2i1p1 = cat(1,nan50_59,out_HadCM3_r2i1p1);

out_HadCM3_r3i1p1 = cat(1,nan50_59,out_HadCM3_r3i1p1);

out_HadCM3_r4i1p1 = cat(1,nan50_59,out_HadCM3_r4i1p1);

out_HadCM3_r5i1p1 = cat(1,nan50_59,out_HadCM3_r5i1p1);

out_HadCM3_r6i1p1 = cat(1,nan50_59,out_HadCM3_r6i1p1);

out_HadCM3_r7i1p1 = cat(1,nan50_59,out_HadCM3_r7i1p1);

out_HadCM3_r8i1p1 = cat(1,nan50_59,out_HadCM3_r8i1p1);

out_HadCM3_r9i1p1 = cat(1,nan50_59,out_HadCM3_r9i1p1);

out_HadCM3_r10i1p1 = cat(1,nan50_59,out_HadCM3_r10i1p1);

% HadGEM2_CC/AO add nan to 1850-1859

out_HadGEM2_CC_r1i1p1 = cat(1,nan50_59,out_HadGEM2_CC_r1i1p1);

out_HadGEM2_AO_r1i1p1 = cat(1,nan50_59,out_HadGEM2_AO_r1i1p1);

% GFDL-ESM2G / M add nan to 1850-1860

out_GFDL_ESM2G_r1i1p1 = cat(1,nan50_60,out_GFDL_ESM2G_r1i1p1);

out_GFDL_ESM2M_r1i1p1 = cat(1,nan50_60,out_GFDL_ESM2M_r1i1p1);

% MRI-ESM1 add nan to 1850

out_MRI_ESM1_r1i1p1 = cat(1,nan50,out_MRI_ESM1_r1i1p1);

% HadGEM2-ES add nan to 1850-1859

out_HadGEM2_ES_r1i1p1 = cat(1,nan50_59,out_HadGEM2_ES_r1i1p1);

out_HadGEM2_ES_r2i1p1 = cat(1,nan50_59,out_HadGEM2_ES_r2i1p1);

out_HadGEM2_ES_r3i1p1 = cat(1,nan50_59,out_HadGEM2_ES_r3i1p1);

out_HadGEM2_ES_r4i1p1 = cat(1,nan50_59,out_HadGEM2_ES_r4i1p1);

out_HadGEM2_ES_r5i1p1 = cat(1,nan50_59,out_HadGEM2_ES_r5i1p1);

% GISS-E2-R_r1i1p121-128 add nan to 1850

out_GISS_E2_R_r1i1p121 = cat(1,nan50,out_GISS_E2_R_r1i1p121);

out_GISS_E2_R_r1i1p124 = cat(1,nan50,out_GISS_E2_R_r1i1p124);

out_GISS_E2_R_r1i1p125 = cat(1,nan50,out_GISS_E2_R_r1i1p125);

out_GISS_E2_R_r1i1p126 = cat(1,nan50,out_GISS_E2_R_r1i1p126);

out_GISS_E2_R_r1i1p127 = cat(1,nan50,out_GISS_E2_R_r1i1p127);

out_GISS_E2_R_r1i1p128 = cat(1,nan50,out_GISS_E2_R_r1i1p128);


% equal weighting models

out_ACCESS1_0 = out_ACCESS1_0_r1i1p1;

out_ACCESS1_3 = cat(3, out_ACCESS1_3_r1i1p1,out_ACCESS1_3_r2i1p1,out_ACCESS1_3_r3i1p1);

out_BNU_ESM = out_BNU_ESM_r1i1p1;

out_CCSM4 = cat(3, out_CCSM4_r1i1p1,out_CCSM4_r2i1p1,out_CCSM4_r3i1p1,out_CCSM4_r4i1p1,out_CCSM4_r5i1p1,out_CCSM4_r6i1p1);

out_CESM1_BGC = out_CESM1_BGC_r1i1p1;

out_CESM1_CAM5 = cat(3, out_CESM1_CAM5_r1i1p1,out_CESM1_CAM5_r2i1p1,out_CESM1_CAM5_r3i1p1);

out_CESM1_FASTCHEM = cat(3, out_CESM1_FASTCHEM_r1i1p1,out_CESM1_FASTCHEM_r2i1p1,out_CESM1_FASTCHEM_r3i1p1);

out_CESM1_WACCM = out_CESM1_WACCM_r1i1p1;

out_CMCC_CESM = out_CMCC_CESM_r1i1p1;

out_CMCC_CMS = out_CMCC_CMS_r1i1p1;

out_CMCC_CM = out_CMCC_CM_r1i1p1;

out_CNRM_CM5_2 = out_CNRM_CM5_2_r1i1p1;

out_CNRM_CM5 = cat(3, out_CNRM_CM5_r1i1p1,out_CNRM_CM5_r2i1p1,out_CNRM_CM5_r3i1p1,out_CNRM_CM5_r4i1p1,out_CNRM_CM5_r5i1p1);

out_CNRM_CM5 = cat(3, out_CNRM_CM5,out_CNRM_CM5_r6i1p1,out_CNRM_CM5_r7i1p1,out_CNRM_CM5_r8i1p1,out_CNRM_CM5_r9i1p1,out_CNRM_CM5_r10i1p1);

out_CSIRO_Mk3_6_0 = cat(3, out_CSIRO_Mk3_6_0_r1i1p1,out_CSIRO_Mk3_6_0_r2i1p1,out_CSIRO_Mk3_6_0_r3i1p1,out_CSIRO_Mk3_6_0_r4i1p1,out_CSIRO_Mk3_6_0_r5i1p1);

out_CSIRO_Mk3_6_0 = cat(3, out_CSIRO_Mk3_6_0,out_CSIRO_Mk3_6_0_r6i1p1,out_CSIRO_Mk3_6_0_r7i1p1,out_CSIRO_Mk3_6_0_r8i1p1,out_CSIRO_Mk3_6_0_r9i1p1,out_CSIRO_Mk3_6_0_r10i1p1);

out_CanESM2 = cat(3, out_CanESM2_r1i1p1,out_CanESM2_r2i1p1,out_CanESM2_r3i1p1,out_CanESM2_r4i1p1,out_CanESM2_r5i1p1);

out_FGOALS_g2 = out_FGOALS_g2_r1i1p1;

out_FIO_ESM = cat(3, out_FIO_ESM_r1i1p1, out_FIO_ESM_r2i1p1, out_FIO_ESM_r3i1p1);

out_GFDL_CM2p1 = cat(3, out_GFDL_CM2p1_r1i1p1, out_GFDL_CM2p1_r2i1p1, out_GFDL_CM2p1_r3i1p1,out_GFDL_CM2p1_r4i1p1,out_GFDL_CM2p1_r5i1p1);

out_GFDL_CM2p1 = cat(3, out_GFDL_CM2p1, out_GFDL_CM2p1_r6i1p1, out_GFDL_CM2p1_r7i1p1, out_GFDL_CM2p1_r8i1p1,out_GFDL_CM2p1_r9i1p1,out_GFDL_CM2p1_r10i1p1);

out_GFDL_CM3 = cat(3, out_GFDL_CM3_r1i1p1, out_GFDL_CM3_r2i1p1, out_GFDL_CM3_r3i1p1,out_GFDL_CM3_r4i1p1,out_GFDL_CM3_r5i1p1);

out_GFDL_ESM2G = out_GFDL_ESM2G_r1i1p1;

out_GFDL_ESM2M =  out_GFDL_ESM2M_r1i1p1;

out_GISS_E2_H_CC = out_GISS_E2_H_CC_r1i1p1;

out_GISS_E2_H = cat(3, out_GISS_E2_H_r1i1p1,out_GISS_E2_H_r2i1p1,out_GISS_E2_H_r3i1p1,out_GISS_E2_H_r4i1p1,out_GISS_E2_H_r5i1p1,out_GISS_E2_H_r6i1p1);

out_GISS_E2_H = cat(3, out_GISS_E2_H,out_GISS_E2_H_r1i1p2,out_GISS_E2_H_r2i1p2,out_GISS_E2_H_r3i1p2,out_GISS_E2_H_r4i1p2,out_GISS_E2_H_r5i1p2);

out_GISS_E2_H = cat(3, out_GISS_E2_H,out_GISS_E2_H_r1i1p3,out_GISS_E2_H_r2i1p3,out_GISS_E2_H_r3i1p3,out_GISS_E2_H_r4i1p3,out_GISS_E2_H_r5i1p3,out_GISS_E2_H_r6i1p3);

out_GISS_E2_R_CC = out_GISS_E2_R_CC_r1i1p1;

out_GISS_E2_R = cat(3, out_GISS_E2_R_r1i1p1,out_GISS_E2_R_r2i1p1,out_GISS_E2_R_r3i1p1,out_GISS_E2_R_r4i1p1,out_GISS_E2_R_r5i1p1,out_GISS_E2_R_r6i1p1);

out_GISS_E2_R = cat(3, out_GISS_E2_R, out_GISS_E2_R_r1i1p2,out_GISS_E2_R_r2i1p2,out_GISS_E2_R_r3i1p2,out_GISS_E2_R_r4i1p2,out_GISS_E2_R_r5i1p2,out_GISS_E2_R_r6i1p2);

out_GISS_E2_R = cat(3, out_GISS_E2_R,out_GISS_E2_R_r1i1p3,out_GISS_E2_R_r2i1p3,out_GISS_E2_R_r3i1p3,out_GISS_E2_R_r4i1p3,out_GISS_E2_R_r5i1p3,out_GISS_E2_R_r6i1p3);

out_GISS_E2_R = cat(3, out_GISS_E2_R,out_GISS_E2_R_r1i1p121,out_GISS_E2_R_r1i1p124,out_GISS_E2_R_r1i1p125,out_GISS_E2_R_r1i1p126,out_GISS_E2_R_r1i1p127,out_GISS_E2_R_r1i1p128);

out_HadCM3 = cat(3,out_HadCM3_r1i1p1,out_HadCM3_r2i1p1,out_HadCM3_r3i1p1,out_HadCM3_r4i1p1,out_HadCM3_r5i1p1);

out_HadCM3 = cat(3, out_HadCM3,out_HadCM3_r6i1p1,out_HadCM3_r7i1p1,out_HadCM3_r8i1p1,out_HadCM3_r9i1p1,out_HadCM3_r10i1p1);

out_HadGEM2_AO = out_HadGEM2_AO_r1i1p1;

out_HadGEM2_CC = out_HadGEM2_CC_r1i1p1;

out_HadGEM2_ES = cat(3, out_HadGEM2_ES_r1i1p1,out_HadGEM2_ES_r2i1p1,out_HadGEM2_ES_r3i1p1,out_HadGEM2_ES_r4i1p1,out_HadGEM2_ES_r5i1p1);

out_INMCM4 = out_INMCM4_r1i1p1;

out_IPSL_CM5A_LR = cat(3, out_IPSL_CM5A_LR_r1i1p1,out_IPSL_CM5A_LR_r2i1p1,out_IPSL_CM5A_LR_r3i1p1,out_IPSL_CM5A_LR_r4i1p1,out_IPSL_CM5A_LR_r5i1p1,out_IPSL_CM5A_LR_r6i1p1);

out_IPSL_CM5A_MR = cat(3, out_IPSL_CM5A_MR_r1i1p1,out_IPSL_CM5A_MR_r2i1p1,out_IPSL_CM5A_MR_r3i1p1);

out_IPSL_CM5B_LR = out_IPSL_CM5B_LR_r1i1p1;

out_MIROC5 = cat(3, out_MIROC5_r1i1p1,out_MIROC5_r2i1p1,out_MIROC5_r3i1p1,out_MIROC5_r5i1p1);

out_MIROC_ESM = cat(3, out_MIROC_ESM_r1i1p1,out_MIROC_ESM_r2i1p1,out_MIROC_ESM_r3i1p1);

out_MPI_ESM_LR = cat(3, out_MPI_ESM_LR_r1i1p1,out_MPI_ESM_LR_r2i1p1,out_MPI_ESM_LR_r3i1p1);

out_MPI_ESM_MR = cat(3, out_MPI_ESM_MR_r1i1p1,out_MPI_ESM_MR_r2i1p1,out_MPI_ESM_MR_r3i1p1);

out_MPI_ESM_P = cat(3, out_MPI_ESM_P_r1i1p1,out_MPI_ESM_P_r2i1p1);

out_MRI_CGCM3 = cat(3, out_MRI_CGCM3_r1i1p1,out_MRI_CGCM3_r2i1p1,out_MRI_CGCM3_r3i1p1,out_MRI_CGCM3_r4i1p2);

out_MRI_ESM1 = out_MRI_ESM1_r1i1p1;

out_NorESM1_ME = out_NorESM1_ME_r1i1p1;

out_NorESM1_M = cat(3, out_NorESM1_M_r1i1p1,out_NorESM1_M_r2i1p1,out_NorESM1_M_r3i1p1);

out_bcc_csm1_1_m = cat(3, out_bcc_csm1_1_m_r1i1p1,out_bcc_csm1_1_m_r2i1p1,out_bcc_csm1_1_m_r3i1p1);

out_bcc_csm1_1 = cat(3, out_bcc_csm1_1_r1i1p1,out_bcc_csm1_1_r2i1p1,out_bcc_csm1_1_r3i1p1);


% calculate model means

out_ACCESS1_0_m = nanmean(out_ACCESS1_0,3);

out_ACCESS1_3_m = nanmean(out_ACCESS1_3,3);

out_BNU_ESM_m = nanmean(out_BNU_ESM,3);

out_CCSM4_m = nanmean(out_CCSM4,3);

out_CESM1_BGC_m = nanmean(out_CESM1_BGC,3);

out_CESM1_CAM5_m = nanmean(out_CESM1_CAM5 ,3);

out_CESM1_FASTCHEM_m = nanmean(out_CESM1_FASTCHEM,3);

out_CESM1_WACCM_m = nanmean(out_CESM1_WACCM,3);

out_CMCC_CESM_m = nanmean(out_CMCC_CESM,3);

out_CMCC_CMS_m = nanmean(out_CMCC_CMS,3);

out_CMCC_CM_m = nanmean(out_CMCC_CM,3);

out_CNRM_CM5_2_m = nanmean(out_CNRM_CM5_2,3);

out_CNRM_CM5_m = nanmean(out_CNRM_CM5,3);

out_CSIRO_Mk3_6_0_m = nanmean(out_CSIRO_Mk3_6_0,3);

out_CanESM2_m = nanmean(out_CanESM2,3);

out_FGOALS_g2_m = nanmean(out_FGOALS_g2,3);

out_FIO_ESM_m = nanmean(out_FIO_ESM,3);

out_GFDL_CM2p1_m = nanmean(out_GFDL_CM2p1,3);

out_GFDL_CM3_m = nanmean(out_GFDL_CM3,3);

out_GFDL_ESM2G_m = nanmean(out_GFDL_ESM2G,3);

out_GFDL_ESM2M_m =  nanmean(out_GFDL_ESM2M,3);

out_GISS_E2_H_CC_m = nanmean(out_GISS_E2_H_CC,3);

out_GISS_E2_H_m = nanmean(out_GISS_E2_H,3);

out_GISS_E2_R_CC_m = nanmean(out_GISS_E2_R_CC,3);

out_GISS_E2_R_m = nanmean(out_GISS_E2_R,3);

out_HadCM3_m = nanmean(out_HadCM3,3);

out_HadGEM2_AO_m = nanmean(out_HadGEM2_AO,3);

out_HadGEM2_CC_m = nanmean(out_HadGEM2_CC,3);

out_HadGEM2_ES_m = nanmean(out_HadGEM2_ES,3);

out_INMCM4_m = nanmean(out_INMCM4,3);

out_IPSL_CM5A_LR_m = nanmean(out_IPSL_CM5A_LR,3);

out_IPSL_CM5A_MR_m = nanmean(out_IPSL_CM5A_MR,3);

out_IPSL_CM5B_LR_m = nanmean(out_IPSL_CM5B_LR,3);

out_MIROC5_m = nanmean(out_MIROC5,3);

out_MIROC_ESM_m = nanmean(out_MIROC_ESM,3);

out_MPI_ESM_LR_m = nanmean(out_MPI_ESM_LR,3);

out_MPI_ESM_MR_m = nanmean(out_MPI_ESM_MR,3);

out_MPI_ESM_P_m = nanmean(out_MPI_ESM_P,3);

out_MRI_CGCM3_m = nanmean(out_MRI_CGCM3,3);

out_MRI_ESM1_m = nanmean(out_MRI_ESM1,3);

out_NorESM1_ME_m = nanmean(out_NorESM1_ME,3);

out_NorESM1_M_m = nanmean(out_NorESM1_M ,3);

out_bcc_csm1_1_m_m = nanmean(out_bcc_csm1_1_m,3);

out_bcc_csm1_1__m = nanmean(out_bcc_csm1_1,3);


% calculate mean of model means

model_m = cat(3, out_ACCESS1_0_m, out_ACCESS1_3_m, out_BNU_ESM_m, out_CCSM4_m, out_CESM1_BGC_m);

model_m = cat(3, model_m,  out_CESM1_CAM5_m, out_CESM1_FASTCHEM_m, out_CESM1_WACCM_m, out_CMCC_CESM_m, out_CMCC_CMS_m);

model_m = cat(3, model_m, out_CMCC_CM_m, out_CNRM_CM5_2_m, out_CNRM_CM5_m, out_CSIRO_Mk3_6_0_m, out_CanESM2_m);

model_m = cat(3, model_m, out_FGOALS_g2_m, out_FIO_ESM_m, out_GFDL_CM2p1_m, out_GFDL_CM3_m);

model_m = cat(3, model_m, out_GFDL_ESM2G_m, out_GFDL_ESM2M_m, out_GISS_E2_H_CC_m, out_GISS_E2_H_m, out_GISS_E2_R_CC_m);

model_m = cat(3, model_m, out_GISS_E2_R_m, out_HadCM3_m, out_HadGEM2_AO_m, out_HadGEM2_CC_m, out_HadGEM2_ES_m);

model_m = cat(3, model_m, out_INMCM4_m, out_IPSL_CM5A_LR_m, out_IPSL_CM5A_MR_m, out_IPSL_CM5B_LR_m, out_MIROC5_m);

model_m = cat(3, model_m, out_MIROC_ESM_m, out_MPI_ESM_LR_m, out_MPI_ESM_MR_m, out_MPI_ESM_P_m, out_MRI_CGCM3_m);

model_m = cat(3, model_m, out_MRI_ESM1_m, out_NorESM1_ME_m, out_NorESM1_M_m, out_bcc_csm1_1_m_m, out_bcc_csm1_1__m);

model_m_mean = nanmean(model_m,3);


% equal weighting realizations

AMO_PDO_out_r = cat(3, out_ACCESS1_0, out_ACCESS1_3, out_BNU_ESM, out_CCSM4, out_CESM1_BGC);

AMO_PDO_out_r = cat(3, AMO_PDO_out_r, out_CESM1_CAM5, out_CESM1_FASTCHEM, out_CESM1_WACCM, out_CMCC_CESM, out_CMCC_CMS);

AMO_PDO_out_r = cat(3, AMO_PDO_out_r, out_CMCC_CM, out_CNRM_CM5_2, out_CNRM_CM5, out_CSIRO_Mk3_6_0, out_CanESM2);

AMO_PDO_out_r = cat(3, AMO_PDO_out_r, out_FGOALS_g2, out_FIO_ESM, out_GFDL_CM2p1, out_GFDL_CM3);

AMO_PDO_out_r = cat(3, AMO_PDO_out_r, out_GFDL_ESM2G, out_GFDL_ESM2M, out_GISS_E2_H_CC, out_GISS_E2_H, out_GISS_E2_R_CC);

AMO_PDO_out_r = cat(3, AMO_PDO_out_r, out_GISS_E2_R, out_HadCM3, out_HadGEM2_AO, out_HadGEM2_CC, out_HadGEM2_ES);

AMO_PDO_out_r = cat(3, AMO_PDO_out_r, out_INMCM4, out_IPSL_CM5A_LR, out_IPSL_CM5A_MR, out_IPSL_CM5B_LR, out_MIROC5);

AMO_PDO_out_r = cat(3, AMO_PDO_out_r, out_MIROC_ESM, out_MPI_ESM_LR, out_MPI_ESM_MR, out_MPI_ESM_P, out_MRI_CGCM3);

AMO_PDO_out_r = cat(3, AMO_PDO_out_r, out_MRI_ESM1, out_NorESM1_ME, out_NorESM1_M, out_bcc_csm1_1_m, out_bcc_csm1_1);

model_r_mean = nanmean(AMO_PDO_out_r,3);


time=[1850:2005]';

% compute the mean detrended AMO PDO (5) of all realizations, regress 
% ensemble global SST (9), regress ensemble target region SST (11), difference target region SST (13), 
% regress target region SST sensitivty (15), difference target region SST sensitivity (17) 

std_all=AMO_PDO_out_r(12:156,[5 9 11 13 15 17],:);
std_all=nanstd(std_all,0,1);
std_all=nanmean(std_all,3);
std_all_pred=std_all/sqrt(170-1);
std_all_act=model_r_mean(12:156,[5 9 11 13 15 17]);
std_all_act=std(std_all_act,0,1);



% repeat calculations using only indirect aerosol models


% add NaN columns to make model series equal length

nan50=nan(1,17);

nan50_59=repmat(nan50,10,1);

nan50_60=repmat(nan50,11,1);

% GFDL_CM3 add nan to 1850-1859

out_ae_GFDL_CM3_r1i1p1 = cat(1,nan50_59,out_ae_GFDL_CM3_r1i1p1);

out_ae_GFDL_CM3_r2i1p1 = cat(1,nan50_59,out_ae_GFDL_CM3_r2i1p1);

out_ae_GFDL_CM3_r3i1p1 = cat(1,nan50_59,out_ae_GFDL_CM3_r3i1p1);

out_ae_GFDL_CM3_r4i1p1 = cat(1,nan50_59,out_ae_GFDL_CM3_r4i1p1);

out_ae_GFDL_CM3_r5i1p1 = cat(1,nan50_59,out_ae_GFDL_CM3_r5i1p1);

% HadGEM2_CC/AO add nan to 1850-1859

out_ae_HadGEM2_CC_r1i1p1 = cat(1,nan50_59,out_ae_HadGEM2_CC_r1i1p1);

out_ae_HadGEM2_AO_r1i1p1 = cat(1,nan50_59,out_ae_HadGEM2_AO_r1i1p1);

% MRI-ESM1 add nan to 1850

out_ae_MRI_ESM1_r1i1p1 = cat(1,nan50,out_ae_MRI_ESM1_r1i1p1);

% HadGEM2-ES add nan to 1850-1859

out_ae_HadGEM2_ES_r1i1p1 = cat(1,nan50_59,out_ae_HadGEM2_ES_r1i1p1);

out_ae_HadGEM2_ES_r2i1p1 = cat(1,nan50_59,out_ae_HadGEM2_ES_r2i1p1);

out_ae_HadGEM2_ES_r3i1p1 = cat(1,nan50_59,out_ae_HadGEM2_ES_r3i1p1);

out_ae_HadGEM2_ES_r4i1p1 = cat(1,nan50_59,out_ae_HadGEM2_ES_r4i1p1);

out_ae_HadGEM2_ES_r5i1p1 = cat(1,nan50_59,out_ae_HadGEM2_ES_r5i1p1);


% equal weighting models

out_ae_ACCESS1_0 = out_ae_ACCESS1_0_r1i1p1;

out_ae_ACCESS1_3 = cat(3, out_ae_ACCESS1_3_r1i1p1,out_ae_ACCESS1_3_r2i1p1,out_ae_ACCESS1_3_r3i1p1);

out_ae_CESM1_CAM5 = cat(3, out_ae_CESM1_CAM5_r1i1p1,out_ae_CESM1_CAM5_r2i1p1,out_ae_CESM1_CAM5_r3i1p1);

out_ae_CSIRO_Mk3_6_0 = cat(3, out_ae_CSIRO_Mk3_6_0_r1i1p1,out_ae_CSIRO_Mk3_6_0_r2i1p1,out_ae_CSIRO_Mk3_6_0_r3i1p1,out_ae_CSIRO_Mk3_6_0_r4i1p1,out_ae_CSIRO_Mk3_6_0_r5i1p1);

out_ae_CSIRO_Mk3_6_0 = cat(3, out_ae_CSIRO_Mk3_6_0,out_ae_CSIRO_Mk3_6_0_r6i1p1,out_ae_CSIRO_Mk3_6_0_r7i1p1,out_ae_CSIRO_Mk3_6_0_r8i1p1,out_ae_CSIRO_Mk3_6_0_r9i1p1,out_ae_CSIRO_Mk3_6_0_r10i1p1);

out_ae_FGOALS_g2 = out_ae_FGOALS_g2_r1i1p1;

out_ae_GFDL_CM3 = cat(3, out_ae_GFDL_CM3_r1i1p1, out_ae_GFDL_CM3_r2i1p1, out_ae_GFDL_CM3_r3i1p1,out_ae_GFDL_CM3_r4i1p1,out_ae_GFDL_CM3_r5i1p1);

out_ae_HadGEM2_AO = out_ae_HadGEM2_AO_r1i1p1;

out_ae_HadGEM2_CC = out_ae_HadGEM2_CC_r1i1p1;

out_ae_HadGEM2_ES = cat(3, out_ae_HadGEM2_ES_r1i1p1,out_ae_HadGEM2_ES_r2i1p1,out_ae_HadGEM2_ES_r3i1p1,out_ae_HadGEM2_ES_r4i1p1,out_ae_HadGEM2_ES_r5i1p1);

out_ae_MIROC5 = cat(3, out_ae_MIROC5_r1i1p1,out_ae_MIROC5_r2i1p1,out_ae_MIROC5_r3i1p1,out_ae_MIROC5_r5i1p1);

out_ae_MIROC_ESM = cat(3, out_ae_MIROC_ESM_r1i1p1,out_ae_MIROC_ESM_r2i1p1,out_ae_MIROC_ESM_r3i1p1);

out_ae_MRI_CGCM3 = cat(3, out_ae_MRI_CGCM3_r1i1p1,out_ae_MRI_CGCM3_r2i1p1,out_ae_MRI_CGCM3_r3i1p1,out_ae_MRI_CGCM3_r4i1p2);

out_ae_MRI_ESM1 = out_ae_MRI_ESM1_r1i1p1;

out_ae_NorESM1_ME = out_ae_NorESM1_ME_r1i1p1;

out_ae_NorESM1_M = cat(3, out_ae_NorESM1_M_r1i1p1,out_ae_NorESM1_M_r2i1p1,out_ae_NorESM1_M_r3i1p1);


% calculate model means

out_ae_ACCESS1_0_m = nanmean(out_ae_ACCESS1_0,3);

out_ae_ACCESS1_3_m = nanmean(out_ae_ACCESS1_3,3);

out_ae_CESM1_CAM5_m = nanmean(out_ae_CESM1_CAM5 ,3);

out_ae_CSIRO_Mk3_6_0_m = nanmean(out_ae_CSIRO_Mk3_6_0,3);

out_ae_FGOALS_g2_m = nanmean(out_ae_FGOALS_g2,3);

out_ae_GFDL_CM3_m = nanmean(out_ae_GFDL_CM3,3);

out_ae_HadGEM2_AO_m = nanmean(out_ae_HadGEM2_AO,3);

out_ae_HadGEM2_CC_m = nanmean(out_ae_HadGEM2_CC,3);

out_ae_HadGEM2_ES_m = nanmean(out_ae_HadGEM2_ES,3);

out_ae_MIROC5_m = nanmean(out_ae_MIROC5,3);

out_ae_MIROC_ESM_m = nanmean(out_ae_MIROC_ESM,3);

out_ae_MRI_CGCM3_m = nanmean(out_ae_MRI_CGCM3,3);

out_ae_MRI_ESM1_m = nanmean(out_ae_MRI_ESM1,3);

out_ae_NorESM1_ME_m = nanmean(out_ae_NorESM1_ME,3);

out_ae_NorESM1_M_m = nanmean(out_ae_NorESM1_M ,3);


% calculate mean of model means

model_m_ae = cat(3, out_ae_ACCESS1_0_m, out_ae_ACCESS1_3_m);

model_m_ae = cat(3, model_m_ae, out_ae_CESM1_CAM5_m, out_ae_CSIRO_Mk3_6_0_m);

model_m_ae = cat(3, model_m_ae, out_ae_FGOALS_g2_m, out_ae_GFDL_CM3_m);

model_m_ae = cat(3, model_m_ae, out_ae_HadGEM2_AO_m, out_ae_HadGEM2_CC_m, out_ae_HadGEM2_ES_m);

model_m_ae = cat(3, model_m_ae, out_ae_MIROC5_m);

model_m_ae = cat(3, model_m_ae, out_ae_MIROC_ESM_m, out_ae_MRI_CGCM3_m);

model_m_ae = cat(3, model_m_ae, out_ae_MRI_ESM1_m, out_ae_NorESM1_ME_m, out_ae_NorESM1_M_m);

model_m_mean_ae = nanmean(model_m_ae,3);


% equal weighting realizations

AMO_PDO_out_r_ae = cat(3, out_ae_ACCESS1_0, out_ae_ACCESS1_3);

AMO_PDO_out_r_ae = cat(3, AMO_PDO_out_r_ae, out_ae_CESM1_CAM5);

AMO_PDO_out_r_ae = cat(3, AMO_PDO_out_r_ae, out_ae_CSIRO_Mk3_6_0);

AMO_PDO_out_r_ae = cat(3, AMO_PDO_out_r_ae, out_ae_FGOALS_g2, out_ae_GFDL_CM3);

AMO_PDO_out_r_ae = cat(3, AMO_PDO_out_r_ae, out_ae_HadGEM2_AO, out_ae_HadGEM2_CC, out_ae_HadGEM2_ES);

AMO_PDO_out_r_ae = cat(3, AMO_PDO_out_r_ae, out_ae_MIROC5);

AMO_PDO_out_r_ae = cat(3, AMO_PDO_out_r_ae, out_ae_MIROC_ESM, out_ae_MRI_CGCM3);

AMO_PDO_out_r_ae = cat(3, AMO_PDO_out_r_ae, out_ae_MRI_ESM1, out_ae_NorESM1_ME, out_ae_NorESM1_M);

model_r_mean_ae = nanmean(AMO_PDO_out_r_ae,3);

time=[1850:2005]';

% compute the mean detrended AMO PDO (5) of all realizations, regress
% ensemble global SST (9), regress ensemble target region SST (11), difference target region SST (13),
% regress target region SST sensitivty (15), difference target region SST sensitivity (17)

std_all_ae=AMO_PDO_out_r_ae(12:156,[5 9 11 13 15 17],:);
std_all_ae=nanstd(std_all_ae,0,1);
std_all_ae=nanmean(std_all_ae,3);
std_all_pred_ae=std_all_ae/sqrt(46-1);
std_all_act_ae=model_r_mean_ae(12:156,[5 9 11 13 15 17]);
std_all_act_ae=std(std_all_act_ae,0,1);