clear
load ./hybridsplitinput
ishybrid = input('enter 1 for hybrid 0 for nonhybrid ')
if ishybrid==1

load ./lowf.filled -mat
load ./highf.filled -mat

islongorshort=input('enter 1 for long calibration 2 for short calibration, 3 for short cal and long ver')
if islongorshort==1
startrunyear=1;
startcalyear=1007;
endverifyyear=1006;
elseif islongorshort==2
startrunyear=1;
startcalyear=1051;
endverifyyear=1050;
else
startrunyear=1;
endverifyyear=1006
startcalyear=1051;
end
xhigh=X;
X=xlow+xhigh;
[nyears,nvars]=size(X);

else
islongorshort=input('enter 1 for long calibration 2 for short calibration, 3 for short cal and long ver')
if islongorshort==1
startrunyear=1;
startcalyear=1007;
endverifyyear=1006;
elseif islongorshort==2
startrunyear=1;
endverifyyear=1050;
else
startrunyear=1;
endverifyyear=1006
startcalyear=1051;
end

    load highf.filled -mat
[nyears,nvars]=size(X);
end

%%%% OK use the reconstructed PCs to reconstruct the field
reconpcs=X(:,1:instpcstokeep);
% put back the pc standard deviations and means

reconpcs=reconpcs.*repmat(keptpcstds,nyears,1);
reconpcs=reconpcs+repmat(keptpcmeans,nyears,1);

recon=zeros([endrunyear-startrunyear+1,1312]);

for jj=1:instpcstokeep
    recon=keptsingvals(jj).*(reconpcs(:,jj)*kepteofs(jj,:))+recon;
end
if islongorshort==2
startrunyear=1007; 
end
    reconstructed=recon;
    %reconstructed=reconstructed(:,1:1312);

%
% calculate verification betas (baseline = calibration period mean)
%
longlat=xloc(1:1312,:);
longlat=fliplr(longlat);
latwt=cos(longlat(:,2).*pi/180);
latwt=latwt';

reconstructed=reconstructed.*repmat(inststds,nyears,1);
reconstructed=reconstructed+repmat(instmeans,nyears,1);

% if you weigthed by sqrt(cos lat in the input file,  remove that here
coslatweight=input('enter 2 if you need to remove cos lat wt, otherwise 1')
[nrows,ncols]=size(X);
latwt2=(latwt);
if coslatweight==2
	reconstructed=reconstructed./repmat(latwt2,nrows,1);
end

% get actual and reconstructed grids and set mean of calibration period to zero
load ./converted.mat
load ./keepers.mat
inst=northtosouth(1:endrunyear,289:2304); % just keep 70n to 70s
inst=inst(:,[enough]);
actual=inst-(repmat(nanmean(inst(startcalyear:endcalyear,:)),nyears,1));
reconstructed=reconstructed-(repmat(nanmean(reconstructed(startcalyear:endcalyear,:)),nyears,1));
keepreconstructed=reconstructed;
keepactual=actual;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% To do the multivariate r.^2, string out the gridboxes into a
%%%%%%%%% single column vector
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
allactualgrids=reshape(actual(startrunyear:startcalyear-1,:),(startcalyear-startrunyear).*1312,1);
allrecongrids=reshape(reconstructed(startrunyear:startcalyear-1,:),(startcalyear-startrunyear).*1312,1);

for jj=1:1312
[coeff,S_err,XTXI,multrsq(jj),Fval,stats,Y_hat,resid,junk] = mregress(actual(startrunyear:startcalyear-1,jj),reconstructed(startrunyear:startcalyear-1,jj),1);
end
fullmultrsq=mean(multrsq)
nhmultrsq=mean(multrsq(1:669))
%startrunyear=950;
%[coeff,S_err,XTXI,multrsq,Fval,stats,Y_hat,resid,junk] = mregress(allactualgrids,allrecongrids,1);
%multrsq
diff=(keepactual(startrunyear:endverifyyear,:)-keepreconstructed(startrunyear:endverifyyear,:));
sumsqdiff=nansum(nansum(diff.^2));
sumsq=nansum(nansum(keepactual(startrunyear:endverifyyear,:).^2)); %
mverificationbeta=1-(sumsqdiff/sumsq)

diff=(keepactual(startrunyear:endverifyyear,1:669)-keepreconstructed(startrunyear:endverifyyear,1:669));
sumsqdiff=nansum(nansum(diff.^2));
sumsq=nansum(nansum(keepactual(startrunyear:endverifyyear,1:669).^2)); %
NHmverificationbeta=1-(sumsqdiff/sumsq)

actual=actual.*repmat(latwt,nrows,1);
reconstructed=reconstructed.*repmat(latwt,nrows,1);
actual=actual-(repmat(nanmean(actual(startcalyear:endcalyear,:)),nyears,1));
reconstructed=reconstructed-(repmat(nanmean(reconstructed(startcalyear:endcalyear,:)),nyears,1));

nhactmean=nansum(actual(:,1:669)'); % there are 669 available NH gridboxes    
sumweights=nansum(latwt(1:669));     
nhactmean=nhactmean./sumweights;
nhreconmean=nansum(reconstructed(:,1:669)');
nhreconmean=nhreconmean./sumweights;
nhreconmean=nhreconmean-nanmean(nhreconmean(startcalyear:endcalyear));
nhactmean=nhactmean-nanmean(nhactmean(startcalyear:endcalyear));

nhreconmean=nhreconmean'; nhactmean=nhactmean';

%
% do you want to lowpass them?
nhactmean=lowpassmin(nhactmean,0.1); nhreconmean=lowpassmin(nhreconmean,0.1);
[coeff,S_err,XTXI,rsq,Fval,stats,Y_hat,resid,junk] = mregress(nhactmean(startrunyear:endverifyyear),nhreconmean(startrunyear:endverifyyear),1);
rsq
savenhreconmean=nhreconmean;

%save nh nhreconmean nhactmean

% NINO 3 region is -147.5 to -92.5 and +2.5 to -2.5 (gridboxes 943-954,
% 1015-1026 
nino3recon=nansum(reconstructed(:,[624:633,673:682])');
nino3recon=nino3recon./nansum(latwt([624:633,673:682]));
nino3inst=nansum(actual(:,[624:633,673:682])');
nino3inst=nino3inst./nansum(latwt([624:633,673:682]));
nino3inst=lowpassmin(nino3inst',0.1);
nino3recon=lowpassmin(nino3recon',0.1);
%nino3inst=nino3inst';
%nino3recon=nino3recon';
nino3diff=nino3inst(startrunyear:endverifyyear)-nino3recon(startrunyear:endverifyyear);
nino3sumsqdiff=nansum(nino3diff.^2);
nino3sumsq=nansum(nino3inst(startrunyear:endverifyyear).^2);
nino3re=1-(nino3sumsqdiff./nino3sumsq)
[coeff,S_err,XTXI,nino3rsq,Fval,stats,Y_hat,resid,junk] = mregress(nino3inst(startrunyear:endverifyyear),nino3recon(startrunyear:endverifyyear),1);
nino3rsq
diffmean= nhactmean(startrunyear:endverifyyear)-(nhreconmean(startrunyear:endverifyyear));
meansumsqdiff=nansum((diffmean.^2));
meansumsq=nansum((nhactmean(startrunyear:endverifyyear).^2));
averageverification=1-(meansumsqdiff/meansumsq)
%save nino3 nino3recon nino3inst
for jj=1:1312
clear diff sumsq sumsqdiff
diff=(actual(startrunyear:endverifyyear,jj)-reconstructed(startrunyear:endverifyyear,jj));
sumsqdiff=(nansum(diff.^2));
sumsq=(nansum(actual(startrunyear:endverifyyear,jj).^2)); %
gridboxre(jj)=1-(sumsqdiff/sumsq);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%
% now calculate CE
%%%%%%%%%%%%%%%%%
reconstructed=keepreconstructed-(repmat(nanmean(keepactual(startrunyear:endverifyyear,1:1312)),nyears,1));
actual=keepactual(:,1:1312)-(repmat(nanmean(keepactual(startrunyear:endverifyyear,1:1312)),nyears,1));

diff=(actual(startrunyear:endverifyyear,1:1312)-reconstructed(startrunyear:endverifyyear,1:1312));
sumsqdiff=nansum(nansum(diff.^2));
sumsq=nansum(nansum(actual(startrunyear:endverifyyear,1:1312).^2)); %
mverificationce=1-(sumsqdiff/sumsq)

diff=(actual(startrunyear:endverifyyear,1:669)-reconstructed(startrunyear:endverifyyear,1:669));
sumsqdiff=nansum(nansum(diff.^2));
sumsq=nansum(nansum(actual(startrunyear:endverifyyear,1:669).^2)); %
NHmverificationce=1-(sumsqdiff/sumsq)

nino3recon=nansum(reconstructed(:,[624:633,673:682])');
nino3recon=nino3recon./nansum(latwt([624:633,673:682]));
nino3inst=nansum(actual(:,[624:633,673:682])');
nino3inst=nino3inst./nansum(latwt([624:633,673:682]));
%nino3inst=lowpassmin(nino3inst',0.1);
%nino3recon=lowpassmin(nino3recon',0.1);
nino3inst=nino3inst';
nino3recon=nino3recon';
nino3diff=nino3inst(startrunyear:endverifyyear)-nino3recon(startrunyear:endverifyyear);
nino3sumsqdiff=nansum(nino3diff.^2);
nino3sumsq=nansum(nino3inst(startrunyear:endverifyyear).^2);
nino3ce=1-(nino3sumsqdiff./nino3sumsq)


nhreconmean=nhreconmean-nanmean(nhactmean(startrunyear:endverifyyear));
nhactmean=nhactmean-nanmean(nhactmean(startrunyear:endverifyyear));
cediffmean= nhactmean(startrunyear:endverifyyear)-(nhreconmean(startrunyear:endverifyyear));
cemeansumsqdiff=nansum((cediffmean.^2));
cemeansumsq=nansum((nhactmean(startrunyear:endverifyyear)).^2);
averagece=1-(cemeansumsqdiff/cemeansumsq)

% for plotting reset the calibration period mean to zero
nhreconmean=nhreconmean-nanmean(nhreconmean(startcalyear:endcalyear));
nhactmean=nhactmean-nanmean(nhactmean(startcalyear:endcalyear));
