# This module load the outputs saved for each individual forecast system
# and creates the multimodel ensemble

## TO DO: 
## Remove empty folders after cleaning files
## Return probabilities

source("modules/Loading/R/dates2load.R")
source("modules/Loading/R/get_timeidx.R")
source("modules/Loading/R/check_latlon.R")
source('modules/Multimodel/load_multimodel.R')
source('modules/Multimodel/load_multimodel_splitted.R')
source('modules/Multimodel/dims_multimodel.R')
source('modules/Multimodel/build_multimodel.R')
source('modules/Multimodel/clean_multimodel.R')
library(startR)
library(CSTools)
library(yaml)
recipe <- read_yaml("recipe_tas.yml")
source("modules/Saving/R/get_dir.R")
source("modules/Loading/Loading.R")
recipe$Run$output_dir <- paste0(recipe$Run$output_dir, "recipe_tas_20240604162251/")
# DON'T KNOW HOW TO SOLVE LOGGER:
recipe$Run$Loglevel <- 'INFO'
recipe$Run$logfile <- "/esarchive/scratch/nperez/git4//recipe_tas_20240531165840/logs/main.log" 
recipe$Run$logger <-  logger <- log4r::logger(threshold = recipe$Run$Loglevel,  
                            appenders = list(console_appender(layout = default_log_layout()),
                                             file_appender(logfile, append = TRUE,   
                                                           layout = default_log_layout())))

Multimodel <- function(recipe) {
  
  # recipe: auto-s2s recipe as provided by read_yaml
  ncores <- recipe$Analysis$ncores
  cross.method <- recipe$Analysis$cross.method
  if (is.null(cross.method)) {
    cross.method <- 'leave-one-out'
  }
  categories <- recipe$Analysis$Workflow$Probabilities$percentiles
  categories <- lapply(categories, function (x) { 
                       sapply(x, function(y) {
                              round(eval(parse(text = y)),2)})})
  na.rm <- recipe$Analysis$remove_NAs 
  skill_metrics <- list()
  requested_metrics <- strsplit(recipe$Analysis$Workflow$Skill$metric,
                                ", | |,")[[1]]
  # Load data splitting by system individual members:
  if (recipe$Analysis$Datasets$Multimodel$approach == 'pooled') { 
    individual_memb <- load_multimodel(recipe)
  } 
  # Always load ensemble mean and probabilities:
  source("modules/Multimodel/load_multimodel_mean.R")
  ensmean <- load_multimodel_mean(recipe) 

  source("modules/Multimodel/load_multimodel_probs.R")
  probs <- load_multimodel_probs(recipe)
 






############# TO REMOVE:
  # Building the multi-model
  multimodel_aux <- build_multimodel(data, recipe)
  data <- multimodel_aux$data
  prob <- multimodel_aux$prob
  rm(multimodel_aux)

  # Saving multimodel
  if (recipe$Analysis$Workflow[[recipe$Analysis$Datasets$Multimodel$createFrom]]$save != 'none') {
    save_forecast(recipe = recipe,
                  data_cube = data$hcst,
                  outdir = NULL,
                  type = 'hcst')
    if (!is.null(data$fcst)) {
      save_forecast(recipe = recipe, 
                    data_cube = data$fcst,
                    outdir = NULL,
                    type = 'fcst')
    }
    save_observations(recipe = recipe, 
                      data_cube = data$obs,
                      outdir = NULL)
  }
  
  # Removing the temporary data of models in case user only requests multimodel
  clean_multimodel(recipe,
                   files_hcst, 
                   files_fcst, 
                   files_obs)
  
  return(list(data = data, 
              prob = prob))
  
}
