
source("modules/Loading/Loading.R")
source("modules/Saving/Saving.R")
source("modules/Units/Units.R")
source("modules/Visualization/Visualization.R")
source("modules/Aggregation/Aggregation.R")
args = commandArgs(trailingOnly = TRUE)
recipe_file <- args[1]
#recipe_file <- "recipe_tas_singl.yml"
#recipe <- read_atomic_recipe(recipe_file)
recipe <- prepare_outputs(recipe_file)
# Load datasets
data <- Loading(recipe)
data <- Units(recipe, data)
data_summary(data$hcst, recipe)
data_summary(data$obs, recipe)
data_summary(data$fcst, recipe)

  #### 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_agg <- Aggregation(recipe = recipe, data = data)
data_summary(data_agg$hcst, recipe)
data_summary(data_agg$obs, recipe)
data_summary(data_agg$fcst, recipe)

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

source("modules/Crossval/Crossval_metrics.R")
skill_metrics <- Crossval_metrics(recipe = recipe, data_crossval = res,
                                fair = FALSE, nmemb = NULL, nmemb_ref = NULL) 

# Required to plot a forecast:
data_agg$fcst <- res$fcst
tmp_probs <- list(probs_fcst = res$probs$fcst[[1]])
dim(tmp_probs$probs_fcst) <- c(bin = 3, syear = 1, var = 1, time = 4, latitude = 180, longitude = 360)

#tmp_probs$probs_fcst <- list()
#tmp_probs$probs_fcst$prob_b33 <- res$probs$fcst[[1]][1,1,1,1,1,1,,,]
#tmp_probs$probs_fcst$prob_33_to_66 <- res$probs$fcst[[1]][2,1,1,1,1,1,,,]
#tmp_probs$probs_fcst$prob_a66 <- res$probs$fcst[[1]][3,1,1,1,1,1,,,]
#tmp_probs$probs_fcst$prob_b33 <- InsertDim(tmp_probs$probs_fcst$prob_b33, len = 1, pos = 1, name = 'var')
#tmp_probs$probs_fcst$prob_33_to_66 <- InsertDim(tmp_probs$probs_fcst$prob_33_to_66, len = 1, pos = 1, name = 'var')
#tmp_probs$probs_fcst$prob_a66 <- InsertDim(tmp_probs$probs_fcst$prob_a66, len = 1, pos = 1, name = 'var')


Visualization(recipe = recipe, data = data_agg, skill_metrics = skill_metrics, significance = TRUE, probabilities = tmp_probs)

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

# scorecards computation:


