# This script compute the FRPSS of the blended predictions against
# the observation + climatology ensemble forecast as the baseline.
# start months from May to October
# GDD

rm(list = ls())
index <- 'GDD'

library(multiApply)
library(s2dv)
library(CSIndicators)

wd <- paste0('/esarchive/scratch/cchou/MEDGOLD/grape/output/', index, '/')
setwd(wd)

# variables
ncores <- 3
startMonths <- c('05', '06', '07', '08', '09', '10')
ftimeini <- c(30, 30 + 31, 30 + 31 + 30, 30 + 31 + 30 + 31, 30 + 31 + 30 + 31 + 31,
30 + 31 + 30 + 31 + 31 + 30)
ftimeend <- c(31 + 30 + 31 + 31 + 30 + 31, 30 + 31 + 31 + 30 + 31, 31 + 31 + 30 + 31,
31 + 30 + 31, 30 + 31, 31)

# print(paste0('Computing ', index, ': SM', startMonth, ' ...'))

# load observed ECVs

var1 <- 'tasmax'; var2 <- 'tasmin'; threshold <- 10
pname <- '/esarchive/scratch/cchou/MEDGOLD/grape/output/ECVs/data/'
load(paste0(pname, 'daily_', var1, '_ERA5grid_SM04_Y93-16.RData'))
load(paste0(pname, 'daily_', var2, '_ERA5grid_SM04_Y93-16.RData'))
rm(S5tasmax, S5tasmin)
obs <- (ERA5tasmax + ERA5tasmin)/2

# generate random indices

ri <- array(NA, dim = c('member' = 25, 'sdate' = 24))
for (i in 1:dim(ri)['sdate']) {
 print(i)
 ri[, i] <- sample(c(1:24)[-i], dim(ri)['member'], replace = TRUE)
}

# compute index for original observations

ERA5 <- AccumulationExceedingThreshold(obs, threshold = threshold, op = '>',
                                       diff = TRUE, time_dim = 'ftime',
                                       ncores = ncores)

# generate the obs + clim for each start month from 05-10

obs.clim <- MeanDims(obs, 'sdate')
obs.ens <- array(NA, dim = c('member' = 25, dim(obs)))

for (sdate in 1:dim(obs.ens)['sdate']) {
 for (member in 1:dim(obs.ens)['member']) {
  print(paste0('Generating obs ensemble: year ', sdate, ' member ', member))
  obs.ens[member, sdate, , , ] <- obs[ri[member, sdate], , , ]
}
}
obs.ens.clim <- MeanDims(obs.ens, 'sdate')

# replace obs with climatology

obs.combineObsClim <- NA * obs.ens

sm <- 1 # sm 1:6: start months from May - October

obs.combineObsClim[, , 1:ftimeini[sm], , ] <- InsertDim(obs[, 1:ftimeini[sm], , ],
 1, dim(obs.combineObsClim)['member']) # replace April with the 'real' observations

obs.combineObsClim[, , (ftimeini[sm] + 1):dim(obs.combineObsClim)['ftime'], , ] <- InsertDim(
 obs.ens.clim[,(ftimeini[sm] + 1):dim(obs.combineObsClim)['ftime'], , ], 2, dim(obs.combineObsClim)['sdate'])

# compute index for obs.clim
Ind.obs.clim <- AccumulationExceedingThreshold(obs.combineObsClim, threshold = threshold, op = '>',
 diff = TRUE, time_dim = 'ftime', ncores = ncores)

library(rgdal)
library(sp)
library(easyNCDF)
library(CSTools)
library(raster)
library(fields)
library(ncdf4)
library(easyVerification)
source('/esarchive/scratch/cchou/MEDGOLD/others/functions/MG_Ind2ThrPro.R')
source('/esarchive/scratch/cchou/MEDGOLD/others/functions/SBC_Verification_Index.R')

res <- Analysis(exp = Ind.obs.clim, obs = ERA5, lon = lon_ERA5, lat = lat_ERA5,
                BCMethod = c('SBC', 'CA'), na.rm = TRUE, ncores = 3,
                parallel = TRUE, maxncpus = 3)

# save to ncfiles
ERA5GDD <- ERA5
names(dim(ERA5GDD)) <- c('sdate','lat_ERA5','lon_ERA5')
metadata1 <- list(ERA5GDD = list(units = '°C', long_name = 'ERA5 Growing Degree Days'))
attr(ERA5GDD, 'variables') <- metadata1

ERA5_CombineClim_GDD <- Ind.obs.clim
names(dim(ERA5_CombineClim_GDD)) <- c('member','sdate','lat_ERA5','lon_ERA5')
metadata2 <- list(ERA5_CombineClim_GDD = list(units = '°C', long_name = 'ERA5 GDD (combine observed and climatology)'))
attr(ERA5_CombineClim_GDD, 'variables') <- metadata2

Bias <- res$bias
names(dim(Bias)) <- c('sdate','lat_ERA5','lon_ERA5')
metadata3 <- list(Bias = list(units = '°C',
                              long_name = 'Ensemble-mean bias (ERA5_CombineClim_GDD vs ERA5GDD)'))
attr(Bias, 'variables') <- metadata3

Thr <- res$thr
names(dim(Thr)) <- c('threshold','sdate','lat_ERA5','lon_ERA5')
metadata4 <- list(Thr = list(units = '°C', long_name = 'ERA5_CombineClim_GDD tercile threshold'))
attr(Thr, 'variables') <- metadata4

Pro <- res$pro
names(dim(Pro)) <- c('bin','sdate','lat_ERA5','lon_ERA5')
metadata5 <- list(Pro = list(units = '(probability)', long_name = 'ERA5_CombineClim_GDD tercile probability'))
attr(Pro, 'variables') <- metadata5

FRPSS <- res$frpss
names(dim(FRPSS)) <- c('sdate','lat_ERA5','lon_ERA5')
metadata6 <- list(FRPSS = list(units = '(skill score)',
                               long_name = 'ERA5_CombineClim_GDD Fair Ranked Probability Skill Score'))
attr(FRPSS, 'variables') <- metadata6

Corr <- res$corr
names(dim(Corr)) <- c('lat_ERA5','lon_ERA5')
metadata7 <- list(Corr = list(units = '(correlation coefficient)',
                              long_name = 'ERA5_CombineClim_GDD Correlation Coefficient'))
attr(Corr, 'variables') <- metadata7

ERA5_CombineClim_GDD_SBC <- res$exp_sbc
names(dim(ERA5_CombineClim_GDD_SBC)) <- c('member','sdate','lat_ERA5','lon_ERA5')
metadata8 <- list(ERA5_CombineClim_GDD_SBC = list(units = '°C',
                                    long_name = 'ERA5_CombineClim_GDD, simple bias corrected'))
attr(ERA5_CombineClim_GDD_SBC, 'variables') <- metadata8

Bias_SBC <- res$bias_sbc
names(dim(Bias_SBC)) <- c('sdate','lat_ERA5','lon_ERA5')
metadata9 <- list(Bias_SBC = list(units = '°C',
                                  long_name = 'Ensemble-mean bias (ERA5_CombineClim_GDD vs ERA5GDD)'))
attr(Bias_SBC, 'variables') <- metadata9

Thr_SBC <- res$exp_sbc_thr
names(dim(Thr_SBC)) <- c('threshold','sdate','lat_ERA5','lon_ERA5')
metadata10 <- list(Thr_SBC = list(units = '°C', long_name = 'ERA5_CombineClim_GDD_SBC  tercile threshold'))
attr(Thr_SBC, 'variables') <- metadata10

Pro_SBC <- res$exp_sbc_pro
names(dim(Pro_SBC)) <- c('bin','sdate','lat_ERA5','lon_ERA5')
metadata11 <- list(Pro_SBC = list(units = '(probability)',
                                  long_name = 'ERA5_CombineClim_GDD_SBC tercile probability'))
attr(Pro_SBC, 'variables') <- metadata11

FRPSS_SBC <- res$frpss_sbc
names(dim(FRPSS_SBC)) <- c('sdate','lat_ERA5','lon_ERA5')
metadata12 <- list(FRPSS_SBC = list(units = '(skill score)',
                                    long_name = 'ERA5_CombineClim_GDD_SBC Fair Ranked Probability Skill Score'))
attr(FRPSS_SBC, 'variables') <- metadata12

Corr_SBC <- res$corr_sbc
names(dim(Corr_SBC)) <- c('lat_ERA5','lon_ERA5')
metadat13 <- list(Corr_SBC = list(units = '(correlation coefficient)',
                                  long_name = 'ERA5_CombineClim_GDD_SBC Correlation Coefficient'))
attr(Corr_SBC, 'variables') <- metadat13

ERA5_CombineClim_GDD_CA <- res$exp_ca
names(dim(ERA5_CombineClim_GDD_CA)) <- c('member','sdate','lat_ERA5','lon_ERA5')
metadata14 <- list(ERA5_CombineClim_GDD_CA = list(units = '°C',
                                   long_name = 'ERA5_CombineClim_GDD, calibrated'))
attr(ERA5_CombineClim_GDD_CA, 'variables') <- metadata14

Bias_CA <- res$bias_ca
names(dim(Bias_CA)) <- c('sdate','lat_ERA5','lon_ERA5')
metadata15 <- list(Bias_CA = list(units = '°C',
                                  long_name = 'Ensemble-mean bias (ERA5_CombineClim_GDD_CA vs ERA5GDD)'))
attr(Bias_CA, 'variables') <- metadata15

Thr_CA <- res$exp_ca_thr
names(dim(Thr_CA)) <- c('threshold','sdate','lat_ERA5','lon_ERA5')
metadata16 <- list(Thr_CA = list(units = '°C', long_name = 'ERA5_CombineClim_GDD_CA tercile threshold'))
attr(Thr_CA, 'variables') <- metadata16

Pro_CA <- res$exp_ca_pro
names(dim(Pro_CA)) <- c('bin','sdate','lat_ERA5','lon_ERA5')
metadata17 <- list(Pro_CA = list(units = '(probability)',
                                 long_name = 'ERA5_CombineClim_GDD_CA tercile probability'))
attr(Pro_CA, 'variables') <- metadata17

FRPSS_CA <- res$frpss_ca
names(dim(FRPSS_CA)) <- c('sdate','lat_ERA5','lon_ERA5')
metadata18 <- list(FRPSS_CA = list(units = '(skill score)',
                                   long_name = 'ERA5_CombineClim_GDD_CA Fair Ranked Probability Skill Score'))
attr(FRPSS_CA, 'variables') <- metadata18

Corr_CA <- res$corr_ca
names(dim(Corr_CA)) <- c('lat_ERA5','lon_ERA5')
metadata19 <- list(Corr_CA = list(units = '(correlation coefficient)',
                                 long_name = 'ERA5_CombineClim_GDD_CA Correlation Coefficient'))
attr(Corr_CA, 'variables') <- metadata19


attr(lon_ERA5, 'variables') <- list(lon_ERA5 = list(units = 'degree_east',
                                                    long_name = 'ERA5-grid longitude'))
names(dim(lon_ERA5)) <- 'lon_ERA5'
attr(lat_ERA5, 'variables') <- list(lat_ERA5 = list(units = 'degrees_north',
                                                    long_name = 'ERA5-grid latitude'))
names(dim(lat_ERA5)) <- 'lat_ERA5'
sdate <- 1993:2016
dim(sdate) <- length(sdate)
names(dim(sdate)) <- 'sdate'
attr(sdate, 'variables') <- list(sdate = list(units = 'year'))
member <- 1: dim(ERA5_CombineClim_GDD)['member']
dim(member) <- length(member)
names(dim(member)) <- 'member'
attr(member, 'variables') <- list(member = list(units = 'number',
                                                long_name = 'number of member of prediction'))
threshold <- 1:2/3
dim(threshold) <- length(threshold)
names(dim(threshold)) <- 'threshold'
attr(threshold, 'variables') <- list(threshold = list(long_name = 'tercile threshold: 1/3 and 2/3'))
bin <- c(1:2/3,1); dim(bin) <- length(bin); names(dim(bin)) <- 'bin'
attr(bin, 'variables') <- list(bin = list(long_name = '(tercile)'))

attr(ri, 'variables') <- list(ri = list(long_name = 'random index for generating the ensemble observations'))
 
ArrayToNc(list(ERA5GDD, ERA5_CombineClim_GDD, Bias, Thr, Pro, FRPSS, Corr,
 ERA5_CombineClim_GDD_SBC, Bias_SBC, Thr_SBC, Pro_SBC, FRPSS_SBC, Corr_SBC,
 ERA5_CombineClim_GDD_CA, Bias_CA, Thr_CA, Pro_CA, FRPSS_CA, Corr_CA,
               lon_ERA5, lat_ERA5, sdate, member, threshold, bin, ri),
          paste0('data/', index, '_ERA5CombineClim_BC_IP_Y93-16_SM', startMonths[sm], '.nc'))




#-------------------------------------
# Load ERA5 reanalysis index, CombineClim index and SEAS5 index
# and compute the FRPSS

rm(list = ls())
index <- 'GDD'

library(multiApply)
library(s2dv)
library(CSIndicators)
library(ncdf4)

wd <- paste0('/esarchive/scratch/cchou/MEDGOLD/grape/output/', index, '/')
setwd(wd)
startMonths <- c('05', '06', '07', '08', '09', '10')
yearini <- 1993; yearend <- 2016

for (sm in 2:6) {

nc <- nc_open(paste0('data/', index, '_S5_ERA5_BC_IP_Y', substr(as.character(yearini), 3, 4), '-',
substr(as.character(yearend), 3, 4), '_SM', startMonths[sm], '.nc'))

lon_ERA5 <- ncvar_get(nc, 'lon_ERA5'); lat_ERA5 <- ncvar_get(nc, 'lat_ERA5')
ERA5GDD <- ncvar_get(nc, paste0('ERA5', index))
names(dim(ERA5GDD)) <- c('sdate', 'lat', 'lon')
S5GDD_SBC <- ncvar_get(nc, paste0('S5', index, '_SBC'))
names(dim(S5GDD_SBC)) <- c('member', 'sdate', 'lat', 'lon')

nc <- nc_open(paste0('data/', index, '_ERA5CombineClim_BC_IP_Y', substr(as.character(yearini), 3, 4), '-',
 substr(as.character(yearend), 3, 4), '_SM', startMonths[sm], '.nc'))

ERA5_CombineClim_GDD <- ncvar_get(nc, 'ERA5_CombineClim_GDD')
names(dim(ERA5_CombineClim_GDD)) <- c('member', 'sdate', 'lat', 'lon')

fcst <- s2dv::Reorder(S5GDD_SBC, c(4,3,2,1))
obs <- s2dv::Reorder(ERA5GDD, c(3,2,1))
fcst.ref <- s2dv::Reorder(ERA5_CombineClim_GDD, c(4,3,2,1))

library(easyVerification)
library(CSTools)
na.rm <- TRUE
parallel <- TRUE
maxncpus <- 1

frpss <- NA * ERA5GDD

print(startMonths[sm])

for (y in 1:dim(frpss)['sdate']) {
 print(y)
f <- veriApply('FairRpss', fcst = fcst[,,-y,], obs = obs[,,-y], fcst.ref = fcst.ref[,,-y,],
 ensdim = 4, tdim = 3, prob = 1:2/3, na.rm = na.rm, parallel = parallel, maxncpus = maxncpus)$skillscore
frpss[y,,] <- s2dv::Reorder(f, c(2,1))

}

save(frpss, file = paste0(wd, 'data/Fcst-ref_added/SM_', startMonths[sm], '.RData'))
}




