randn('state',sum(100*clock))

year=850:1999;

%%%%%%%%%%%%%%%
%% load the model output
%%%%%%%%%%%%%%%%

load csmfulllonglat
load csm1-4grids.mat
load nh_csm.dat -ascii
load longlatlandmask.txt

longlatlandmask(:,3)=longlatlandmask(:,3)./longlatlandmask(:,3); % make ocean nan and land 1
%%%%%%%%%%%%%%%%%%%
%%% Calculate the extratropical mean for future use
%%%%%%%%%%%%%%%%%%%
extralandmask=longlatlandmask(1:792,:);
landlocs=find(~isnan(extralandmask(:,3))); % find the land gridboxes
randompicks=randperm(length(landlocs)); randompicks=randompicks(1:36);
alllocs=(landlocs([randompicks(1:36)])); % randomize the land locations and pick 36

instextra=northtosouth(:,1:792); % that keeps 20N to 90N
extralocs=csmfulllonglat(1:792,:); % just keep 20N to 90N

instextraland=instextra(:,[landlocs]); % land only
extralandlocs=extralocs([landlocs],:); % land only

%%%%%%%%%%%%%%%%%
%% set the reconstruction and calibration interval
%%%%%%%%%%%%%%%%%
startrunyear=850;
endrunyear=1999;
startcalyear=1850;
endcalyear=1980;

startrunyear=startrunyear-850+1;
endrunyear=endrunyear-850+1;


startcalyear=startcalyear-850;
endcalyear=1999-endcalyear; endcalyear=1150-endcalyear;

%%%%%%%%%%%%%%%%%%
%% pseudoproxy settings
%%%%%%%%%%%%%%%%%%%

coeff=1.0; %mean proportion of signal in 'X'
dcoeff=0.0; %range (std) of coeff around the mean
inl=0; % flag for non local relationship (not implemented)
inoise=1; % do you want noise? 1=yes, 0=no
a=1.0; % average fraction of dependence of proxy(t) on inst(t), lag proxy dependence
da=0.0;% range (std) about a
rhon0=0; %lag-1 autocorellation of noise (red noise=pos, blue noise=neg)
drhon=0.0; %range (std) about rhon0
s1=0.55;% ave signal:noise for 'X'
r1=0.0; % range (std) about average s:n w.r.t 'X'
pctwarm=0; % percent of warm season proxies
pctannual=100-pctwarm;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Prep for pseudoproxy generation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
orig1=northtosouth; % that's the model grid data
orig2=northtosouth; % that's the model grid data
gridlocs=csmfulllonglat;

xloc1(:,2)=gridlocs(:,2);
xloc1(:,1)=gridlocs(:,1);
xloc2(:,2)=gridlocs(:,2);
xloc2(:,1)=gridlocs(:,1);
orig1=orig1(startrunyear:endrunyear,:);
orig2=orig2(startrunyear:endrunyear,:);

%%%%%%%%%%%%%%%%%%%%%%%%
%%% break out the regions and send each region for pseudoproxy generation
%%%%%%%%%%%%%%%%%%%%%%%%
clear alllocs
load ./alllocsrand41adjust5.txt
alllocs=alllocsrand41adjust5;
	workinggrids=alllocs';
	np=length(workinggrids);
  	nwarm=pctwarm*np/100; % number of warm season proxies
  	nann = np-nwarm;

  	[orig,xloc,xnorm, x, sd, proxy,proxynorm,ploc,merged,N,m1,m2,index1,index2,instnorm]=makedarrigopseudoproxies(orig1,orig2,xloc1,xloc2,inoise,s1,r1,coeff,dcoeff,inl,a,da,rhon0,drhon,np,nann,nwarm,ncut,earlylate,workinggrids);


%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% The next two lines are to use the pseudoproxies used in the paper. 
%%% Otherwise, a new pseudoproxy noise realization is calculated each time the program is run.
%%% Furthermore, the lines above select random gridboxes and would not choose the same gridbox
%%% locations used in the paper
clear proxynorm
load proxynorm
%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%
%%% Now create the NH mean
%%%%%%%%%%%%%%%%%%%%%%%%%%%
nhrecon=mean(proxynorm'); nhrecon=nhrecon';

%%%%%%%%%%%%%%%%%
%% scale the nh recon to have the same annual mean and standard deviation
%%%%%%%%%%%%%%%%%
	
	decadalnhrecon=lowpassadaptive(nhrecon,0.1);
	inst=lowpassadaptive(nh_csm,0.1);
	reconstd=std(decadalnhrecon(startcalyear:endcalyear));
	instmean=mean(inst(startcalyear:endcalyear));
	inststd=std(inst(startcalyear:endcalyear));

	decadalnhreconscaled=decadalnhrecon./reconstd.*inststd; % scale the low pass filter
		
	nhreconscaled=nhrecon./reconstd.*inststd;
	
	
	reconmean=mean(nhreconscaled(startcalyear:endcalyear));
	nhreconscaled=nhreconscaled-reconmean+instmean;
	reconstd=std(nhrecon(startcalyear:endcalyear));
	instmean=mean(nh_csm(startcalyear:endcalyear));
	inststd=std(nh_csm(startcalyear:endcalyear));


	nhreconscaled=nhrecon./reconstd.*inststd;
	reconmean=mean(nhreconscaled(startcalyear:endcalyear));
	nhreconscaled=nhreconscaled-reconmean+instmean;
