library(pryr)
source("modules/Loading/Loading.R")
source("modules/Saving/Saving.R")
source("modules/Units/Units.R")
source("modules/Visualization/Visualization.R")
#args = commandArgs(trailingOnly = TRUE)
#recipe_file <- args[1]
recipe_file <- "recipe_tas.yml"
# recipe_file <- "recipe_tas_decadal.yml"
# Will it work with a single job?
#recipe <- read_atomic_recipe(recipe_file)
original_recipe <- prepare_outputs(recipe_file, disable_checks = TRUE)
# Load datasets
models <- unlist(original_recipe$Analysis$Datasets$System)


recipe_aux <- original_recipe
datos <- list()
datos$hcst <- list()
datos$fcst <- list()
source("modules/Crossval/Crossval_metrics.R")
source("modules/Crossval/Crossval_Calibration.R")
source("modules/Aggregation/Aggregation.R")
# sys <- models[1]
for (sys in models) {
  recipe_aux$Analysis$Datasets$System <- NULL
  recipe_aux$Analysis$Datasets$System$name <- as.vector(sys)
  # recipe_aux$Analysis$Datasets$System$member <-  read_yaml("conf/archive_decadal.yml")$esarchive$System[[sys]]$member
  data <- Loading(recipe = recipe_aux)
  #### Reorde for MostLikely to plot EU centered:
  data$hcst <- CST_Subset(data$hcst, along = 'longitude',
                          indices=c(181:360, 1:180))
  data$fcst <- CST_Subset(data$fcst, along = 'longitude',
                          indices=c(181:360, 1:180))
  data$obs <- CST_Subset(data$obs, along = 'longitude',
                          indices=c(181:360, 1:180))
  data$hcst$coords$longitude[1:180] <- data$hcst$coords$longitude[1:180] - 360
  data$fcst$coords$longitude[1:180] <- data$fcst$coords$longitude[1:180] - 360
  data$obs$coords$longitude[1:180] <- data$obs$coords$longitude[1:180] - 360
  ####
  data <- Units(recipe = recipe_aux, data = data)
  data <- Aggregation(recipe = recipe_aux, data = data)
  # verification individual models
  product <- Crossval_Calibration(recipe = recipe_aux, data = data)
  skill_model <- Crossval_metrics(recipe = recipe_aux, data_crossval = product,
                                fair = FALSE, nmemb = NULL, nmemb_ref = NULL)

  tmp_probs <- list(probs_fcst = product$probs$fcst[[1]])
  lats <- length(data$hcst$coords$latitude)
  lons <- length(data$hcst$coords$longitude)
  timesteps <- as.numeric(dim(data$hcst$data)['time'])
  dim(tmp_probs$probs_fcst) <- c(bin = 3, syear = 1, var = 1, time = timesteps, 
				#latitude = 20, longitude = 20)
				latitude = lats, longitude = lons)

  Visualization(recipe = recipe_aux, data = data, probabilities = tmp_probs,
		skill_metrics = skill_model, significance = TRUE)
  ## end ver
  datos$hcst <- append(datos$hcst, list(data$hcst))
  datos$fcst <- append(datos$fcst, list(data$fcst))
  names(datos$hcst)[length(datos$hcst)] <- gsub('\\.','', sys)
  names(datos$fcst)[length(datos$fcst)] <- gsub('\\.','', sys)
  gc()
  print(mem_used())
}
data_aux <- data
datos$obs <- data$obs
data <- datos
rm(list = 'datos')

source("modules/Crossval/Crossval_multimodel_Calibration.R")
res <- Crossval_multimodel_Calibration(recipe = original_recipe, data = data)

source("modules/Crossval/Crossval_multimodel_metrics.R")
skill_metrics <- Crossval_multimodel_metrics(recipe = original_recipe,
                                    data = res, Fair = FALSE)

recipe_aux$Analysis$Datasets$System$name <- 'Multimodel'
tmp_probs <- list(probs_fcst = res$probs$probs_fcst[[1]][[1]])
Visualization(recipe = recipe_aux, data = data_aux, probabilities = tmp_probs, skill_metrics = skill_metrics, significance = TRUE)


## Check logo size is appropiated for your maps:
source("tools/add_logo.R")
add_logo(recipe_aux, "rsz_rsz_bsc_logo.png")

# scorecards computation:


