sunset_prov_dataset<-function(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

#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 = TRUE )

#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)
} else{graph_fcst=NULL}

#hcst
if(!is.null(data$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 = TRUE )

#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)
}else{graph_hcst=NULL}

#obs
if(!is.null(data$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 = TRUE)

#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)
} else{graph_obs=NULL}


##REGRIDDING OPERATION!!
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 = "transformeR", #We will need to modify the code to change this!!!
                                graph = graph_obs,
                                 InterpolationMethod = recipe$Analysis$Regrid$method,
                                 fun = "Loading.R",
                                 RefSpatialExtent = refSp,
                                 arg.list = "Loading(recipe)",
                                 disable.command = TRUE)


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

         graph_fcst <-metaclipR.Interpolation_SUNSET(package = "transformeR", 
                                 graph = graph_fcst,
                                 InterpolationMethod = recipe$Analysis$Regrid$method,
                                 fun = "Loading.R",
                                 RefSpatialExtent = refSp,
                                 arg.list = "Loading(recipe)",
                                 disable.command = TRUE)                         

#}else if(recipe$Analysis$Regrid$type =="r360x180"){ #Further research, but should obtain the grid and simply load it into metaclipR.SpatialExtent!!!
                            
}

#Final ensemble of the dataset graphs + added command call. It contais an igraph object in graph$graph and the name o fall the datasetsubset nodes in graph$parentnodename[i]
graph<-combination_graph(graph_fcst, graph_hcst, graph_obs, append.parentnodename)

#graph<-metaclip.graph.Command(graph1$graph, "Loading", "2.0.0", "/modules/Loading/Loading.R", arg.list=list("Recipe, path"="recipe_file"), 
#                     list( Dataset.name_hcst, Dataset.name_obs))






return(graph)
}