From b511835d43e09f8a701c14c003db362bd80ced09 Mon Sep 17 00:00:00 2001 From: unknown <ldecicco@usgs.gov> Date: Mon, 3 Nov 2014 13:45:12 -0600 Subject: [PATCH] Improved error handling of no data. --- R/importWaterML1.r | 21 +++++++++++++++++---- R/importWaterML2.r | 4 ++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/R/importWaterML1.r b/R/importWaterML1.r index 8d57474a..096b3fda 100644 --- a/R/importWaterML1.r +++ b/R/importWaterML1.r @@ -75,6 +75,10 @@ importWaterML1 <- function(obs_url,asDateTime=FALSE, tz=""){ ns <- xmlNamespaceDefinitions(doc, simplify = TRUE) 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)){ chunk <- xmlDoc(timeSeries[[i]]) @@ -107,7 +111,7 @@ importWaterML1 <- function(obs_url,asDateTime=FALSE, tz=""){ attNames <- xpathSApply(subChunk, "ns1:value/@*",namespaces = chunkNS) attributeNames <- unique(names(attNames)) - + x <- lapply(attributeNames, function(x) xpathSApply(subChunk, paste0("ns1:value/@",x),namespaces = chunkNS)) valueName <- paste(methodID,pCode,statCd,sep="_") @@ -119,8 +123,17 @@ importWaterML1 <- function(obs_url,asDateTime=FALSE, tz=""){ stringsAsFactors=FALSE) for(k in 1:length(attributeNames)){ - df <- cbind(df, as.character(x[[k]]),stringsAsFactors=FALSE) - names(df)[length(df)] <- attributeNames[k] + attVal <- as.character(x[[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)) @@ -151,7 +164,7 @@ importWaterML1 <- function(obs_url,asDateTime=FALSE, tz=""){ df$tz_cd <- tzAbbriev } 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 diff --git a/R/importWaterML2.r b/R/importWaterML2.r index d572e04c..2f19c6e7 100644 --- a/R/importWaterML2.r +++ b/R/importWaterML2.r @@ -81,6 +81,10 @@ importWaterML2 <- function(obs_url, asDateTime=FALSE, tz=""){ 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)){ chunk <- xmlDoc(timeSeries[[i]]) -- GitLab