# This is an example for forecast verification and evaluation.

Sys.setenv(LANG = "en")
source("modules/Loading/Loading.R")
source("modules/Calibration/Calibration.R")
source("modules/Anomalies/Anomalies.R")
source("modules/Skill/Skill.R")
source("modules/Saving/Saving.R")
source("modules/Units/Units.R")
source("modules/Scorecards/Scorecards.R")
source("modules/Visualization/Visualization.R")
source("modules/Downscaling/Downscaling.R")

#args = commandArgs(trailingOnly = TRUE)
#recipe_file <- args[1]
recipe_file <- "recipe_ecvs_seasonal_scorecards.yml"
recipe <- prepare_outputs(recipe_file)
# Load datasets
data <- Loading(recipe)

# Units
data <- Units(recipe, data)

# Calibrate data
#data <- Calibration(recipe,data)

# Calculate anomalies
#data <- Anomalies (recipe, data)

# Calculate skill metrics
skill_metrics <- Skill(recipe, data)

scorecards <- Scorecards(recipe)

# Calculate probabilities
probabilities <- Probabilities(recipe, data)

# Visualize data
Visualization(recipe, data, skill_metrics, probabilities, significance = T)

## Add BSC logo
#logo <- "tools/BSC_logo_95.jpg"
#system <- list.files(paste0(recipe$Run$output_dir, "/plots"))
#variable <- strsplit(recipe$Analysis$Variable$name, ", | |,")[[1]]
#files <- lapply(variable, function(x) {
#           f <- list.files(paste0(recipe$Run$output_dir, "/plots/",
#                                   system, "/", x))
#           full_path <- paste0(recipe$Run$output_dir, "/plots/",
#                                   system, "/", x,"/", f)})[[1]]
#dim(files) <- c(file = length(files))
#Apply(list(files), target_dims = NULL, function(x) {
#  system(paste("composite -gravity southeast -geometry +10+10",
#               logo, x, x))}, ncores = recipe$Analysis$ncores)


