sunset_prov_dataset<-function(recipe,data){
library(transformeR)
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

#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]]
  }

  else if(System.name %in% names(archive$mars$System)) {
    archive_system<- archive$mars$System[[System.name]]
    archive_reference<- archive$mars$Reference[[Reference.name]]
  }

} else if (Horizon=="decadal") {
  archive<-read_yaml("conf/archive_decadal.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]]
}
else{
  print("The System or Reference datasource is not registered")
}
}

#fcst
if(!is.null(data$fcst)){
#Dataset graph
graph_fcst<- metaclipR.Dataset( Dataset.name = archive_system$name,
  DataProvider = archive_system$institution,
  # DataProvider.URL = url,
  Dataset.subclass = "SeasonalOperationalForecast",  # Right?
  #Project = "metaclipR test" # ?
  #ModellingCenter = "ECMWF"
)
Dataset.name_fcst<-graph_fcst$parentnodename
#Transformation to climate4R object
climate4R_object_fcst<- s2dv_cube2Climate4R(data$fcst, time_dim = "time", sdate_dim = "syear", 
                               lat_dim = "latitude", lon_dim = "longitude",
                               memb_dim = "ensemble", copy_data = FALSE )

#Definition of the datasetsubset node
graph_fcst<- metaclipR.DatasetSubset(package = "Loading",
                                version = "1.0", graph = graph_fcst,
                                fun = "/modules/Loading/Loading.R", arg.list = list("recipe"="recipe"), output = climate4R_object_fcst,
                                disable.command = TRUE)
#Add member realization
DatasetSubset.nodename_fcst<- graph_fcst$parentnodename
graph_fcstt<-graph_fcst$graph
for(i in 1:length(data$fcst$coords$ensemble)){
mem<-paste0("Member_",i)
mem.nodename<- paste0("Realization.", randomName())
graph_fcstt<- add_vertices(graph_fcstt,
                      nv = 1,
                      name = mem.nodename,
                      label = mem,
                      className = "ds:Realization",
                      description = "Encode metadata of Realizations",
                      attr = list("ds:hasMember" = mem))
graph_fcstt <- add_edges(graph_fcstt, 
                      c(getNodeIndexbyName(graph_fcstt, DatasetSubset.nodename_fcst),
                      getNodeIndexbyName(graph_fcstt, mem.nodename)),
                      label = "ds:hasRealization")
}
graph_fcst$graph<-graph_fcstt




} else{graph_fcst=NULL}



#hcst
#Dataset graph
graph_hcst<- metaclipR.Dataset( Dataset.name = archive_system$name,
  DataProvider = archive_system$institution,
  # DataProvider.URL = url,
  Dataset.subclass = "SeasonalHindcast",  # Right?
  #Project = "metaclipR test" # ?
  #ModellingCenter = "ECMWF"
)

Dataset.name_hcst<-graph_hcst$parentnodename

#Transformation to climate4R object
climate4R_object_hcst<- s2dv_cube2Climate4R(data$hcst, time_dim = "time", sdate_dim = "syear", 
                               lat_dim = "latitude", lon_dim = "longitude",
                               memb_dim = "ensemble", copy_data = FALSE )

#Definition of the datasetsubset node
graph_hcst<- metaclipR.DatasetSubset(package = "Loading",
                                version = "1.0", graph = graph_hcst,
                                fun = "/modules/Loading/Loading.R", arg.list = list("recipe"="recipe"), output = climate4R_object_hcst,
                                disable.command = TRUE)
#Add member realization
DatasetSubset.nodename_hcst<- graph_hcst$parentnodename
graph_hcstt<-graph_hcst$graph
for(i in 1:length(data$hcst$coords$ensemble)){
mem<-paste0("Member_",i)
mem.nodename<- paste0("Realization.", randomName())
graph_hcstt<- add_vertices(graph_hcstt,
                      nv = 1,
                      name = mem.nodename,
                      label = mem,
                      className = "ds:Realization",
                      description = "Encode metadata of Realizations",
                      attr = list("ds:hasMember" = mem))
graph_hcstt <- add_edges(graph_hcstt, 
                      c(getNodeIndexbyName(graph_hcstt, DatasetSubset.nodename_hcst),
                      getNodeIndexbyName(graph_hcstt, mem.nodename)),
                      label = "ds:hasRealization")
}
graph_hcst$graph<-graph_hcstt

#obs
#Dataset graph
graph_obs<- metaclipR.Dataset(Dataset.name = archive_reference$name,
  DataProvider = archive_reference$name,
  # DataProvider.URL = url,
  Dataset.subclass = "ObservationalDataset",  # Right?
  #Project = "metaclipR test" # ?
  #ModellingCenter = "ECMWF"
)

Dataset.name_obs<-graph_obs$parentnodename

#Transformation to climate4R object
climate4R_object_obs<- s2dv_cube2Climate4R(data$obs, time_dim = "time", sdate_dim = "syear", 
                               lat_dim = "latitude", lon_dim = "longitude",
                               memb_dim = "ensemble", copy_data = FALSE)

#Definition of the datasetsubset node
graph_obs<- metaclipR.DatasetSubset(package = "Loading",
                                version = "1.0", graph = graph_obs,
                                fun = "/modules/Loading/Loading.R", arg.list = list("recipe"="recipe"), output = climate4R_object_obs,
                                disable.command = TRUE)

##REGRIDDING OPERATION!!
type<- recipe$Analysis$Regrid$method
type <- switch(type,
                "con2" = "ConservativeRemapping",
                "bilinear"="bilinear",
                "bicubic"="bicubic",
                "distance-weighted"="IDW",
                "laf"="spline",
                "nn"="nearest"

                 )
if(recipe$Analysis$Regrid$type =="to_system"){
        append.parentnodename<-graph_obs$parentnodename
        refSp <- metaclipR.SpatialExtent(climate4R_object_hcst$xyCoords) #Node for reference grid
        graph_obs <- metaclipR.Interpolation_SUNSET(package = "Loading", #We will need to modify the code to change this!!!
                                 graph = graph_obs,
                                 InterpolationMethod = type,
                                 fun = "Loading.R",
                                 RefSpatialExtent = refSp,
                                 arg.list = list(type, "type"),
                                 disable.command = TRUE)
     


}else if(recipe$Analysis$Regrid$type =="to_reference"){
        refSp <- metaclipR.SpatialExtent(climate4R_object_obs$xyCoords) #Node for reference grid
        if(!is.null(graph_fcst)){
        append.parentnodename<-list(graph_fcst$parentnodename, graph_hcst$parentnodename) 
        graph_fcst <-metaclipR.Interpolation_SUNSET(package = "Loading", 
                                 graph = graph_fcst,
                                 InterpolationMethod = type,
                                 fun = "Loading.R",
                                 RefSpatialExtent = refSp,
                                 arg.list = list(type, "type"),
                                 disable.command = TRUE)      
        } else{append.parentnodename<-list(graph_hcst$parentnodename)  }
        graph_hcst <- metaclipR.Interpolation_SUNSET(package = "Loading", 
                                 graph = graph_hcst,
                                 InterpolationMethod = type,
                                 fun = "Loading.R",
                                 RefSpatialExtent = refSp,
                                 arg.list = list(type, "type"),
                                 disable.command = TRUE)
      

}else if (recipe$Analysis$Regrid$type =="none"){
         print("No interpolation done when loading data")
}else { 
        refSp <- metaclipR.SpatialExtent(climate4R_object_obs$xyCoords) #We could load the SpatialExtent with either fcst, hcst or obs. It's be the same
        if(!is.null(graph_fcst)){
          append.parentnodename<-list(graph_fcst$parentnodename, graph_hcst$parentnodename, graph_obs$parentnodename)  
          graph_fcst <-metaclipR.Interpolation_SUNSET(package = "Loading", 
                                 graph = graph_fcst,
                                 InterpolationMethod = type,
                                 fun = "Loading.R",
                                 RefSpatialExtent = refSp,
                                 arg.list = list(type, "type"),
                                 disable.command = TRUE,
                                 dc.description=list("grid_type"=recipe$Analysis$Regrid$type))
        } else {
          append.parentnodename<-list(graph_hcst$parentnodename, graph_obs$parentnodename)
          }
        graph_hcst <- metaclipR.Interpolation_SUNSET(package = "Loading", 
                                 graph = graph_hcst,
                                 InterpolationMethod = type,
                                 fun = "Loading.R",
                                 RefSpatialExtent = refSp,
                                 arg.list = list(type, "type"),
                                 disable.command = TRUE,
                                 dc.description=list("grid_type"=recipe$Analysis$Regrid$type))
        graph_obs <- metaclipR.Interpolation_SUNSET(package = "Loading", #We will need to modify the code to change this!!!
                                 graph = graph_obs,
                                 InterpolationMethod = type,
                                 fun = "Loading.R",
                                 RefSpatialExtent = refSp,
                                 arg.list = list(type, "type"),
                                 disable.command = TRUE,
                                 dc.description=list("grid_type"=recipe$Analysis$Regrid$type))                                                   
}
#Final ensemble of the dataset graphs + added command call. It contais an igraph object in graph$graph and the name of all the datasetsubset nodes after reggriding operation in graph$parentnodename[i]
graph<-combination_graph_dataset(graph_fcst, graph_hcst, graph_obs, append.parentnodename)

if(!is.null(graph_fcst)){
return(list("main_graph"=graph, "graph_fcst"=graph_fcst, "graph_hcst"=graph_hcst, "graph_obs"=graph_obs))
}else {
return(list("main_graph"=graph, "graph_hcst"=graph_hcst, "graph_obs"=graph_obs))
}


}


