prov_Units <- function(recipe, data, orig_units) {

  #NEW UNITS (Recipe)
  vars <- recipe$Analysis$Variables$name
  user_units <- recipe$Analysis$Variable$units
  var.freq <- recipe$Analysis$Variables$freq 

  #Considering the case of more the one variable being loaded simulateonsly
  vars_split <- strsplit(vars, ", ")[[1]]

  graph <- data$graph.prov
  parentnodenames <- graph$parentnodename
  orig <- parentnodenames
  graph <- graph$graph
  
  if (!is.null(data$fcst)){
    aux_vec <- list("fcst", "hcst", "obs")
  } else{
    aux_vec <- list("hcst", "obs")
  }
  
  j <- 1
  for (type in aux_vec){
    description <- ""
    for (i in seq_along(user_units)) {
      var <- vars_split[i]
      unit <- user_units[i][[var]]
      description <- paste0(description, "   -", var, ": From ", orig_units[[j]][i], " to ", unit, ".  ")
    }

    unitconversion.nodename <- paste0("UnitConversion_", type)

    parentnodenames[[j]] <- unitconversion.nodename

    graph <- add_vertices_sunset(graph,
                                  nv = 1,
                                  name = unitconversion.nodename,
                                  label = "Unit conversion",
                                  className = "ds:UnitConversion",
                                  attr = list(
                                    "dc:description" = description
                                  )
                                  )

    graph <- add_edges(graph,
                    c(getNodeIndexbyName(graph, orig[[j]]),
                        getNodeIndexbyName(graph, unitconversion.nodename)),
                      label = "ds:hadDerivation")
    j <- j+1
  }

  #Command Call and output definition
  if (!is.null(data$fcst)) {
    graph <- prov_Command(graph, package = "Units",
                                version = "2.0.0",
                                fun = "modules/Units/Units.R",
                                arg.list =
                                  list("Recipe",
                                       "Data"),
                                origin.node.name =
                                  c(parentnodenames[[1]],
                                    parentnodenames[[2]],
                                    parentnodenames[[3]]))
    graph <- list("graph" = graph,
                  "parentnodename"
                  = list("parentnodename_fcst"
                         = parentnodenames[[1]],
                         "parentnodename_hcst"
                         = parentnodenames[[2]],
                         "parentnodename_obs"
                         = parentnodenames[[3]]))
  } else {
    graph <- prov_Command(graph, package = "Units",
                                version = "2.0.0",
                                fun = "modules/Units/Units.R",
                                arg.list =
                                  list(
                                       "Recipe",
                                       "Data"),
                                origin.node.name =
                                  list(parentnodenames[[1]],
                                       parentnodenames[[2]]))
    graph <- list("graph" = graph,
                  "parentnodename"
                  = list("parentnodename_hcst"=parentnodenames[[1]],
                         "parentnodename_obs"=parentnodenames[[2]]))
  }

  message("INFO  ", "[", Sys.time(), "]",
          " Provenance correctly generated for the Units 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)

}
