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

Multimodel <- function(recipe) {
  
  # recipe: auto-s2s recipe as provided by read_yaml
    
  # Loading data saved in the jobs for individual models
  if (tolower(recipe$Analysis$Datasets$Multimodel$split_loading) %in% c('true','yes')){
    # Load data splitting by system
    data_aux <- load_multimodel_splitted(recipe)
    
    data <- data_aux$data
    
    files_hcst <- data_aux$files_hcst
    files_fcst <- data_aux$files_fcst
    files_obs <- data_aux$files_obs
    
    rm(data_aux)
    
  } else { 
    # Load data without splitting
    data <- load_multimodel(recipe)   
    files_hcst <- data$hcst$attrs$source_files
    files_fcst <- data$fcst$attrs$source_files
    files_obs <- data$obs$attrs$source_files
  } 
  
  # 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))
  
}
