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

Improved error handling of no data.

parent faa0155a
No related branches found
No related tags found
1 merge request!39Overhaul of function names. Move some functionality to EGRET.
...@@ -75,6 +75,10 @@ importWaterML1 <- function(obs_url,asDateTime=FALSE, tz=""){ ...@@ -75,6 +75,10 @@ importWaterML1 <- function(obs_url,asDateTime=FALSE, tz=""){
ns <- xmlNamespaceDefinitions(doc, simplify = TRUE) ns <- xmlNamespaceDefinitions(doc, simplify = TRUE)
timeSeries <- xpathApply(doc, "//ns1:timeSeries", namespaces = ns) timeSeries <- xpathApply(doc, "//ns1:timeSeries", namespaces = ns)
if(0 == length(timeSeries)){
stop("No data to return for URL:", obs_url)
}
for (i in 1:length(timeSeries)){ for (i in 1:length(timeSeries)){
chunk <- xmlDoc(timeSeries[[i]]) chunk <- xmlDoc(timeSeries[[i]])
...@@ -107,7 +111,7 @@ importWaterML1 <- function(obs_url,asDateTime=FALSE, tz=""){ ...@@ -107,7 +111,7 @@ importWaterML1 <- function(obs_url,asDateTime=FALSE, tz=""){
attNames <- xpathSApply(subChunk, "ns1:value/@*",namespaces = chunkNS) attNames <- xpathSApply(subChunk, "ns1:value/@*",namespaces = chunkNS)
attributeNames <- unique(names(attNames)) attributeNames <- unique(names(attNames))
x <- lapply(attributeNames, function(x) xpathSApply(subChunk, paste0("ns1:value/@",x),namespaces = chunkNS)) x <- lapply(attributeNames, function(x) xpathSApply(subChunk, paste0("ns1:value/@",x),namespaces = chunkNS))
valueName <- paste(methodID,pCode,statCd,sep="_") valueName <- paste(methodID,pCode,statCd,sep="_")
...@@ -119,8 +123,17 @@ importWaterML1 <- function(obs_url,asDateTime=FALSE, tz=""){ ...@@ -119,8 +123,17 @@ importWaterML1 <- function(obs_url,asDateTime=FALSE, tz=""){
stringsAsFactors=FALSE) stringsAsFactors=FALSE)
for(k in 1:length(attributeNames)){ for(k in 1:length(attributeNames)){
df <- cbind(df, as.character(x[[k]]),stringsAsFactors=FALSE) attVal <- as.character(x[[k]])
names(df)[length(df)] <- attributeNames[k] if(length(attVal) == nrow(df)){
df$temp <- as.character(x[[k]])
} else {
attrList <- xpathApply(subChunk, "ns1:value", namespaces = chunkNS, xmlAttrs)
df$temp <- sapply(1:nrow(df),function(x) as.character(attrList[[x]][attributeNames[k]]))
df$temp[is.na(df$temp)] <- ""
}
names(df)[which(names(df) %in% "temp")] <- attributeNames[k]
} }
df <- cbind(df, get(valueName)) df <- cbind(df, get(valueName))
...@@ -151,7 +164,7 @@ importWaterML1 <- function(obs_url,asDateTime=FALSE, tz=""){ ...@@ -151,7 +164,7 @@ importWaterML1 <- function(obs_url,asDateTime=FALSE, tz=""){
df$tz_cd <- tzAbbriev df$tz_cd <- tzAbbriev
} else { } else {
datetime <- as.Date(strptime(xpathSApply(subChunk, "ns1:value/@dateTime",namespaces = chunkNS),"%Y-%m-%dT%H:%M:%S")) datetime <- as.character(xpathSApply(subChunk, "ns1:value/@dateTime",namespaces = chunkNS))
} }
df$dateTime <- datetime df$dateTime <- datetime
......
...@@ -81,6 +81,10 @@ importWaterML2 <- function(obs_url, asDateTime=FALSE, tz=""){ ...@@ -81,6 +81,10 @@ importWaterML2 <- function(obs_url, asDateTime=FALSE, tz=""){
timeSeries <- xpathApply(doc, "//wml2:Collection", namespaces = ns) timeSeries <- xpathApply(doc, "//wml2:Collection", namespaces = ns)
if(0 == length(timeSeries)){
stop("No data to return for URL:", obs_url)
}
for (i in 1:length(timeSeries)){ for (i in 1:length(timeSeries)){
chunk <- xmlDoc(timeSeries[[i]]) chunk <- xmlDoc(timeSeries[[i]])
......
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