forked from seanlhodges/WQualityStateTrend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadAC.R
264 lines (206 loc) · 7.94 KB
/
loadAC.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
## Import data from Council Data Source
## Export to a Hilltop XML file with all supplied data intact.
## ----------------------------------------------------------------------------
## Write Hilltop XML for Water Quality Data
# The process flag assumes this script will be called from a batch file. IF the flag is true, the code in this file will run.
Process<-TRUE
message(paste("AC: Loading data from Kisters/Hydrotel",Process))
if(Process){
## --- 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 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)
# }
}
#function to create xml file from url.
ld <- function(url){
(download.file(url,destfile="tmpr",method="wininet"))
pause(1)
xmlfile <- xmlParse(file = "tmpr")
unlink("tmpr")
return(xmlfile)
}
pause <- function(x)
{
p1 <- proc.time()
Sys.sleep(x)
proc.time() - p1 # The cpu usage should be negligible
}
fname <- "//file/herman/R/OA/08/02/2018/Water Quality/R/lawa_state/2018_csv_config_files/acSWQ_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)
## 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)){
#i<-33
#j<-3
url <- paste("http://aklc.hydrotel.co.nz:8080/KiWIS/KiWIS?datasource=2&service=SOS&version=2.0&request=GetObservation&featureOfInterest="
,sites[i], "&observedProperty=", Measurements[j], "&Procedure=raw&temporalfilter=om:phenomenonTime,P15Y", sep="")
url <- gsub(" ", "%20", url)
xmlfile <- ld(url)
#Create vector of times
time <- sapply(getNodeSet(doc=xmlfile, "//wml2:time"), xmlValue)
#Create vector of values
value <- sapply(getNodeSet(doc=xmlfile, "//wml2:value"), xmlValue)
if(length(time!=0)){
#Get QC metadata
xPath <-"//wml2:qualifier"
c<-getNodeSet(xmlfile,path=xPath)
QC<-sapply(c,function(el) xmlGetAttr(el, "xlink:title"))
#Create dataframe holding both
df <- as.data.frame(time, stringsAsFactors = FALSE)
df$value <- value
#Create vector of units
myPath<-"//wml2:uom"
c<-getNodeSet(xmlfile, path=myPath)
u<-sapply(c,function(el) xmlGetAttr(el, "code"))
u <-unique(u)
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)
}
}
}
}
#p <- sapply(getNodeSet(doc=xmlfilec ,path="//sos:ObservationOffering/swes:name"), xmlValue)
#procedure <- c("RERIMP.Sample.Results.P", "WARIMP.Sample.Results.P")
#----------------
tm<-Sys.time()
cat("Building XML\n")
cat("Creating:",Sys.time()-tm,"\n")
con <- xmlOutputDOM("Hilltop")
con$addTag("Agency", "AC")
#saveXML(con$value(), file="out.xml")
#-------
if(length(t)==0){
next
} else{
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]
cat(" - ",Data$Measurement[i],"\n") ### Monitoring progress as code runs
while(Data$Measurement[i]==d){
# for each tvp
con$addTag("E",close=FALSE)
con$addTag("T",Data$time[i])
con$addTag("I1", Data$value[i])
con$addTag("I2", paste("Units", Data$Units[i], sep="\t"))
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("I2", paste("QC", QC, sep="\t"))
con$closeTag() # E
}
con$closeTag() # Data
con$closeTag() # Measurement
}
}
cat("Saving: ",Sys.time()-tm,"\n")
if(exists("importDestination")){
saveXML(con$value(), paste(importDestination,file="acSWQ.xml",sep=""))
} else {
saveXML(con$value(), file="acSWQ.xml")
}
cat("Finished",Sys.time()-tm,"\n")
}
rm(Process)