fair <- TRUE

## START SKILL ASSESSMENT:
# RPS
source("https://earth.bsc.es/gitlab/es/sunset/-/raw/dev-test_CERISE/GetProbs.R")
cal_hcst_probs_ev <- GetProbs(res$cal_hcst_ev, time_dim = 'syear',
                 prob_thresholds = NULL,
                 bin_dim_abs = 'probs',
                 indices_for_quantiles = NULL,
                 memb_dim = 'ensemble', abs_thresholds = res$lims_cal_hcst_tr,
                 ncores = recipe$Analysis$ncores)
cal_obs_probs_ev <- GetProbs(data$obs$data, time_dim = 'syear',
                 prob_thresholds = NULL,
                 bin_dim_abs = 'probs',
                 indices_for_quantiles = NULL,
                 memb_dim = 'ensemble',
                 abs_thresholds = res$lims_cal_obs_tr,
                 ncores = recipe$Analysis$ncores)
source("https://earth.bsc.es/gitlab/es/sunset/-/raw/dev-test_CERISE/modules/Skill/R/tmp/RPS.R")
rps <- RPS(exp = ano_hcst_probs_ev, obs = ano_obs_probs_ev, memb_dim = NULL,
           cat_dim = 'probs', cross.val = FALSE, time_dim = 'syear',
           Fair = fair, nmemb = nmemb,
           ncores = recipe$Analysis$ncores)
source("modules/Skill/R/RPS_clim.R")
rps_clim <- Apply(list(ano_obs_probs_ev),
                  target_dims = c('probs', 'syear'),
                  RPS_clim, bin_dim_abs = 'probs', Fair = fair,
                  cross.val = FALSE, ncores = recipe$Analysis$ncores)$output1
# RPSS
source("https://earth.bsc.es/gitlab/es/sunset/-/raw/dev-test_CERISE/RPSS.R")
source("https://earth.bsc.es/gitlab/es/sunset/-/raw/dev-test_CERISE/RandomWalkTest.R")
rpss <- RPSS(exp = cal_hcst_probs_ev, obs = cal_obs_probs_ev,
                   time_dim = 'syear', memb_dim = NULL,
                   cat_dim = 'probs', Fair = fair, nmemb = nmemb,
                   # We should use a line like this 
                   #abs_threshold = res$lims_ano_hcst_tr, 
                   #prob_threshold = c(1/3, 2/3),
                   cross.val = FALSE,
                   ncores = recipe$Analysis$ncores)

cal_fcst <- CST_Calibration(data$hcst, data$obs, data$fcst,
                            sdate_dim = 'syear', memb_dim = 'ensemble')
RPS
crps <- CRPS(exp  = res$ano_hcst_ev, obs = res$ano_obs_ev,
             time_dim = 'syear', memb_dim = 'ensemble',
             Fair = fair,
             ncores = recipe$Analysis$ncores)
# Este no sé como se calcula????:
# Aquí no se puede porque estaría incluyendo información de los otros años
#source("modules/Skill/R/CRPS_clim.R")
# Pero si lo hago con el ano_obs_tr si puedo hacerlo aquí
# el resultado es igual a dentro del bucle.
crps_clim <- CRPS(exp = res$ano_obs_tr, obs = res$ano_obs_ev,
                  time_dim = 'syear', memb_dim = 'sample.syear',
                  Fair = fair
                  ncores = recipe$Analysis$ncores)


# CRPSS
ref <- res$ano_obs_tr
dim(ref) <- c(ensemble = as.numeric(sdate_dim) -1,
              nftime, nlats, nlons, sdate_dim)
crpss <- CRPSS(exp = res$ano_hcst_ev, obs = res$ano_obs_ev, ref = ref,
               memb_dim = 'ensemble', Fair = fair,
               time_dim = 'syear', clim.cross.val = FALSE,
               ncores = recipe$Analysis$ncores)

# Corr
source("modules/Skill/R/tmp/Corr.R")
enscorr <- Corr(res$ano_hcst_ev, res$ano_obs_ev,
                          dat_dim = NULL,
                          time_dim = 'syear',
                          method = 'pearson',
                          memb_dim = 'ensemble',
                          memb = F,
                          conf = F,
                          pval = F,
                          sign = T,
                          alpha = 0.05,
                          ncores = recipe$Analysis$ncores)

# Mean Bias
#mean_bias <- Bias(res$ano_hcst_ev, res$ano_obs_ev,
mean_bias <- Bias(data$hcst$data, data$obs$data,
                 time_dim = 'syear',
                 memb_dim = 'ensemble',
                 ncores = recipe$Analysis$ncores)

mean_bias_sign <- Apply(list(data$hcst$data, data$obs$data),
                        target_dims = list(c('syear', 'ensemble'), 'syear'),
                        fun = function(x,y) {
                          if (!(any(is.na(x)) || any(is.na(y)))) {
                            res <- t.test(x = y,
                                          y = apply(x, 1, mean, na.rm = T),
                                          alternative = "two.sided")$p.value
                          } else {
                            res <- NA
                          }
                          return(res)},
                        ncores = sdate_dim)$output1
mean_bias_sign <- mean_bias_sign <= 0.05

# Spread error ratio
source("SprErr.R")
enssprerr <- SprErr(exp = res$ano_hcst_ev, obs = res$ano_obs_ev,
                    memb_dim = 'ensemble', dat_dim = NULL,
                    time_dim = 'syear', pval = TRUE,
                    ncores = recipe$Analysis$ncores)
enssprerr_sign <- enssprerr$p.val
enssprerr_sign <- enssprerr_sign <= 0.05
enssprerr <- enssprerr$ratio

# RMSE
rms <- RMS(exp = res$ano_hcst_ev, obs = res$ano_obs_ev,
           memb_dim = 'ensemble', dat_dim = NULL,
           time_dim = 'syear', alpha = 0.05,
           ncores = recipe$Analysis$ncores)

skill_metrics <- list(mean_bias = mean_bias,
                      mean_bias_significance = mean_bias_sign,
                      enscorr = enscorr$corr,
                      enscorr_significance = enscorr$sign,
                      enssprerr = enssprerr,
                      enssprerr_significance = enssprerr_sign,
                      rps = rps, rps_clim = rps_clim, crps = crps, crps_clim = crps_clim,
                      rpss = rpss$rpss, rpss_significance = rpss$sign, #crps = crps,
                      crpss = crpss$crpss, crpss_significance = crpss$sign,
                      rms = rms$rms)
skill_metrics <- lapply(skill_metrics, function(x) {
                      InsertDim(drop(x), len = 1, pos = 1, name = 'var')})
original <- recipe$Run$output_dir
recipe$Run$output_dir <- paste0(original, "/outputs/Skill/")

skill_metrics <- lapply(skill_metrics, function(x) {
                   if (is.logical(x)) {
                     dims <- dim(x)
                     res <- as.numeric(x)
                     dim(res) <- dims
                   } else {
                     res <- x
                   }
                   return(res)
                      })
# Compute save metrics 
source("modules/Saving/Saving.R")
#Saving <- Saving(recipe = recipe, data = data, skill = skill_metrics)
      save_metrics(recipe = recipe,
                   metrics = skill_metrics,
                   data_cube = data$hcst, agg = 'global',
                   outdir = recipe$Run$output_dir)

recipe$Run$output_dir <- original

