

source("modules/Loading/Loading.R")
source("modules/Units/Units.R")
source("modules/Calibration/Calibration.R")
source("modules/Skill/Skill.R")
source("modules/Anomalies/Anomalies.R")
source("modules/Saving/Saving.R")
source("modules/Visualization/Visualization.R")

recipe_file <- "./tests/recipes/recipe-dev-plot_extremes.yml"
recipe <- prepare_outputs(recipe_file, disable_checks = FALSE)

data <- Loading(recipe)
data <- Units(recipe, data)
data <- Calibration(recipe, data)

skill_metrics <- Skill(recipe, data)
probabilities <- Probabilities(recipe, data)

outdir <- "./modules/Visualization/R/"
output_conf <- read_yaml("modules/Visualization/output_size.yml",
                         eval.exp = TRUE)$region

source("./modules/Visualization/R/plot_extreme_probabilities.R")
plot_extreme_probabilities(recipe = recipe, fcst = data$fcst,
                           outdir = outdir, output_conf = output_conf,
                           method = "mean")
source("./modules/Visualization/R/plot_extreme_probabilities.R")
source("./modules/Visualization/R/tmp/ColorBar.R")
plot_extreme_probabilities(recipe = recipe, fcst = data$fcst,
                           probabilities = prob_array, threshold = 0.9,
                           outdir = outdir, output_conf = output_conf,
                           method = "mean")
# plot_ensemble_mean()




# Colorbar

# Browse[2]> brks; cols; bar_limits; var_limits
# [1] 0.00 0.86 1.00
# [1] "#33BFD1" "#FF764D"
# [1] 0 1
# [1] 0.1 1.0
# 
# 
# # taking the first ensemble member:
# data_ens1 <- data$fcst$data[, , , , , , , , 1]
# # ensemble mean
# ensemble_mean <- s2dv::MeanDims(data$fcst$data, 'ensemble')
# 
# # making a probability array with the same dimensions as ensemble_mean:
# dims <- c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 1, time = 1, latitude = 12, longitude = 15)
# # prob_array <- array(runif(prod(dims), min = 0, max = 1), dim = dims)
# dim(prob_array)
# 
# 
# Browse[2]> all(unique(var_ens_mean) %in% c(0, 1))
# [1] TRUE
# 
# 
# 
# data$fcst["ensemble"]
# 
# # BROWSER:
# 
# Browse[2]> dim(fcst$data)
# dat       var      sday     sweek     syear      time  latitude longitude  ensemble 
# 1         1         1         1         1         1        12        15        51 
# 
# Browse[2]> dim(ensemble_mean)
# dat       var      sday     sweek     syear      time  latitude longitude 
# 1         1         1         1         1         1        12        15 
# 
# Browse[2]> dim(var_ens_mean)
# syear      time  latitude longitude
# 1         1        12        15
# 
# # exemple:
# # ...
# # , , 14, 12
# # 
# # [,1]
# # [1,] 290.2433
# # 
# # , , 15, 12
# # 
# # [,1]
# # [1,] 291.326
# 
# 
# 
# Browse[2]> dim(i_var_ens_mean)
# time longitude  latitude
# 1        15        12
# 
# # exemple:
# # , , 12
# # 
# # [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]     [,9]    [,10]
# # [1,] 291.5779 291.6805 291.7167 291.8615 292.2305 292.6121 292.9793 293.2251 292.8278 293.8573
# # [,11]    [,12]    [,13]    [,14]   [,15]
# # [1,] 293.0403 292.0609 288.9186 290.2433 291.326
# 
# 
# Browse[2]> outfile
# [1] "./modules/Visualization/R/forecast_ensemble_mean-20230601"



