Prov.Reggriding<-function(recipe,data,graph){
  #Extract the type from previous nodename
  parent.node<-graph$parentnodename
  graph<-graph$graph
  type <- strsplit(parent.node,"_")[[1]][[2]]
  regtype<- recipe$Analysis$Regrid$method
  regtypeclass <- switch(regtype,
                                "nn" = "ds:NearestNeighbor",
                                "bilinear" = "ds:BilinearInterpolation",
                                "bicubic" = "ds:BicubicInterpolation",
                                "distance-weighted" = "ds:InverseDistanceWeighting",
                                "laf"= "ds:InterpolationMethod",
                                "conservative" = "ds:ConservativeRemapping")
  regtype <- switch(regtype,
                "con2" = "Conservative Remapping",
                "bilinear"="Bilinear",
                "bicubic"="Bicubic",
                "distance-weighted"="Distance Weighting",
                "laf"="Spline",
                "nn"="Nearest Neighbor"
                 )

  if(recipe$Analysis$Regrid$type =="to_system"){
        refgraph<-getSpatialExtent(data,"hcst",Reg=TRUE)
        refgraphnode<-refgraph$parentnodename
        refgraph<-refgraph$graph
        #Interpolation definition
        if(type=="obs"){
          regnodename <- paste0("Interpolation_",type)
          graph <- add_vertices(graph,
                        nv = 1,
                        label = paste(regtype, "interpolation"),
                        name = regnodename,
                        className = "ds:Interpolation",
                        attr = list("dc:description" = "Interpolation to system, either to forecast or hindcast grid."))
          graph <- add_edges(graph,
                     c(getNodeIndexbyName(graph, paste0("DatasetSubset_",type)),
                       getNodeIndexbyName(graph, regnodename)),
                     label = "ds:hadInterpolation")
          regmethod.nodename <- paste0("InterpolationMethod_",type)
          graph <- add_vertices(graph,
                        nv = 1,
                        name = regmethod.nodename,
                        label = regtype,
                        className = regtypeclass)
          graph <- add_edges(graph,
                     c(getNodeIndexbyName(graph, regnodename),
                       getNodeIndexbyName(graph, regmethod.nodename)),
                     label = "ds:hadInterpolationMethod")
          #Reference coordinates
          graph<-my_union_graph(graph,refgraph)
          graph <- add_edges(graph,
                             c(getNodeIndexbyName(graph, regnodename),
                               getNodeIndexbyName(graph, refgraphnode)),
                             label = "ds:usedReferenceCoordinates")
        }
     
  }else if(recipe$Analysis$Regrid$type =="to_reference"){
        refgraph<-getSpatialExtent(data,"obs",Reg=TRUE)
        refgraphnode<-refgraph$parentnodename
        refgraph<-refgraph$graph
        #Interpolation definition
        if(type=="hcst" | type=="fcst"){
          regnodename <- paste0("Interpolation_",type)
          graph <- add_vertices(graph,
                        nv = 1,
                        label = paste(regtype, "interpolation"),
                        name = regnodename,
                        className = "ds:Interpolation",
                        attr = list("dc:description" = "Interpolation to referenc, to the observations grid."))
          graph <- add_edges(graph,
                     c(getNodeIndexbyName(graph, paste0("DatasetSubset_",type)),
                       getNodeIndexbyName(graph, regnodename)),
                     label = "ds:hadInterpolation")
          regmethod.nodename <- paste0("InterpolationMethod_",type)
          graph <- add_vertices(graph,
                        nv = 1,
                        name = regmethod.nodename,
                        label = regtype,
                        className = regtypeclass)
          graph <- add_edges(graph,
                     c(getNodeIndexbyName(graph, regnodename),
                       getNodeIndexbyName(graph, regmethod.nodename)),
                     label = "ds:hadInterpolationMethod")
          #Reference coordinates
          graph<-my_union_graph(graph,refgraph)
          graph <- add_edges(graph,
                             c(getNodeIndexbyName(graph, regnodename),
                               getNodeIndexbyName(graph, refgraphnode)),
                             label = "ds:usedReferenceCoordinates")
        }
      

  }else if (recipe$Analysis$Regrid$type =="none"){
         print("No interpolation done when loading data")
  
  }else { 
        refgraph<-getSpatialExtent(data,"obs",Reg=TRUE)
        refgraphnode<-refgraph$parentnodename
        refgraph<-refgraph$graph
        #Interpolation definition
        if(type=="hcst" | type=="fcst" | type=="obs"){
          regnodename <- paste0("Interpolation_",type)
          graph <- add_vertices(graph,
                        nv = 1,
                        label = paste(regtype, "interpolation"),
                        name = regnodename,
                        className = "ds:Interpolation",
                        attr = list("dc:description" = paste("Interpolation to a third grid defined as:",recipe$Analysis$Regrid$type)))
          graph <- add_edges(graph,
                     c(getNodeIndexbyName(graph, paste0("DatasetSubset_",type)),
                       getNodeIndexbyName(graph, regnodename)),
                     label = "ds:hadInterpolation")
          regmethod.nodename <- paste0("InterpolationMethod_","all")
          graph <- add_vertices(graph,
                        nv = 1,
                        name = regmethod.nodename,
                        label = regtype,
                        className = regtypeclass)
          graph <- add_edges(graph,
                     c(getNodeIndexbyName(graph, regnodename),
                       getNodeIndexbyName(graph, regmethod.nodename)),
                     label = "ds:hadInterpolationMethod")
          #Reference coordinates
          graph<-my_union_graph(graph,refgraph)
          graph <- add_edges(graph,
                             c(getNodeIndexbyName(graph, regnodename),
                               getNodeIndexbyName(graph, refgraphnode)),
                             label = "ds:usedReferenceCoordinates")
        }
    
    print(paste0("Dataset provenance computed for ", type))
    return(list("graph"=graph,"parentnodename"=regnodename))
  }
}

        