
################################
### SEASONAL MULTIMODEL TEST ###
################################

# Load modules
source("modules/Loading/Loading.R")
source("modules/Units/Units.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("modules/Multimodel/Multimodel.R")

horizon <- 'decadal' # seasonal

## Cleaning output directory and splitting recipe
sunset_outputs_folder <- '/esarchive/scratch/cdelgado/sunset_outputs/'
recipe_file <- paste0("recipes/recipe_multimodel_",horizon,".yml")
# system(paste0('rm -r ',sunset_outputs_folder,'recipe_multimodel_',horizon,'_*'))
system(paste0('Rscript split.R ',recipe_file))

## Finding atomic recipes
atomic_recipe_folder <- paste0(sunset_outputs_folder,list.files(sunset_outputs_folder, pattern = horizon),'/logs/recipes/')
atomic_recipe_folder <- atomic_recipe_folder[length(atomic_recipe_folder)]
atomic_recipes <- list.files(paste0(atomic_recipe_folder), pattern = '.yml')
atomic_recipes <- atomic_recipes[-length(atomic_recipes)]
atomic_recipes_multimodel <- list.files(paste0(atomic_recipe_folder,'multimodel'))
atomic_recipes <- paste0(atomic_recipe_folder,atomic_recipes)
atomic_recipes_multimodel <- paste0(atomic_recipe_folder,'multimodel/',atomic_recipes_multimodel)

## Running atomic recipes
for (recipe_file in c(atomic_recipes,atomic_recipes_multimodel)){
  # Read recipe
  recipe <- read_atomic_recipe(recipe_file)
  if (recipe$Analysis$Datasets$System$name == 'Multimodel'){
    # Load datasets and create multimodel
    mm <- Multimodel(recipe)
    data <- mm$data
    probabilities <- mm$prob
  } else {
    # Load datasets
    data <- Loading(recipe)
    # Change units
    data <- Units(recipe, data)
    # Compute anomalies
    data <- Anomalies(recipe, data)
    # Compute percentiles and probability bins
    probabilities <- Probabilities(recipe, data)
  }
  # Compute skill metrics 
  skill_metrics <- Skill(recipe, data)
  # Plot data
  Visualization(recipe, data, skill_metrics, probabilities, significance = T)
}
