Skip to content
Snippets Groups Projects
Commit 0e39edeb authored by Laura A DeCicco's avatar Laura A DeCicco
Browse files

Working on improving WaterML2 parser.

parent f7750628
No related branches found
No related tags found
1 merge request!59Error handling.
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
#' "featureID=MD-BC-BC-05", #' "featureID=MD-BC-BC-05",
#' "offering=RAW", #' "offering=RAW",
#' "observedProperty=WATER",sep="&") #' "observedProperty=WATER",sep="&")
#' \dontrun{ #'
#' dataReturned1 <- importWaterML2(URL) #' dataReturned1 <- importWaterML2(URL)
#' dataReturn2 <- importWaterML2(URL2, TRUE) #' dataReturn2 <- importWaterML2(URL2, TRUE)
#' URLmulti <- paste(baseURL, #' URLmulti <- paste(baseURL,
...@@ -39,26 +39,16 @@ ...@@ -39,26 +39,16 @@
#' fileName <- "WaterML2Example.xml" #' fileName <- "WaterML2Example.xml"
#' fullPath <- file.path(filePath, fileName) #' fullPath <- file.path(filePath, fileName)
#' UserData <- importWaterML2(fullPath) #' UserData <- importWaterML2(fullPath)
#' } #'
importWaterML2 <- function(obs_url, asDateTime=FALSE, tz=""){ importWaterML2 <- function(obs_url, asDateTime=FALSE, tz=""){
if(file.exists(obs_url)){ if(file.exists(obs_url)){
returnedDoc <- obs_url rawData <- obs_url
} else { } else {
h <- basicHeaderGatherer() rawData <- getWebServiceData(obs_url)
possibleError = tryCatch({
returnedDoc <- getURL(obs_url, headerfunction = h$update)
}, warning = function(w) {
warning(w, "with url:", obs_url)
}, error = function(e) {
stop(e, "with url:", obs_url)
})
} }
doc <- xmlTreeParse(returnedDoc, getDTD = FALSE, useInternalNodes = TRUE) doc <- xmlTreeParse(rawData, getDTD = FALSE, useInternalNodes = TRUE)
if(tz != ""){ if(tz != ""){
tz <- match.arg(tz, c("America/New_York","America/Chicago", tz <- match.arg(tz, c("America/New_York","America/Chicago",
...@@ -90,10 +80,10 @@ importWaterML2 <- function(obs_url, asDateTime=FALSE, tz=""){ ...@@ -90,10 +80,10 @@ importWaterML2 <- function(obs_url, asDateTime=FALSE, tz=""){
xp <- xpathApply(chunk, "//wml2:MeasurementTimeseries/wml2:point/wml2:MeasurementTVP", xp <- xpathApply(chunk, "//wml2:MeasurementTimeseries/wml2:point/wml2:MeasurementTVP",
xpathSApply, ".//*[not(*)]", xpathSApply, ".//*[not(*)]",
function(x) setNames(ifelse(nzchar(xmlValue(x)), function(x) setNames(ifelse(nzchar(xmlValue(x)),
xmlValue(x), xmlValue(x),
ifelse("qualifier" == xmlName(x), ifelse("qualifier" == xmlName(x),
xpathSApply(x,"./@xlink:title",namespaces = ns),"")), #originally I had the "" as xmlAttr(x) xpathSApply(x,"./@xlink:title",namespaces = ns),"")), #originally I had the "" as xmlAttr(x)
xmlName(x)), xmlName(x)),
namespaces = chunkNS) namespaces = chunkNS)
if(length(xpathApply(doc, if(length(xpathApply(doc,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment