Commit 670342c0 authored by David Watkins's avatar David Watkins
Browse files

tests and documentation

parent 2061d22f
......@@ -7,7 +7,7 @@ export(constructWQPURL)
export(countyCd)
export(countyCdLookup)
export(getWebServiceData)
export(importNGMWN_wml2)
export(importNGWMN_wml2)
export(importRDB1)
export(importWQP)
export(importWaterML1)
......
......@@ -6,7 +6,7 @@
#' @param input character or raw, containing the url for the retrieval or a path to the data file, or raw XML.
#' @param asDateTime logical, if \code{TRUE} returns date and time as POSIXct, if \code{FALSE}, character
#' @param tz character to set timezone attribute of datetime. Default is an empty quote, which converts the
#' datetimes to UTC (properly accounting for daylight savings times based on the data's provided tz_cd column).
#' datetimes to UTC (properly accounting for daylight savings times based on the data's provided time zone offset).
#' Possible values to provide are "America/New_York","America/Chicago", "America/Denver","America/Los_Angeles",
#' "America/Anchorage","America/Honolulu","America/Jamaica","America/Managua","America/Phoenix", and "America/Metlakatla"
#' @return mergedDF a data frame source, time, value, uom, uomTitle, comment, gmlID
......@@ -20,9 +20,12 @@
#' \dontrun{
#' url <- "http://cida.usgs.gov/ngwmn_cache/sos?request=GetObservation&service=SOS&version=2.0.0&observedProperty=urn:ogc:def:property:OGC:GroundWaterLevel&responseFormat=text/xml&featureOfInterest=VW_GWDP_GEOSERVER.USGS.403836085374401"
#' data <- importNGWMN_wml2(url)
#'
#' url <- "http://cida.usgs.gov/ngwmn_cache/sos?request=GetObservation&service=SOS&version=2.0.0&observedProperty=urn:ogc:def:property:OGC:GroundWaterLevel&responseFormat=text/xml&featureOfInterest=VW_GWDP_GEOSERVER.USGS.474011117072901"
#' data <- importNGWMN_wml2(url)
#' }
importNGMWN_wml2 <- function(input, asDateTime=FALSE, tz=""){
#TODO: update documentation
#'
importNGWMN_wml2 <- function(input, asDateTime=FALSE, tz=""){
if(tz != ""){
tz <- match.arg(tz, c("America/New_York","America/Chicago",
"America/Denver","America/Los_Angeles",
......@@ -91,6 +94,7 @@ importNGMWN_wml2 <- function(input, asDateTime=FALSE, tz=""){
}
attr(mergedDF, "gml:identifier") <- xml_text(xml_find_all(returnedDoc, ".//gml:identifier"))
attr(mergedDF, "generationDate") <- xml_text(xml_find_all(returnedDoc, ".//wml2:generationDate"))
mergedDF$value[mergedDF$value == -999999.0] <- NA
}else if(response == "GetFeatureOfInterestResponse"){
site <- xml_text(xml_find_all(returnedDoc,".//gml:identifier"))
......@@ -107,18 +111,15 @@ importNGMWN_wml2 <- function(input, asDateTime=FALSE, tz=""){
siteLocs <- data.frame(lat=siteLocs[[1]][1], lon=siteLocs[[1]][2], stringsAsFactors = FALSE)
mergedDF <- cbind.data.frame(site, description = siteDesc, siteLocs, stringsAsFactors = FALSE)
}
else if(response == "ExceptionReport"){
#TODO: what happens if exception?
else{
stop("Unrecognized response from the web service")
}
return(mergedDF)
}
#replace blank cells with NAs
removeBlanks <- function(input){
input <- sapply(input, function(f){is.na(f)<-which(f == '');f})
return(input)
}
#' import data from the National Groundwater Monitoring Network \link{http://cida.usgs.gov/ngwmn/}
#' Only water level data and site metadata is currently available through the web service.
#' @param asDateTime logical
#' @param featureID character
#' @param request character
#' import data from the National Groundwater Monitoring Network \link{http://cida.usgs.gov/ngwmn/}.
#'
#' Only water level data is currently available through the web service.
#' @param asDateTime logical if \code{TRUE}, will convert times to POSIXct format. Currently defaults to
#' \code{FALSE} since time zone information is not included.
#' @param featureID character Vector of feature IDs in the formatted with agency code and site number
#' separated by a period, e.g. \code{USGS.404159100494601}.
#' @param request character Identifies which web service to access. Only \code{observation} is currently
#' supported, which retrieves all water level for each site.
#' @import utils
#' @importFrom dplyr mutate
#' @importFrom dplyr bind_rows
#' @export
#'
#TODO: documentation once more solidified
readNGWMNdata <- function(featureID, request = "observation", asDateTime = TRUE){
#' @examples
#' \dontrun{
#' #one site
#' site <- "USGS.430427089284901"
#' oneSite <- readNGWMNdata(featureID = site)
#'
#' #multiple sites
#' sites <- c("USGS.272838082142201","USGS.404159100494601", "USGS.401216080362703")
#' multiSiteData <- readNGWMNdata(sites)
#'
#' #non-USGS site
#' site <- "MBMG.892195"
#' data <- readNGWMNdata(featureID = site)
#'
#' #site with no data returns empty data frame
#' noDataSite <- "UTGS.401544112060301"
#' noDataSite <- readNGWMNdata(featureID = noDataSite, request = "observation")
#' }
#'
readNGWMNdata <- function(featureID, request = "observation", asDateTime = FALSE){
match.arg(request, c("observation", "featureOfInterest"))
if(asDateTime){
warning("Times zones will be incorrect. This will be fixed in the future")
}
if(request == "observation"){
allObs <- NULL
......@@ -25,17 +51,15 @@ readNGWMNdata <- function(featureID, request = "observation", asDateTime = TRUE)
if(is.null(allObs)){
allObs <- obsFID
allSites <- siteFID
allAttrs <- saveAttrs(attrs, allObs)
}else{
#TODO: can attaching attributes wait till the end?
obsFIDatt <- saveAttrs(attrs, obsFID)
allAttrs <- saveAttrs(attrs, allObs)
allObs <- removeAttrs(attrs, allObs)
obsFID <- removeAttrs(attrs, obsFID)
allAttrs <- bind_rows(allAttrs, obsFIDatt)
allObs <- bind_rows(allObs, obsFID)
attributes(allObs) <- c(attributes(allObs),as.list(allAttrs))
allSites <- bind_rows(allSites, siteFID)
}
attributes(allObs) <- c(attributes(allObs),as.list(allAttrs))
attr(allObs, "siteInfo") <- allSites
returnData <- allObs
}
......@@ -49,7 +73,7 @@ retrieveObservation <- function(featureID, asDateTime, attrs){
baseURL <- "http://cida.usgs.gov/ngwmn_cache/sos?request=GetObservation&service=SOS&version=2.0.0&observedProperty=urn:ogc:def:property:OGC:GroundWaterLevel&responseFormat=text/xml&featureOfInterest=VW_GWDP_GEOSERVER."
url <- paste0(baseURL, featureID)
returnData <- importNGMWN_wml2(url, asDateTime)
returnData <- importNGWMN_wml2(url, asDateTime)
if(nrow(returnData) == 0){
#need to add NA attributes, so they aren't messed up when stored as DFs
attr(returnData, "gml:identifier") <- NA
......@@ -75,7 +99,7 @@ retrieveObservation <- function(featureID, asDateTime, attrs){
retrieveFeatureOfInterest <- function(featureID){
baseURL <- "http://cida.usgs.gov/ngwmn_cache/sos?request=GetFeatureOfInterest&service=SOS&version=2.0.0&observedProperty=urn:ogc:def:property:OGC:GroundWaterLevel&responseFormat=text/xml&featureOfInterest=VW_GWDP_GEOSERVER."
url <- paste0(baseURL, featureID)
siteDF <- importNGMWN_wml2(url, asDateTime)
siteDF <- importNGWMN_wml2(url, asDateTime)
return(siteDF)
}
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/importNGWMN_wml2.R
\name{importNGMWN_wml2}
\alias{importNGMWN_wml2}
\name{importNGWMN_wml2}
\alias{importNGWMN_wml2}
\title{Function to return data from the National Ground Water Monitoring Network waterML2 format}
\usage{
importNGMWN_wml2(input, asDateTime = FALSE, tz = "")
importNGWMN_wml2(input, asDateTime = FALSE, tz = "")
}
\arguments{
\item{input}{character or raw, containing the url for the retrieval or a path to the data file, or raw XML.}
......@@ -12,7 +12,7 @@ importNGMWN_wml2(input, asDateTime = FALSE, tz = "")
\item{asDateTime}{logical, if \code{TRUE} returns date and time as POSIXct, if \code{FALSE}, character}
\item{tz}{character to set timezone attribute of datetime. Default is an empty quote, which converts the
datetimes to UTC (properly accounting for daylight savings times based on the data's provided tz_cd column).
datetimes to UTC (properly accounting for daylight savings times based on the data's provided time zone offset).
Possible values to provide are "America/New_York","America/Chicago", "America/Denver","America/Los_Angeles",
"America/Anchorage","America/Honolulu","America/Jamaica","America/Managua","America/Phoenix", and "America/Metlakatla"}
}
......@@ -27,6 +27,10 @@ but the general functionality is correct.
\dontrun{
url <- "http://cida.usgs.gov/ngwmn_cache/sos?request=GetObservation&service=SOS&version=2.0.0&observedProperty=urn:ogc:def:property:OGC:GroundWaterLevel&responseFormat=text/xml&featureOfInterest=VW_GWDP_GEOSERVER.USGS.403836085374401"
data <- importNGWMN_wml2(url)
url <- "http://cida.usgs.gov/ngwmn_cache/sos?request=GetObservation&service=SOS&version=2.0.0&observedProperty=urn:ogc:def:property:OGC:GroundWaterLevel&responseFormat=text/xml&featureOfInterest=VW_GWDP_GEOSERVER.USGS.474011117072901"
data <- importNGWMN_wml2(url)
}
}
......@@ -2,20 +2,41 @@
% Please edit documentation in R/readNGWMNdata.R
\name{readNGWMNdata}
\alias{readNGWMNdata}
\title{import data from the National Groundwater Monitoring Network \link{http://cida.usgs.gov/ngwmn/}
Only water level data and site metadata is currently available through the web service.}
\title{import data from the National Groundwater Monitoring Network \link{http://cida.usgs.gov/ngwmn/}.}
\usage{
readNGWMNdata(featureID, request = "observation", asDateTime = TRUE)
readNGWMNdata(featureID, request = "observation", asDateTime = FALSE)
}
\arguments{
\item{featureID}{character}
\item{featureID}{character Vector of feature IDs in the formatted with agency code and site number
separated by a period, e.g. \code{USGS.404159100494601}.}
\item{request}{character}
\item{request}{character Identifies which web service to access. Only \code{observation} is currently
supported, which retrieves all water level for each site.}
\item{asDateTime}{logical}
\item{asDateTime}{logical if \code{TRUE}, will convert times to POSIXct format. Currently defaults to
\code{FALSE} since time zone information is not included.}
}
\description{
import data from the National Groundwater Monitoring Network \link{http://cida.usgs.gov/ngwmn/}
Only water level data and site metadata is currently available through the web service.
Only water level data is currently available through the web service.
}
\examples{
\dontrun{
#one site
site <- "USGS.430427089284901"
oneSite <- readNGWMNdata(featureID = site)
#multiple sites
sites <- c("USGS.272838082142201","USGS.404159100494601", "USGS.401216080362703")
multiSiteData <- readNGWMNdata(sites)
#non-USGS site
site <- "MBMG.892195"
data <- readNGWMNdata(featureID = site)
#site with no data returns empty data frame
noDataSite <- "UTGS.401544112060301"
noDataSite <- readNGWMNdata(featureID = noDataSite, request = "observation")
}
}
......@@ -97,3 +97,4 @@ test_that("Dates with no days can be handled", {
testthat::skip_on_cran()
expect_error(readNWISgwl("425957088141001", startDate = "1980-01-01"))
})
......@@ -173,3 +173,12 @@ test_that("External WQP tests", {
STORETdata <- importWQP(STORETex)
expect_is(STORETdata$ActivityStartDateTime, 'POSIXct')
})
context("importNGWMN_wml2")
test_that("External NGMMN import tests", {
testthat::skip_on_cran()
url <- "http://cida.usgs.gov/ngwmn_cache/sos?request=GetObservation&service=SOS&version=2.0.0&observedProperty=urn:ogc:def:property:OGC:GroundWaterLevel&responseFormat=text/xml&featureOfInterest=VW_GWDP_GEOSERVER.USGS.403836085374401"
data <- importNGWMN_wml2(url)
expect_true(is.numeric(data$value))
expect_true(nrow(data) > 0)
})
......@@ -215,3 +215,18 @@ test_that("state county tests",{
fromIDs <- countyCdLookup(state = 13, county = 5, output = "fullName")
expect_equal(fromIDs, "Bacon County")
})
test_that("NGWMN functions working", {
noDataSite <- "UTGS.401544112060301"
noDataSite <- readNGWMNdata(featureID = noDataSite, request = "observation")
expect_true(is.data.frame(noDataSite))
#one site
site <- "USGS.430427089284901"
oneSite <- readNGWMNdata(featureID = site)
expect_true(is.numeric(oneSite$value))
expect_true(is.character(oneSite$site))
expect_true(nrow(oneSite) > 0)
})
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