library(metaclipR)
library(yaml)
library(igraph)

recipe<-read_yaml("/esarchive/scratch/apuiggro/sunset/recipes/atomic_recipes/recipe_decadal_provenance.yml")


#Create empty graph
graph <- make_empty_graph(directed = TRUE)


#Dataset definitions
Dataset.nodename = recipe$Analysis$Datasets$System$name
DataProvider = recipe$Analysis$Dataset$Reference$name
Dataset.subclass = "SeasonalHindcast" #Should be defined accordingly to the loaded data
Project = recipe$Analysis$Datasets$Reference$name
ModellingCenter = recipe$Analysis$Dataset$Reference$name #Extract from the loaded data itself!!!


#Add dataset node
Dataset.name<-paste0("ds:", Dataset.subclass)

graph <- add_vertices(graph,
                      nv = 1,
                      name = Dataset.nodename,
                      label = "SUNSET Sample Seasonal Dataset",
                      className = Dataset.name)


#Data Provider Node
DataProvider.nodename<-paste0("ds:",DataProvider)

graph <- my_add_vertices(graph,
                         nv=1,
                         name = DataProvider.nodename,
                         label = DataProvider,
                         className = "ds:DataProvider"
)

graph <- add_edges(graph, 
                   c(getNodeIndexbyName(graph, Dataset.nodename),
                     getNodeIndexbyName(graph, DataProvider.nodename)),
                   label = "ds:hadDataProvider")



#Add Modelling Center node

ModellingCenter.nodename<-paste0("ds:",ModellingCenter)

graph <- my_add_vertices(graph,
                         nv = 1,
                         name=ModellingCenter.nodename,
                         label = ModellingCenter,
                         className = "ds:ModellingCenter")

graph <- add_edges(graph, 
                   c(getNodeIndexbyName(graph, Dataset.nodename),
                     getNodeIndexbyName(graph, ModellingCenter.nodename)),
                   label = "ds:hadModellingCenter")

#Have to fix the fact that DataProvider and Modelling Center are the same? Can the intepreter read it without doubling the node?


#Add Project Node
Project.nodename<-paste0("Project.", randomName())
graph <- my_add_vertices(graph,
                         nv = 1,
                         name = Project.nodename,
                         label = Project,
                         className = "ds:Project")
graph <- add_edges(graph, 
                   c(getNodeIndexbyName(graph, Dataset.nodename),
                     getNodeIndexbyName(graph, Project.nodename)),
                   label = "ds:hadProject")

#DATASETSUBCLASS

#Ad Datasubset node
DatasetSubset.nodename <- paste0("DatasetSubset.", randomName())
graph <- add_vertices(graph,
                      nv = 1,
                      name = DatasetSubset.nodename,
                      label = "DatasetSubset",
                      className = "ds:DatasetSubset")

graph <- add_edges(graph, 
                   c(getNodeIndexbyName(graph, Dataset.nodename),
                     getNodeIndexbyName(graph, DatasetSubset.nodename)),
                   label = paste0("ds:hadDatasetSubset"))    

#Get Units, Variable names, grid values...
var.name<-"Temperature"
var.nodename <- paste("Variable", randomName(), sep = ".")
graph <- add_vertices(graph,
                      nv = 1,
                      name = var.nodename,
                      label = var.name,
                      className = "ds:Variable",
                      description = "Encode metadata of a Variable",
                      attr = list("ds:withUnits" = "Temperature",
                                  "ds:hasShortName" = "tas"
                                  ))
graph <- add_edges(graph, 
                   c(getNodeIndexbyName(graph, DatasetSubset.nodename),
                     getNodeIndexbyName(graph, var.nodename)),
                     label = "ds:hasVariable")

#Frequency
var.name<-"Frequency"
var.nodename <- paste("Variable2", randomName(), sep = ".")
graph <- add_vertices(graph,
                      nv = 1,
                      name = var.nodename,
                      label = var.name,
                      className = "ds:Variable",
                      description = "Encode metadata of a Variable",
                      attr = list("ds:withUnits" = "Months",
                                  "ds:hasShortName" = "months"
                      ))
graph <- add_edges(graph, 
                   c(getNodeIndexbyName(graph,DatasetSubset.nodename),
                     getNodeIndexbyName(graph, var.nodename)),
                   label = "ds:hasVariable")

#Temporal resolution

timeres.nodename <- paste0("TemporalResolution", randomName(), sep = ".")
cell.method<- 
graph <- add_vertices(graph,
                      nv = 1,
                      name = timeres.nodename,
                      label = "TemporalResolution",
                      className = "ds:TemporalResolution",
                      description = "Encode metadata of temporal resolution",
                      attr = list("ds:hasTimeStep" = "month",
		      	          "ds:hasCellMethod"=recipe$Analysis$Variables$freq
                                  ))
graph <- add_edges(graph, 
                   c(getNodeIndexbyName(graph, DatasetSubset.nodename),
                     getNodeIndexbyName(graph, timeres.nodename)),
                   label = "ds:hasTemporalResolution")

#Temporal Period
timeper.nodename<-paste0("TemporalPeriod", randomName(), sep = ".")
graph <- add_vertices(graph,
                      nv = 1,
                      name = timeper.nodename,
                      label = "TemporalPeriod",
                      className = "ds:TemporalPeriod",
                      description = "Encode metadata of temporal resolution",
                      attr =list("prov:startedAtTime"=paste0(recipe$Analysis$Time$hcst_start,"-12-0100:00:00GMT"),
                                 "prov:endedAtTime"=paste0(recipe$Analysis$Time$hcst_end,"-12-0100:00:00GMT")))
                      
graph <- add_edges(graph, 
                   c(getNodeIndexbyName(graph, DatasetSubset.nodename),
                     getNodeIndexbyName(graph, timeper.nodename)),
                   label = "ds:hasTemporalPeriod")



metadata<- list("graph"=graph, "parentnodename"=Dataset.nodename)

graph2json(metadata$graph, "/esarchive/scratch/apuiggro/sunset/graph22.json")
plot(graph)