#clear workspace
rm(list=ls())
gc()
#________________________________________________________________________________________________________
#
#                  Reliability Diagram 
#________________________________________________________________________________________________________

#load sources and libraries
library(s2dverification)
library(SpecsVerification)
library(ncdf)
library(statmod)
library(maps)
library(mapdata)
library(parallel)
library(doMC)
library(psych)
library(RColorBrewer)
library(MASS)
library(maptools)  #for shapefiles
library(scales)  #for transparency
library(abind)
library(TeachingDemos)
#source('~/R/scripts/functions_s2dverification/PlotEquiMapV.R')
source('../R/ReliabilityDiagramHist.R')
source('../R/PlotRD.dy.MME4.R')

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Select parameters
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
variable<-'sfcwind' # variable
var_name<-'10m Wind Speed'
#variable<-'tas' # variable
#var_name<-'2m Temperature'
s1<-1991 # initial start date
s2<-2013 # final start date
s3<-s2-1 # final start date of the hindcast
sea<-1 # number of season (in targ)
# Lead times
ltmin<-2
ltmax<-4
# Reanalysis
nrls <- 1
ranl <- list('wERAint','wJRA55')  # exp: data of experiments
rinp <- list('ERA_Int','JRA_55')  # exp: data of experiments

# Season: 1:DJF, 2:JJA, 3:MAM, 4:SON 
sea<-4
targ <- list('DJF','JJA','MAM','SON')  # targ: forecast season
fsti <- list('November','May','February','August')# fcst: start month of the forecast

# Regions
nrg <- 10
rgn <- list('GL','NH','SH','NA','SA','AU','EA','EU','TR','AF')
region<-list("Globe","Northern H.","Southern H.","North Am.","South Am.","Australia","East Asia","Europe","Tropics","Africa")
area1<-c(0,360,-90,90)
area2<-c(0,360,20,90)
area3<-c(0,360,-90,-20)
area4<-c(190,310,10,75)
area5<-c(270,330,-60,10)
area6<-c(110,180,-50,0)
area7<-c(90,150,20,50)
area8<-c(-15,40,35,75)
area9<-c(0,360,-20,20)
area10<-c(-20,55,-35,40)
range <- list(area1,area2,area3,area4,area5,area6,area7,area8,area9,area10)
#########################################################################
for (js in 1:nrls){     # 2 Reanalysis
#for (js in 1){
print(paste("Reanalysis = ", rinp[[js]]))

for (is in 1:sea){      # 4 seasons
#for (is in 1){           
print(paste("season = ", targ[[is]]))

for (ms in 1:nrg){     # 10 Regions
#for (ms in 1){
lonlatbox=range[[ms]]
print(rgn[[ms]])
#########################################################################
# Colours of the rank histogram
#col_line<-c('#137AE1',"#feb24c","#FF0000")
col_line<-c("#FF0000","#feb24c",'#137AE1','#01854d','#4D0185')

## alpha => light(0) to dark(1)
#col_bar<-c('#C7E7F9','#F5EAC0','#F9C7D4')
col_bar<-c(adjustcolor(col_line[1],alpha=0.3),adjustcolor(col_line[2],alpha=0.5),adjustcolor(col_line[3],alpha=0.3),adjustcolor(col_line[4],alpha=0.3),adjustcolor(col_line[5],alpha=0.3))
#col_bar<-c('#A2D5F7','#FACD7F','#F6A4A4')

file<-list('RelDiag_rawdata','RelDiag_sbc_cross','RelDiag_sbc_nocross','RelDiag_cal_cross','RelDiag_cal_nocross')
  Stitle<-c('Raw Data','Simple bias correction in cross-validation', 'Simple bias correction without cross-validation',
            'Calibration in cross-validation','Calibration without cross-validation')
####################################################################

  dirfiles <-paste('/esarchive/oper/DATA/skill/Exp5/',variable,'/',ranl[[js]],'/rld/mask/',sep='')
  outputdir<-paste('/esarchive/oper/DATA/fig/Exp5/',ranl[[js]],'/',variable,'/rld/mask_mme4/',targ[[is]],'/',sep='')

  fileobs <-paste(dirfiles,rinp[[js]],'_',variable,'_',targ[[is]],'_1lead_',s1,'_',s3,'_global_ProBins_',rgn[[ms]],'.RData',sep='')
  filepred1<-paste(dirfiles,'ECMWF_S4_',variable,'_',targ[[is]],'_1lead_',s1,'_',s3,'_global_ProBins_',rgn[[ms]],'.RData',sep='')
  filepred2<-paste(dirfiles,'METFR_S3_',variable,'_',targ[[is]],'_1lead_',s1,'_',s3,'_global_ProBins_',rgn[[ms]],'.RData',sep='')
  filepred3<-paste(dirfiles,'METFR_S4_',variable,'_',targ[[is]],'_1lead_',s1,'_',s3,'_global_ProBins_',rgn[[ms]],'.RData',sep='')
  filepred4<-paste(dirfiles,'METFR_S5_',variable,'_',targ[[is]],'_1lead_',s1,'_',s3,'_global_ProBins_',rgn[[ms]],'.RData',sep='')

  # data: observations and predictions
  obs<-get(load(fileobs))
  pred1<-get(load(filepred1))
  pred2<-get(load(filepred2))
  pred3<-get(load(filepred3))
  pred4<-get(load(filepred4))
  predm<-pred1

#######################################################
 for (i in (1:5)){         # post-processing datasets
# for (i in 5){              # post-processing datasets
print(paste("post-pro = ", i))
#print(dim(obs[[i]]))    #  3 100804
#print(dim(pred[[i]]))   #  3 100804

#######################################################
 for (jc in (1:3)){         # 3 categories  (jc=1 ->'below', jc=2 ->'near', jc=3 ->'above')
# for (jc in 1){              # 3 categories
   predm[[i]][jc, ]  <- (pred1[[i]][jc, ]+pred2[[i]][jc, ]+pred3[[i]][jc, ]+pred4[[i]][jc, ])/4
   model <- rbind(rbind(pred1[[i]][jc,],pred2[[i]][jc,]),pred3[[i]][jc,],pred4[[i]][jc,],predm[[i]][jc,])
print(dim(pred1)); print(dim(predm)); print(dim(model))

  categ<-list('Below','Near','Above')
  Model_Name <- list('ECMWF_S4','METFR_S3','METFR_S4','METFR_S5','MME')
  nthr<-dim(model)[1]

#    fileout<-paste(outputdir,i,'_',categ[jc],'_',file[[i]],'_',variable,'_',s1,'_',s3,'_',rgn[[ms]],'_no_topo.ps',sep='')
    fileout<-paste(outputdir,i,'_',categ[jc],'_',file[[i]],'_',variable,'_',s1,'_',s3,'_',rgn[[ms]],'.ps',sep='')
    cairo_ps(fileout,width=8,height=7)
    title<-paste('Reliability Diagram. (',var_name,', ',categ[jc],') 
1 month lead, ',targ[[is]],' (',s1,'-',s3,'), ',region[[ms]],' 
',rinp[[js]],', ',Stitle[[i]],sep='')

    rd<-list()
    for(j in 1:nthr){     # j = Loop of 4 models + MME , jc = 3 categories
      rd[[j]]<-ReliabilityDiagramHist(model[j,],obs[[i]][jc,],plot=F,nboot=500)
    }

#  For Consistency Bar, consbars = T, otherwise, consbars = F
#    PlotRD(rd,nbins=10,consbars=T,colLine=col_line,colBar=col_bar,tit=title,hist_ylim=c(0,100))
    PlotRD(rd,nbins=10,consbars=T,colLine=col_line,colBar=col_bar,tit=title,Lg=Model_Name)
    dev.off()

   }         # 3 categories  (i=1 ->'below', i=2 ->'near', i=3 ->'above')
#######################################################

   }         # 1 raw data and 4 post-processing datasets
#######################################################

  }          # 10 Regions

 }          # 4 seasons

}           # 2 Reanalysis
