forked from lawaFreshwater/WQualityStateTrend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloadWCRC.R
397 lines (297 loc) · 12.1 KB
/
loadWCRC.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
## Import data from Council Hilltop Server
## Export to a Hilltop XML file with all supplied data intact.
## ----------------------------------------------------------------------------
## Write Hilltop XML for Water Quality Data
Process<-TRUE
message(paste("WCRC: Loading data from WCRC Hilltop Server",Process))
if(Process){
## Import data from Council Spreadsheets
## Export to a Hilltop XML file with all supplied data intact.
## ----------------------------------------------------------------------------
## Write Hilltop XML for Water Quality Data
## --- Functions ---
# returns string w/o leading or trailing whitespace
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
## Convert datestring to mow seconds (number of seconds since 1-Jan-1940 00:00)
#mowSecs <- function(x){
# s<-strptime("1940-01-01","%Y-%m-%d")
# t<-strptime(x,"%Y-%m-%d %H:%M:%S")
# t<-strptime(x,"%Y-%m-%d %H:%M:%S")
# x<-(t-s)*86400
#}
require(XML) ### XML library to write hilltop XML
require(dplyr) ### dply library to manipulate table joins on dataframes
require(RCurl)
od<-getwd()
setwd("//file/herman/R/OA/08/02/2018/Water Quality/R/lawa_state")
#function to create xml file from url.
ld <- function(url){
str<- tempfile(pattern = "file", tmpdir = tempdir())
(download.file(url,destfile=str,method="wininet"))
xmlfile <- xmlParse(file = str)
unlink(str)
return(xmlfile)
}
#function to determine which created xmls have an error message.
#I/e/ the measurement value does not exist for that site.
htsServiceError <- function(url){
xmldata <- ld(url)
error<-as.character(sapply(getNodeSet(doc=xmldata, path="//Error"), xmlValue))
if(length(error)==0){
return(TRUE) # if no error, return TRUE
} else {
return(FALSE)
}
}
#function to either create full xml file or return xml file as NULL depending
#on the result from the above funciton
requestData <- function(url){
#url<-"http://hilltopdev.horizons.govt.nz/data.hts?service=Hilltop"
#RCurl::getURL(paste(url,"&request=Reset",sep=""))
#url <- paste(url,request,sep="")
cat(url,"\n")
ret <- htsServiceError(url)
if(ret==TRUE){
xmldata <- ld(url)
return(xmldata)
}else {
xmldata <- NULL
return(xmldata)
}
}
fname <- "//file/herman/R/OA/08/02/2018/Water Quality/R/lawa_state/2018_csv_config_files/wcrcSWQ_config.csv"
df <- read.csv(fname,sep=",",stringsAsFactors=FALSE)
sites <- subset(df,df$Type=="Site")[,2]
sites <- as.vector(sites)
Measurements <- subset(df,df$Type=="Measurement")[,2]
Measurements <- as.vector(Measurements)
# fname <- "WCRC Rec data.csv"
#
# rec <- read.csv(fname,sep=",",stringsAsFactors=FALSE)
# rec$Time2 <- paste(rec$Date,rec$Time,sep=" ")
# rec$Time2 <- as.POSIXct(strptime(rec$Time2,format = "%Y-%m-%d %H:%M:%S",tz="GMT"))
#
# rectime <- as.character(rec$Time2)
tab<-"\t"
## Load libraries ------------------------------------------------
require(RODBC) ### ODBC library for SQL connection
require(dplyr) ### dply library to manipulate table joins on dataframes
require(XML) ### XML library to write hilltop XML
for(i in 1:length(sites)){
for(j in 1:length(Measurements)){
cat(sites[i],Measurements[j],"\n")
if(Measurements[j]=="xE.coli (Mem Filtration)"){
url <- paste("http://hilltop.wcrc.govt.nz/wq.hts?service=Hilltop",
"&request=GetData",
"&Site=",sites[i],
"&Measurement=E.coli (Mem Filtration)",
"&From=2004-01-01",
"&To=2018-01-01",sep="")
url <- gsub(" ", "%20", url)
cat(url,"\n")
xmlfile <- requestData(url)
if(is.null(xmlfile)){
next
} else{
time <- sapply(getNodeSet(doc=xmlfile, "//T"), xmlValue)
time <- time[! time %in% rectime]
df <- data.frame(time)
if(nrow(df)!=0){
time <- sapply(getNodeSet(doc=xmlfile, "//T"), xmlValue)
#Create vector of values
value <- sapply(getNodeSet(doc=xmlfile, "//I1"), xmlValue)
#Add in bit here to get the measurements I2 Info
df4 <- data.frame(time, value, stringsAsFactors=FALSE)
df4 <- merge(df,df4, by= "time", all = TRUE)
df4 <- na.omit(df4)
u <- sapply(getNodeSet(doc=xmlfile, "//Units"), xmlValue)
df4$Site <- sites[i]
df4$Measurement <- Measurements[j]
df4$Units <- u
df4 <- df4[,c(3,4,1,2,5)]
if(!exists("Data")){
Data <- df4
} else{
Data <- rbind.data.frame(Data, df4)
}
}
}
#add an else if for WQ sample to pull in data to add as a column in data frame,
}else{
url <- paste("http://hilltop.wcrc.govt.nz/wq.hts?service=Hilltop",
"&request=GetData",
"&Site=",sites[i],
"&Measurement=",Measurements[j],
"&From=2004-01-01",
"&To=2018-01-01",sep="")
url <- gsub(" ", "%20", url)
xmlfile <- requestData(url)
if(is.null(xmlfile)){
next
} else{
#Create vector of times
time <- sapply(getNodeSet(doc=xmlfile, "//T"), xmlValue)
#Create vector of values
value <- sapply(getNodeSet(doc=xmlfile, "//I1"), xmlValue)
#Add in bit here to get the I2 info
df <- as.data.frame(time, stringsAsFactors = FALSE)
df$value <- value
#Add dataframes together, Maybe needs to be put somewhere else
#if(nrow(df)==0 {
# next
#} else{
# xmldata <- xmlfile
# }
#Create vector of units
#myPath<-"//wml2:uom"
#c<-getNodeSet(xmldata, path=myPath)
#u<-sapply(c,function(el) xmlGetAttr(el, "code"))
u <- sapply(getNodeSet(doc=xmlfile, "//Units"), xmlValue)
df$Site <- sites[i]
df$Measurement <- Measurements[j]
df$Units <- u
df <- df[,c(3,4,1,2,5)]
if(!exists("Data")){
Data <- df
} else{
Data <- rbind.data.frame(Data, df)
}
}
}
}
}
#----------------
tm<-Sys.time()
cat("Building XML\n")
cat("Creating:",Sys.time()-tm,"\n")
con <- xmlOutputDOM("Hilltop")
con$addTag("Agency", "WCRC")
#saveXML(con$value(), file="out.xml")
#-------
max<-nrow(Data)
#max<-nrows(datatbl)
i<-1
#for each site
while(i<=max){
s<-Data$Site[i]
# store first counter going into while loop to use later in writing out sample values
start<-i
cat(i,Data$Site[i],"\n") ### Monitoring progress as code runs
while(Data$Site[i]==s){
#for each measurement
#cat(datatbl$SiteName[i],"\n")
con$addTag("Measurement", attrs=c(SiteName=Data$Site[i]), close=FALSE)
con$addTag("DataSource", attrs=c(Name=Data$Measurement[i],NumItems="2"), close=FALSE)
con$addTag("TSType", "StdSeries")
con$addTag("DataType", "WQData")
con$addTag("Interpolation", "Discrete")
con$addTag("ItemInfo", attrs=c(ItemNumber="1"),close=FALSE)
con$addTag("ItemName", Data$Measurement[i])
con$addTag("ItemFormat", "F")
con$addTag("Divisor", "1")
con$addTag("Units", Data$Units[i])
#con$addTag("Units", "Joking")
con$addTag("Format", "#.###")
con$closeTag() # ItemInfo
con$closeTag() # DataSource
#saveXML(con$value(), file="out.xml")
# for the TVP and associated measurement water quality parameters
con$addTag("Data", attrs=c(DateFormat="Calendar", NumItems="2"),close=FALSE)
d<- Data$Measurement[i]
e<-Data$Site[i]
cat(" - ",Data$Measurement[i],"\n") ### Monitoring progress as code runs
while(Data$Measurement[i]==d & Data$Site[i]==s){
# for each tvp
con$addTag("E",close=FALSE)
con$addTag("T",Data$time[i])
#Check for < or > or *
## Hand Greater than symbol
if(grepl(pattern = "^\\>",x = Data$value[i],perl = TRUE)){
elemValue <- substr(Data$value[i],2,nchar(Data$value[i]))
item2 <- paste("$ND",tab,">",tab,sep="")
# Handle Less than symbols
} else if(grepl(pattern = "^\\<",x = Data$value[i],perl = TRUE)){
elemValue <- substr(Data$value[i],2,nchar(Data$value[i]))
item2 <- paste("$ND",tab,"<",tab,sep="")
# Handle Asterixes
} else if(grepl(pattern = "^\\*",x = Data$value[i],perl = TRUE)){
elemValue <- gsub(pattern = "^\\*", replacement = "", x = Data$value[i])
item2 <- paste("$ND",tab,"*",tab,sep="")
} else{
elemValue <- Data$value[i]
item2 <- ""
}
con$addTag("I1", elemValue)
if(exists("item2")){
item2 <- paste(item2,"Units", tab, Data$Units[i], tab, sep="")
} else {
item2 <- paste("Units", tab, Data$Units[i], tab, sep="")
}
con$addTag("I2", item2)
rm(item2)
con$closeTag() # E
i<-i+1 # incrementing overall for loop counter
if(i>max){break}
}
# next
con$closeTag() # Data
con$closeTag() # Measurement
if(i>max){break}
# Next
}
# store last counter going out of while loop to use later in writing out sample values
end<-i-1
# Adding WQ Sample Datasource to finish off this Site
# along with Sample parameters
con$addTag("Measurement", attrs=c(SiteName=Data$Site[start]), close=FALSE)
con$addTag("DataSource", attrs=c(Name="WQ Sample", NumItems="1"), close=FALSE)
con$addTag("TSType", "StdSeries")
con$addTag("DataType", "WQSample")
con$addTag("Interpolation", "Discrete")
con$addTag("ItemInfo", attrs=c(ItemNumber="1"),close=FALSE)
con$addTag("ItemName", "WQ Sample")
con$addTag("ItemFormat", "S")
con$addTag("Divisor", "1")
con$addTag("Units")
con$addTag("Format", "$$$")
con$closeTag() # ItemInfo
con$closeTag() # DataSource
# for the TVP and associated measurement water quality parameters
con$addTag("Data", attrs=c(DateFormat="Calendar", NumItems="1"),close=FALSE)
# for each tvp
## LOAD SAMPLE PARAMETERS
## SampleID, ProjectName, SourceType, SamplingMethod and mowsecs
sample<-Data[start:end,3]
sample<-unique(sample)
sample<-sample[order(sample)]
## THIS NEEDS SOME WORK.....
for(a in 1:length(sample)){
con$addTag("E",close=FALSE)
con$addTag("T",sample[a])
#put metadata in here when it arrives
con$addTag("I1", "")
con$closeTag() # E
}
con$closeTag() # Data
con$closeTag() # Measurement
}
cat("Saving: ",Sys.time()-tm,"\n")
if(exists("importDestination")){
saveXML(con$value(), paste(importDestination,file="wcrcSWQ.xml",sep=""))
# Setting timeseries to be WQData
x <- readLines(paste(importDestination,"wcrcSWQ.xml",sep=""))
y <- gsub( "NumItems=\"1\"", "NumItems=\"2\"", x, ignore.case = TRUE )
y <- gsub( "SimpleTimeSeries", "WQData", y, ignore.case = TRUE )
writeLines(y,paste(importDestination,"wcrcSWQ.xml",sep=""))
} else {
saveXML(con$value(), file="wcrcSWQ.xml")
# Setting timeseries to be WQData
x <- readLines("wcrcSWQ.xml")
y <- gsub( "NumItems=\"1\"", "NumItems=\"2\"", x, ignore.case = TRUE )
y <- gsub( "SimpleTimeSeries", "WQData", y, ignore.case = TRUE )
writeLines(y,"wcrcSWQ.xml")
}
cat("Finished",Sys.time()-tm,"\n")
}
setwd(od)
rm(Process)