% define number of surrogates to be used
nsurr1=2000;  % number of monte carlo surrogates used in estimating statistical model uncertainties
nsurr2=2000;  % number of surrogates in sediment composite age model MC permutations
%
% specify modern TC count bias correction
% ibias=1: use Mann/Sabbatelli/Neu '07 corrections
% ibias=2: use Landsea corrections
ibias=1;
%
% indicate weighting option in forming basin composite from regional
% records
% iweight=1: weight by inverse return periods, variable sensitivity
% iweight=2: weight by inverse return periods, uniform cat 3 assumed sensitivity
% iweight=3: uniform weighting of regional composites
iweight=1;
%
% indicate normalization option for regional composites
% inorm=0: do not normalize regional composites by total number of events
% inorm=1: normalize each regional composite by total number of events
inorm=1;
%
%
%%%%%%%%%%%%%%%%%%%%
%
%
% Calculate statistical TC model (TC counts as a function of MDR, Nino3,and NAO)
% and apply to decadal proxy-reconstructed MDR, Nino3, and NAO series,
% calculate +/- sigma limits of statistical model given uncertainties
% in reconstructions and model calibration
%
[yearstorms,storms,smoothstorms,tcreconshort,yearreconshort,tcinstr,yearinstr,tcdist,nrecon,nreconshort,resid]  = StatModelPaleoTC(nsurr1,ibias);
%
% calculate multidecadal smooth of model and associated +/- 2 sigma
% uncertainties
freq=0.025;  % 40 year smoothing timescale
tcrecon40yr=lowpassadaptive(tcreconshort,freq);
%
% smooth historical statistical model and actual storm numbers 
tcreconmod40yr=lowpassadaptive(tcinstr,freq);
tcobs40yr=lowpassadaptive(storms,freq);
%
% define uncertainties for historical statistical model based on
% validation residuals
resid40yr=lowpassadaptive(resid,freq);
sigresid=std(resid40yr);
%
% Calculate mean and variance scales for statistical model
%
tcstatfull(1:1351)=tcreconshort; % year AD 500-1850
tcstatfull(1352:1507)=tcinstr(2:157);  % years 1851-2006
tcstatfull=tcstatfull';
tcstatoverlap = tcstatfull(1352:1492); % years 1851-1991
tcstatoverlap40yr = lowpassadaptive(tcstatoverlap,freq);
meanrecon = mean(tcstatoverlap40yr);
stdrecon = std(tcstatoverlap40yr);
%
% sort to determine 95%ile limits of surrogate distribution
%
for i=1:nsurr1
    nrm(i)=norm(tcreconshort-tcdist(1:nreconshort,i));
end
[nrmsort,isort]=sort(nrm);
nkeep=round(95*nsurr1/100);
%
%
%%%%%%%%%%%%%%%%%%%%
%
%
% Now generate ensemble of sediment composites records consistent with age model uncertainties
%
% determine record lengths
load Data/PR000.dat
nv=size(PR000,1);
load Data/FL000.dat
nw=size(FL000,1);
load Data/NYNJ000.dat
nb=size(NYNJ000,1);
load Data/RIMA000.dat
nn=size(RIMA000,1);
load Data/SC000.dat
nc=size(SC000,1);
for i=1:nsurr2
% calculate required number of uniform random deviates for sediment age model surrogates
    for j=1:nv
        ranpert(j,1)=rand;
    end
    for j=1:nw
        ranpert(j,2)=rand;
    end
    for j=1:nb
        ranpert(j,3)=rand;
    end
    for j=1:nn
        ranpert(j,4)=rand;
    end
    for j=1:nc
        ranpert(j,5)=rand;
    end
    [yeartcprox,tcproxsurr0] = makeproxMCNoRecentVieques(ranpert,iweight,inorm);
    [yeartcprox,tcproxsurr1] = makeproxMCStandard(ranpert,iweight,inorm);
    [yeartcprox,tcproxsurrjack1] = makeproxMCNoPR(ranpert,iweight,inorm);
    [yeartcprox,tcproxsurrjack2] = makeproxMCNoGulf(ranpert,iweight,inorm);
    [yeartcprox,tcproxsurrjack3] = makeproxMCNoMidAtl(ranpert,iweight,inorm);
    [yeartcprox,tcproxsurrjack4] = makeproxMCNoNE(ranpert,iweight,inorm);
    [yeartcprox,tcproxsurrjack5] = makeproxMCNoSC(ranpert,iweight,inorm);
    proxarray0(:,i)=tcproxsurr0;
    proxarray1(:,i)=tcproxsurr1;
    proxarrayjack1(:,i)=tcproxsurrjack1;
    proxarrayjack2(:,i)=tcproxsurrjack2;
    proxarrayjack3(:,i)=tcproxsurrjack3;
    proxarrayjack4(:,i)=tcproxsurrjack4;
    proxarrayjack5(:,i)=tcproxsurrjack5;
end
%
% define sediment composite series
%
% first, do composite using recent (downweighted) post AD 1700 Vieques data
%
% we define series as the maximum probability of an event for each given
% year ranging over all chronologies consistent with age model uncertainties
sortarray=max(proxarray0')';
% smooth and scale to have same mean and multidecadal variance as
tcproxadj=lowpassadaptive(sortarray,freq);
stdprox = std(tcproxadj(1:1351));
tcproxadj = tcproxadj*stdrecon/stdprox; % center to have same long-term multidecadal variance as stat model
meanprox = mean(tcproxadj(1352:1492)); 
tcproxadj = tcproxadj+meanrecon-meanprox; % center to have same mean as modern stat model
tcproxadjsmooth0=lowpassadaptive(tcproxadj,freq);
%
% now do standard composite (no post AD 1700 Vieques data)
%
% we define series as the maximum probability of an event for each given
% year ranging over all chronologies consistent with age model uncertainties
sortarray=max(proxarray1')';
% smooth and scale to have same mean and multidecadal variance as
tcproxadj=lowpassadaptive(sortarray,freq);
stdprox = std(tcproxadj(1:1351));
tcproxadj = tcproxadj*stdrecon/stdprox; % center to have same long-term multidecadal variance as stat model
meanprox = mean(tcproxadj(1352:1492)); 
tcproxadj = tcproxadj+meanrecon-meanprox; % center to have same mean as modern stat model
tcproxadjsmooth1=lowpassadaptive(tcproxadj,freq);
%
% jacknife w.r.t. each of the 4 individual composites to get a spread
% 
sortarray=max(proxarrayjack1')';
tcproxadj=lowpassadaptive(sortarray,freq);
stdprox = std(tcproxadj(1:1351));
tcproxadj = tcproxadj*stdrecon/stdprox; % center to have same long-term multidecadal variance as stat model
meanprox = mean(tcproxadj(1352:1492)); 
tcproxadj = tcproxadj+meanrecon-meanprox; % center to have same mean as modern stat model
tcproxadjsmoothjack1=lowpassadaptive(tcproxadj,freq);
%
sortarray=max(proxarrayjack2')';
tcproxadj=lowpassadaptive(sortarray,freq);
stdprox = std(tcproxadj(1:1351));
tcproxadj = tcproxadj*stdrecon/stdprox; % center to have same long-term multidecadal variance as stat model
meanprox = mean(tcproxadj(1352:1492)); 
tcproxadj = tcproxadj+meanrecon-meanprox; % center to have same mean as modern stat model
tcproxadjsmoothjack2=lowpassadaptive(tcproxadj,freq);
%
sortarray=max(proxarrayjack3')';
tcproxadj=lowpassadaptive(sortarray,freq);
stdprox = std(tcproxadj(1:1351));
tcproxadj = tcproxadj*stdrecon/stdprox; % center to have same long-term multidecadal variance as stat model
meanprox = mean(tcproxadj(1352:1492)); 
tcproxadj = tcproxadj+meanrecon-meanprox; % center to have same mean as modern stat model
tcproxadjsmoothjack3=lowpassadaptive(tcproxadj,freq);
%
sortarray=max(proxarrayjack4')';
tcproxadj=lowpassadaptive(sortarray,freq);
stdprox = std(tcproxadj(1:1351));
tcproxadj = tcproxadj*stdrecon/stdprox; % center to have same long-term multidecadal variance as stat model
meanprox = mean(tcproxadj(1352:1492)); 
tcproxadj = tcproxadj+meanrecon-meanprox; % center to have same mean as modern stat model
tcproxadjsmoothjack4=lowpassadaptive(tcproxadj,freq);
%
%
sortarray=max(proxarrayjack5')';
tcproxadj=lowpassadaptive(sortarray,freq);
stdprox = std(tcproxadj(1:1351));
tcproxadj = tcproxadj*stdrecon/stdprox; % center to have same long-term multidecadal variance as stat model
meanprox = mean(tcproxadj(1352:1492)); 
tcproxadj = tcproxadj+meanrecon-meanprox; % center to have same mean as modern stat model
tcproxadjsmoothjack5=lowpassadaptive(tcproxadj,freq);
%
% determine upper and lower jacknife limits
%
for i=1:1492
    s1=tcproxadjsmoothjack1(i);
    s2=tcproxadjsmoothjack2(i);
    s3=tcproxadjsmoothjack3(i);
    s4=tcproxadjsmoothjack4(i);
    s5=tcproxadjsmoothjack5(i);
    spread(i)=std([s1 s2 s3 s4 s5]);
    tcproxadjsmoothupper(i)=tcproxadjsmooth1(i)+2.0*spread(i);
    tcproxadjsmoothlower(i)=tcproxadjsmooth1(i)-2.0*spread(i);
end
%
%
%
%%%%%%%%%%%%%%%
%
% Plot results
%
%
figure(1);
hold
%
% plot 95% region for statistical model reconstruction
for i=1:nkeep
tcrecon40yrsurr=lowpassmin(tcdist(1:nreconshort,isort(i)),freq);
% plot out surrogates, but eliminate first/last five years to eliminate boundary artifacts
hd=plot(yearreconshort(6:1346),tcrecon40yrsurr(6:1346));
set(hd,'color',[.75 .75 .75])
end
% plot 95% region for modern statistical model fit
for i=1:200
    yval=(-2.0+4*i/200)*sigresid;
    hd=plot(yearinstr,tcreconmod40yr+yval);
    set(hd,'color',[.86 .86 .86])
end
% plot final matter (eliminated first/last five years to remove smoothing
% artifacts
%
plot(yearreconshort(6:1346),tcrecon40yr(6:1346)); % plot reconstruction, eliminate boundary smooth artifacts
plot(yeartcprox,tcproxadjsmooth0,'g') % plot sediment composite series (w/ Vieques)
plot(yeartcprox,tcproxadjsmooth1,'cyan') % plot standard sediment composite series (w/o Vieques)
plot(yeartcprox,tcproxadjsmoothlower,'g--') % lower jacknife limit on standard composite series 
plot(yeartcprox,tcproxadjsmoothupper,'g--') % upper jacknife limit on standard composite series
plot(yearinstr,tcreconmod40yr,'k') % plot modern statistical model fit
plot(yearstorms,tcobs40yr,'r') % plot bias-corrected TC count series





