% Calculate time evolution of fingerprint of planetary wave reasonance
% in GISTEMP data
% (c) Michael E. Mann
% 
% calculate zonal mean fingerprint of planetary wave resonance
% based on PIK analysis
%
% read in NCEP fingerprint in zonal mean temp
%
load Data/T_clim_JJA_NH_ERA.dat
load Data/T_QRA_JJA_NH_ERA.dat
Tzone_anom=T_QRA_JJA_NH_ERA-T_clim_JJA_NH_ERA;
Tzone_anom=Tzone_anom-mean(Tzone_anom);
lat0=(0:2.5:90)';
nlat0=length(lat0);
% smooth to 5 deg res using 1-2-1 filter
for i=2:nlat0-1
    T1=Tzone_anom(i-1);
    T2=Tzone_anom(i);
    T3=Tzone_anom(i+1);
    Tsmooth0(i)=0.25*(T1+2.0*T2+T3);
end
%
% also calculate meridional gradient
%
for i=2:nlat0-2
    Tgrad0(i)=0.5*(Tsmooth0(i+1)-Tsmooth0(i-1));
end
% subsample at 5 deg resolution from 25N to 75N
lat=(25.0:5.0:75.)';
nlats=length(lat);
istart=find(lat0==25.0);
iend=find(lat0==75.0);
Tprof=Tsmooth0(istart:2:iend)';
Tgrad=Tgrad0(istart:2:iend)';
% define fingerprint as zero mean
Tprof=Tprof-mean(Tprof);
Tgrad=Tgrad-mean(Tgrad);
figure(1)
hold
plot(lat,Tprof,'b')
plot(lat,Tgrad,'k')
%
%
% now calculate fingerprint in observations
%
%
load Data/GISTEMP_latband_JJA % read in GISTEMP zonal mean temps 1880-2015
%
nyears=size(lattemp,2);
yeargiss=(1880:2015)';
zonalobs=lattemp';
for i=1:nlats
    zonemean=mean(zonalobs(:,i));
    zonalobs(:,i)=zonalobs(:,i)-zonemean;
end
latte=zonalobs';
for j=1:nyears
    obsprofile=latte(:,j);
    obsprofile=obsprofile-mean(obsprofile); % insure profile is zero mean
    Y = Tprof;
    X = [obsprofile ones(nlats,1)];
   [B,BINT,R,RINT,STATS] = regress(Y,X);
   bfingerGISS(j)=B(1);
end
%
figure(2)
hold
plot(yeargiss,bfingerGISS,'b')
% show one-sided two sig positive limit
twosig(1:nyears)=1.645*std(bfingerGISS);
plot(yeargiss,twosig,'b--');
%
% now calculate fingerprint in HadCRUT4 observations
%
load Data/HadCRUT_latband % read in HadCRUT zonal mean temps 1850-2015
%
nyears0=size(lattemp_int,2);
year0=(1880:2015)';
% use data from 1894-2015 which are continuous
yearhad=(1894:2015)';
j1=1894-1850+1;
j2=2015-1850+1;
nyearshad=length(yearhad);
zonalobs=lattemp_int';
figure(3)
hold
for i=1:nlats
    zonemean=mean(zonalobs(j1:j2,i));
    zonalobs(:,i)=zonalobs(:,i)-zonemean;
    plot(yearhad,zonalobs(j1:j2,i));
end
figure(4)
hold
latte=zonalobs';
for j=j1:j2
    obsprofile=latte(:,j);
    Y = Tprof;
    X = [obsprofile ones(nlats,1)];
    plot(obsprofile)
   [B,BINT,R,RINT,STATS] = regress(Y,X);
   bfingerHAD(j-j1+1)=B(1);
end
figure(2)
plot(yearhad,bfingerHAD,'g')
%
load Data/HadCRUT_krig_latband % read in Cowtan zonal mean temps 1850-2015
%
nyears0=size(lattemp_int,2);
year0=(1880:2015)';
% use data from 1894-2015 which are continuous
yearhad=(1894:2015)';
j1=1894-1850+1;
j2=2015-1850+1;
nyearshad=length(yearhad);
zonalobs=lattemp_int';
for i=1:nlats
    zonemean=mean(zonalobs(j1:j2,i));
    zonalobs(:,i)=zonalobs(:,i)-zonemean;
end
latte=zonalobs';
%
for j=j1:j2
    obsprofile=latte(:,j);
    Y = Tprof;
    X = [obsprofile ones(nlats,1)];
   [B,BINT,R,RINT,STATS] = regress(Y,X);
   bfingerCOW(j-j1+1)=B(1);
end
%
plot(yearhad,bfingerCOW,'k');
%
% calculate observational trend 1970-2015
%
%
ntrnd=46;
ntrnd0=ntrnd;
%
year1=(1970:2015)';
clear X;
clear Y;
% GISTEMP
bf1=bfingerGISS(91:136)';
X = [year1 ones(ntrnd,1)];
Y = bf1;
[BGISS,BINTGISS,R,RINT,STATS] = regress(Y,X);
trendGISS=BGISS(1)*X(:,1)+BGISS(2);
sigmaGISS=(BINTGISS(1,2)-BINTGISS(1,1))/4.0;
tvalGISS = BGISS(1)/sigmaGISS
% estimate DOFs
rhoGISS=lincor(R,R,1);
dofGISS=((1.0-rhoGISS)/(1.0+rhoGISS))*ntrnd-2
plot(year1,trendGISS,'b--');
% HADCRUT4
bf1=bfingerHAD(77:122)';
X = [year1 ones(ntrnd,1)];
Y = bf1;
[BHAD,BINTHAD,R,RINT,STATS] = regress(Y,X);
trendHAD=BHAD(1)*X(:,1)+BHAD(2);
sigmaHAD=(BINTHAD(1,2)-BINTHAD(1,1))/4.0;
tvalHAD = BHAD(1)/sigmaHAD
rhoHAD=lincor(R,R,1);
dofHAD=((1.0-rhoHAD)/(1.0+rhoHAD))*ntrnd-2
plot(year1,trendHAD,'g--');
% COWTAN & WAY
bf1=bfingerCOW(77:122)';
X = [year1 ones(ntrnd,1)];
Y = bf1;
[BCOW,BINTCOW,R,RINT,STATS] = regress(Y,X);
trendCOW=BCOW(1)*X(:,1)+BCOW(2);
sigmaCOW=(BINTCOW(1,2)-BINTCOW(1,1))/4.0;
tvalCOW = BCOW(1)/sigmaCOW
rhoHAD=lincor(R,R,1);
dofHAD=((1.0-rhoHAD)/(1.0+rhoHAD))*ntrnd-2
plot(year1,trendCOW,'k--');
%
%
% also calculate observational 1970-2005
% (period of overlapp w/ CMIP5 historical)
%
ntrnd=36;
%
year2=(1970:2005)';
clear X;
clear Y;
% GISTEMP
bf2=bfingerGISS(91:126)';
X = [year2 ones(ntrnd,1)];
Y = bf2;
[BGISS2,BINTGISS2,R,RINT,STATS] = regress(Y,X);
trendGISS2=BGISS2(1)*X(:,1)+BGISS2(2);
sigmaGISS2=(BINTGISS2(1,2)-BINTGISS2(1,1))/4.0;
tvalGISS2 = BGISS2(1)/sigmaGISS2
rhoGISS2=lincor(R,R,1);
dofGISS2=((1.0-rhoGISS2)/(1.0+rhoGISS2))*ntrnd-2
% HADCRUT4
bf2=bfingerHAD(77:112)';
X = [year2 ones(ntrnd,1)];
Y = bf2;
[BHAD2,BINTHAD2,R,RINT,STATS] = regress(Y,X);
trendHAD2=BHAD2(1)*X(:,1)+BHAD2(2);
sigmaHAD2=(BINTHAD2(1,2)-BINTHAD2(1,1))/4.0;
tvalHAD2 = BHAD2(1)/sigmaHAD2
rhoHAD2=lincor(R,R,1);
dofHAD2=((1.0-rhoHAD2)/(1.0+rhoHAD2))*ntrnd-2
% COWTAN & WAY
bf2=bfingerCOW(77:112)';
X = [year2 ones(ntrnd,1)];
Y = bf2;
[BCOW2,BINTCOW2,R,RINT,STATS] = regress(Y,X);
trendCOW2=BCOW2(1)*X(:,1)+BCOW2(2);
sigmaCOW2=(BINTCOW2(1,2)-BINTCOW2(1,1))/4.0;
tvalCOW2 = BCOW2(1)/sigmaCOW2
rhoCOW2=lincor(R,R,1);
dofCOW2=((1.0-rhoCOW2)/(1.0+rhoCOW2))*ntrnd-2
%
%
%
% analyze CMIP5 historical simulations
%
%
load cmip5hmisc_latband_ta;
%
% start out w/ multimodel mean
modelmean = meanofmodels;
nyearsmmm=size(meanofmodels,1);
yearmmm=(1861:2005)';
%
zonalmmmave=modelmean(:,2:12);
figure(5)
hold
clear globe
globe(1:nyearsmmm)=0.0;
weightsum=0.0;
globe=globe';
for i=1:nlats
    latitude=25.0+(i-1)*5.0;
    rad = latitude*pi/180.0;
    coslat(i)=cos(rad);
    weightsum=weightsum+coslat(i);
    globe=globe+coslat(i)*zonalmmmave(:,i);
    zonemean=mean(zonalmmmave(:,i));
    zonalmmmave(:,i)=zonalmmmave(:,i)-zonemean;
    plot(yearmmm,zonalmmmave(:,i));
%   calculate a global mean temp as cos lat weighted ave
%   of latitude zones
end
globe=globe/weightsum;
globe=globe-mean(globe);
plot(yearmmm,globe,'k')
%
figure(6)
hold
%
for j=1:nyearsmmm
    mmmprofile=zonalmmmave(j,:)';
    mmmprofile=mmmprofile-mean(mmmprofile);
    plot(mmmprofile)
    Y = Tprof;
    X = [mmmprofile ones(nlats,1)];
   [B,BINT,R,RINT,STATS] = regress(Y,X);
   bfingerMMM(j)=B(1);
end
figure(7)
plot(yearmmm,bfingerMMM)
%
% now look at all individual CMIP5 historical simulations
%
figure(8)
hold
%nmodels=174;
%nmodels=51;
nmodels=40;
clear globemod
%
for j=1:nyearsmmm
    bfingersum(j)=0.0;
    sigsum(j)=0.0;
end
for k=1:nmodels
zonalmmm=lattemp_allr(:,2:12,k);
%zonalmmm=lattemp_allm(:,2:12,k);
globemod(1:nyearsmmm,k)=0.0;
for i=1:nlats
    zonemean=mean(zonalmmm(:,i));
    zonalmmm(:,i)=zonalmmm(:,i)-zonemean;
    globemod(:,k)=globemod(:,k)+coslat(i)*zonalmmm(:,i);
end
globemod(:,k)=globemod(:,k)/weightsum;
%
for j=1:nyearsmmm
    mmmprofile=zonalmmm(j,:)';
    mmmprofile=mmmprofile-mean(mmmprofile);
    Y = Tprof;
    X = [mmmprofile ones(nlats,1)];
   [B,BINT,R,RINT,STATS] = regress(Y,X);
   bfingermod(j)=B(1);
   bfingermodel(j,k)=B(1);
   bfingersum(j)=bfingersum(j)+bfingermod(j);
end
%
% calculate trend since 1970
ntrnd=36;
yearmod=(1970:2005)';
clear X;
clear Y;
bf1=bfingermod(110:145)';
X = [yearmod ones(ntrnd,1)];
Y = bf1;
[Bmod,BINTmod,R,RINT,STATS] = regress(Y,X);
trendmod(:,k)=Bmod(1)*X(:,1)+Bmod(2);
sigmamod(k)=(BINTmod(1,2)-BINTmod(1,1))/4.0;
Bmodsave(k)=Bmod(1);
tvalmod(k) = Bmod(1)/sigmamod(k);
diffhad(k) = Bmod(1)-BHAD2(1);
diffgiss(k) = Bmod(1)-BGISS2(1);
diffcow(k) = Bmod(1)-BCOW2(1);
%
% calculate time-dependent fractional exceedence of
% two sigma level for fingerprint projection for
% each simulation
%
sigthresh=1.645*std(bfingermod); % one-sided two sig thresh
for j=1:nyearsmmm
    if (bfingermod(j)>sigthresh)        
       sigsum(j)=sigsum(j)+1.0;
    end
end
plot(yearmmm,bfingermod)
end
%
% calculate fraction of multimodel ensemble members
% that have positive post-1970 trend
%
tvalsort=sort(tvalmod);
bmodsort=sort(Bmodsave);
BModAve=mean(Bmodsave)
SigmaModAve=mean(sigmamod)
BModMedian=bmodsort(round(nmodels/2))
BModSigmaPlus=bmodsort(round(0.84*nmodels))
BModSigmaMinus=bmodsort(round(0.16*nmodels))
BModTwoSigmaPlus=bmodsort(round(0.975*nmodels))
BModTwoSigmaMinus=bmodsort(round(0.025*nmodels))
pctpos=100.0*(1.0-min(find(tvalsort>0))/nmodels)
%
% now calculate fraction of multimodel ensemble members
% with trends that equal or exceed observational trend
%
diffhadsort=sort(diffhad);
diffgisssort=sort(diffgiss);
diffcowsort=sort(diffcow);
pcthad=100.0*(1.0-min(find(diffhadsort>0))/nmodels)
pctgiss=100.0*(1.0-min(find(diffgisssort>0))/nmodels)
pctcow=100.0*(1.0-min(find(diffcowsort>0))/nmodels)
%
% now do Monte Carlo analysis to estimate statistical significance
%
% first fit AR(1) model to each model simulation fingerprint series
%
for k=1:nmodels
    series=bfingermodel(:,k);
    rho(k)=lincor(series,series,1);
    if (rho<0)
        rho(k)=0.0;
    end
    stochamp=std(series);
    ampfactor=1.0/sqrt(1.0/(1.0-rho(k)^2));
    ampnoise(k)=stochamp*ampfactor;
end
%
nsurr=10000;
%nsurr=10;
% now generate ensemble of Monte Carlo surrogates
%
for j=1:nsurr
for k=1:nmodels
    rednoise(1)=gasdev;
    for i=2:nyearsmmm
        rednoise(i)=rho(k)*rednoise(i-1)+gasdev;
    end
    rednoise=ampnoise(k)*rednoise;
    X = [yearmod ones(ntrnd,1)];
    Y = rednoise(110:145)';
   [Bred,BINTred,R,RINT,STATS] = regress(Y,X);
   sigmared=(BINTred(1,2)-BINTred(1,1))/4.0;
   tvalred(k) = Bred(1)/sigmared;
   diffhadred(k) = Bred(1)-BHAD2(1);
   diffgissred(k) = Bred(1)-BGISS2(1);
   diffcowred(k) = Bred(1)-BCOW2(1);
end
diffhadsortred=sort(diffhadred);
diffgisssortred=sort(diffgissred);  
diffcowsortred=sort(diffcowred);
if (max(diffhadsortred)<0)
    pcthadred(j)=0.0;
else
pcthadred(j)=100.0*(1.0-min(find(diffhadsortred>0))/nmodels);
end
if (max(diffgisssortred)<0)
    pctgissred(j)=0.0;
else
pctgissred(j)=100.0*(1.0-min(find(diffgisssortred>0))/nmodels);
end
if (max(diffcowsortred)<0)
    pctcowred(j)=0.0;
else
pctcowred(j)=100.0*(1.0-min(find(diffcowsortred>0))/nmodels);
end
tvalsortred=sort(tvalred);
if (max(tvalsortred)<0)
    pctposred(j)=0.0;
else
pctposred(j)=100.0*(1.0-min(find(tvalsortred>0))/nmodels);
end
end
pcthadredsort=sort(pcthadred);
pctgissredsort=sort(pctgissred);
pctcowredsort=sort(pctcowred);
pctposredsort=sort(pctposred);
'95 % limit (Had)',pcthadredsort(round(99*nsurr/100))
'95 % limit (GISS)',pctgissredsort(round(99*nsurr/100))
'95 % limit (Cowtan)',pctcowredsort(round(99*nsurr/100))

%
'99 % limit (Had)',pcthadredsort(round(99*nsurr/100))
'99 % limit (GISS)',pctgissredsort(round(99*nsurr/100))
'99 % limit (Cowtan)',pctcowredsort(round(99*nsurr/100))
%
%
'99.9 % limit (Had)',pcthadredsort(round(999*nsurr/1000))
'99.9 % limit (GISS)',pctgissredsort(round(999*nsurr/1000))
'99.9 % limit (Cowtan)',pctcowredsort(round(999*nsurr/1000))
%
'max % limit (Had)',max(pcthadredsort)
'max % limit (GISS)',max(pctgissredsort)
'max % limit (Cowtan)',max(pctcowredsort)
%
% calculate ave time-dependent fractional
% exceedance of two sigma level for 
% fingerprint
%
'95 % limit (% pos)',pctposredsort(round(95*nsurr/100))
'99 % limit (% pos)',pctposredsort(round(99*nsurr/100))
'99.9 % limit (% pos)',pctposredsort(round(999*nsurr/1000))
'max % limit (% pos)',max(pctposredsort)
%
sigmean=sigsum/nmodels;
bfingermean=bfingersum/nmodels;
plot(yearmmm,bfingermean,'k');
figure(9)
hold
plot(yearmmm,bfingermean,'k')
% calculate trend since 1970
ntrnd=36;
year2=(1970:2005)';
clear X;
clear Y;
bf1=bfingermean(110:145)';
X = [year2 ones(ntrnd,1)];
Y = bf1;
[BM,BINTM,R,RINT,STATS] = regress(Y,X);
trendM=BM(1)*X(:,1)+BM(2);
sigmaM=(BINTM(1,2)-BINTM(1,1))/4.0;
tvalM = BM(1)/sigmaM
rhoM=lincor(R,R,1);
dofM=((1.0-rhoM)/(1.0+rhoM))*ntrnd-2
plot(year2,trendM,'k--');
%
% rescale and plot global mean series for comparison
scalefactor=0.85;
oldmean=mean(globe);
newmean=mean(bfingermean);
oldstd=std(globe);
newstd=scalefactor*std(bfingermean);
globerescaled=globe*newstd/oldstd;
globerescaled=globerescaled+newmean-oldmean;
plot(yearmmm,globerescaled,'b')
%
figure(10)
hold
plot(yearmmm,sigmean,'k')
%
%
% now examine projection of fingerprint series onto 300 mb U field
%
clear meanofmodels;
load cmip5hmisc_latband_ua;
%
figure(11)
hold
%
% start out w/ multimodel mean fingerprint vs. mean U field
modelmeanu = meanofmodels;
for i=2:12;
    Y = modelmeanu(:,i);
    X = [bfingermean' ones(nyearsmmm,1)];
    [B,BINT,R,RINT,STATS] = regress(Y,X);
    uproject(i-1)=B(1);
end
%
%
% now do for each model individually
% and calculate composite over all models
for i=2:12
    uprojectsum(i-1)=0.0;
end
for k=1:nmodels
    for i=2:12;
    Y = latwspd_allr(:,i,k);
    bfingm = bfingermodel(:,k);
    X = [bfingm ones(nyearsmmm,1)];
    [B,BINT,R,RINT,STATS] = regress(Y,X);
    uprojectm(i-1,k)=B(1);
    uprojectsum(i-1)=uprojectsum(i-1)+uprojectm(i-1,k);
    end
    plot(lat,uprojectm(1:11,k));
end
for i=2:12
    uprojectmean(i-1)=uprojectsum(i-1)/nmodels;
end
%
plot(lat,uprojectmean(1:11),'k','Linewidth',2);
plot(lat,uproject(1:11),'k--','Linewidth',2);
%
    

%