Commit d928f477 authored by David Watkins's avatar David Watkins
Browse files

split out parseML2Timeseriesin ngwmn

parent 460fe70f
......@@ -16,6 +16,7 @@ export(importWaterML1)
export(importWaterML2)
export(pCodeToName)
export(parameterCdFile)
export(parseWaterML2Timeseries)
export(readNGWMNdata)
export(readNGWMNlevels)
export(readNGWMNsites)
......
......@@ -63,44 +63,8 @@ importNGWMN_wml2 <- function(input, asDateTime=FALSE, tz=""){
mergedDF <- NULL
for(t in timeSeries){
gmlID <- xml_attr(t,"id")
TVP <- xml_find_all(t, ".//wml2:MeasurementTVP")#time-value pairs
rawTime <- xml_text(xml_find_all(TVP,".//wml2:time"))
valueNodes <- xml_find_all(TVP,".//wml2:value")
values <- as.numeric(xml_text(valueNodes))
nVals <- length(values)
gmlID <- rep(gmlID, nVals)
df <- parseWaterML2Timeseries(t, asDateTime)
#df of date, time, dateTime
oneCol <- rep(NA, nVals)
timeDF <- data.frame(date=oneCol, time=oneCol, dateTime=oneCol)
splitTime <- data.frame(matrix(unlist(strsplit(rawTime, "T")), nrow=nVals, byrow = TRUE), stringsAsFactors=FALSE)
if(ncol(splitTime) > 1){ #some sites only have a date
names(splitTime) <- c("date", "time")
}else{
names(splitTime) <- "date"
splitTime <- mutate(splitTime, time = NA)
}
timeDF <- mutate(splitTime, dateTime = NA)
logicVec <- nchar(rawTime) > 19
timeDF$dateTime[logicVec] <- rawTime[logicVec]
if(asDateTime){
timeDF$dateTime <- parse_date_time(timeDF$dateTime, c("%Y","%Y-%m-%d","%Y-%m-%dT%H:%M","%Y-%m-%dT%H:%M:%S",
"%Y-%m-%dT%H:%M:%OS","%Y-%m-%dT%H:%M:%OS%z"), exact = TRUE)
#^^setting tz in as.POSIXct just sets the attribute, does not convert the time!
attr(time, 'tzone') <- tz
}
uom <- xml_attr(valueNodes, "uom", default = NA)
source <- xml_attr(xml_find_all(TVP, ".//wml2:source"), "title")
comment <- xml_text(xml_find_all(TVP, ".//wml2:comment"))
df <- cbind.data.frame(source, timeDF, value=values, uom, comment, gmlID,
stringsAsFactors=FALSE)
if (is.null(mergedDF)){
mergedDF <- df
} else {
......@@ -147,3 +111,47 @@ importNGWMN_wml2 <- function(input, asDateTime=FALSE, tz=""){
}
return(mergedDF)
}
#' parse the timeseries portion of a waterML2 file
#' @param input XML with only the wml2:MeasurementTimeseries node and children
#'
#' @export
parseWaterML2Timeseries <- function(input, asDateTime) {
gmlID <- xml_attr(input,"id")
TVP <- xml_find_all(input, ".//wml2:MeasurementTVP")#time-value pairs
rawTime <- xml_text(xml_find_all(TVP,".//wml2:time"))
valueNodes <- xml_find_all(TVP,".//wml2:value")
values <- as.numeric(xml_text(valueNodes))
nVals <- length(values)
gmlID <- rep(gmlID, nVals)
#df of date, time, dateTime
oneCol <- rep(NA, nVals)
timeDF <- data.frame(date=oneCol, time=oneCol, dateTime=oneCol)
splitTime <- data.frame(matrix(unlist(strsplit(rawTime, "T")), nrow=nVals, byrow = TRUE), stringsAsFactors=FALSE)
if(ncol(splitTime) > 1){ #some sites only have a date
names(splitTime) <- c("date", "time")
}else{
names(splitTime) <- "date"
splitTime <- mutate(splitTime, time = NA)
}
timeDF <- mutate(splitTime, dateTime = NA)
logicVec <- nchar(rawTime) > 19
timeDF$dateTime[logicVec] <- rawTime[logicVec]
if(asDateTime){
timeDF$dateTime <- parse_date_time(timeDF$dateTime, c("%Y","%Y-%m-%d","%Y-%m-%dT%H:%M","%Y-%m-%dT%H:%M:%S",
"%Y-%m-%dT%H:%M:%OS","%Y-%m-%dT%H:%M:%OS%z"), exact = TRUE)
#^^setting tz in as.POSIXct just sets the attribute, does not convert the time!
attr(time, 'tzone') <- tz
}
uom <- xml_attr(valueNodes, "uom", default = NA)
source <- xml_attr(xml_find_all(TVP, ".//wml2:source"), "title")
comment <- xml_text(xml_find_all(TVP, ".//wml2:comment"))
df <- cbind.data.frame(source, timeDF, value=values, uom, comment, gmlID,
stringsAsFactors=FALSE)
return(df)
}
\ No newline at end of file
......@@ -254,7 +254,8 @@ test_that("NGWMN functions working", {
#sites with colons and NAs work
na_colons <- c(NA, bboxSites$site[200:212], NA, NA)
returnDF <- readNGWMNdata(service = "observation", featureID = na_colons)
returnDF <- readNGWMNdata(service = "observation",
featureID = na_colons, asDateTime = FALSE)
expect_is(returnDF, "data.frame")
expect_true(nrow(returnDF) > 1)
expect_true(!is.null(attributes(returnDF)$siteInfo))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment