#########################################################################################
#                               Sub-seasonal Skill Scores                               #
#########################################################################################
# you can run it from the terminal with the syntax:
#
# Rscript SkillScores_v7.R <chunk to compute>
#
# i.e: to run only the chunk number 3, write:
#
# Rscript SkillScores_v4.R 3
#
# if you don't specify any argument, or you run it from the R interface, it runs all the chunks in a sequential way.
# Use this last approach only if the computational speed is not a problem.

rm(list=ls())

##########################################################################################
#                            Load functions and libraries                                #
##########################################################################################

#load libraries and functions:
library(SpecsVerification) # for skill scores
library(s2dverification)   # for Load() function
library(easyVerification)  # for veriApply() function
library(abind)             # for merging arrays
source('/home/Earth/ncortesi/scripts/Rfunctions.R')

##########################################################################################
#                                MareNostrum settings                                    #
##########################################################################################

args <- commandArgs(TRUE) # put into variable args the list with all the optional arguments with whom the script was run from the terminal

if(length(args) == 0) print("Running the script in a sequential way")
if(length(args) > 1) stop("Only one argument is required")

mare <- ifelse(length(args) == 1 ,TRUE, FALSE) # set variable 'mare' to TRUE if we are running the script in MareNostrum, FALSE otherwise

if(mare) my.month <- as.integer(args[1])  # number of the month to run in this script (if mare == TRUE)

##########################################################################################
#                                     User's settings                                    #
##########################################################################################
#if(!mare) {source('/home/Earth/ncortesi/Downloads/scripts/Rfunctions.R')} else {source('/gpfs/projects/bsc32/bsc32842/scripts/Rfunctions.R')}

# working dir where to put the output maps and files in the /Data and /Maps subdirs:
work.dir <- ifelse(!mare, "/scratch/Earth/ncortesi/RESILIENCE/Subestacional", "/gpfs/projects/bsc32/bsc32842/results")

# path of the weekly var rean data:
rean.dir <- '/esarchive/old-files/recon_ecmwf_erainterim/weekly_mean/$VAR_NAME$_f6h/$VAR_NAME$_$START_DATE$.nc'
rean.name <- "ERA-Interim"

# path of the monthly forecast system files:
forecast.year <- 2014                    # starting year of the weekly sequence of the forecasts
forecast.dir <- paste0('/esnas/exp/ECMWF/monthly/ensforhc/weekly_mean/$VAR_NAME$_f6h/',forecast.year,'$MONTH$$DAY$00/$VAR_NAME$_$START_DATE$00.nc')

cfs.name <- 'ECMWF'                      # name of the climate forecast system (CFS) used for monthly predictions (to be used in map titles)
var.name <- 'sfcWind'                    # forecast variable. It is the name used inside the netCDF files and as suffix in map filenames, i.e: 'sfcWind' or 'psl'
var.name.map <- '10m Wind Speed'         # forecast variable to verify (name used in the map titles), i.e: '10m Wind Speed' or 'SLP'

mes <- 1                                 # starting forecast month (1: january, 2: february, etc.)
day <- 2                                 # starting forecast day

yr1.hind <- 1994 #1994         # first hindcast year
yr2.hind <- 2013 #2013         # last hindcast year (usually the forecast year -1)

leadtime.week <- c('5-11','12-18','19-25','26-32') # which leadtimes are available in the weekly dataset
n.members <- 4  # number of hindcast members

my.score.name <- c('FairRpss','FairCrpss','EnsCorr','RelDiagr')  # choose one or more skills scores to compute between EnsCorr, FairRPSS, FairCRPSS and/or RelDiagr
              
veri.month <- 1:12      # select the month(s) you want to compute the chosen skill scores

my.pvalue <- 0.05          # in case of computing the EnsCorr, set the p_value level to show in the ensemble mean correlation maps
my.prob <- c(1/3,2/3)      # quantiles used for FairRPSS and the RelDiagr (usually they are the terciles)
conf.level <- c(0.05, 0.95)# percentiles employed for the calculation of the confidence level for the Rpss and Crpss (can be more than 2 if needed)
int.lat <- NULL #1:160     # latitude position of grid points selected for the Reliability Diagram (if you want to subset a region; if not, put NULL)
int.lon <- NULL #1:320     # longitude position of grid points selected for the Reliability Diagram (if you want to subset a region; if not, put NULL)

pref.rean <- FALSE         # If TRUE, instead of measuring the skill scores, it computes the anomalies and climatologies both for reanalisys and forecast.
                           # To do it once the first time you evaluate a new forecast system or you use a new reanalysis, and then set it to FALSE to measure the skill
pref.forecasts <- FALSE

boot <- TRUE               # in case of computing the FairRpss and/or the FairCrpss, you can choose to do a bootstrap to evaluate the uncertainty of the skill scores
n.boot <- 20               # number of resamples considered in the bootstrapping

# coordinates of a chosen point in the world to plot the time series over the 52 maps (they must be the same values in objects la y lo)
my.lat <- 55.3197120 
my.lon <- 0.5625

###################################### Derived variables #############################################################################

#source(paste0(workdir,'/ColorBarV.R'))  # Color scale used for maps
n.categ <- 1+length(my.prob)  # number of forecasting categories (usually 3 if terciles are used)

# blue-yellow-red colors:
#col <- ifelse(!mare, as.character(read.csv("/scratch/Earth/ncortesi/RESILIENCE/rgbhex.csv",header=F)[,1]), as.character(read.csv("/gpfs/projects/bsc32//bsc32842/scripts/rgbhex.csv",header=F)[,1]))  
#col<-as.character(read.csv("/home/Earth/vtorralb/rgbhex.csv",header=F)[,1])  # blue-yellow-red colors
col <- c("#0C046E", "#1914BE", "#2341F7", "#2F55FB", "#3E64FF", "#528CFF", "#64AAFF", "#82C8FF", "#A0DCFF", "#B4F0FF", "#FFFBAF", "#FFDD9A", "#FFBF87", "#FFA173", "#FF7055", "#FE6346", "#F7403B", "#E92D36", "#C80F1E", "#A50519")

sdates.seq <- weekly.seq(forecast.year,mes,day)  # load the sequence of dates corresponding to all the thursday of the year
n.leadtimes <- length(leadtime.week)
n.yrs.hind <- yr2.hind-yr1.hind+1
my.month.short <- substr(my.month,1,3)

## extract geographic coordinates; do only ONCE for each preducton system and then save the lat/lon in a coordinates.RData and comment these rows to use function load() below: 
#data.hindcast <- Load(var='sfcWind', exp = 'EnsEcmwfWeekHind', 
#	      	 obs = NULL, sdates=paste0(yr1.hind:yr2.hind,substr(sdates.seq[startdate],5,6),substr(sdates.seq[startdate],7,8)),
#		 nleadtime = 1, leadtimemin = 1, leadtimemax = 1, output = 'lonlat', configfile = file_path, method='distance-weighted' )

#lats<-data.hindcast$lat
#lons<-data.hindcast$lon
#save(lats,lons,file=paste0(workdir,'/coordinates.RData'))
#rm(data.hindcast);gc()

# format geographic coordinates to use with PlotEquiMap:
#load(paste0(workdir,'/coordinates.RData'))
#n.lon <- length(lons)
#n.lat <- length(lats)
#n.lonr <- n.lon - ceiling(length(lons[lons<180 & lons > 0]))
#la<-rev(lats)
#lo<-c(lons[c((n.lonr+1):n.lon)]-360,lons[1:n.lonr])

# load only 1 year of weekly var rean data from reanalysis to get lat and lon:        
data.rean <- Load(var = var.name, exp = list(list(path=rean.dir)),
                  obs = NULL, sdates=paste0(yr1.hind,substr(sdates.seq[1],5,6),substr(sdates.seq[1],7,8)),
                  nleadtime = 1, leadtimemin = 1, leadtimemax = 1, output = 'lonlat', nprocs=1) 

lons <- data.rean$lon
lats <- data.rean$lat
n.lon <- length(lons)
n.lat <- length(lats)
n.lonr <- n.lon - ceiling(length(lons[lons<180 & lons > 0]))
if(mare) n.lat == 1

my.grid<-paste0('r',n.lon,'x',n.lat)

#file_path='/home/Earth/ngonzal2/R/ConfFiles/IC3.conf'              # Load() file path
#file_path <- '/home/Earth/ncortesi/Downloads/RESILIENCE/IC3.conf'

#########################################################################################
#     Calculate and save up to all the chosen Skill Scores for a selected period        #
#########################################################################################
# if we run the script from the terminal with an argument, it only computes the month we specify in the second argument and save the results for that month
if(mare) veri.month <- my.month

for(month in veri.month){
    #month=1 # for the debug

    # select the startdates (weeks) you want to compute:
    my.startdates <- which(as.integer(substr(sdates.seq,5,6)) == month) #startdates.monthly[[month]] #c(1:5)
    
    startdate.name <- my.month[month]                       # name given to the period of the selected startdates  # i.e:"January" 
    n.startdates <- length(my.startdates)                   # number of startdates in the selected month
    n.yrs <- length(my.startdates)*n.yrs.hind               # number of total years for the selected month
    
    print(paste0("Computing Skill Scores for ",startdate.name))

    # Merge all selected startdates:
    hind.dim <- c(n.members, n.yrs.hind*n.startdates, n.leadtimes, n.lat, n.lon)  # dimensions of the hindcast array
        
    my.FairRpss <- my.FairCrpss <- my.EnsCorr <- my.PValue <- array(NA,c(n.leadtimes, n.lat,n.lon))
    if(boot) my.FairRpssBoot <- my.FairCrpssBoot <- array(NA, c(n.boot, n.leadtimes, n.lat, n.lon))
    if(boot) my.FairRpssConf <- my.FairCrpssConf <- array(NA, c(length(conf.level), n.leadtimes, n.lat, n.lon))
       
       anom.rean.chunk <- anom.hindcast.mean.chunk <- array(NA, c(n.startdates*n.yrs.hind, n.leadtimes, n.lat, n.lon))
       anom.hindcast.chunk <- array(NA, c(n.members, n.startdates*n.yrs.hind, n.leadtimes, n.lat, n.lon))
       #my.interv<-(1 + sub.size*(s-1)):((sub.size*s) + add.last)          # longitude interval where to load data

       for(startdate in my.startdates){
	  pos.startdate <- which(startdate == my.startdates) # count of the number of stardates already loaded+1
          my.time.interv <- (1+(pos.startdate-1)*n.yrs.hind):(pos.startdate*n.yrs.hind)  # time interval where to load data
          
          print(paste0('Computing reanalysis anomalies for startdate ', which(startdate==my.startdates),'/',length(my.startdates)))

          # load weekly var rean data in: /esnas/reconstructions/ecmwf/eraint/weekly_mean
          data.rean <- Load(var = var.name, exp = list(list(path=rean.dir)),
                          obs = NULL, sdates=paste0(yr1.hind:yr2.hind,substr(sdates.seq[startdate],5,6),substr(sdates.seq[startdate],7,8)),
                          nleadtime = n.leadtimes, leadtimemin = 1, leadtimemax = n.leadtimes, output = 'lonlat', nprocs=1) 

          # dim(data.rean$mod) # [1,1,20,4,320,640]

          # Mean of the 20 years (for each point and leadtime)
          clim.rean <- apply(data.rean$mod,c(1,2,4,5,6),mean)[1,1,,,] # average of all years for each leadtime and pixel
          clim.rean <- InsertDim(InsertDim(InsertDim(clim.rean,1,n.yrs.hind),1,1),1,1) # repeat the same values <n.years> times to be able to calculate the anomalies #[1,1,20,4,320,640]
          anom.rean <- data.rean$mod - clim.rean 
          #anom.rean<-InsertDim(anom.rean[1,1,,,,],1,1) # [1,20,4,320,640]

          rm(data.rean, clim.rean)
          gc()
          
	  #load(paste0(workdir,'/Data_',var.name,'/anom_rean_startdate',startdate,'.RData'))
	  #anom.rean <- drop(anom.rean)
	  anom.rean.chunk[my.time.interv,,,] <- anom.rean
          
          # Load hindcast data:
          print(paste0('Computing forecast anomalies for startdate ', which(startdate==my.startdates),'/',length(my.startdates)))
        
          # Load weekly var subseasonal data in: /esnas/exp/ECMWF/monthly/ensforhc/weekly_mean and interpolate them to the same ERA-Interim resolution (512x256)
          data.hindcast <- Load(var = var.name, exp = list(list(path=forecast.dir)),
                              obs = NULL, sdates = paste0(yr1.hind:yr2.hind, substr(sdates.seq[startdate],5,6), substr(sdates.seq[startdate],7,8)),
                              nleadtime = n.leadtimes, leadtimemin = 1, leadtimemax = n.leadtimes, output = 'lonlat',  grid=my.grid, method='bilinear', nprocs=1) 
        
          # dim(data.hindcast$mod) # [1,4,20,4,320,640]

          # Mean of the 4 members of the Hindcasts and of the 20 years (for each point and leadtime);
          clim.hindcast <- apply(data.hindcast$mod,c(1,4,5,6),mean)[1,,,] # average of all years and members for each leadtime and pixel
          clim.hindcast <- InsertDim(InsertDim(InsertDim(clim.hindcast,1,n.yrs.hind),1,n.members),1,1)  # repeat the same values <n.members> and <n.yrs.hind> times to calc. anomalies
          anom.hindcast <- data.hindcast$mod - clim.hindcast
        
          rm(data.hindcast,clim.hindcast)
          gc()

          # average the anomalies over the 4 hindcast members to compute the ensemble mean [1,20,4,320,640]
          anom.hindcast.mean<-apply(anom.hindcast,c(1,3,4,5,6),mean)

          gc()
              
	  #load(paste0(workdir,'/Data_',var.name,'/anom_hindcast_startdate',startdate,'.RData')) 
	  anom.hindcast <- drop(anom.hindcast)
	  anom.hindcast.chunk[,my.time.interv,,,] <- anom.hindcast
          
	  rm(anom.hindcast, anom.rean)
          gc()

	  if(any(my.score.name=="EnsCorr")){
              
	     #load(paste0(workdir,'/Data_',var.name,'/anom_hindcast_mean_startdate',startdate,'.RData'))  # load ensemble mean hindcast data
              
	     anom.hindcast.mean <- drop(anom.hindcast.mean)
	     anom.hindcast.mean.chunk[my.time.interv,,,] <- anom.hindcast.mean
              
	     rm(anom.hindcast.mean)
             gc()
	  }

       }
        
       if(any(my.score.name=="FairRpss")){
         
            #anom.hindcast.chunk.double <- abind(list(anom.hindcast.chunk,anom.hindcast.chunk),along=4)
            #anom.rean.chunk.double <- abind(list(anom.rean.chunk,anom.rean.chunk),along=3)
            
	    #my.FairRpss.sub <- veriApply("FairRpss", fcst=anom.hindcast.sub, obs=anom.rean.sub, prob=my.prob, tdim=2, ensdim=1, parallel=FALSE, ncpus=n.cpus)
            my.FairRps.chunk <- veriApply("FairRps", fcst=anom.hindcast.chunk, obs=anom.rean.chunk, prob=my.prob, tdim=2, ensdim=1, parallel=FALSE, ncpus=1)
            #my.FairRps.chunk.double <- veriApply("FairRps", fcst=anom.hindcast.chunk.double, obs=anom.rean.chunk.double, prob=my.prob, tdim=2, ensdim=1, parallel=FALSE, ncpus=n.cpus)
  
            # the prob.for the climatological ensemble can be set without using convert2prob and they are the same for all points and leadtimes (33% for each tercile):
            ens <- array(33,c(n.startdates*n.yrs.hind,3)) 

            # the observed probabilities vary from point to point and for each leadtime:  
            obs <- apply(anom.rean.chunk, c(2,3,4), function(x){convert2prob(x, prob <- my.prob)})

            # reshape obs to be able to apply EnsRps(ens, obs) below:  
            obs2 <- InsertDim(obs,1,3)
            obs2[2,1:(n.startdates*n.yrs.hind),,,] <- obs2[1,(n.startdates*n.yrs.hind + 1:(n.startdates*n.yrs.hind)),,,]
            obs2[3,1:(n.startdates*n.yrs.hind),,,] <- obs2[1,(2*n.startdates*n.yrs.hind + 1:(n.startdates*n.yrs.hind)),,,]
            obs3 <- obs2[,1:(n.startdates*n.yrs.hind),,,,drop=F] # drop=F is used not to loose the dimension(s) with only 1 element
              
            my.Rps.clim.chunk <- apply(obs3,c(3,4,5), function(x){ EnsRps(ens,t(x)) })
        
            my.FairRps.chunk.sum <- apply(my.FairRps.chunk, c(2,3,4), sum)
            my.Rps.clim.chunk.sum <- apply(my.Rps.clim.chunk, c(2,3,4), sum)
          
	    my.FairRpss.chunk <- 1-(my.FairRps.chunk.sum/my.Rps.clim.chunk.sum)
              
            #my.FairRpss[,c,] <- my.FairRpss.chunk
              
	    #rm(my.FairRpss.chunk, ens, obs, obs2, obs3, my.FairRps.chunk.sum, my.Rps.clim.chunk.sum)
            gc()
              
            # bootstrapping:                
             
            for(b in 1:n.boot){
                cat(paste0("resampling n. ",b,"/",n.boot))
                yrs.sampled <- sample(1:n.yrs, replace=TRUE)

                #for(y in 1:n.yrs) anom.rean.chunk.sampled[y,,,] <- anom.rean.chunk[yrs.sampled[y],,,]
                #for(y in 1:n.yrs) anom.hindcast.chunk.sampled[,y,,,] <- anom.hindcast.chunk[,yrs.sampled[y],,,]

                my.FairRps.sampled <- my.FairRps.chunk              
                my.Rps.clim.sampled <- my.Rps.clim.chunk
                
                for(y in 1:n.yrs) my.FairRps.sampled[y,,,] <- my.FairRps.chunk[yrs.sampled[y],,,]
                for(y in 1:n.yrs) my.Rps.clim.sampled[y,,,] <- my.Rps.clim.chunk[yrs.sampled[y],,,]
           
                #my.FairRps.chunk <- veriApply("FairRps", fcst=anom.hindcast.chunk.sampled, obs=anom.rean.chunk.sampled, prob=my.prob, tdim=2, ensdim=1, parallel=FALSE, ncpus=n.cpus)
                #ens <- array(33,c(n.startdates*n.yrs.hind,3)) 
                #obs <- apply(anom.rean.chunk.sampled, c(2,3,4), function(x){convert2prob(x, prob <- my.prob)})
                #obs2 <- InsertDim(obs,1,3)
                #obs2[2,1:(n.startdates*n.yrs.hind),,,] <- obs2[1,(n.startdates*n.yrs.hind + 1:(n.startdates*n.yrs.hind)),,,]
                #obs2[3,1:(n.startdates*n.yrs.hind),,,] <- obs2[1,(2*n.startdates*n.yrs.hind + 1:(n.startdates*n.yrs.hind)),,,]
                #obs3 <- obs2[,1:(n.startdates*n.yrs.hind),,,,drop=F]
                #my.Rps.clim.chunk <- apply(obs3,c(3,4,5), function(x){ EnsRps(ens,t(x)) })
                
                my.FairRps.chunk.sum <- apply(my.FairRps.chunk,c(2,3,4), sum)
                my.Rps.clim.chunk.sum <- apply(my.Rps.clim.chunk,c(2,3,4), sum)
                
                my.FairRpss.chunk <- 1-(my.FairRps.chunk.sum/my.Rps.clim.chunk.sum)

                my.FairRpssBoot[b,,,chunk$int[[c]]] <- my.FairRpss.chunk
                rm(my.FairRpss.chunk, ens, obs, obs2, obs3, my.FairRps.chunk.sum, my.Rps.clim.chunk.sum)
                gc()
            }
              
            cat('\n')
              
            # calculate the percentiles of the skill score:
            my.FairRpssConf[,,,chunk$int[[c]]] <- apply(my.FairRpssBoot[,,,chunk$int[[c]]], c(2,3,4), function(x) quantile(x, probs=conf.level, type=8)) 
         
       }

       if(any(my.score.name=="FairCrpss")){
          
	    my.FairCrps.chunk <- veriApply("FairCrps", fcst=anom.hindcast.chunk, obs=anom.rean.chunk, tdim=2, ensdim=1, parallel=FALSE, ncpus=n.cpus)
            anom.all.chunk <- abind(anom.hindcast.chunk,anom.rean.chunk, along=1) # merge exp and obs together to use apply:
            my.Crps.clim.chunk <- apply(anom.all.chunk, c(3,4,5), function(x){ EnsCrps(t(x[1:n.members,]), x[n.members+1,])})
            
            my.FairCrps.chunk.sum <- apply(my.FairCrps.chunk,c(2,3,4), sum)
            my.Crps.clim.chunk.sum <- apply(my.Crps.clim.chunk,c(2,3,4), sum)
            my.FairCrpss.chunk <- 1-(my.FairCrps.chunk.sum/my.Crps.clim.chunk.sum)

	    my.FairCrpss[,,chunk$int[[c]]]<-my.FairCrpss.chunk
	    rm(my.FairCrpss.chunk, my.FairCrps.chunk.sum, my.Crps.clim.chunk.sum)
            gc()
            
          # bootstrapping:
              
            for(b in 1:n.boot){
                print(paste0("resampling n. ",b,"/",n.boot))
                yrs.sampled <- sample(1:n.yrs, replace=TRUE)

                for(y in 1:n.yrs) anom.hindcast.chunk.sampled[,y,,,] <- anom.hindcast.chunk[,yrs.sampled[y],,,]
                for(y in 1:n.yrs) anom.rean.chunk.sampled[y,,,] <- anom.rean.chunk[yrs.sampled[y],,,]

                my.FairCrps.chunk <- veriApply("FairCrps", fcst=anom.hindcast.chunk.sampled, obs=anom.rean.chunk.sampled, tdim=2, ensdim=1, parallel=TRUE, ncpus=n.cpus)
                anom.all.chunk <- abind(anom.hindcast.chunk.sampled, anom.rean.chunk.sampled, along=1) # merge exp and obs together to use apply:
                my.Crps.clim.chunk <- apply(anom.all.chunk, c(3,4,5), function(x){ EnsCrps(t(x[1:n.members,]), x[n.members+1,])})
                my.FairCrps.chunk.sum <- apply(my.FairCrps.chunk,c(2,3,4), sum)
                my.Crps.clim.chunk.sum <- apply(my.Crps.clim.chunk,c(2,3,4), sum)
                my.FairCrpss.chunk <- 1-(my.FairCrps.chunk.sum/my.Crps.clim.chunk.sum)
	         
                my.FairCrpssBoot[b,,,chunk$int[[c]]] <- my.FairCrpss.chunk
                rm(my.FairCrpss.chunk, my.FairCrps.chunk.sum, my.Crps.clim.chunk.sum)
                gc()
          
             }
              
             cat('\n')
              
             # calculate the percentiles of the skill score:
             my.FairCrpssConf[,,,chunk$int[[c]]] <- apply(my.FairCrpssBoot[,,,chunk$int[[c]]], c(2,3,4), function(x) quantile(x, probs=conf.level, type=8)) 
        
          
        }

       if(any(my.score.name=="EnsCorr")){
	  # ensemble mean correlation for the selected startdates (first dim, 'week') and all leadtimes (second dim):
	  my.EnsCorr.chunk <- my.PValue.chunk <- array(NA, c(n.leadtimes, n.lat, chunk$n.int[[c]]))

	  for(i in 1:n.leadtimes){
	    for(j in 1:n.lat){
	      for(k in 1:chunk$n.int[[c]]){                      
		my.EnsCorr.chunk[i,j,k] <- cor(anom.hindcast.mean.chunk[,i,j,k],anom.rean.chunk[,i,j,k], use="complete.obs")
	        # option 'na.method' is chosen from the following list: c("all.obs", "complete.obs", "pairwise.complete.obs", "everything", "na.or.complete")
               
                my.PValue.chunk[i,j,k] <- cor.test(anom.hindcast.mean.chunk[,i,j,k],anom.rean.chunk[,i,j,k], use="complete.obs")$p.value
                
	      }
	    }
	  }

	  my.EnsCorr[,,chunk$int[[c]]] <- my.EnsCorr.chunk
	  my.PValue[,,chunk$int[[c]]] <- my.PValue.chunk

	  rm(anom.hindcast.chunk,anom.hindcast.mean.chunk,my.EnsCorr.chunk,my.PValue.chunk)
       }

       rm(anom.rean.chunk)
       gc()
       #mem()

       #if(mare == TRUE) {
       #   if(any(my.score.name=="FairRpss")) save(my.FairRpss, file=paste0(workdir,'/Data_',var.name,'/FairRpss_',startdate.name,'_chunk_',c,'.RData'))
       #   if(any(my.score.name=="FairCrpss")) save(my.FairCrpss, file=paste0(workdir,'/Data_',var.name,'/FairCrpss_',startdate.name,'_chunk_',c'.RData'))
       #   if(any(my.score.name=="EnsCorr")) save(my.EnsCorr, file=paste0(workdir,'/Data_',var.name,'/EnsCorr_',startdate.name,'_chunk_',c'.RData'))
       #}
                   

    # the Reliability diagram cannot be computed splitting the data in chunk, so it is computed only if the script is run from the R interface or with no arguments:

        if(any(my.score.name=="RelDiagr")){ # in this case the computation cannot be split in groups of grid points, but can be split for leadtime instead:   
            my.RelDiagr<-list()
            
            for(lead in 1:n.leadtimes){
                cat(paste0('leadtime n.: ',lead,'/',n.leadtimes))
                anom.rean.chunk <- array(NA,c(n.startdates*n.yrs.hind,n.lat,n.lon))
                anom.hindcast.chunk <- array(NA,c(n.members,n.startdates*n.yrs.hind,n.lat,n.lon))
                cat('  startdate: ')
                i=0 

                for(startdate in my.startdates){
                    i=i+1
                    cat(paste0(i,'/',length(my.startdates),'  '))

                    pos.startdate<-which(startdate==my.startdates) # count of the number of stardates already loaded+1
                    my.time.interv<-(1+(pos.startdate-1)*n.yrs.hind):(pos.startdate*n.yrs.hind)  # time interval where to load data

                    # Load reanalysis data of next startdate:
                    load(paste0(work.dir,'/Data_',var.name,'/anom_rean_startdate',startdate,'.RData'))
                    anom.rean <- drop(anom.rean)
                    anom.rean.chunk[my.time.interv,,] <- anom.rean[,lead,,]
                    
                    # Lead hindcast data of next startdate:
                    load(paste0(work.dir,'/Data_',var.name,'/anom_hindcast_startdate',startdate,'.RData'))  # load hindcast data
                    anom.hindcast <- drop(anom.hindcast)
                    anom.hindcast.chunk[,my.time.interv,,] <- anom.hindcast[,,lead,,]
                    
                    rm(anom.rean,anom.hindcast)
                }   


                anom.hindcast.chunk.perm<-aperm(anom.hindcast.chunk,c(2,1,3,4))  # swap the first two dimensions to put the years as first dimension (needed for convert2prob)
                gc()

                cat('Computing the Reliability Diagram. Please wait... ')
                ens.chunk<-apply(anom.hindcast.chunk.perm, c(3,4), convert2prob, prob<-my.prob) # its dimens. are: [n.startdates*n.yrs.hind*n.forecast.categories, n.lat, n.lon]
                obs.chunk<-apply(anom.rean.chunk,c(2,3), convert2prob, prob<-my.prob) # the first n.members*n.yrs.hind elements belong to the first tercile, and so on.
                ens.chunk.prob<-apply(ens.chunk, c(2,3), function(x) count2prob(matrix(x, n.startdates*n.yrs.hind, n.categ), type=4)) # type=4 is the only method with sum(prob)=1 !!  
                obs.chunk.prob<-apply(obs.chunk, c(2,3), function(x) count2prob(matrix(x, n.startdates*n.yrs.hind, n.categ), type=4)) #no parallelization here: it only takes 10s

                if(is.null(int.lat)) int.lat <- 1:n.lat  # if there is no region selected, select all the world
                if(is.null(int.lon)) int.lon <- 1:n.lon

                int1 <- 1:(n.startdates*n.yrs.hind)  # time interval of the data of the first tercile
                my.RelDiagr[[lead]]<-ReliabilityDiagram(ens.chunk.prob[int1,int.lat,int.lon], obs.chunk.prob[int1,int.lat,int.lon], nboot=0, plot=FALSE, plot.refin=F) #tercile 1

                int2 <- (1+(n.startdates*n.yrs.hind)):(2*n.startdates*n.yrs.hind)  # time interval of the data of the second tercile
                my.RelDiagr[[n.leadtimes+lead]]<-ReliabilityDiagram(ens.chunk.prob[int2,int.lat,int.lon], obs.chunk.prob[int2,int.lat,int.lon], nboot=0, plot=FALSE, plot.refin=F)

                int3 <- (1+(2*n.startdates*n.yrs.hind)):(3*n.startdates*n.yrs.hind)  # time interval of the data of the third tercile
                my.RelDiagr[[2*n.leadtimes+lead]]<-ReliabilityDiagram(ens.chunk.prob[int3,int.lat,int.lon], obs.chunk.prob[int3,int.lat,int.lon], nboot=0, plot=FALSE, plot.refin=F)

                cat('done\n')
                #print(my.RelDiagr) # for debugging
                
                rm(anom.rean.chunk,anom.hindcast.chunk,ens.chunk,obs.chunk,ens.chunk.prob,obs.chunk.prob)
                gc()

            } # next lead
            
        } # close if on RelDiagr

        # we can save the results for all chunks only if mare == FALSE (if it is TRUE, we have the results for 1 chunk only, that have already been saved
        save(my.FairRpss.chunk, my.FairCrpss.chunk, my.EnsCorr.chunk, my.RelDiagr.chunk, my.FairRpssBoot, my.FairCrpssBoot, file=paste0(work.dir,'/',var.name,'/FairRpss_',startdate.name,'.RData'))
    
        #if(any(my.score.name=="FairRpss")) save(my.FairRpss.chunk, file=paste0(work.dir,'/',var.name,'/FairRpss_',startdate.name,'.RData'))
        #if(any(my.score.name=="FairCrpss")) save(my.FairCrpss.chunk, file=paste0(work.dir,'/',var.name,'/FairCrpss_',startdate.name,'.RData'))
        #if(any(my.score.name=="EnsCorr")) save(my.EnsCorr.chunk, file=paste0(work.dir,'/',var.name,'/EnsCorr_',startdate.name,'.RData'))
        #if(any(my.score.name=="RelDiagr")) save(my.RelDiagr.chunk, my.PValue, file=paste0(work.dir,'/',var.name,'/RelDiagr_',startdate.name,'.RData'))
    
        # bootstrapping output:
        #if(any(my.score.name=="FairRpss")) save(my.FairRpssBoot, my.FairRpssConf, file=paste0(work.dir,'/',var.name,'/FairRpssBoot_',startdate.name,'.RData'))
        #if(any(my.score.name=="FairCrpss")) save(my.FairCrpssBoot, my.FairCrpssConf, file=paste0(work.dir,'/',var.name,'/FairCrpssBoot_',startdate.name,'.RData'))
        
} # next m (month)




###########################################################################################
#                  Visualize and save the score maps for one score and period             #
###########################################################################################

# run it only after computing and saving all the skill score data:

my.months=1:12 #9:12  # choose the month(s) to plot and save


my.score.name <- c('FairRpss','FairCrpss','EnsCorr','RelDiagr')  # choose one or more skills scores to compute between EnsCorr, FairRPSS, FairCRPSS and/or RelDiagr

for(month in my.months){
    #month=1  # for the debug                     
    startdate.name.map<-my.month[month]  # i.e:"January"  

    for(my.score.name.map in my.score.name){
	#my.score.name.map='EnsCorr'  # for the debug

	if(my.score.name.map=="EnsCorr") { load(file=paste0(workdir,'/Data_',var.name,'/EnsCorr_',startdate.name.map,'.RData')); my.score<-my.EnsCorr }
	if(my.score.name.map=='FairRpss') { load(file=paste0(workdir,'/Data_',var.name,'/FairRpss_',startdate.name.map,'.RData')); my.score<-my.FairRpss }
	if(my.score.name.map=='FairCrpss') { load(file=paste0(workdir,'/Data_',var.name,'/FairCrpss_',startdate.name.map,'.RData')); my.score<-my.FairCrpss }
	if(my.score.name.map=="RelDiagr") { load(file=paste0(workdir,'/Data_',var.name,'/RelDiagr_',startdate.name.map,'.RData')); my.score<-my.RelDiagr }

	# old green-red palette:
	# brk.rpss<-c(-1,seq(0,1,by=0.05))
	# col.rpss<-c("darkred",colorRampPalette(c("red","white","darkgreen"))(length(brk.rpss)-2))
	# brk.rps<-c(seq(0,1,by=0.1),10)
	# col.rps<-c(colorRampPalette(c("darkgreen","white","red"))(length(brk.rps)-2),"darkred")
	# if(my.score.name.map==my.Rps | my.score==my.Rps.clim) {my.brk<-brk.rps} else {my.brk<-brk.rpss}
	# if(my.score.name.map==my.Rps | my.score==my.Rps.clim) {my.col<-col.rps} else {my.col<-col.rpss}

	brk.rpss<-c(seq(-1,1,by=0.1))
	col.rpss<-colorRampPalette(col)(length(brk.rpss)-1)

	my.brk<-brk.rpss # at present all breaks and colors are the same, so there is no need to differenciate between indexes
	my.col<-col.rpss

	for(lead in 1:n.leadtimes){

	  #my.title<-paste0(my.score.name.map,' of ',cfs.name,' 
	  #10m Wind Speed for ',startdate.name.map,'. Forecast time ',leadtime.week[lead],'.') 

	  if(my.score.name.map!="RelDiagr"){

	      #postscript(file=paste0(workdir,'/Maps_',var.name,'/',my.score.name.map,'_',startdate.name.map,'_Forecast_time_',leadtime.week[lead],'_',var.name,'.ps'),
	      #           paper="special",width=12,height=7,horizontal=F)

	      jpeg(file=paste0(workdir,'/Maps_',var.name,'/',my.score.name.map,'_',startdate.name.map,'_Forecast_time_',leadtime.week[lead],'_',var.name,'.jpg'),width=1000,height=600)  

	      layout(matrix(c(1,2), 1, 2, byrow = TRUE),widths=c(11,1.2))
	      par(oma=c(1,1,4,1))

	      # lat values must be in decreasing order from +90 to -90 degrees and long from 0 to 360 degrees:         
	      PlotEquiMap(my.score[lead,,][n.lat:1,c((n.lonr+1):n.lon,1:n.lonr)], lons,lats ,sizetit=0.8, brks=my.brk, cols=my.col,axelab=F, filled.continents=FALSE, drawleg=F)
	      my.title<-paste0(my.score.name.map,' of ',cfs.name,'\n ', var.name.map,' for ',startdate.name.map,'. Forecast time ',leadtime.week[lead],'.') 
	      title(my.title,line=0.5,outer=T)

	      ColorBar(my.brk, cols=my.col, vert=T, cex=1.4)

	      if(my.score.name.map=="EnsCorr"){  #add a small grey point if the corr.is significant:

		# arrange lat/ lon in my.PValue (a second variable in the EnsCorr file) to start from +90 degr. (lat) and from 0 degr (lon):
		my.PValue.rev <- my.PValue
		my.PValue.rev[lead,,] <- my.PValue[i,n.lat:1,c((n.lonr+1):n.lon,1:n.lonr)] 
		for (x in 1:n.lon) {
		  for (y in 1:n.lat) {
		      if (my.PValue.rev[lead,y, x] == TRUE) {     
			 text(x = lo[x], y = la[y], ".", cex = .2)
		      }
		  }
		}
	      }
	      dev.off()

	  } # close if on !RelDiagr

	  if(my.score.name.map=="RelDiagr") {

	      jpeg(file=paste0(workdir,'/Maps_',var.name,'/RelDiagr_',startdate.name.map,'_Forecast_time_',leadtime.week[lead],'_',var.name,'.jpg'),width=600,height=600)  

	      my.title<-paste0('Reliability Diagram of ',cfs.name,'\n ',var.name.map,' for ',startdate.name.map,'. Forecast time ',leadtime.week[lead],'.')

	      plot(c(0,1),c(0,1),type="l",xlab="Forecast Frequency",ylab="Observed Frequency", col='gray30', main=my.title)
	      lines(my.score[[lead]]$p.avgs[!is.na(my.score[[lead]]$p.avgs)],my.score[[lead]]$cond.prob[!is.na(my.score[[lead]]$cond.prob)],type="o", pch=16, col="blue")
	      #lines(my.score[[n.leadtimes+lead]]$p.avgs[!is.na(my.score[[n.leadtimes+lead]]$p.avgs)],my.score[[n.leadtimes+lead]]$cond.prob[!is.na(my.score[[n.leadtimes+lead]]$cond.prob)],type="o",pch=16,col="darkgreen")
	      lines(my.score[[2*n.leadtimes+lead]]$p.avgs[!is.na(my.score[[2*n.leadtimes+lead]]$p.avgs)], my.score[[2*n.leadtimes+lead]]$cond.prob[!is.na(my.score[[2*n.leadtimes+lead]]$cond.prob)],type="o", pch=16, col="red")
	      legend("bottomright", legend=c('Lower Tercile','Upper Tercile'), lty=c(1,1), lwd=c(2.5,2.5), col=c('blue','red'))
    
	      dev.off()
	  }

	} # next lead

    } # next score

} # next month


###############################################################################################
#                        Composite map of the four skill scores                               #
###############################################################################################

# run it only when you have all the 4 skill scores for each month:

my.months=9:12  # choose the month(s) to plot and save

for(month in my.months){

    startdate.name.map<-my.month[month]  # i.e:"January"  

    #postscript(file=paste0(workdir,'/Maps_',var.name,'/SkillScores_',startdate.name.map,'_',var.name,'.ps'), paper="special",width=12,height=7,horizontal=F)

    jpeg(file=paste0(workdir,'/Maps_',var.name,'/SkillScores_',startdate.name.map,'_',var.name,'.jpg'), width=4000, height=2400,	quality=100)  

    layout(matrix(1:16, 4, 4, byrow=FALSE), widths=c(1.8,1.8,1.8,1.2), heights=c(1,1,1,1)) 
    #layout.show(16)

    for(score in 1:4){
	for(lead in 1:n.leadtimes){

	img<-readJPEG(paste0(workdir,'/Maps_',var.name,'/',my.score.name[score],'_',startdate.name.map,'_Forecast_time_',leadtime.week[lead],'_',var.name,'.jpg'))
	par(mar = rep(0, 4), xaxs='i', yaxs='i' )
	plot.new()
	rasterImage(img, 0, 0, 1, 1, interpolate=FALSE)

	} # next lead

    } # next score

    dev.off()
    
} # next month



# Dani Map script for the small figure:
#/home/Earth/vtorralb/R/scripts/Veronica_2014/TimeSeries/PlotAno_mod_obs.R

                      

###############################################################################################
#  Preformatting: calculate weekly climatologies and anomalies for each forecast startdate    #
###############################################################################################
#
# finish!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#    
# the only difference is that anomalies are computed using climatologies from the hincast data, 
# and not from the forecast data.

# Run it only once at the beginning:

file_path='/home/Earth/ngonzal2/R/ConfFiles/IC3.conf'              # Load() file path
#file_path <- '/home/Earth/ncortesi/Downloads/RESILIENCE/IC3.conf'

my.startdates=1:52  # choose a sequence of forecast startdates to save their anomalies

for(startdate in my.startdates){ # the startdate week to load inside the sdates weekly sequence:
   print(paste0('Computing anomalies for startdate ', which(startdate==my.startdates),'/',length(my.startdates)))

   data.hindcast <- Load(var=var.name, exp = 'EnsEcmwfWeekHind', 
	      	 obs = NULL, sdates=paste0(yr1.hind:yr2.hind,substr(sdates.seq[startdate],5,6),substr(sdates.seq[startdate],7,8)),
		 nleadtime = n.leadtimes, leadtimemin = 1, leadtimemax = n.leadtimes, output = 'lonlat', configfile = file_path, method='distance-weighted' )

   # dim(data.hindcast$mod) # [1,4,20,4,320,640]

   # Mean of the 4 members of the Hindcasts and of the 20 years (for each point and leadtime);
   clim.hindcast<-apply(data.hindcast$mod,c(1,4,5,6),mean)[1,,,] # average for each leadtime and pixel
   clim.hindcast<-InsertDim(InsertDim(InsertDim(clim.hindcast,1,n.yrs.hind),1,n.members),1,1)  # repeat the same values <n.members> and <n.yrs.hind> times to calc. anomalies

   anom.hindcast <- data.hindcast$mod - clim.hindcast
 
   rm(data.hindcast,clim.hindcast)
   gc()

   # average the anomalies over the 4 hindcast members  [1,20,4,320,640]
   anom.hindcast.mean<-apply(anom.hindcast,c(1,3,4,5,6),mean)

   my.grid<-paste0('r',n.lon,'x',n.lat)
   data.rean <- Load(var=var.name, exp = 'ERAintEnsWeek',
                      obs = NULL, sdates=paste0(yr1.hind:yr2.hind,substr(sdates.seq[startdate],5,6),substr(sdates.seq[startdate],7,8)),
		      nleadtime = n.leadtimes, leadtimemin = 1, leadtimemax = n.leadtimes, output = 'lonlat', configfile = file_path, grid=my.grid, method='distance-weighted')
   # dim(data.rean$mod) # [1,1,20,4,320,640]

   # Mean of the 20 years (for each point and leadtime)
   clim.rean<-apply(data.rean$mod,c(1,2,4,5,6),mean)[1,1,,,] # average for each leadtime and pixel
   clim.rean<-InsertDim(InsertDim(InsertDim(clim.rean,1,n.yrs.hind),1,1),1,1) # repeat the same values <n.years> times to be able to calculate the anomalies #[1,1,20,4,320,640]

   anom.rean <- data.rean$mod - clim.rean 
  
   anom.rean<-InsertDim(anom.rean[1,1,,,,],1,1) # [1,20,4,320,640]
   
   #save(anom.hindcast.mean,anom.rean,file=paste0(workdir,'/Data/anom_for_ACC_startdate',startdate,'.RData'))

   save(anom.hindcast,file=paste0(workdir,'/Data/anom_hindcast_startdate',startdate,'.RData'))
   save(anom.hindcast.mean,file=paste0(workdir,'/Data/anom_hindcast_mean_startdate',startdate,'.RData'))
   save(anom.rean,file=paste0(workdir,'/Data/anom_rean_startdate',startdate,'.RData'))
   save(clim.rean,file=paste0(workdir,'/Data/clim_rean_startdate',startdate,'.RData'))

   rm(data.rean,anom.hindcast,anom.hindcast.mean,clim.rean,anom.rean)
   gc()

}







##################################################################################
#                                    Toy Model                                   #
##################################################################################

library(s2dverification)
library(SpecsVerification)
library(easyVerification)

my.prob=c(1/3,2/3)      # quantiles used for FairRPSS and the RelDiagr (usually they are the terciles)

N=80 # number of years
M=14 # number of members

for(M in c(2:10,20,51,100,200)){ 

for(N in c(5,10,15,20,25,30,40,50,100,200,500,1000,2000,5000,10000,20000,50000,100000)){ 

anom.rean<-rnorm(N)
anom.hind<-array(rnorm(N*M),c(N,M))
#quantile(anom.hind,prob=my.prob,type=8)

obs<-convert2prob(anom.rean,prob<-my.prob) 
#mean(obs[,1]) # check if it is equal to 0.333 for K=3

ens<-convert2prob(anom.hind,prob<-my.prob)

# climatological forecast:
anom.hind.clim<-array(sample(anom.hind,N*M),c(N,M))   # extract a random sample with no replacement of the hindcast
ens.clim<-convert2prob(anom.hind.clim,prob<-my.prob)
#mean(ens.clim[,1]) # check if it is equal to M/3 (1.333 for M=4)

# alternative definition of climatological forecast, based on observations instead (as applied by veriApply via convert2prob when option fcst.ref is missing):
#anom.rean.clim<-ClimEns(anom.rean) # create a climatological ensemble from a vector of observations (anom.rean) with the leave-one-out cross validation
anom.rean.clim<-t(array(anom.rean,c(N,N))) # function used by convert2prob when ref is not specified; it is ~ equal to the above function ClimEns, it only has a column more
ens.clim.rean<-convert2prob(anom.rean.clim,prob<-c(1/3,2/3))  # create a new prob.table based on the climatological ensemble of observations

p<-count2prob(ens)  # we should add the option type=4, in not the sum for each year is not 100%!!!
y<-count2prob(obs)  # we should add the option type=4, in not the sum for each year is not 100%!!!
ReliabilityDiagram(p[,1], y[,1], bins=10, nboot=0,  plot=TRUE, cons.prob=c(0.05, 0.85),plot.refin=F)

### computation of verification scores:

Rps<-round(mean(EnsRps(ens,obs)),4)
FairRps<-round(mean(FairRps(ens,obs)),4)
#Rps.easy<-round(mean(veriApply("EnsRps", fcst=anom.hind, obs=anom.rean, prob=my.prob, tdim=1, ensdim=2)),4)  # check once to see if it is the same value of Rps
#FairRps.easy<-round(mean(veriApply("FairRps", fcst=anom.hind, obs=anom.rean, prob=my.prob, tdim=1, ensdim=2)),4)  # check once to see if it is the same value of FairRps

Rps.clim<-round(mean(EnsRps(ens.clim,obs)),4)
FairRps.clim<-round(mean(FairRps(ens.clim,obs)),4)
FairRps.clim.rean<-round(mean(FairRps(ens.clim.rean,obs)),4)

#FairRps.clim.stable<-0.44444444
#Rps.clim.easy<-round(mean(veriApply("EnsRps", fcst=anom.hind.clim, obs=anom.rean, prob=my.prob, tdim=1, ensdim=2)),4) # check once to see if it is the same value of Rps.clim
#FairRps.clim.easy<-round(mean(veriApply("FairRps", fcst=anom.hind.clim, obs=anom.rean, prob=my.prob, tdim=1, ensdim=2)),4)  # check once to see if it is = to FairRps.clim

Rpss<-round(1-(Rps/Rps.clim),4)
#Rpss.specs<-round(EnsRpss(ens=ens, ens.ref=ens.clim, obs=obs)$rpss,4)  # check it once to see if it is the same as Rpss (yes)
Rpss.easy<-veriApply("EnsRpss", fcst=anom.hind, fcst.ref=anom.hind.clim, obs=anom.rean, prob=my.prob, tdim=1, ensdim=2)$rpss # we provide the clim.forecast; = to Rpss.specs
system.time(Rpss.easy.noclim<-round(veriApply("EnsRpss", fcst=anom.hind, obs=anom.rean, prob=my.prob, tdim=1, ensdim=2, parallel=FALSE, ncpus=5)$rpss,4))  # using their climatological forecast
#cat(Rps.clim.noclim<-Rps/(1-Rpss.easy.noclim)) # sale igual a ~0.45 per ogni hindcast; é diverso sia da Rps.clim che da Rps.clim per N-> +oo (0.555)

Rpss.easy<-veriApply("EnsRpss", fcst=anom.hind, fcst.ref=anom.rean.clim, obs=anom.rean, prob=my.prob, tdim=1, ensdim=2)$rpss # we provide the clim.forecast; = to Rpss.specs
Rpss.easy.noclim<-veriApply("EnsRpss", fcst=anom.hind, obs=anom.rean, prob=my.prob, tdim=1, ensdim=2)$rpss # we provide the clim.forecast; = to Rpss.specs

FairRpss<-round(1-(FairRps/FairRps.clim),4)
#FairRpss.specs<-round(FairRpss(ens=ens,ens.ref=ens.clim,obs=obs)$rpss,4)  # check it once to see if it is the same as FairRpss (yes)
#FairRpss.easy<-veriApply("FairRpss", fcst=anom.hind, fcst.ref=anom.hind.clim, obs=anom.rean, prob=c(1/3,2/3), tdim=1, ensdim=2)$rps
#FairRpss.easy.noclim<-round(veriApply("FairRpss", fcst=anom.hind, obs=anom.rean, prob=c(1/3,2/3), tdim=1, ensdim=2)$rpss,4)  # using their climatological forecast
#cat(FairRps.clim.noclim<-Rps/(1-FairRpss.easy.noclim)) # sale igual a ~0.55; é diverso sia da FairRps.clim che da FairRps.clim per N-> +oo (0.444)

cat(paste0('n.memb: ' ,M,'  n.yrs: ',N,'  <FairRps>: ',FairRps,'  <FairRps(clim)>: ' ,FairRps.clim,'  FairRpss: ',FairRpss,'\n'))

#cat(paste0('n.memb: ' ,M,'  n.yrs: ',N,'  <FairRps>: ',FairRps,'  <FairRps(clim)>: ' ,FairRps.clim,'  FairRpss: ',FairRpss,'\n'))

#cat(paste0('n.memb: ' ,M,'  n.yrs: ',N,'  <Rps>:     ',Rps,'  <Rps(clim)>:     ' ,Rps.clim,'  Rpss: ',Rpss,'\n','n.memb: ' ,M,'  n.yrs: ',N,'  <FairRps>: ',FairRps,'  <FairRps(#clim)>: ' ,FairRps.clim,'  FairRpss: ',FairRpss,'\n'))

#cat(paste0('n.memb: ' ,M,'  n.yrs: ',N,'  <Rps>:     ',Rps,'  <Rps(clim)>:     ' ,Rps.clim,'  Rpss: ',Rpss,'  Rpss.easy: ',Rpss.easy.noclim,'\n','n.memb: ' ,M,'  n.yrs: ',N,'  #<FairRps>: ',FairRps,'  <FairRps(clim)>: ' ,FairRps.clim,'  FairRpss: ',FairRpss,'  FairRpss.easy: ',FairRpss.easy.noclim))

}

# Crpss:

#Crps<-round(mean(EnsCrps(ens,obs)),4)
#FairCrps<-round(mean(FairCrps(ens,obs)),4)

#cat(Crps.clim<-round(mean(EnsCrps(ens.clim,obs)),4))
#FairCrps.clim<-round(mean(FairCrps(ens.clim,obs)),4)

#Crpss<-round(1-(Crps/Crps.clim),4)
#Crpss.specs<-round(EnsCrpss(ens=ens, ens.ref=ens.clim, obs=obs)$rpss,4)

#FairCrpss<-round(1-(FairCrps/FairCrps.clim),4)
#FairCrpss.specs<-round(FairCrpss(ens=ens,ens.ref=ens.clim,obs=obs)$rpss,4)

}


##################################################################################
#                                 Other analysis                                 #
##################################################################################

my.startdates=1 #c(1:9) # select the startdates (weeks) you want to merge together

#tm<-toyarray(dims=c(32,64),N=20,nens=4) # in the number of startdates in case of forecasts or the number of years in case of hindcasts
#str(tm) # tm$fcst: [32,64,20,4]  tm$obs: [32,64,20]
#
#f.corr <- veriApply("EnsCorr", fcst=tm$fcst, obs=tm$obs)
#f.me <- veriApply('EnsMe', tm$fcst, tm$obs)
#f.crps <- veriApply("FairCrps", fcst=tm$fcst, obs=tm$obs)
#str(f.crps) # [32,64,20]

#cst <- array(rnorm(prod(4:8)), 4:8)
#obs <- array(rnorm(prod(4:7)), 4:7)
#f.me <- veriApply('EnsMe', fcst=fcst, obs=obs)
#dim(f.me)
#fcst2 <- array(aperm(fcst, c(5,1,4,2,3)), c(8, 4, 7, 5*6))  # very interesting use of aperm not only to swap dimensions, but also to merge two dimensions in the same one!
#obs2 <- array(aperm(obs, c(1,4,2,3)), c(4,7,5*6))
#f.me2 <- veriApply('EnsMe', fcst=fcst2, obs=obs2, tdim=3, ensdim=1)
#dim(f.me2)

# when you have for each leadtime all the 52 weeks of year 2014 stored in a map,
# you can plot the time serie for a particular point and leadtime:
pos.lat<-which(round(la,3)==round(my.lat,3))
pos.lon<-which(round(lo,3)==round(my.lon,3))

x11()
plot(1:week,EnMeanCorr[,leadtime],type="b",
     main=paste0("Weekly time serie of point with lat=",round(my.lat,2),", lon=",round(my.lon,2)),
     xlab="week",ylab="Ensemble Mean Corr.",ylim=c(-1,1))



###   check if 4 time steps are better than one: 

yrs.hind<-yr2.hind-yr1.hind+1
my.point.obs<-array(NA,c(yrs.hind,28))

# load obs.data for the four time steps:
for(year in yr1.hind:yr2.hind){
  data_erai_6h <- Load(var=var.name, exp = 'ERAint6h',
                      obs = NULL, sdates=paste0(year,'01'), leadtimemin = 61, 
                      leadtimemax = 88, output = 'lonlat', configfile = file_path, grid=my.grid,method='distance-weighted')

  my.point.obs[year-yr1.hind+1,]<-data_erai_6h$mod[1,1,1,,65,633]
}

utm0<-c(1,5,9,13,17,21,25)
utm6<-utm0+1
utm12<-utm0+2
utm18<-utm0+3
my.points.obs.weekly.mean.utm0<-rowMeans(my.point.obs[,utm0])
my.points.obs.weekly.mean.utm6<-rowMeans(my.point.obs[,utm6])
my.points.obs.weekly.mean.utm12<-rowMeans(my.point.obs[,utm12])
my.points.obs.weekly.mean.utm18<-rowMeans(my.point.obs[,utm18])
my.points.obs.weekly.mean.all<-rowMeans(my.point.obs)

cor(my.points.exp.weekly.mean.all,my.points.obs.weekly.mean.all,use="complete.obs")

my.points.obs.weekly.mean.utm<-c(my.points.obs.weekly.mean.utm0,my.points.obs.weekly.mean.utm6,my.points.obs.weekly.mean.utm12,my.points.obs.weekly.mean.utm18)
my.points.exp.weekly.mean.utm<-c(my.points.exp.weekly.mean.utm0,my.points.exp.weekly.mean.utm6,my.points.exp.weekly.mean.utm12,my.points.exp.weekly.mean.utm18)
cor(my.points.exp.weekly.mean.utm,my.points.obs.weekly.mean.utm,use="complete.obs")


##### Calculate Reanalysis climatology loading only 1 file each 4 (it's quick but it needs to have all files beforehand):

ss<-c(seq(1,52,by=4),50,51,52) # take only 1 startdate each 4, more the last 3 startdates that must be computed individually because they are not a complete set of 4

for(startdate in ss){ # the startdate day of 2014 to load in the sdates weekly sequence along with the same startdates of the previous 20 years
  data.ERAI <- Load(var=var.name, exp = 'ERAintEnsWeek',
                      obs = NULL, sdates=paste0(yr1.hind:yr2.hind,substr(sdates.seq[startdate],5,6),substr(sdates.seq[startdate],7,8)),
		      nleadtime = 4, leadtimemin = 1, leadtimemax = 4, output = 'lonlat', configfile = file_path, grid=my.grid, method='distance-weighted')

  clim.ERAI[startdate,,,]<-apply(data.ERAI$mod,c(1,2,4,5,6),mean)[1,1,,,] # average for each leadtime and pixel

  # the intermediate startdate between startdate week i and startdate week i+4 are repeated:
  if(startdate <= 49){
  clim.ERAI[startdate+1,1,,]<- clim.ERAI[startdate,2,,] # startdate  leadtime (week)
  clim.ERAI[startdate+1,2,,]<- clim.ERAI[startdate,3,,] #   (week)   1 2 3 4 
  clim.ERAI[startdate+2,1,,]<- clim.ERAI[startdate,3,,] #     1      a b c d <- only startdate 1 and 5 are necessary to calculate all startdates between 1 and 5
  clim.ERAI[startdate+1,3,,]<- clim.ERAI[startdate,4,,] #     2      b c d e
  clim.ERAI[startdate+2,2,,]<- clim.ERAI[startdate,4,,] #     3      c d e f
  clim.ERAI[startdate+3,1,,]<- clim.ERAI[startdate,4,,] #     4      d e f g
  }                                                     #     5      e f g h 

  if(startdate > 1 && startdate <=49){  # fill the previous startdates:                                          
     clim.ERAI[startdate-1,4,,]<- clim.ERAI[startdate,3,,]
     clim.ERAI[startdate-1,3,,]<- clim.ERAI[startdate,2,,]
     clim.ERAI[startdate-2,4,,]<- clim.ERAI[startdate,2,,]
     clim.ERAI[startdate-1,2,,]<- clim.ERAI[startdate,1,,]
     clim.ERAI[startdate-2,3,,]<- clim.ERAI[startdate,1,,]
     clim.ERAI[startdate-3,4,,]<- clim.ERAI[startdate,1,,]
  }

}




 # You can make cor much faster by stripping away all the error checking code and calling the internal c function directly:
	  # r <- apply(m1, 1, function(x) { apply(m2, 1, function(y) { cor(x,y) })}) 
	  # r2 <- apply(m1, 1, function(x) { apply(m2, 1, function(y) {.Internal(cor(x, y, 4L, FALSE)) })})
          C_cor<-get("C_cor", asNamespace("stats"))
          test<-.Call(C_cor, x=rnorm(100,1), y=rnorm(100,1), na.method=2, FALSE)
          my.EnsCorr.chunk[i,j,k]<-.Call(C_cor, x=anom.hindcast.mean.chunk[,i,j,k], y=nom.rean.chunk[,i,j,k], na.method=2, FALSE)



#######################################################################################
#                              Raster Animations                                      #
#######################################################################################

library(png)
library(animation)


clustPNG <- function(pngobj, nclust = 3){

    j <- pngobj
    # If there is an alpha component remove it...
    # might be better to just adjust the mat matrix
    if(dim(j)[3] > 3){
        j <- j[,,1:3]
    }
    k <- apply(j, c(1,2), c)
    mat <- matrix(unlist(k), ncol = 3, byrow = TRUE)
    grd <- expand.grid(1:dim(j)[1], 1:dim(j)[2])
    clust <- kmeans(mat, nclust, iter.max = 20)
    new <- clust$centers[clust$cluster,]

    for(i in 1:3){
        tmp <- matrix(new[,i], nrow = dim(j)[1])
        j[,,i] <- tmp
    }

    plot.new()
    rasterImage(j, 0, 0, 1, 1, interpolate = FALSE)
}

makeAni <- function(file, n = 10){
    j <- readPNG(file)
    for(i in 1:n){
        clustPNG(j, i)
        mtext(paste("Number of clusters:", i))
    }

    j <- readPNG(file)
    plot.new()
    rasterImage(j, 0, 0, 1, 1, interpolate = FALSE)
    mtext("True Picture")
}

file <- "~/Dropbox/Photos/ClassyDogCropPng.png"
n <- 20
saveGIF(makeAni(file, n), movie.name = "tmp.gif", interval = c(rep(1,n), 5))




#######################################################################################
#                                    ProgressBar                                      #
#######################################################################################

for(i in 1:10) {
  Sys.sleep(0.2)
  # Dirk says using cat() like this is naughty ;-)
  #cat(i,"\r")
  # So you can use message() like this, thanks to Sharpie's
  # comment to use appendLF=FALSE.
  message(i,"\r",appendLF=FALSE)
  flush.console()
}


for(i in 1:10) {
  Sys.sleep(0.2)
  # Dirk says using cat() like this is naughty ;-)
  cat(i,"\r")
 
  
}



#######################################################################################
#                            Load large datasets with ff                              #
#######################################################################################

library(ff)
my.scratch<-"/scratch/Earth/ncortesi/myBigData"
anom.hind<-as.ff(anom.hind, vmode="double", file=my.scratch)
ffsave(anom.hind,file=my.scratch)
close(anom.hind);rm(anom.hind)

ffload(file=my.scratch, list=c(anom.hind))
open(anom.hind)
my.SkillScore <- veriApplyBig("FairRpss", fcst=anom.hind, obs=anom.rean, tdim=2, ensdim=1, prob=c(1/3,1/3))
close(anom.hind);rm(anom.hind)



BigDataPath <- "/scratch/Earth/ncortesi/myBigData"
source('/home/Earth/ncortesi/Downloads/RESILIENCE/veriApplyBig.R')

anom.hind.dim<-c(5,3,1,256,512)
anom.hind<-array(rnorm(prod(anom.hind.dim)),anom.hind.dim)  # doesn't fit in memory
save.big(array=anom.hind, path=BigDataPath)

veriApplyBig <- function(, obsmy.scratch){
    ffload(file=my.scratch)
    open(anom.hind)
    my.SkillScore <- veriApplyBig("FairRpss", fcst=anom.hind, obs=anom.rean, tdim=2, ensdim=1, prob=c(1/3,1/3))
    close(anom.hind)
}


anom.hind<-as.ff(anom.hind, vmode="double", file=my.scratch)
ffsave(anom.hind, file=my.scratch)

my.scratch<-"/scratch/Earth/ncortesi/myBigData"

anom.hind<-as.ff(anom.hind, vmode="double", file=my.scratch)
ffsave(anom.hind, file=my.scratch)
close(anom.hind); rm(anom.hind)

ffload(file=my.scratch, list=c(anom.hind))
open(anom.hind)
my.SkillScore <- veriApplyBig("FairRpss", fcst=anom.hind, obs=anom.rean, tdim=2, ensdim=1, prob=c(1/3,1/3))
close(anom.hind); rm(anom.hind)

anom.hind.dim<-c(51,30,1,256,51)
anom.hind<-ff(rnorm(prod(anom.hind.dim)), dim=anom.hind.dim, vmode="double", filename="/scratch/Earth/ncortesi/anomhind")
str(anom.hind)
dim(anom.hind)

anom.hind.dim<-c(51,30,1,256,51)
anom.hind<-array(rnorm(prod(anom.hind.dim)),anom.hind.dim)  # doesn't fit in memory
anom.hind<-as.ff(anom.hind, vmode="double", file="/scratch/Earth/ncortesi/myBigData")
str(anom.hind)
dim(anom.hind)

ffsave(anom.hind,file="/scratch/Earth/ncortesi/anomhind")
#ffinfo(file="/scratch/Earth/ncortesi/anomhind")
close(anom.hind)
#delete(anom.hind)
#rm(anom.hind)

ffload(file="/scratch/Earth/ncortesi/anomhind")

anom.rean<-array(rnorm(prod(anom.hind.dim[-1])), anom.hind.dim[-1])  # doesn't fit in memory

open(anom.hind)
my.SkillScore <- veriApplyBig("FairRpss", fcst=anom.hind, obs=anom.rean, tdim=2, ensdim=1, prob=c(1/3,1/3))
fcst<-anom.hind
obs<-anom.rean

close(anom.hind)
rm(anom.hind)

#ffdrop(file="/scratch/Earth/ncortesi/anomhind")

a<-ffapply(X=anom.hind.big, MARGIN=c(1,3,4,5), AFUN=mean, RETURN=TRUE)
a<-ffapply(X=anom.hind.big, MARGIN=c(1,3,4,5), AFUN=function(x) sample(x, replace=TRUE), CFUN="list", RETURN=TRUE)


pred_Cal_cross_ff <- as.ff(pred_Cal_cross, vmode='double', file=fileoutput_cross)
ffsave(pred_Cal_cross_ff, file=paste0(fileoutput_cross)) 
delete(pred_Cal_cross_ff)

assign("pred_Cal_cross_ff",  as.ff(pred_Cal_cross, vmode='double', file=fileoutput_cross))
ffsave(pred_Cal_cross_ff, file=paste0(fileoutput_cross)) 
delete(pred_Cal_cross_ff)
rm(pred_Cal_cross_ff)

assign("pred_Cal_cross_ff",  as.ff(pred_Cal_cross, vmode='double', file=fileoutput_cross))
a<-get("pred_Cal_cross_ff")           # get() works well in this case but not in the row below!  (you must use do.call, see WT_drivers_v2.R)
ffsave(get("pred_Cal_cross_ff"), file=paste0(fileoutput_cross)) 
delete(pred_Cal_cross_ff)
rm(pred_Cal_cross_ff)


    
#########################################################################################
#   Calculate and save the FairRpss and/or the FairCrpss for a chosen period using ff   #
#########################################################################################

bigdata=FALSE           # if TRUE, compute the Rpss and the Crpss using the package ff (storing arrays in the disk instead than in the RAM) to remove the memory limit

for(month in veri.month){
    month=1 # for debug
    my.startdates <- which(as.integer(substr(sdates.seq,5,6)) == month) #startdates.monthly[[month]] #c(1:5)    # select the startdates (weeks) you want to compute
    startdate.name=my.month[month]                        # name given to the period of the selected startdates, i.e:"January" 
    n.yrs <- length(my.startdates)*n.yrs.hind             # number of total years for the selected month
    print(paste0("Computing Skill Scores for ",startdate.name))
    
    if(!boot) anom.rean.big<-array(NA, dim=c(n.yrs, n.leadtimes, n.lat, n.lon)) # reanalysis data is not converted to ff because it is a small array
    
    if(boot) mod<-1 else mod=0  # in case of the boostrap, anom.hind.big includes also the reanalysis data as additional last member
    
    if(bigdata)  anom.hind.big <- ff(NA, dim=c(n.members + mod, n.yrs, n.leadtimes, n.lat, n.lon), vmode="double", filename=paste0(workdir,"/anomhindbig.ff"))
    if(!bigdata) anom.hind.big <- array(NA, dim=c(n.members + mod, n.yrs, n.leadtimes, n.lat, n.lon))
    
    for(startdate in my.startdates){
      
       pos.startdate<-which(startdate==my.startdates) # count of the number of stardates already loaded +1
       my.time.interv<-(1+(pos.startdate-1)*n.yrs.hind):(pos.startdate*n.yrs.hind)  # time interval where to load data

       load(paste0(workdir,'/Data_',var.name,'/anom_rean_startdate',startdate,'.RData'))
       if(!boot) {
           anom.rean<-drop(anom.rean)
           anom.rean.big[my.time.interv,,,] <- anom.rean
       }
      
       if(any(my.score.name=="FairRpss") || any(my.score.name=="FairCrpss")){
	     load(paste0(workdir,'/Data_',var.name,'/anom_hindcast_startdate',startdate,'.RData'))  # load hindcast data
	     anom.hindcast<-drop(anom.hindcast)
       
             if(!boot) anom.hind.big[,my.time.interv,,,] <- anom.hindcast
             if(boot) anom.hind.big[,my.time.interv,,,] <- abind(anom.hindcast, anom.rean, along=1)
                 
             rm(anom.hindcast, anom.rean)
       }
       gc()
    }

    if(any(my.score.name=="FairRpss" || my.score.name=="FairCrpss")){
        if(!boot) {
           if(any(my.score.name=="FairRpss")) my.FairRpss <- veriApplyBig("FairRpss", fcst=anom.hind.big, obs=anom.rean.big,
                                                                            prob=my.prob, tdim=2, ensdim=1, parallel=TRUE, ncpus=n.cpus)
           if(any(my.score.name=="FairCrpss")) my.FairCrpss <- veriApplyBig("FairCrpss", fcst=anom.hind.big, obs=anom.rean.big, tdim=2, ensdim=1, parallel=TRUE, ncpus=n.cpus)
           
        } else {
           # bootstrapping: 
           my.FairRpssBoot<-my.FairCrpssBoot<-array(NA, c(n.boot, n.leadtimes, n.lat, n.lon))
           if(!bigdata) save(anom.hind.big, file=paste0(workdir,"/anom_hind_big.RData")) # save the anomalies to free memory
               
           for(b in 1:n.boot){
               print(paste0("resampling n. ",b,"/",n.boot))
               yrs.sampled <- sample(1:n.yrs, replace=TRUE)
               
               if(bigdata)  anom.hind.big.sampled <- ff(NA, dim=c(n.members+1, n.yrs, n.leadtimes, n.lat, n.lon), vmode="double", filename=paste0(workdir,"/anomhindbigsampled"))
               if(!bigdata) anom.hind.big.sampled <- array(NA, dim=c(n.members+1, n.yrs, n.leadtimes, n.lat, n.lon))
               
               if(!bigdata && b>1) load(paste0(workdir,"/anom_hind_big.RData")) # need to load the hindcasts if b>1
                                        
               for(y in 1:n.yrs) anom.hind.big.sampled[,y,,,] <-  anom.hind.big[,yrs.sampled[y],,,] # with ff takes 53s x 100 ~1h!!! (without, only 4s x 100 ~6m)

               if(!bigdata) rm(anom.hind.big); gc()  # free memory for the following commands
               
               # faster way that will be able to replace the above one when ffapply output parameters wll be improved:
               #a<-ffapply(X=anom.hind.big, MARGIN=c(3,4,5), AFUN=function(x) my.sample(x,n.members+1, replace=TRUE), CFUN="list", RETURN=TRUE, FF_RETURN=TRUE) # takes 3m only
               #aa<-abind(a[1:10], along=4) # it doesn't fit into memory!
 
               if(any(my.score.name=="FairRpss")) my.FairRpssBoot[b,,,]  <- veriApplyBig("FairRpss", fcst=anom.hind.big.sampled[1:n.members,,,,],
                                                                                     obs=anom.hind.big.sampled[n.members+1,,,,],
                                                                                     prob=my.prob, tdim=2, ensdim=1, parallel=TRUE, ncpus=n.cpus) #$rpss
               
               if(any(my.score.name=="FairCrpss")) my.FairCrpssBoot[b,,,] <- veriApplyBig("FairCrpss", fcst=anom.hind.big.sampled[1:n.members,,,,],
                                                                                       obs=anom.hind.big.sampled[n.members+1,,,,],
                                                                                       tdim=2, ensdim=1, parallel=TRUE, ncpus=n.cpus) #$crpss
               
               if(bigdata) delete(anom.hind.big.sampled)
               rm(anom.hind.big.sampled) # delete the sampled hindcast

           }

           # calculate 5 and 95 percentiles of skill scores:
           if(any(my.score.name=="FairRpss"))  my.FairRpssConf <- apply(my.FairRpssBoot, c(2,3,4), function(x) quantile(x, probs=c(0.05,0.95), type=8))
           if(any(my.score.name=="FairCrpss")) my.FairCrpssConf <- quantile(my.FairCrpssBoot, probs=c(0.05,0.95), type=8)
           
        } # close if on boot
       
    }

    if(bigdata) delete(anom.hind.big) # delete the file with the hindcasts
    rm(anom.hind.big) 
    rm(anom.rean.big)
    gc()

    if(!boot){
        if(any(my.score.name=="FairRpss")) save(my.FairRpss, file=paste0(workdir,'/Data_',var.name,'/FairRpss_',startdate.name,'.RData'))
        if(any(my.score.name=="FairCrpss")) save(my.FairCrpss, file=paste0(workdir,'/Data_',var.name,'/FairCrpss_',startdate.name,'.RData'))
    } else {
        if(any(my.score.name=="FairRpss")) save(my.FairRpssBoot, my.FairRpssConf, file=paste0(workdir,'/Data_',var.name,'/FairRpssBoot_',startdate.name,'.RData'))
        if(any(my.score.name=="FairCrpss")) save(my.FairCrpssBoot, my.FairCrpssConf, file=paste0(workdir,'/Data_',var.name,'/FairCrpssBoot_',startdate.name,'.RData'))
    }


    if(is.object(my.FairRpss)) rm(my.FairRpss); if(is.object(my.FairCrpss)) rm(my.FairCrpss)
    if(is.object(my.FairRpssBoot)) rm(my.FairRpssBoot); if(is.object(my.FairCrpssBoot)) rm(my.FairCrpssBoot)
    if(is.object(my.FairRpssConf)) rm(my.FairRpssConf); if(is.object(my.FairCrpssConf)) rm(my.FairCrpssConf)

} # next m (month)


#########################################################################################
#     Calculate and save the FairRpss for a chosen period using ff and Load()           #
#########################################################################################

    anom.rean.big<-array(NA, dim=c(length(my.startdates)*n.yrs.hind, n.leadtimes, n.lat, n.lon)) 
    anom.hind.big <- ff(NA, dim=c(n.members,length(my.startdates)*n.yrs.hind, n.leadtimes, n.lat, n.lon), vmode="double", filename="/scratch/Earth/anomhindbig")
   
    for(startdate in my.startdates){
       pos.startdate<-which(startdate==my.startdates) # count of the number of stardates already loaded+1
       my.time.interv<-(1+(pos.startdate-1)*n.yrs.hind):(pos.startdate*n.yrs.hind)  # time interval where to load data

       #load(paste0(workdir,'/Data_',var.name,'/anom_rean_startdate',startdate,'.RData'))
       #anom.rean<-drop(anom.rean)
       #anom.rean.big[my.time.interv,,,] <- anom.rean

       my.date <- paste0(yr1.hind:yr2.hind,substr(sdates.seq[startdate],5,6),substr(sdates.seq[startdate],7,8))
       anom.rean <- Load(var=var.name, exp = 'EnsEcmwfWeekHind', 
	      	 obs = NULL, sdates=my.date, nleadtime = n.leadtimes, leadtimemin = 1, leadtimemax = n.leadtimes,
		 output = 'lonlat', configfile = file_path, method='distance-weighted' )

       #load(paste0(workdir,'/Data_',var.name,'/anom_hindcast_startdate',startdate,'.RData'))  # load hindcast data
       #anom.hindcast<-drop(anom.hindcast)

       anom.hind.big[,my.time.interv,,,] <- anom.hindcast
       rm(anom.hindcast)
    }

    my.FairRpss <- veriApplyBig("FairRpss", fcst=anom.hind.big, obs=anom.rean.big, prob=my.prob, tdim=2, ensdim=1, parallel=TRUE, ncpus=n.cpus)
   
    save(my.FairRpss, file=paste0(workdir,'/Data_',var.name,'/FairRpss_',startdate.name,'.RData'))


#########################################################################################
#                      Alternative way to speed up Load()                               #
#########################################################################################
    
ERAint <- list(path = '/esnas/reconstructions/ecmwf/eraint/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$YEAR$$MONTH$.nc')
       var.rean=ERAint   # daily reanalysis dataset used for the var data; it can have a different resolution of the MSLP dataset
       var.name='sfcWind' #'tas' #'prlr'         # any daily variable we want to find if it is WTs-driven
       var <- Load(var = var.name, exp = NULL, obs = list(var.rean), paste0(y,'0101'), storefreq = 'daily', leadtimemax = 1, output = 'lonlat')

       # Forecast system list: 
       S4 <- list(path = ...)
       CFSv2 <- list(path = ...)

       # Reanalysis list:
       ERAint <- list(path = '/esnas/reconstructions/ecmwf/eraint/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$YEAR$$MONTH$.nc')
       JRA55 <- list(path = ...)



    
       # Select one forecast system and one reanalysis (put NULL if you don't need to load any experiment/observation):
       exp.source <- NULL    
       obs.source <- list(path = '/esnas/reconstructions/ecmwf/eraint/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$YEAR$$MONTH$.nc')  

       # Select one variable and its frequency:
       var.name <- 'sfcWind'
       store.freq <- 'daily'

       # Extract only the directory path of the forecast and reanalysis:
       obs.source2 <- gsub("\\$STORE_FREQ\\$", store.freq, obs.source$path)
       obs.source3 <- gsub("\\$VAR_NAME\\$", var.name, obs.source2)
       obs.source4 <- strsplit(obs.source3,'/')
       obs.source5 <- paste0(obs.source4[[1]][-length(obs.source4[[1]])], collapse="/")
    
       obs.source6 <- list.files(path = obs.source5)
        
       system(paste0("ncks -O -h -d longitude,5,6 ", '/esnas/reconstructions/ecmwf/eraint/',STORE_FREQ,'_mean/',VAR_NAME,'_f6h'))
       system("ncks -O -h -d longitude,5,6 sfcWind_201405.nc ~/test.nc")
        
       y <- 1990
       var <- Load(var = VAR_NAME, exp = list(exp.source), obs = list(obs.source), sdates = paste0(y,'0101'), storefreq = store.freq, leadtimemax = 1, output = 'lonlat')

    
} # close if on mare


