#########################################################################################
#                               Sub-seasonal Skill Scores                               #
#########################################################################################
# you can run it in a sequential way from the terminal of your workstation with the syntax:
#
# Rscript SkillScores_v7.R
#
# or in parallel on MareNostrum:
#
# bsub < SkillScores_v7.job
#


# i.e: to split the data in 8 chunks and run only the chunk number 3, write:
#
# Rscript SkillScores_v4.R 8 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 if the computational speed is not a problem.
#



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

#load libraries and functions:
library(s2dverification)
library(SpecsVerification)
#library(easyVerification)
#library(jpeg)
#library(abind)

# Load function split.array:
source('/gpfs/projects/bsc32/bsc32842/scripts/Rfunctions.R')

##########################################################################################
#                                     User's settings                                    #
##########################################################################################

# working dir where to put the output maps and files in the /Data and /Maps subdirs:
workdir <- "/gpfs/projects/bsc32/bsc32842/RESILIENCE"

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'

yr1 <- 2014                    # starting year of the weekly sequence of the forecasts
mes <- 1                       # starting forecast month (usually january)
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') #c('EnsCorr','FairRpss','FairCrpss','RelDiagr')  # choose one or more skills scores to compute between EnsCorr, FairRPSS, FairCRPSS and/or RelDiagr
              
veri.month <- 2 #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)

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 #############################################################################

args <- commandArgs(TRUE) # put into variable args the list with all the optional arguments with whom the script was run from the terminal
chunk <- as.integer(args[1])  # number of the chunk to run in this script

#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 <- as.character(read.csv("/gpfs/projects/bsc32//bsc32842/scripts/rgbhex.csv",header=F)[,1])

sdates.seq <- weekly.seq(yr1,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)

# Monthly Startdates for 2014 reforecasts: (in future you can modify it to work for a generic year)
startdates.monthly<-list()
startdates.monthly[[1]]<-1:5
startdates.monthly[[2]]<-6:9
startdates.monthly[[3]]<-10:13
startdates.monthly[[4]]<-14:17
startdates.monthly[[5]]<-18:22
startdates.monthly[[6]]<-23:26
startdates.monthly[[7]]<-27:31
startdates.monthly[[8]]<-32:35
startdates.monthly[[9]]<-36:39
startdates.monthly[[10]]<-40:44
startdates.monthly[[11]]<-45:48
startdates.monthly[[12]]<-49:52

## extract geographic coordinates; do only ONCE for each prediction 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])

n.lat == 1

#ERAint <- list(path = '/esnas/reconstructions/ecmwf/eraint/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$YEAR$$MONTH$.nc')
#var <- Load(var = 'z500', exp = NULL, obs = list(rean), paste0(2001,'0101'), storefreq = 'daily', leadtimemax = 365, output = 'lonlat')
#var <- Load(var = 'z500', exp = list(exp), obs = NULL, paste0(2000,'0101'), storefreq = 'daily', leadtimemax = 1, output = 'lonlat')

for(month in veri.month){
    #month=1 # for the debug
    my.startdates <- 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.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.hindcast.chunk <- anom.hindcast.chunk.sampled <- array(NA, c(n.members, n.startdates*n.yrs.hind, n.leadtimes, n.lat, n.lon))
    anom.rean.chunk <- anom.hindcast.mean.chunk <- anom.rean.chunk.sampled <- array(NA, c(n.startdates*n.yrs.hind, n.leadtimes, n.lat, n.lon))

    time <- system.time({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 reanalysis data:
	  load(paste0(workdir,'/Data_',var.name,'/anom_rean_startdate',startdate,'.RData'))
#	  anom.rean <- drop(anom.rean)
#	  anom.rean.chunk[my.time.interv,,,] <- anom.rean[,,1,]
#          
          # Load hindcast data:
#	  if(any(my.score.name=="FairRpss") || any(my.score.name=="FairCrpss" || any(my.score.name=="RelDiagr"))){
#	     load(paste0(workdir,'/Data_',var.name,'/anom_hindcast_startdate',startdate,'.RData')) 
#	     anom.hindcast <- drop(anom.hindcast)
#	     anom.hindcast.chunk[,my.time.interv,,,] <- anom.hindcast[,,,1,]  
#	     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[,,1,]
	  #   rm(anom.hindcast.mean)
          #   gc()
	  #}

     } # close for on startdate
     })

    
    #save(time, file=paste0(workdir,'/test_',startdate.name,'_chunk_',chunk,'_time_',time,'.RData'))
    save(time, file=paste0(workdir,'/test_',startdate.name,'_chunk_',chunk,'_time_',time[3],'.RData'))

} # close for on month

