rm(list=ls())
source("modules/Loading/Loading.R")
source("modules/Units/Units.R")
source("modules/Anomalies/Anomalies.R")

recipe_file <- "recipe_product.yml"
recipe <- prepare_outputs(recipe_file)

source("contingency.R")

output_dir <- "/esarchive/scratch/nperez/product/"
years <- 1993:2016
for (i in 1:12) {
  recipe$Analysis$Time$sdate <- paste0(sprintf("%02d", i), '01')
  posm <- i:(i+5)
  posm[posm > 12] <- posm[posm > 12] - 12
  months_names <- month.name[posm]
  data <- Loading(recipe)
  data <- Units(recipe = recipe, data = data)
  nlats <- as.numeric(dim(data$hcst$data)['latitude'])
  nlons <- as.numeric(dim(data$hcst$data)['longitude'])
#  bias <- Bias(data$hcst$data, data$obs$data, time_dim = 'syear', memb_dim = 'ensemble')

  # Bias Adjustment:
#  hcst_adj <- CST_BiasCorrection(exp = data$hcst,
#                                 obs = data$obs,
#                                 sdate_dim = 'syear',
#                                 memb_dim = 'ensemble',
#                                 ncores = 16)
  hcst_adj <- CST_Calibration(exp = data$hcst,
                              obs = data$obs,
                              cal.method = "mse_min",
                              eval.method = "leave-one-out",
                              sdate_dim = 'syear',
                              memb_dim = 'ensemble',
                              ncores = 4)

  # ENSEMBLE MEAN ERROR
  EME <- RMS(hcst_adj$data, data$obs$data,
             time_dim = 'syear', memb_dim = 'ensemble',
             ncores = 4)
  
  PlotLayout(PlotEquiMap, c('longitude', 'latitude'), EME$rms[1,1,1,1,,,],
             lon = data$hcst$coords$lon,
             lat = data$hcst$coords$lat,
             filled.c = F, brks = seq(0, 7, 1),
             toptitle = paste("RMS / sdate", i), 
             titles = months_names,
             fileout = paste0(output_dir, "Cal_EU_RMS_sdate",
                              i, ".png"))  
#  source("contingency.R")

  result <- Contingency(exp = hcst_adj$data, obs = data$obs$data,
                        sdate_dim = 'syear', memb_dim = 'ensemble',
                        perc_lim = F, err = 1, prob = 0.6, method = 'mean',
                        ncores = 4)


  PlotLayout(PlotEquiMap, c('longitude', 'latitude'),
              result$metrics$accuracy[1,1,1,1,,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(0, 1, 0.1),
              titles = months_names,
              toptitle = paste("Accuracy sdate", i),
              units = "Accuracy",
              fileout = paste0(output_dir, "Cal_Accuracy_s", i, ".png"))
  
  PlotLayout(PlotEquiMap, c('longitude', 'latitude'),
              result$metrics$hit_rate[1,1,1,1,,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(0, 1, 0.1),
              titles = months_names,
              toptitle = paste("Hit rate sdate", i),
              units = "Hit rate",
              fileout = paste0(output_dir, "Cal_HitRate_s", i, ".png"))


  PlotLayout(PlotEquiMap, c('longitude', 'latitude'),
              result$score$hit[1,1,1,1,,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(0, 24, 2),
              titles = months_names,
              toptitle = paste("Hits sdate", i),
              units = "Hits",
              fileout = paste0(output_dir, "Cal_Hits_s", i, ".png"))
 
 year <- 1

 PlotLayout(PlotEquiMap, c('longitude', 'latitude'),
              result$prob$cat[1,1,1,1,year,,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(0, 100, 10),
              titles = months_names,
              toptitle = paste("Probability sdate", i),
              units = "Probability",
              fileout = paste0(output_dir, "Cal_Prob_year",
                               year, "_s", i, ".png"))

 PlotLayout(PlotEquiMap, c('longitude', 'latitude'),
              result$pred$det[1,1,1,1,year,,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(-40, 40, 5),
              titles = months_names,
              toptitle = paste("Prediction sdate", i),
              units = "temperature (ºC)",
              fileout = paste0(output_dir,"Cal_Pred_year",
                               year, "_s", i, ".png"))

 PlotLayout(PlotEquiMap, c('longitude', 'latitude'),
              result$pred$inf[1,1,1,1,year,,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(-40, 40, 5),
              titles = months_names,
              toptitle = paste("Lower limit prediction category sdate", i),
              units = "temperature (ºC)",
              fileout = paste0(output_dir,"Cal_LowerLim_year",
                               year, "_s", i, ".png"))

 PlotLayout(PlotEquiMap, c('longitude', 'latitude'),
              result$pred$sup[1,1,1,1,year,,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(-40, 40, 5),
              titles = months_names,
              toptitle = paste("Upper limit prediction category sdate", i),
              units = "temperature (ºC)",
              fileout = paste0(output_dir, "Cal_UpperLim_year",
                               year, "_s", i, ".png"))

clim <- Contingency(exp = hcst_adj$data, obs = data$obs$data,
                    sdate_dim = 'syear', memb_dim = 'ensemble',
                    perc_lim = F, err = 1, prob = 0.6, method = 'climatology',
                    ncores = 4)

# Probs
hcst_probs <- abind::abind(result$probs$lowercat,
                           result$probs$cat,
                           result$probs$uppercat, along = 10)
dim(hcst_probs) <- c(dat = 1, var = 1, sday = 1, sweek = 1,
                     syear = 24, time = 6, latitude = nlats, 
                     longitude = nlons, ensemble = 1, cat = 3)
clim_probs <- abind::abind(clim$probs$lowercat,
                           clim$probs$cat,
                           clim$probs$uppercat, along = 10)
dim(clim_probs) <- c(dat = 1, var = 1, sday = 1, sweek = 1,
                     syear = 24, time = 6, latitude = nlats,
                     longitude = nlons, ensemble = 1, cat = 3)
source("https://earth.bsc.es/gitlab/es/s2dv/-/raw/master/R/GetProbs.R")
abs_thre <- abind::abind(result$prediction$inf_lim,
                         result$prediction$sup_lim, along = 10)
dim(abs_thre) <- c(dat = 1, var = 1, sday = 1, sweek = 1,
                   syear = 24, time = 6, latitude = nlats,
                   longitude = nlons, ensemble = 1, cat = 2)
obs_probs <- GetProbs(data$obs$data, time_dim = 'syear', memb_dim = NULL,
                      prob_thresholds = NULL,
                      abs_thresholds = abs_thre, bin_dim_abs = 'cat')

abs_thre_ref <- abind::abind(clim$prediction$inf_lim,
                             clim$prediction$sup_lim, along = 10)
dim(abs_thre_ref) <- c(dat = 1, var = 1, sday = 1, sweek = 1,
                       time = 6, latitude = nlats,
                       longitude = nlons, ensemble = 1, syear = 24, cat = 2)
obs_probs_ref <- GetProbs(data$obs$data, time_dim = 'syear', memb_dim = NULL,
                          prob_thresholds = NULL,
                          abs_thresholds = abs_thre_ref, bin_dim_abs = 'cat')

source("https://earth.bsc.es/gitlab/es/s2dv/-/raw/master/R/RPS.R")
RPS_prod <- RPS(exp = hcst_probs/100, obs = obs_probs, time_dim = 'syear',
           memb_dim = NULL, cat_dim = 'cat', prob_thresholds = c(0.1,0.9),
           return_mean = T)
RPS_clim <- RPS(exp = clim_probs/100, obs = obs_probs_ref,
                time_dim = 'syear',
                memb_dim = NULL, cat_dim = 'cat', prob_thresholds = c(0.1,0.9),
                return_mean = T) 
RPSS <- 1 - RPS_prod/RPS_clim
 
 PlotLayout(PlotEquiMap, c('longitude', 'latitude'),
              RPSS[1,1,1,1,,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(-1, 1, 0.2),
              titles = months_names,
              toptitle = paste("RPSS", i),
              units = "RPSS",
              fileout = paste0(output_dir, "RPSS",
                               "_s", i, ".png"))
saveRDS(list(product = result, clim = clim, RPSS = RPSS, RPS_prod = RPS_prod,
             RPS_clim = RPS_clim),
        file = paste0(output_dir, "sdate", i, "results.RDS"))
}
#  PlotEquiMap(contingency$hits_mean[1,1,1,1,1,,,1],
#              lon = data$hcst$coords$longitude,
#              lat = data$hcst$coords$latitude, filled.c = F,
#              brks = seq(0, 24, 2), 
#              sizetit = 0.8,
#              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
#              units = "Hits")
#
#  PlotEquiMap(contingency$falseA_mean[1,1,1,1,1,,,1],
#              lon = data$hcst$coords$longitude,
#              lat = data$hcst$coords$latitude, filled.c = F,
#              brks = seq(0, 24, 2),
#              sizetit = 0.8,
#              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
#              units = "False Alarms")

#  PlotEquiMap(contingency$miss_mean[1,1,1,1,1,,,1],
#              lon = data$hcst$coords$longitude,
#              lat = data$hcst$coords$latitude, filled.c = F,
#              brks = seq(0, 24, 2),
#              sizetit = 0.8,
#              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
#              units = "Missed")
#  PlotEquiMap(contingency$correctR_mea[1,1,1,1,1,,,1],
#              lon = data$hcst$coords$longitude,
#              lat = data$hcst$coords$latitude, filled.c = F,
#              brks = seq(0, 24, 2),
#              sizetit = 0.8,
#              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
#              units = "Correct Rejected")

#  PlotEquiMap(contingency$bias[1,1,1,1,1,,,1],
#              lon = data$hcst$coords$longitude,
#              lat = data$hcst$coords$latitude, filled.c = F,
#              brks = seq(0, 2, 0.2),
#              sizetit = 0.8,
#              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
#              units = "Bias Score")
#  PlotEquiMap(contingency$hit_rate[1,1,1,1,1,,,1],
#              lon = data$hcst$coords$longitude,
#              lat = data$hcst$coords$latitude, filled.c = F,
#              brks = seq(0, 1, 0.1),
#              sizetit = 0.8,
#              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
#              units = "Hit rate")
#  PlotEquiMap(contingency$fa_rate[1,1,1,1,1,,,1],
#              lon = data$hcst$coords$longitude,
#              lat = data$hcst$coords$latitude, filled.c = F,
#              brks = seq(0, 1, 0.1),
#              sizetit = 0.8,
#              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
#              units = "False Alarm rate")




#saveRDS(list(biases = biases, pvals = pvals), file = 'bias_results.RDS')


