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 <- "/esarchive/scratch/ptrascas/R/dev-test_bigpredidata/sunset/sunset/recipes/recipe_bigpredidata_oper_test_sfcWind.yml"
#recipe_file <- "/esarchive/scratch/ptrascas/R/dev-test_bigpredidata/sunset/sunset/recipes/recipe_bigpredidata_oper.yml"
recipe <- read_atomic_recipe(recipe_file)

if (recipe$Analysis$Region$name == "EU"){
	if (recipe$Analysis$Horizon == "subseasonal"){
  recipe$Analysis$Regrid$type <- "/gpfs/projects/bsc32/esarchive_cache/exp/ncep/cfs-v2/weekly_mean/s2s/tas_f24h/tas_20050109.nc"
} else if (recipe$Analysis$Horizon == "seasonal"){
  recipe$Analysis$Regrid$type <- "/gpfs/projects/bsc32/esarchive_cache/exp/ecmwf/system51c3s/monthly_mean/tasmax_f24h/tasmax_20250701.nc"
}
}


#recipe <- prepare_outputs(recipe_file)
# Load datasets
data <- Loading(recipe)
data <- Units(recipe, data)

source("modules/Crossval/Crossval_calibration.R")
data <- Crossval_calibration(recipe = recipe, data = data)

# Save calibrated hindcast, forecast and probabilities

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

Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              significance = TRUE, probabilities = data$probs)

