;
; Computes EOFs of observed Apr-Sep SLP, to test and compare different methods.
; (1) Compare infilled and uninfilled datasets
; (2) Compare non-rotated covariance & correlation, and rotated covariance
;     and correlation EOFs.
;
useinfill=1     ; 0=un-infilled, 1=infilled
usecorr=1       ; 0=covariance,  1=correlation
userot=1        ; 0=unrotated,   1=rotated
nretain=9
;
; Restore NOAA mode summer timeseries
;
restore,filename='noaa_summer.idlsave'
;  nsummer,summerind,summerts,summertslow
restore,filename='nao_summer.idlsave'
;  timey,seasts,tslow
naosummer=seasts
naoshort=naosummer(where(timey ge 1950))
; Select series to combine and overplot the PCs
mnan=!values.f_nan
iuse=intarr(nsummer+1,9)
iuse(*,0)=[mnan,mnan,mnan,mnan,mnan,mnan,mnan,mnan,mnan,mnan,mnan]
iuse(*,1)=[mnan,mnan,mnan,mnan,+0.5,-1.0,mnan,mnan,mnan,mnan,mnan]
iuse(*,2)=[mnan,mnan,mnan,mnan,mnan,mnan,mnan,-1.0,mnan,mnan,+1.0]
iuse(*,3)=[+1.0,mnan,mnan,mnan,mnan,mnan,-0.5,mnan,mnan,mnan,mnan]
iuse(*,4)=[-0.5,-1.0,mnan,mnan,mnan,mnan,mnan,mnan,mnan,mnan,mnan]
iuse(*,5)=[mnan,mnan,mnan,-1.0,mnan,mnan,mnan,mnan,mnan,mnan,mnan]
iuse(*,6)=[+0.5,mnan,+1.0,mnan,mnan,mnan,-0.5,mnan,mnan,mnan,mnan]
iuse(*,7)=[mnan,mnan,mnan,mnan,mnan,mnan,mnan,mnan,mnan,mnan,mnan]
iuse(*,8)=[mnan,mnan,+1.0,mnan,mnan,mnan,mnan,mnan,mnan,mnan,+0.5]
;
; Restore Apr-Sep MSLP gridded dataset
;
if useinfill eq 0 then restore,filename='obs_mslp_as.idlsave' $
                  else restore,filename='obs_mslp_as_infill.idlsave'
;  timey,fdseas,xlon,ylat,nyr,nx,ny,fdltm,fdsd,missfrac
;
; Select the years to analyse
;
ksub=where(missfrac lt 0.1,nsub)
if nsub ne 67 then message,'Ooops!'
;
fdsub=fdseas(*,*,ksub)
yrsub=timey(ksub)
;
; Compute them
;
print,'Computing EOFs'
myeof2d_rot,fdsub,ev,ea,lam,lampct,lamcum,$
  evrot,earot,lamrot,lampctrot,lamcumrot,$
  nretain=nretain,correlation=usecorr
;myeof2d,fdkeep,ev,ea,lam,lampct,lamcum,$
;  nretain=nretain,correlation=cormat
;
; Choose rotated or unrotated EOFs
;
if userot eq 1 then begin
  ev=evrot & ea=earot & lam=lamrot & lampct=lampctrot & lamcum=lamcumrot
endif
;
; Try to infill the amplitude timeseries for those years not used in the EOF
; analysis (indeed, apply it to all the data to see if it gives the correct
; values!).
;
; First remove long-term mean (not 61-90 mean!)
fdanom=reform(fdseas,nx*ny,nyr)
mkanomaly,fdanom
fdanom=reform(fdanom,nx,ny,nyr)
allea=fltarr(nyr,nretain)
for iretain = 0 , nretain-1 do begin
  for iyr = 0 , nyr-1 do begin
    allea(iyr,iretain)=total(fdanom(*,*,iyr)*ev(*,*,iretain),/nan)
  endfor
endfor
fillea=allea
fillea(ksub,*)=ea(*,*)
;
; Now plot them
;
loadct,39
multi_plot,nrow=3,ncol=2,layout='large'
if !d.name eq 'X' then window,ysize=850
;
lampct=[!values.f_nan,lampct]
lamcum=[0,lamcum]
xt=timey
plot,lampct,/xstyle,xtitle='Mode',ytitle='% variance explained',$
  psym=10,xrange=[0,20],/ylog
plot,lampct,/xstyle,xtitle='Mode',ytitle='% variance explained',$
  psym=10,xrange=[0,20]
plot,lamcum,/xstyle,xtitle='Mode',ytitle='Cumulative % variance explained',$
  psym=10,xrange=[0,20]
plot,[0,1],/nodata,xstyle=5,ystyle=5
xyouts,0,0.9,'PCA of observed Apr-Sep SLP'
if useinfill eq 0 then ttt='Not infilled data' else ttt='Infilled data'
xyouts,0,0.6,ttt
if usecorr eq 0 then ttt='Covariance matrix' else ttt='Correlation matrix'
xyouts,0,0.3,ttt
if userot eq 0 then ttt='Not rotated' else ttt='Varimax rotated'
xyouts,0,0.,ttt
;
map=def_map(/npolar)  &  map.limit(0)=15.
coast=def_coast(/get_device)  &  coast.double=0  &  coast.fill=1
coast.fillcolor=50
labels=def_labels(/off)
sm=def_sm()  &  sm.thresh=0.5         ; &  sm.method=3  & sm.npole=0
;
def_1color,cr,cg,cb,50,color='lgrey'
def_1color,cr,cg,cb,100,color='mdgrey'
;
levels=findgen(25)*0.02-0.24
c_thick=[replicate(2.,12),replicate(4.,13)]
th=10.
;
for i = 0 , nretain-1 do begin
  pause
  ;
  yt=fltarr(nyr)*!values.f_nan
  yt(ksub)=ea(*,i)
  filter_cru,th,tsin=yt,tslow=tslow,/nan
  plot,xt,yt,title='Mode'+string(i+1,format='(I2)'),$
    /xstyle,$
    xtitle='Year',ytitle='Amplitude'
  ;
  ml=where(finite(yt) eq 0)
  yt(ml)=allea(ml,i)
  oplot,xt,yt,linestyle=1
  filter_cru,th,tsin=yt,tslow=tslow,/nan
  oplot,xt,tslow,thick=5,linestyle=1
  tslow(ml)=!values.f_nan
  oplot,xt,tslow,thick=5
  ;
  oplot,!x.crange,[0,0],linestyle=1
  ;
  ; Compute and print correlations with NOAA summer indices
  ;
  kcorr=where(xt ge 1950)
  pcshort=yt(kcorr)
  allr=fltarr(nsummer)
  for isummer = 0 , nsummer-1 do $
    allr(isummer)=correlate(yt(kcorr),summerts(isummer,*))
  print,i,allr,format='(I2,10F6.2)'
  ;
  ; ...and summer stations-based NAO
  ;
  print,i,correlate(yt,naosummer)
  ;
  ; Overlay combination of NOAA and NAO indices
  ;
  mknormal,yt,refmean=refmean,refsd=refsd
  ovx=xt(kcorr)
  nov=n_elements(kcorr)
  ovy=fltarr(nov)
  for iov = 0 , nov-1 do begin
    ovy(iov)=total(iuse(*,i)*[naoshort(iov),reform(summerts(*,iov))],/nan)
  endfor
  if (max(ovy,/nan)-min(ovy,/nan)) gt 0 then begin
    mknormal,ovy
    ovy=ovy*refsd(0)+refmean(0)
    filter_cru,th,tsin=ovy,tslow=tslow,/nan
    oplot,ovx,tslow,color=100,thick=6
  endif
  ;
  fd=reform(ev(*,*,i))
  inter_confd,fd,xlon,ylat,$
    map=map,coast=coast,labels=labels,sm=sm,$
    /hi_on,/follow,levels=levels,c_thick=c_thick   ;,miss_grey='white'
endfor
;
save,filename='obs_summer_modes.idlsave',$
  timey,nretain,useinfill,usecorr,userot,xlon,ylat,nyr,nx,ny,fillea,ev,$
  lampct
;
end
