getSpatialExtent<-function(data, type, Reg=FALSE, Cal=FALSE){

  graph<-make_empty_graph(0)
  ymax<-as.numeric(data[[type]]$coords$lat)[[length(as.numeric(data[[type]]$coords$lat))]]
  ymin<-as.numeric(data[[type]]$coords$lat)[[1]]
  xmax<-as.numeric(data[[type]]$coords$lon)[[length(as.numeric(data[[type]]$coords$lon))]]
  xmin<-as.numeric(data[[type]]$coords$lon)[[1]]
  resY<-abs(as.numeric(data[[type]]$coords$lon)[[2]]-as.numeric(data[[type]]$coords$lon)[[1]])
  yunits<-attr(data[[type]]$attr$Variable$metadata$longitude,"variables")$lon$units
  resX<-abs(as.numeric(data[[type]]$coords$lat)[[2]]-as.numeric(data[[type]]$coords$lat)[[1]])
  xunits<-attr(data[[type]]$attr$Variable$metadata$latitude,"variables")$lat$units



  if(Reg){
    spatextent.nodename <- paste0("SpatialExtent_","Reggriding")
  }else if(Cal){
    spatextent.nodename <- paste0("SpatialExtent_","Calibration",type)
  }else{
    spatextent.nodename <- paste0("SpatialExtent_","DatasetSubset_",type)
  }
  
  graph<- add_vertices(graph,
                        nv = 1,
                        name = spatextent.nodename,
                        label = "SpatialExtent",
                        className = "ds:HorizontalExtent",
                        attr = list("ds:xmin" = xmin,
                                    "ds:xmax" = xmax,
                                    "ds:ymin" = ymin,
                                    "ds:ymax" = ymax,
                                    "ds:hasHorizontalResX" = resX,
                                    "ds:hasHorizontalResY" = resY,
                                    "ds:hasProjection:"=NULL,
                                    "ds:withUnits"=paste0("X:",xunits,"Y:", yunits)
                                    #"ds:hasProjection" = proj,
                                    )
                       )
  
  return(list("graph"=graph, "parentnodename"=spatextent.nodename))
}
            