-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.R
117 lines (85 loc) · 3.33 KB
/
demo.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
library(roxygen2)
roxygenise(".")
library(devtools)
dev_mode()
install(".")
library(neonTerms)
options(stringsAsFactors = F)
createDB("test.sqlite")
db <- "test.sqlite"
options(termDB = "test.sqlite")
dpd <- read.csv("testdata/dpd.csv")
space <- read.csv("testdata/spatial.csv")
temporal <- read.csv("testdata/temporal.csv")
datapub <- read.csv("testdata/mam_datain_test.csv")
genIS <- read.csv("testdata/genericIS_L1.csv")
## Add rev numbers
dpd$rev <- 1
## Ok now let's just add the tables
addDPD(dpd)
addTempRes(temporal)
addSpRes(space)
inputDataPub(datapub,db)
inputDataPub(genIS,db)
## I input a generic table, so we can make extra linkages by adding a new entry to the table description table
toAdd <- data.frame(c("NEON.DOM.SIT.DP1.00002","NEON.DOM.SIT.DP1.00003"))
toAdd <- cbind(c(3,3),toAdd)
colnames(toAdd) <- c("tableID","dpID")
addTDPL(toAdd)
drv <- dbDriver("SQLite")
dbC <- dbConnect(drv, dbname = db)
## Get all tables associated with a dataProduct ID
q <- "SELECT TableDPLink.dpID, TableDefinition.termID, TableDefinition.tableID, TermDefinition.termName,TermDefinition.termDefinition FROM TableDPLink
INNER JOIN TableDefinition
ON TableDPLink.dpID = 'NEON.DOM.SIT.DP1.00003' AND TableDPLink.tableID = TableDefinition.tableID
INNER JOIN TermDefinition
ON TermDefinition.termID = TableDefinition.termID
"
out <- dbGetQuery(conn = dbC, q)
## Try it again on smammal
q <- "SELECT TableDPLink.dpID, TableDefinition.termID, TableDefinition.tableID, TermDefinition.termName FROM TableDPLink
INNER JOIN TableDefinition
ON TableDPLink.dpID = 'NEON.DOM.SIT.DP0.10001' AND TableDPLink.tableID = TableDefinition.tableID
INNER JOIN TermDefinition
ON TermDefinition.termID = TableDefinition.termID
"
out <- dbGetQuery(conn = dbC, q)
q <- "SELECT TableDPLink.dpID, TableDefinition.termID, TableDefinition.tableID, TermDefinition.termName,
SpatialResolution.spatialRes,SpatialResolution.spatialDesc,TemporalResolution.timeRes,TemporalResolution.timeDesc
FROM TableDPLink
INNER JOIN TableDefinition
ON TableDPLink.dpID = 'NEON.DOM.SIT.DP0.10001' AND TableDPLink.tableID = TableDefinition.tableID
INNER JOIN TermDefinition
ON TermDefinition.termID = TableDefinition.termID
INNER JOIN SpatialResolution
ON TableDPLink.dpID = SpatialResolution.dpID
INNER JOIN TemporalResolution
ON TableDPLink.dpID = TemporalResolution.dpID
ORDER BY TableDefinition.tableID,SpatialResolution.spatialRes,TemporalResolution.timeRes,TableDefinition.termID"
out <- dbGetQuery(conn = dbC, q)
###Display unique ID for a given field
out$fullID <- genFieldUID(out,db)
out
genFieldUID <- function(out,db){
drv <- dbDriver("SQLite")
dbC <- dbConnect(drv, dbname = db)
q <- paste("SELECT rev FROM DataProductDescription WHERE dpID = ","'",out$dpID[1],"'",sep="")
rev <- dbGetQuery(conn = dbC, q)
s <- dim(out)[1]
rev <- rep(addBuff(3,rev),s)
spat <- sapply(out$spatialRes,addBuff,buf=3)
tmp <- sapply(out$timeRes,addBuff,buf=3)
tbl <- sapply(out$tableID,addBuff,buf=3)
terms <- sapply(out$termID,addBuff,buf=5)
fullIDs <- cbind(out$dpID,tmp,spat,rev,terms,tbl)
return(apply(fullIDs,1,paste,collapse="."))
}
addBuff <- function(buf, num){
num <- as.character(num)
if(buf-nchar(num) > 0){
tocat <- paste(rep("0",(buf-nchar(num))),collapse="")
} else {
stop("Your buffer character size is too small, make it BIG!")
}
return(paste(tocat,num,sep=""))
}