
source("modules/Loading/Loading.R")
source("modules/Units/Units.R")
source("s2dvcube_to_netML.R")
source("conf/archive.yml")
source("conf/archive_decadal.yml")
library(yaml)
library(metaclipR)
library(ClimProjDiags)

#SUNSET loading module execution
recipe_file <- "recipes/atomic_recipes/recipe_seasonal_provenance.yml"
recipe <- prepare_outputs(recipe_file)
data <- Loading(recipe)

Horizon<-tolower(recipe$Analysis$Horizon) #Either seasonal or decadal
System.name<- recipe$Analysis$Datasets$System$name #System name
Reference.name <- recipe$Analysis$Datasets$Reference$name #Reference name

#Seasonal dataset setting
if (Horizon=="seasonal") {
  archive<-read_yaml("conf/archive.yml")
if(System.name %in% names(archive$esarchive$System) & (Reference.name %in% names(archive$esarchive$Reference))) {
  archive_system<- archive$esarchive$System[[System.name]]
  archive_reference<- archive$esarchive$Reference[[Reference.name]]
}
ifelse(System.name %in% names(archive$mars$System)){
  archive_system<- archive$mars$System[[System.name]]
  archive_reference<- archive$mars$Reference[[Reference.name]]
}
else{
  print("The System or Reference datasource is not registered")
}
}

#Decadal dataset setting
if (Horizon=="decadal") {
archive_system<-read_yaml("conf/archive_decadal.yml")
if(System.name %in% names(archive$esarchive$System)) {
  archive_system<- archive$esarchive$System[[System.name]]
}
else{
  print("The System datasource is not registered")
  print("The Reference datasource is not registered")
}
}


#The first provenance function would start here!!!
climate4R_object<- s2dvcube_to_netML(data$hcst, time_dim = "time", sdate_dim = "syear", 
                               lat_dim = "latitude", lon_dim = "longitude" )



#We start the graph
graph<-make_empty_graph(directed=TRUE)

# Dataset node ------------------
graph <- my_add_vertices(graph,
                        nv = 1,
                        name = ,
                        label = Dataset.name,
                        className = paste0("ds:", "Dataset")) #Need to define it according to metaclip ontology!!!!



 graph<- metaclipR.DatasetSubset(package = "Loading",
  version = "1.0", graph = graph_dataset,
  fun = "/modules/Loading/Loading.R", arg.list = list("recipe"=recipe), output = climate4R_object,
  disable.command = FALSE)


graph_reference <- metaclipR.Dataset( Dataset.name = recipe$Analysis$Datasets$Reference$name,
  DataProvider = recipe$Analysis$Datasets$Reference$name,
  # DataProvider.URL = url,
  Dataset.subclass = "ObservationalDataset",  # Right?
  Project = "metaclipR test" # ?
  #ModellingCenter = "ECMWF"
)

graph_training <- metaclipR.Dataset(
  Dataset.name = "Training Dataset",
  DataProvider = recipe$Analysis$Datasets$Reference$names,
  # DataProvider.URL = url,
  Dataset.subclass = "SeasonalHindcast",  # Right?
  Project = "metaclipR test" # ?
  #ModellingCenter = "ECMWF"
)

#graph_union<- list("graph"= my_union_graph(graph_dataset$graph, graph_reference$graph), "parentnodename"= graph_dataset$parentnodename)

graph<- metaclipR.BiasCorrection(package = "Calibration", version = "1.0",
  fun = "/modules/Calibration/Calibration.R", arg.list=list(), graph=graph, TrainingGraph=graph_training, 
  ReferenceGraph=graph_reference,
  ReferenceGraphSpatialExtent = graph_SpatialExtent, BC.method= "QDM",
  BC.class = "BiasCorrection")

plot(graph$graph)
graph2json(graph$graph, "/esarchive/scratch/apuiggro/sunset/graph333.json")



