source("modules/Loading/Loading.R")
source("modules/Units/Units.R")
source("s2dvcube_to_netML.R")
library(metaclipR)
library(ClimProjDiags)
recipe_file <- "use_cases/ex1_1_single_analysis_terminal/ex1_1-recipe.yml"
recipe <- prepare_outputs(recipe_file)
data <- Loading(recipe)


climate4R_object<- s2dvcube_to_netML(data$hcst, time_dim = "time", sdate_dim = "syear", 
                               lat_dim = "latitude", lon_dim = "longitude" )

#Extract Coordinates from s2dv_cube to make them suitable to run metaclipR.SpatialExtent()!!
xyCoords<-list("x","y")
xyCoords$x<-data$hcst$coords$latitude [1:22]
xyCoords$y<-data$hcst$coords$longitude [1:53]

#Generate SpatialExtent graph for Calibration step
graph_SpatialExtent<-metaclipR.SpatialExtent(xyCoords=xyCoords)

 #dataset.subset <- subsetGrid(grid = climate4R_object,
 #                            members = 1:3, 
 #                            season = NULL,
 #                            years = 1991:1995, 
 #                            lonLim = c(-5,3), 
 #                           latLim = c(37,43),
 #                           drop = FALSE)



graph<- metaclipR.DatasetSubset(package = "transformeR",
version = as.character(packageVersion(package)), graph = graph_dataset,
fun = "metaclipR.SpatialExtent", arg.list = list("xyCoords"=xyCoords), output = NULL,
RefSpatialExtent = graph_SpatialExtent, xmin = NULL, xmax = NULL, ymin = NULL,
ymax = NULL, proj = NULL, resX = NULL, resY = NULL,
disable.command = FALSE)

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