
library('s2dverification')
library('RColorBrewer')
source('/home/nevensf/gitrep/s2dverification/R/Load.R')
source('/home/nevensf/gitrep/s2dverification/R/Composite.R')
source('/home/nevensf/gitrep/s2dverification/R/PlotStereoMap.R')
source('/home/nevensf/gitrep/s2dverification/R/Eno.R')

#print(' ')
#print(' Start Load of 30-year b02p SST and ERSST')
#print(' ')
#SST1=Load(var='tos',exp='b02p',obs='ERSST',sdates=c(19501101),nleadtime=360,leadtimemin=1,leadtimemax=360,storefreq="monthly",sampleperiod=1,output='lonlat')
#print(' ')
#print(' Load Done')
#print(' ')

#save(SST1,file='SST1_test10.sav')
load('SST1_test10.sav')

print(' ')
print(' mod dimensions')
print(dim(SST1$mod))
print(' ')
print(' obs dimensions')
print(dim(SST1$obs))
print(' ')
print(' longitude axis')
print(dim(SST1$lon))
print(' ')
print(' latitude axis')
print(dim(SST1$lat))

SSTmod=Mean1Dim(SST1$mod,2)
print(' ')
print(' new mod dimensions')
print(dim(SSTmod))

#tensor_mod=array(dim=c(length(SSTmod[1,1,1,1,]),length(SSTmod[1,1,1,,1]),length(SSTmod[1,1,,1,1])))
# The line above can be simplified as :
tensor_mod <- array(dim = dim(SSTmod)[c(5,4,3)])
print(' ')
print(' mod tensor')
print(dim(tensor_mod))

#tensor_obs=array(dim=c(length(SST1$obs[1,1,1,1,1,]),length(SST1$obs[1,1,1,1,,1]),length(SST1$obs[1,1,1,,1,1])))
# The line above can be simplified as :
tensor_obs <- array(dim = dim(SST1$obs)[c(6,5,4)])
print(' ')
print(' obs tensor')
print(dim(tensor_obs))

#for (xx in 1:length(SSTmod[1,1,1,1,])) {
#    for (yy in 1:length(SSTmod[1,1,1,,1])) {
#        tensor_mod[xx,yy,]=SSTmod[1,1,,yy,xx]
#        tensor_obs[xx,yy,]=SST1$obs[1,1,1,,yy,xx]
#    }
#}
# The 5 lines above can be simplifed into
tensor_mod <- aperm(SSTmod[1,1,,,],c(3,2,1))
tensor_obs <- aperm(SST1$obs[1,1,1,,,],c(3,2,1))

ts0=seq(1,12,1)
tso=seq(1,12,1)
for (i in 1:29) {
    tso=c(tso,ts0)
}

print(' ')
print('The start of mod Composite')
print(' ')
bubi_mod=Composite(tensor_mod,tso,0,tflag=FALSE)
print(' ')
print('The dimensions of bubi_mod')
print(' ')
print(dim(bubi_mod$Composite))

#load('Nalani1.sav')

cols=rev(c(brewer.pal(11,"Spectral")))
lims=c(271,274,277,280,283,286,289,292,295,298,301,304)

for (j in 1:12 ) {
    if (j<10) {
      postscript(paste('Mod_cluster_p_0',j,'.eps',sep=''))
      }
      else {
           postscript(paste('Mod_cluster_p_',j,'.eps',sep=''))
      }
    flag=array(F,dim=dim(bubi_mod$Pvalue[,,j]))
    flag[which(bubi_mod$Pvalue[,,j]<0.05)]=TRUE
    PlotEquiMap(bubi_mod$Composite[,,j],SST1$lon,SST1$lat,toptitle=paste('Model cluster ',j,sep=''),sizetit=0.8,brks=lims,cols=cols,colNA=grey(0.4),filled.continents=T,dots=flag,intylat=30)
    dev.off()
}

#cols=rev(c(brewer.pal(11,"Spectral")))
#lims=c(271,273,275,277,279,281,283,285,287,289,291,293)

#for (j in 1:max(tso) ) {
#    if (j<10) {
#      postscript(paste('Mod_cluster_polar_0',j,'.eps',sep=''))
#      }
#      else {
#           postscript(paste('Mod_cluster_polar_',j,'.eps',sep=''))
#      }
#    PlotStereoMap(bubi_mod$Composite[,,j],SST1$lon,SST1$lat,latlims=c(40,90),toptitle=paste('Model cluster Npolar ',j,sep=''),sizetit=0.8,brks=lims,cols=cols,filled.continents=T)
#    dev.off()
#}

# 22/07 To check that the result is correct, you need to compute again the climatology
# 22/07 with an independent methodology and check that the result is the same. You could
# 22/07 for example, write tensor_mod in a netcdf file and apply cdo ymonmean on this
# 22/07 netcdf file. You would obtain a new netcdf file with 12 maps that should correspond
# 22/07 exactly to the 12 maps you have computed through Composite here.

#print(' ')
#print('The start of obs Composite')
#print(' ')
#bubi_obs=Composite(tensor_obs,tso,0)
#print(' ')
#print('The dimensions of bubi_obs')
#print(' ')
#print(dim(bubi_obs$Composite))

#cols=rev(c(brewer.pal(11,"Spectral")))
#lims=c(271,274,277,280,283,286,289,292,295,298,301,304)

#for (j in 1:max(tso) ) {
#    if (j<10) {
#      postscript(paste('Obs_cluster_0',j,'.eps',sep=''))
#      }
#      else {
#           postscript(paste('Obs_cluster_',j,'.eps',sep=''))
#      }
#    PlotEquiMap(bubi_obs$Composite[,,j],SST1$lon,SST1$lat,toptitle=paste('Observational cluster ',j,sep=''),sizetit=0.8,brks=lims,cols=cols,colNA=grey(0.4),filled.continents=T,intylat=30)
#    dev.off()
#}

#cols=rev(c(brewer.pal(11,"Spectral")))
#lims=c(271,273,275,277,279,281,283,285,287,289,291,293)

#for (j in 1:max(tso) ) {
#    if (j<10) {
#      postscript(paste('Obs_cluster_polar_0',j,'.eps',sep=''))
#      }
#      else {
#           postscript(paste('Obs_cluster_polar_',j,'.eps',sep=''))
#      }
#    PlotStereoMap(bubi_obs$Composite[,,j],SST1$lon,SST1$lat,latlims=c(-90,-40),toptitle=paste('Observational cluster Spolar ',j,sep=''),sizetit=0.8,brks=lims,cols=cols,filled.continents=T)
#    dev.off()
#}


