# 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/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_20240605141443/")
# 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) {
  
  # Load data splitting by system individual members:
  if (recipe$Analysis$Datasets$Multimodel$approach == 'pooled') { 
    datos <- load_multimodel(recipe)
  } else if (recipe$Analysis$Datasets$Multimodel$approach == 'mean') {
    # In this case the ensemble dim is 1 for each model
    # The functions in skill can still work
    source("modules/Multimodel/load_multimodel_mean.R")
    datos <- load_multimodel_mean(recipe) 
    source("modules/Multimodel/load_multimodel_probs.R")
    probs <- load_multimodel_probs(recipe)
  } else { #(recipe$Analysis$Datasets$Multimodel$approac == "weighted") 
      stop("Implement the weighting method here")
      # I imagine the probs and the ensmean are modified and returned.
      multimodel_aux <- build_multimodel(data, recipe)
      ensmean <- multimodel_aux$ensmean
      probs <- multimodel_aux$prob
      rm(multimodel_aux)
  } 
  
  # assessment:
  source("modules/Multimodel/multimodel_metrics.R")
  skill_metrics <- multimodel_metrics(recipe = recipe,
                                      indv_membs = individual_memb,
                                      ensmean = ensmean,
                                      probs = probs)





  # 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))
  
}
