source("provenance/R/prov_calibration.R")

prov_Calibration <- function(recipe, graph) {

  #Check if Calibration is requested
  if (recipe$Analysis$Workflow$Calibration$method != "raw") {
    #fcst definition (Dataset+Datasubset)
    if (!is.null(data$fcst)) {
      graph_fcst <- prov_calibration(recipe, data, graph, type = "fcst")
    }
    #hcst definition (Dataset+Datasubset)
    graph_hcst <- prov_calibration(recipe, data, graph, type = "hcst")

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

    #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 = "Calibration",
                                  version = "2.0.0",
                                  fun = "modules/Calibration/Calibration.R",
                                  arg.list =
                                    list(
                                         "Recipe",
                                         "Data"),
                                  origin.node.name =
                                    list(
                                        graph_fcst$parentnodename,
                                        graph_hcst$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 = "Calibration",
                                  version = "2.0.0",
                                  fun = "modules/Calibration/Calibration.R",
                                  arg.list =
                                    list(
                                         "Recipe",
                                         "Data"),
                                  origin.node.name =
                                    list(graph_hcst$parentnodename))

      graph <- list("graph" = graph, 
                    "parentnodename" =
                      list("parentnodename_hcst" = graph_hcst$parentnodename,
                           "parentnodename_obs" = graph_obs$parentnodename))
    }
    return(graph)
  }
}
