function [uncert,keepcent,RE_2,CE_2,r2_2,r2_1,RE_1,CE_1]=calc_error(region,expe,iseries);

  res=numel(expe);
  if (res > 0)
    expe=strcat('_',expe);
  end
switch upper(region)
case 'NH'
 switch lower(iseries)
   case 'cru'
      insts='CRU_';
      critre=0.4;
      critce=-5.26;
   case 'icru'
      insts='iCRU_';
      critre=0.35;
      critce=-1.17;
   case 'had'
      insts='HAD_';
      critre=0.45;
      critce=-3.11;
   case 'ihad'
      insts='iHAD_';
      critre=0.39;
      critce=-1.48;
 end
case 'SH'
 switch lower(iseries)
   case 'cru'
      insts='CRU_';
      critre=0.29;
      critce=-1.13;
   case 'icru'
      insts='iCRU_';
      critre=0.08;
      critce=-0.8;
   case 'had'
      insts='HAD_';
      critre=0.46;
      critce=-1.68;
   case 'ihad'
      insts='iHAD_';
      critre=0.1;
      critce=-1.55;
 end
case 'GL'
 switch lower(iseries)
   case 'cru'
      insts='CRU_';
      critre=0.35;
      critce=-2.07;
   case 'icru'
      insts='iCRU_';
      critre=0.28;
      critce=-1.0;
   case 'had'
      insts='HAD_';
      critre=0.47;
      critce=-2.84;
   case 'ihad'
      insts='iHAD_';
      critre=0.33;
      critce=-1.48;
 end
end

if (region == 'GL') 
  goop=load(strcat(insts,'NH_reform'));
  yrs=goop(:,1);
  inst=goop(:,2);
  [smoot,icb,ice,mse0]=lowpassmin(inst,0.1);
  nhinst=smoot;

  ooga=load(strcat('cps_results/',upper('NH'),'_ea100',expe,'_newgrid_',iseries,'_gbeachyear.mat'));
  nhearly=ooga.gbcpsfull(1851:1996,:);
  ooga=load(strcat('cps_results/',upper('NH'),'_la100',expe,'_newgrid_',iseries,'_gbeachyear.mat'));
  nhlate=ooga.gbcpsfull(1851:1996,:);

  goop=load(strcat(insts,'SH_reform'));
  yrs=goop(:,1);
  inst=goop(:,2);
  [smoot,icb,ice,mse0]=lowpassmin(inst,0.1);
  shinst=smoot;

  ooga=load(strcat('cps_results/',upper('SH'),'_ea100',expe,'_newgrid_',iseries,'_gbeachyear.mat'));
  shearly=ooga.gbcpsfull(1851:1996,:);
  ooga=load(strcat('cps_results/',upper('SH'),'_la100',expe,'_newgrid_',iseries,'_gbeachyear.mat'));
  shlate=ooga.gbcpsfull(1851:1996,:);

  inst=0.5*(nhinst+shinst);
  early=0.5*(nhearly+shearly);
  late=0.5*(nhlate+shlate);
 
else
   goop=load(strcat(insts,upper(region),'_reform'));
  yrs=goop(:,1);
  inst=goop(:,2);
  [smoot,icb,ice,mse0]=lowpassmin(inst,0.1);
  inst=smoot;

  ooga=load(strcat('cps_results/',upper(region),'_ea100',expe,'_newgrid_',iseries,'_gbeachyear.mat'));
  early=ooga.gbcpsfull(1851:1996,:);
  ooga=load(strcat('cps_results/',upper(region),'_la100',expe,'_newgrid_',iseries,'_gbeachyear.mat'));
  late=ooga.gbcpsfull(1851:1996,:);

end

     	cals1=1;
  	cale1=100;
  	vals1=101;
  	vale1=146;
  	cals2=47;
 	cale2=146;
  	vals2=1;
  	vale2=46;
	
	
	fid=fopen(strcat('stats_',region,expe,'_newgrid_',iseries,'.csv'),'w');
	fprintf (fid, ' , Early Validation,,,Late Validation,,,Average,,,Adjusted to Overfitting,,,\n');
	fprintf(fid,'century, RE, CE, r^2,  RE, CE, r^2,RE, CE, r^2,RE, CE, r^2 \n');

adjRE=-99999.;
adjCE=-99999.;
adjR2=-99999.;

adjr2_1=-9999.;
adjr2_2=-9999.;

for n=19:-1:1 

  e_recon=early(:,n);
  l_recon=late(:,n);

  r_val=corr(e_recon(vals1:vale1), inst(vals1:vale1));
  r2_1(n)=r_val.*r_val;
  r_val=corr(l_recon(vals2:vale2),inst(vals2:vale2));
  r2_2(n)=r_val.*r_val;

  sd_1=std(inst(cals1:cale1));
  sd_2=std(inst(cals2:cale2));
   
 %% RE calculation e_/1 is early calib l_/2 is late calibration
    
  e_reconmean=e_recon-nanmean(e_recon(cals1:cale1));
  l_reconmean=l_recon-nanmean(l_recon(cals2:cale2));
  
  e_actmean=inst-mean(inst(cals1:cale1));
  l_actmean=inst-mean(inst(cals2:cale2));
  
  diffmean=e_actmean(vals1:vale1)-e_reconmean(vals1:vale1);
  meansqdiff=nansum((diffmean.^2));
  meansq=nansum((e_actmean(vals1:vale1)).^2);
  RE_1(n)=1.-(meansqdiff/meansq);

  diffmean=l_actmean(vals2:vale2)-l_reconmean(vals2:vale2);
  meansqdiff=nansum((diffmean.^2));
  meansq=nansum((l_actmean(vals2:vale2)).^2);
  RE_2(n)=1.-(meansqdiff/meansq);

%% CE calculation e_/1 is early calib l_/2 is late calibration
  e_reconmean=e_recon-mean(inst(vals1:vale1));
  l_reconmean=l_recon-mean(inst(vals2:vale2));
  
  e_actmean=inst-mean(inst(vals1:vale1));
  l_actmean=inst-mean(inst(vals2:vale2));
  
  diffmean=inst(vals1:vale1)-e_recon(vals1:vale1);
  meansqdiff=nansum((diffmean.^2));
  meansq=nansum((e_actmean(vals1:vale1)).^2);
  CE_1(n)=1.-(meansqdiff/meansq);

  diffmean=inst(vals2:vale2)-l_recon(vals2:vale2);
  meansqdiff=nansum((diffmean.^2));
  meansq=nansum((l_actmean(vals2:vale2)).^2);
  CE_2(n)=1.-(meansqdiff/meansq);
  
  cent=(19-n)*100;
  
  avgCE(n)=0.5.*(CE_1(n)+CE_2(n));
  avgRE(n)=0.5.*(RE_1(n)+RE_2(n));
  avgR2(n)=0.5.*(r2_1(n)+r2_2(n));
  keepcent(n)=0;

  if (adjRE > -9990 | (avgRE(n) >= critre & avgCE(n) >= critce) )
   if (avgRE(n) >= adjRE & avgCE(n) >= adjCE)
     adjRE=avgRE(n);
     adjCE=avgCE(n);
     adjR2=avgR2(n);
     adjr2_1=r2_1(n);
     adjr2_2=r2_2(n);
     keepcent(n)=1;
   end
  end
  kadjRE(n)=adjRE;
  kadjCE(n)=adjCE;
  kadjR2(n)=adjR2;
  kadjr2_1(n)=adjr2_1;
  kadjr2_2(n)=adjr2_2;

  uncert_e(n)=sd_1.*sqrt(1-kadjr2_1(n)); %%% 1-sigma uncertainties here!!!
  uncert_l(n)=sd_2.*sqrt(1-kadjr2_2(n)); %%% 1-sigma uncertainties here!!!
  uncert(n)= ( uncert_e(n) + uncert_l(n) ); %%%% 2-sigma uncertainties here!!!!
  if (kadjr2_1(n) < -99.)
      uncert(n)=NaN;
      uncert_e(n)=NaN;
      uncert_l(n)=NaN;
  end 
    
end
for n=1:19
   cent=(19-n)*100;
   sdf=sprintf('%d,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f \n',cent, RE_2(n), CE_2(n), r2_2(n), RE_1(n), CE_1(n), r2_1(n),avgRE(n),avgCE(n),avgR2(n),kadjRE(n),kadjCE(n),kadjR2(n));
%   disp(sdf);
   fprintf (fid,sdf);
end 
fclose(fid);
