source("modules/Loading/Loading.R")
source("modules/Units/Units.R")
source("modules/Calibration/Calibration.R")
source("modules/Anomalies/Anomalies.R")
source("modules/Skill/Skill.R")
source("modules/Saving/Saving.R")
source("modules/Visualization/Visualization.R")

recipe_file <- "recipe_subseasonal_scorecards_atomic.yml"
recipe <- prepare_outputs(recipe_file)
archive <- read_yaml(paste0(recipe$Run$code_dir, "conf/archive.yml"))$archive # you can delete this line
outdir <- recipe$Run$output_dir # just for the test script
var <- recipe$Analysis$Variable$name # just for the test script

data_cube <- test_hcst

# Dimensions at this stage, might need to reorder:
# dim(data$hcst$data)
#      dat       var      sday     sweek  ensemble     syear      time  latitude longitude
#        1         1         1         1        15         6         3        22        53 
# dim(data$obs$data)
#      dat       var      sday     sweek     syear      time  ensemble  latitude longitude
#        1         1         1         1         6         3         1        22        53 


#####################################
###   SKILL METRICS CALCULATION   ###
#####################################


# For subseasonal, we need to change "time_dims" in "Skill.R", merging "syear" with "sweek"
# This way, we make a larger sample made of the number of years of the hindcast ('syear') times the week window used ('sweek')
# at the moment, we see that in "Skill.R" there is "time_dim <- 'syear' "
# so we can do:
# hcst <- CST_MergeDims(data$hcst$data, c('syear', 'sweek'))
# obs <- CST_MergeDims(data$obs$data, c('syear', 'sweek'))

skill_metrics <- Skill(recipe, data)
# At this point, you will have data a data cube (it is seasonal data and we need to transform dates to a
# subseasonal case, at least), and the list of metrics computed in skill_metrics object.


# Dimensions at this stage:
# dim(skill_metrics$rpss)
#      var      time  latitude longitude 
#        1         3        22        53 

# Transform data to subseasonal - add dates in the Visualization module



#########################
###   PROBABILITIES   ###
#########################

#probabilities <- Probabilities(recipe, data)

# Issue with probabilities:
# 1: ! Warning: Detect gap in 'lon' vector, which is considered as crossing the border. 

#########################
###   VISUALIZATION   ###
#########################
Visualization(recipe = recipe, data = test_hcst, skill_metrics = skill_metrics,
               significance = TRUE)



