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")
source("modules/Indices/Indices.R")

source("tools/s2dv_cube2Climate4R.R")
source("SUNSET_PROVENANCE/sunset_prov_dataset.R")
source("SUNSET_PROVENANCE/combination_graph_dataset.R")
source("metaclipR_SUNSET/metaclipR.Interpolation_SUNSET.R")
source("SUNSET_PROVENANCE/sunset_prov_calibration.R")
source("SUNSET_PROVENANCE/sunset_prov_anomalies.R")
source("SUNSET_PROVENANCE/sunset_prov_indices.R")
source("SUNSET_PROVENANCE/sunset_prov_verification.R")
source("SUNSET_PROVENANCE/sunset_prov_downscale.R")
source("SUNSET_PROVENANCE/SkillScore-helpers.R")
library(yaml)
library(metaclipR)
library(igraph)

#SUNSET loading module execution
recipe_file <- "recipes/atomic_recipes/recipe_seasonal_provenance.yml"

        #"recipes/examples/NAO_recipe.yml"
        #"recipes/atomic_recipes/recipe_seasonal_provenance.yml"
        #"recipes/atomic_recipes/recipe_system7c3s-tas.yml"
        
recipe <- prepare_outputs(recipe_file)
data <- Loading(recipe)
data <- Units(recipe, data)

#Dataset and DatasetSubset provenance generation + Interpolation operations!!!
graph_dataset<-sunset_prov_dataset(recipe,data)
#If there is fcst:
#graph$parentnodename==> list(fcst_node, hcst_node, obs_node)

#If there is no fcst: 
#graph$parentnodename==> list(hcst_node, obs_node)

#Also, the igraph ensemble is located:
#graph$graph

#graph2json(graph_dataset$main_graph$graph, "/esarchive/scratch/apuiggro/sunset/graph333.json")

calibrated_data <- Calibration(recipe, data)

graph_calibration<-sunset_prov_calibration(recipe, calibrated_data, graph_dataset)

anomalies_data<-Anomalies(recipe,calibrated_data)

graph_anomalies <- sunset_prov_anomalies(recipe, anomalies_data, graph_calibration)

graph_downscaling<- sunset_prov_downscale(recipe, graph_anomalies)

#nao_indices <- Indices(data = calibrated_data, recipe = recipe)

#graph_indices <- sunset_prov_indices(recipe, nao_indices, graph_calibration, graph_downscaling) 

graph<-sunset_prov_verification(recipe, calibrated_data, graph_calibration, graph_downscaling)

#(recipe, data, requested_graph, main_graph)

#Conversion to JSON file to run the interpreter!
graph2json(graph$main_graph$graph, "/esarchive/scratch/apuiggro/sunset/graph333333.json")

