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

# recipe <- "recipe_tas.yml" # I have included multimodel yes
library(yaml)
recipe <- read_yaml(recipe)

# (tolower(recipe$Analysis$Datasets$Multimodel$split_loading) %in% c('true','yes')) ## Fails becuase I don't know where split_loading comes from

# I guess we need this:
# From load_multmodel

archive <- read_yaml("conf/archive.yml")$esarchive
ref.name <- recipe$Analysis$Datasets$Reference$name

# lets try creating a vector
## I don't know where this comes from 
## recipe$Analysis$Datasets$System$models
exp.name <- c("ECMWF-SEAS5.1", "Meteo-France-System7")

library(startR)
 store.freq <- recipe$Analysis$Variables$freq
  variable <- "tas" #strsplit(recipe$Analysis$Variables$name, ", | |,")[[1]]
  exp_descrip <- archive$System[[exp.name[1]]]
  reference_descrip <- archive$Reference[[ref.name]]
  sdates <- dates2load(recipe, recipe$Run$logger)
  lats.min <- recipe$Analysis$Region$latmin
  lats.max <- recipe$Analysis$Region$latmax
  lons.min <- recipe$Analysis$Region$lonmin
  lons.max <- recipe$Analysis$Region$lonmax
  circularsort <- check_latlon(lats.min, lats.max, lons.min, lons.max)


recipe$Run$output_dir <- file.path("/esarchive/scratch/nperez/git4/recipe_tas_20240531165840", "outputs", 
                          recipe$Analysis$Datasets$Multimodel$createFrom)

hcst_start <- recipe$Analysis$Time$hcst_start
    hcst_end <- recipe$Analysis$Time$hcst_end
    shortdate <- substr(recipe$Analysis$Time$sdate, start = 1, stop = 2)
    filename <- paste0("scorecards_$model$_", ref.name, "_$var$__$file_date$__",
                       hcst_start, "-", hcst_end, "_s", shortdate, ".nc")

source("modules/Saving/Saving.R")
  hcst.path <- file.path(recipe$Run$output_dir, "$model$", ref.name, 
                         recipe$Analysis$Workflow$Calibration$method,
                         "$var$", filename)
#  hcst.path <- gsub(variable[1], "$var$", hcst.path)
#  hcst.path <- gsub('Multimodel', "$model$", hcst.path)
  fcst.path <- obs.path <- hcst.path
  obs.path <- gsub("_$file_date$", "-obs_$file_date$", obs.path, fixed = T)
  obs.path <- gsub("$model$", gsub('\\.','',exp.name[1]), obs.path, fixed = T)
 
 hcst <- Start(dat = hcst.path,
                var = variable,
                file_date = sdates$hcst,
                model = gsub('\\.','',exp.name),
                time = 'all',
                latitude = 'all',
                latitude_reorder = Sort(),
                longitude = 'all',
                longitude_reorder = circularsort,
                synonims = list(latitude = c('lat', 'latitude'),
                                longitude = c('lon', 'longitude'),
                                ensemble = c('member', 'ensemble')),
                ensemble = 'all',
                metadata_dims = 'var',
                largest_dims_length = TRUE,
                return_vars = list(latitude = 'dat',
                                   longitude = 'dat',
                                   time = 'file_date'),
                split_multiselected_dims = split_multiselected_dims,
                retrieve = TRUE)
  
  hcst <- as.s2dv_cube(hcst)
  # Adjust dates for models where the time stamp goes into the next month
  # No need to check the dates if the files are saved by SUNSET:
  #if (recipe$Analysis$Variables$freq == "monthly_mean") {
  #  hcst$attrs$Dates[] <- hcst$attrs$Dates - seconds(exp_descrip$time_stamp_lag)
  #}

fcst <- as.s2dv_cube(fcst)

 fcst <- Start(dat = fcst.path,
                  var = variable,
                  file_date = sdates$fcst,
                  model = gsub('\\.','',exp.name),
                  time = 'all',
                  latitude = 'all',
                  latitude_reorder = Sort(),
                  longitude = 'all',
                  longitude_reorder = circularsort,
                  synonims = list(latitude = c('lat', 'latitude'),
                                  longitude = c('lon', 'longitude'),
                                  ensemble = c('member', 'ensemble')),
                  ensemble = 'all',
                  metadata_dims = 'var',
                  largest_dims_length = TRUE,
                  return_vars = list(latitude = 'dat',
                                     longitude = 'dat',
                                     time = 'file_date'),
                  split_multiselected_dims = split_multiselected_dims,
                  retrieve = TRUE)
    
datos <- list(hcst = hcst, fcst = fcst, obs = obs)

 obs <- Start(dat =  obs.path,
                 var = variable,
                 file_date = sdates$hcst,
                 time = 'all',
                 latitude = 'all',
                 latitude_reorder = Sort(),
                 longitude = 'all',
                 longitude_reorder = circularsort,
                 synonims = list(latitude = c('lat','latitude'),
                                 longitude = c('lon','longitude')),
                 metadata_dims = 'var',
                 return_vars = list(latitude = 'dat',
                                    longitude = 'dat',
                                    time = 'file_date'),
                 split_multiselected_dims = TRUE,
                 retrieve = TRUE)

 default_dims <- c(dat = 1, var = 1, sday = 1, 
                    sweek = 1, syear = 1, time = 1, 
                    latitude = 1, longitude = 1, ensemble = 1)
  default_dims[names(dim(obs))] <- dim(obs)
  dim(obs) <- default_dims
  
  # Convert obs to s2dv_cube
  obs <- as.s2dv_cube(obs) 
