function [X,xloc,xnorm, x, sd, proxy, proxynorm, ploc, merged,N,m1,m2,index1,index2] = pseudoproxytwo(X1,X2,xloc1,xloc2,inoise,s1,r1,coeff,dcoeff,inl,a,da,rhon0,drhon,np,nann,nwarm,ncut,earlylate)
%PSEUDOPROXY  Create Synthetic Set of "proxy" climate records
%
%    [proxy, ploc] = PSEUDOPROXY(X,xloc,s1,r1,Y,
%         coeff,inl,s2,r2,a,da,rhon0,drhon,np,ncut) 
%    accepts fields from data matrix X and Y and produces a
%    a new "pseudoproxy" field Z from these data.
%
%    The proxies are allowed to integrate the instrumental signal
%   (w/ zero and first-lag serial dependence) as:
%        proxy(t)=a*instr(t)+b*instr(t-1)+[additive noise]
%    where the additive noise is allowed 
%    both negative (blue noise) and positive (red noise) serial 
%    dependence, to simulate frequency-dependent loss of resolved
%    variance. Signal-to-noise ratios and autocorrelation coefficients 
%    are randomly assigned in terms of an ensemble mean and 
%    standard deviation
%
%    PSEUDUOPROXY takes as input,
%  
%       X,    input field 1 
%       xloc, lat/lon positions of X gridpoints
%       inoise, 0=no noise
%       s1,   average signal-to-noise ratio with respect to "X"
%       r1,   range (std +/-r1) about average signal-to-noise w.r.t. "X"
%       Y,    input field 2 
%       coeff,mean relative proportion of signal contained in 
%          "X" field
%       dcoeff range (std +/- dcoeff) about average value of coeff
%       inl,  flag indicating non-local relationship of Z w.r.t. Y
%       s2,   average signal-to-noise ratio with respect to "Y"
%       r2,   range (std +/-r2) about average signal-to-noise w.r.t. "Y"
%       a,    average fraction of dependance of proxy(t) on instr(t)
%           [b=1-a is average fraction depending on instr(t-1)]
%       da,   range (std +/-da) about average value of a
%       rhon, lag one autocorrelation of noise (red noise=positive;
%                    blue noise=negative)
%       drhon,range (std +/- drho) about average rhon
%       np,   number of proxies to be produced
%       ncut, time cutoff between calibration/reconstruction period
%
%       and produced on output,
%
%       proxy,    the output "proxy" field
%       ploc, the lat and lon coordinates of the ith proxy
%             is locally correlated with corresponding ith proxy
  X  = [X1 X2];
  xloc = [xloc1' xloc2']';
  m3=size(xloc,1)
  [N1, m1] = size(X1);        
  [N2, m2] = size(X2);
  X0=X(:,1:m3);
  %
  N=N1
  
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %%%%%%%%%%%%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  greatdistance=1;
  load mbhlonglat
  proxylocs=mbhlonglat;
  greatdistance=1;

  % need to find the closest matching gridbox
if greatdistance==1
% calculate minimum great circle distance from each proxy to the center of a gridbox  
eqrad = 6378 %km (3963 mi) Equatorial radius (surface to center distance)
b = 6357 %km (3950 mi) Polar radius (surface to center distance)
ecc = 0.081082 %Eccentricity
 	               lon1=proxylocs(:,1)*pi/180;
                lat1=proxylocs(:,2)*pi/180;
                lon2=xloc1(:,2)*pi/180;
                lat2=xloc2(:,1)*pi/180;

for i=1:length(proxylocs)
    r = eqrad * sqrt(1 - ecc^2) / (1 - ecc^2 * (sin(lat1(i)))^2);%radius of curvature
                dlon = lon2 - lon1(i);
                dlat = lat2 - lat1(i);
                one = (sin(dlat/2)).^2 + cos(lat1(i)).*cos(lat2).*(sin(dlon/2)).^2;
                c = 2 * atan2( sqrt(one),sqrt(1-one) );
                d = r.*c;
        nearest(i)=min(d);
        test=find(d==nearest(i));
        nearestcolumn(i)=test(1,1);
end

index1=nearestcolumn;
index2=index1;
end % end the greatdistance 

save locationindices index1 index2 
xx=X0;
x=xx-repmat(mean(xx),N1,1);
sd=std(xx);
xnorm=x./repmat(sd,N1,1);
  for i=1:np
     ix=index1(i);
     if (i>nann) ix=index2(i); end
	     loc(i,1)=xloc(ix,1);
  	   ploc(i,2)=xloc(ix,2);
     		amp1=s1+r1*gasdev;
     if (amp1<=0) amp1=0.01; end
     ampnoise=1;
     if (inoise==0) ampnoise=0; amp1=1; end
     cf=coeff+dcoeff*gasdev;
     if (cf>1.0) cf=1.0; end
     amp1=amp1*cf;
     % form standardized series
     x1=X0(:,ix);
     x1=(x1-mean(x1))/std(x1);
     nois(1)=gasdev;
     alpha=a+da*gasdev;
     beta=1-alpha;
     rhon=rhon0+drhon*gasdev;
     for j=2:N
        nois(j)=rhon*nois(j-1)+gasdev;
     end 
     noise0=nois';
     noise0=noise0/std(noise0);
     signa(1)=alpha*(amp1*x1(1));
     for j=2:N
        signa(j)=alpha*amp1*x1(j)+beta*amp1*x1(j-1);
     end
     sig=signa';
     noise0=ampnoise*noise0;
     noise(:,i)=noise0;
     signal(:,i)=sig;
     signalnorm(:,i)=(sig-mean(sig))/std(sig);
     z=sig+noise0;
     instr=amp1*x1;
     instrnorm(:,i)=(instr-mean(instr))/std(instr);
     proxy(:,i)=z;
     proxynorm(:,i)=(z-mean(z))/std(z);
     
  end
  %
  % create merged *normalized* proxy/instrumental(X field) dataset
  %

  ntot=np+m1;
if earlylate==1
  merged(1:N,1:ntot)=NaN;
  merged(ncut+1:N,1:m1)=xnorm(ncut+1:N,1:m1);
  merged(1:N,m1+1:ntot)=proxynorm(1:N,1:np);
elseif earlylate==2
  merged(1:N,1:ntot)=NaN;
  merged(1:ncut,1:m1)=xnorm(1:ncut,1:m1);
  merged(1:N,m1+1:ntot)=proxynorm(1:N,1:np);
end

