prov_Aggregation <- function(recipe, data) {

  source("provenance/R/prov_aggregation.R")
  graph <- data$graph.prov
  
  #hcst definition (Dataset+Datasubset)
  graph_hcst <- prov_aggregation(recipe, data, graph, type = "hcst")

  #obs definition (Dataset+Datasubset)
  graph_obs <- prov_aggregation(recipe, data, graph, type = "obs")

  #fcst definition (Dataset+Datasubset)
  if (!is.null(data$fcst)) {
    graph_fcst <- prov_aggregation(recipe, data, graph, type = "fcst")
  }
  #Command Call and output definition
  if (!is.null(data$fcst)) {
    graph <- my_union_graph(graph_fcst$graph, graph_hcst$graph, graph_obs$graph)
    graph <- prov_Command(graph, package = "Aggregation",
                                version = "2.0.0",
                                fun = "modules/Aggregation/Aggregation.R",
                                arg.list =
                                  list("Recipe",
                                       "Data"),
                                origin.node.name =
                                  c(graph_fcst$parentnodename,
                                    graph_hcst$parentnodename,
                                    graph_obs$parentnodename))
    graph <- list("graph" = graph,
                  "parentnodename"
                  = list("parentnodename_fcst"
                         = graph_fcst$parentnodename,
                         "parentnodename_hcst"
                         = graph_hcst$parentnodename,
                         "parentnodename_obs"
                         = graph_obs$parentnodename))
  } else {
    graph <- my_union_graph(graph_hcst$graph, graph_obs$graph)
    graph <- prov_Command(graph, package = "Aggregation",
                                version = "2.0.0",
                                fun = "modules/Aggregation/Aggregation.R",
                                arg.list =
                                  list(
                                       "Recipe",
                                       "Data"),
                                origin.node.name =
                                  list(graph_hcst$parentnodename,
                                       graph_obs$parentnodename))
    graph <- list("graph" = graph,
                  "parentnodename"
                  = list("parentnodename_hcst"=graph_hcst$parentnodename,
                         "parentnodename_obs"=graph_obs$parentnodename))
  }

  message("INFO  ", "[", Sys.time(), "]",
          " Provenance correctly generated for the Aggregation module")

  #Name of the generated JSON according to recipe values
  main_dir <- paste0(recipe$Run$output_dir, "/outputs", "/provenance/")
  recipe_model <- paste0("sys-",
                         gsub('\\.', '', recipe$Analysis$Datasets$System$name))
  recipe_split <- paste0("_ref-", recipe$Analysis$Datasets$Reference$name,
                         "_var-", recipe$Analysis$Variables$name,
                         "_reg-", recipe$Analysis$Region$name,
                         "_sdate-", recipe$Analysis$Time$sdate)

  json_name <- paste0("main_provenance", "_", recipe_model, recipe_split)

  #Convert from igraph to JSON format and store in the output directory
  graph2json(graph$graph, output.file = paste0(main_dir,
                                               json_name,  ".json"))

  return(graph)
 
}