rm(list=ls())
gc()

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/Visualization/Visualization.R")
source("tools/prepare_outputs.R")

# Read recipe
## To run many recipes using launch_SUNSET.sh:
# args = commandArgs(trailingOnly = TRUE)
# recipe_file <- args[1]
# recipe <- read_atomic_recipe(recipe_file)
## To test a single recipe:
#  recipe_file <- "recipes/examples/recipe_tas_seasonal_units.yml"
#  recipe_file <- "recipes/examples/recipe_prlr_seasonal_units.yml"

recipe <- prepare_outputs(recipe_file)

# Load datasets
data <- Loading(recipe)
# Units transformation
source("modules/Units/Units.R")
test <- Units(recipe, data)
# Calibrate datasets
data <- Calibration(recipe, test)
# Compute skill metrics 
skill_metrics <- Skill(recipe, data)
# Compute percentiles and probability bins
probabilities <- Probabilities(recipe, data)

# Plot data
# Below, many examples of different ways to plot the data are shown, using
# different projections and ways of adding statistical significance masking.
Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = F)
list.files(paste0(recipe$Run$output_dir, "/plots/ECMWF-SEAS51/ERA5/evmos/tas/"))
# signif TRUE:
Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = T)
list.files(paste0(recipe$Run$output_dir, "/plots/ECMWF-SEAS51/ERA5/evmos/tas/"))
# signif mask: this option is not available in PlotEquiMap, it return dots
Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = 'mask')
list.files(paste0(recipe$Run$output_dir, "/plots/ECMWF-SEAS51/ERA5/evmos/tas/"))
## The difference between TRUE and mask for PlotEquiMap is the file name

# signif dots: 
visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = 'dots')
list.files(paste0(recipe$run$output_dir, "/plots/ecmwf-seas51/era5/evmos/tas/"))

# In this case duplicates files but simplifying the code makes it very complex
Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = 'both')

recipe$Analysis$Workflow$Visualization$multi_panel <- TRUE
recipe$Analysis$Workflow$Visualization$mask_terciles <- NULL
Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = F)

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

Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = 'mask')

recipe$Analysis$Workflow$Visualization$multi_panel <- FALSE
recipe$Analysis$Workflow$Visualization$projection <- 'Robinson'
Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = F)
Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = T)
Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = 'dots')
Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = 'mask')
Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = 'both')

