diff --git a/NAMESPACE b/NAMESPACE index 9f1c52fde1a474f42006d740e024d103201a3cd7..7145580bd1310f70fe201e4656cc474dd6da1680 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,12 +9,16 @@ export(constructWQPURL) export(countyCd) export(countyCdLookup) export(getWebServiceData) +export(importNGWMN_wml2) export(importRDB1) export(importWQP) export(importWaterML1) export(importWaterML2) export(pCodeToName) export(parameterCdFile) +export(readNGWMNdata) +export(readNGWMNlevels) +export(readNGWMNsites) export(readNWISdata) export(readNWISdv) export(readNWISgwl) @@ -43,10 +47,12 @@ import(stats) import(utils) importFrom(curl,curl_version) importFrom(dplyr,arrange) +importFrom(dplyr,bind_cols) importFrom(dplyr,bind_rows) importFrom(dplyr,everything) importFrom(dplyr,full_join) importFrom(dplyr,left_join) +importFrom(dplyr,mutate) importFrom(dplyr,mutate_) importFrom(dplyr,mutate_each_) importFrom(dplyr,rbind_all) @@ -73,11 +79,13 @@ importFrom(readr,read_delim) importFrom(readr,read_lines) importFrom(reshape2,dcast) importFrom(reshape2,melt) +importFrom(stats,na.omit) importFrom(xml2,read_xml) importFrom(xml2,xml_attr) importFrom(xml2,xml_attrs) importFrom(xml2,xml_children) importFrom(xml2,xml_find_all) +importFrom(xml2,xml_find_first) importFrom(xml2,xml_name) importFrom(xml2,xml_root) importFrom(xml2,xml_text) diff --git a/R/importNGWMN_wml2.R b/R/importNGWMN_wml2.R new file mode 100644 index 0000000000000000000000000000000000000000..b5d22988382ab31ffc7f505dd4701bea5b6a05b1 --- /dev/null +++ b/R/importNGWMN_wml2.R @@ -0,0 +1,149 @@ +#' Function to return data from the National Ground Water Monitoring Network waterML2 format +#' +#' This function accepts a url parameter for a WaterML2 getObservation. This function is still under development, +#' but the general functionality is correct. +#' +#' @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 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 +#' @export +#' @importFrom xml2 read_xml +#' @importFrom xml2 xml_find_all +#' @importFrom xml2 xml_text +#' @importFrom xml2 xml_attr +#' @importFrom xml2 xml_find_first +#' @importFrom lubridate parse_date_time +#' @examples +#' \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&featureOf +#' Interest=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&featureOf +#' Interest=VW_GWDP_GEOSERVER.USGS.474011117072901" +#' data <- importNGWMN_wml2(url) +#' } +#' +#' +#TODO: separate id and agency name, give also as separate dimensions +importNGWMN_wml2 <- function(input, asDateTime=FALSE, tz=""){ + if(tz != ""){ + tz <- match.arg(tz, OlsonNames()) + }else{tz = "UTC"} + + raw <- FALSE + if(class(input) == "character" && file.exists(input)){ + returnedDoc <- read_xml(input) + }else if(class(input) == 'raw'){ + returnedDoc <- read_xml(input) + raw <- TRUE + } else { + returnedDoc <- xml_root(getWebServiceData(input, encoding='gzip')) + } + + response <- xml_name(returnedDoc) + if(response == "GetObservationResponse"){ + + timeSeries <- xml_find_all(returnedDoc, "//wml2:MeasurementTimeseries") #each parameter/site combo + + if(0 == length(timeSeries)){ + df <- data.frame() + if(!raw){ + attr(df, "url") <- input + } + return(df) + } + + 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 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 { + similarNames <- intersect(colnames(mergedDF), colnames(df)) + mergedDF <- full_join(mergedDF, df, by=similarNames) + } + } + + if(!raw){ + url <- input + attr(mergedDF, "url") <- url + } + if(asDateTime){ + mergedDF$date <- as.Date(mergedDF$date) + } + nonDateCols <- grep("date",names(mergedDF), value=TRUE, invert = TRUE) + + mergedDF[nonDateCols][mergedDF[nonDateCols] == "" | mergedDF[nonDateCols]== -999999.0] <- NA + attr(mergedDF, "gml:identifier") <- xml_text(xml_find_all(returnedDoc, ".//gml:identifier")) + attr(mergedDF, "generationDate") <- xml_text(xml_find_all(returnedDoc, ".//wml2:generationDate")) + meta <- xml_find_all(returnedDoc, ".//gmd:contact") + attr(mergedDF, "contact") <- xml_attr(meta, "href") + attr(mergedDF, "responsibleParty") <- xml_text(xml_find_all(meta, ".//gco:CharacterString")) + + + }else if(response == "GetFeatureOfInterestResponse"){ + featureMembers <- xml_find_all(returnedDoc, ".//sos:featureMember") + site <- xml_text(xml_find_all(featureMembers,".//gml:identifier")) + site <- substring(site, 8) + + #some sites don't have a description + siteDesc <- xml_text(xml_find_first(featureMembers, ".//gml:description")) + + siteLocs <- strsplit(xml_text(xml_find_all(featureMembers, ".//gml:pos")), " ") + siteLocs <- data.frame(matrix(unlist(siteLocs), nrow=length(siteLocs), byrow=TRUE), stringsAsFactors = FALSE) + names(siteLocs) <- c("dec_lat_va", "dec_lon_va") + dec_lat_va <- "dplyr var" + dec_lon_va <- "dplyr var" + siteLocs <- mutate(siteLocs, dec_lat_va=as.numeric(dec_lat_va), dec_lon_va=as.numeric(dec_lon_va)) + mergedDF <- cbind.data.frame(site, description = siteDesc, siteLocs, stringsAsFactors = FALSE) + } + else{ + stop("Unrecognized response from the web service") + } + return(mergedDF) +} diff --git a/R/importWaterML2.r b/R/importWaterML2.r index de982db56670731585f7c6e0fc3bdb9a707d3389..bb4fc26cd032fa67ebe1741cc643509d43a71c49 100644 --- a/R/importWaterML2.r +++ b/R/importWaterML2.r @@ -72,7 +72,6 @@ importWaterML2 <- function(obs_url, asDateTime=FALSE, tz="UTC"){ for(t in timeSeries){ TVP <- xml_find_all(t, ".//wml2:MeasurementTVP")#time-value pairs time <- xml_text(xml_find_all(TVP,".//wml2:time")) - #TODO: if asDateTime.... if(asDateTime){ time <- parse_date_time(time, 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) diff --git a/R/readNGWMNdata.R b/R/readNGWMNdata.R new file mode 100644 index 0000000000000000000000000000000000000000..b4ef50bc43a1d50bafc48feb6a18506af6cc265e --- /dev/null +++ b/R/readNGWMNdata.R @@ -0,0 +1,228 @@ +#' import data from the National Groundwater Monitoring Network \url{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 service character Identifies which web service to access. \code{observation} retrieves all water level for each site, +#' and \code{featureOfInterest} retrieves a data frame of site information, including description, latitude, and longitude. +#' @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 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" +#' @param \dots Other parameters to supply, namely \code{featureID} or \code{bbox} +#' @import utils +#' @importFrom dplyr mutate +#' @importFrom dplyr bind_rows +#' @importFrom dplyr bind_cols +#' @importFrom stats na.omit +#' @export +#' @examples +#' \dontrun{ +#' #one site +#' site <- "USGS.430427089284901" +#' oneSite <- readNGWMNdata(featureID = site) +#' +#' #multiple sites +#' sites <- c("USGS.272838082142201","USGS.404159100494601", "USGS.401216080362703") +#' multiSiteData <- readNGWMNdata(sites) +#' attributes(multiSiteData) +#' +#' #non-USGS site +#' #accepts colon or period between agency and ID +#' site <- "MBMG:892195" +#' data <- readNGWMNdata(featureID = site) +#' +#' #site with no data returns empty data frame +#' noDataSite <- "UTGS.401544112060301" +#' noDataSite <- readNGWMNdata(featureID = noDataSite, service = "observation") +#' +#' #bounding box +#' bboxSites <- readNGWMNdata(service = "featureOfInterest", bbox = c(30, -99, 31, 102)) +#' } +#' +readNGWMNdata <- function(..., service = "observation", asDateTime = TRUE, tz = ""){ + message(" ******************************************************** + DISCLAIMER: NGWMN retrieval functions are still in flux, + and no future behavior or output is guaranteed + *********************************************************") + + match.arg(service, c("observation", "featureOfInterest", "getCapabilities")) + dots <- list(...) + + if(service == "observation"){ + allObs <- data.frame() + allAttrs <- data.frame() + + #these attributes are pulled out and saved when doing binds to be reattached + attrs <- c("url","gml:identifier","generationDate","responsibleParty", "contact") + featureID <- na.omit(gsub(":",".",dots[['featureID']])) + + for(f in featureID){ + obsFID <- retrieveObservation(featureID = f, asDateTime, attrs) + obsFIDattr <- saveAttrs(attrs, obsFID) + obsFID <- removeAttrs(attrs, obsFID) + allObs <- bind_rows(allObs, obsFID) + allAttrs <- bind_rows(allAttrs, obsFIDattr) + + } + allSites <- retrieveFeatureOfInterest(featureID = featureID) + attr(allObs, "siteInfo") <- allSites + attr(allObs, "other") <- allAttrs + returnData <- allObs + + }else if(service == "featureOfInterest"){ + if("featureID" %in% names(dots)){ + featureID <- na.omit(gsub(":",".",dots[['featureID']])) + allSites <- retrieveFeatureOfInterest(featureID = featureID) + } + if("bbox" %in% names(dots)){ + allSites <- retrieveFeatureOfInterest(bbox=dots[['bbox']]) + } + returnData <- allSites + }else{ + stop("getCapabilities is not yet implemented") + #TODO: fill in getCapabilites + } + return(returnData) +} + +#' Retrieve groundwater levels from the National Ground Water Monitoring Network \url{http://cida.usgs.gov/ngwmn/}. +#' +#' @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 asDateTime logical Should dates and times be converted to date/time objects, +#' or returned as character? Defaults to \code{TRUE}. Must be set to \code{FALSE} if a site +#' contains non-standard dates. +#' +#' @export +#' +#' @examples +#' \dontrun{ +#' #one site +#' site <- "USGS.430427089284901" +#' oneSite <- readNGWMNlevels(featureID = site) +#' +#' #multiple sites +#' sites <- c("USGS.272838082142201","USGS.404159100494601", "USGS.401216080362703") +#' multiSiteData <- readNGWMNlevels(sites) +#' +#' #non-USGS site +#' site <- "MBMG.892195" +#' data <- readNGWMNlevels(featureID = site) +#' +#' #site with no data returns empty data frame +#' noDataSite <- "UTGS.401544112060301" +#' noDataSite <- readNGWMNlevels(featureID = noDataSite) +#' } + +readNGWMNlevels <- function(featureID, asDateTime = TRUE){ + data <- readNGWMNdata(featureID = featureID, service = "observation", + asDateTime = asDateTime) + return(data) +} + +#' Retrieve site data from the National Ground Water Monitoring Network \url{http://cida.usgs.gov/ngwmn/}. +#' +#' @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}. +#' +#' @export +#' @return A data frame the following columns: +#' #' \tabular{lll}{ +#' Name \tab Type \tab Description \cr +#' site \tab char \tab Site FID \cr +#' description \tab char \tab Site description \cr +#' dec_lat_va, dec_lon_va \tab numeric \tab Site latitude and longitude \cr +#' } +#' @examples +#' \dontrun{ +#' #one site +#' site <- "USGS.430427089284901" +#' oneSite <- readNGWMNsites(featureID = site) +#' +#' #multiple sites +#' sites <- c("USGS.272838082142201","USGS.404159100494601", "USGS.401216080362703") +#' multiSiteInfo <- readNGWMNsites(sites) +#' +#' #non-USGS site +#' site <- "MBMG.892195" +#' siteInfo <- readNGWMNsites(featureID = site) +#' +#' } + +readNGWMNsites <- function(featureID){ + sites <- readNGWMNdata(featureID = featureID, service = "featureOfInterest") + return(sites) +} + + +retrieveObservation <- function(featureID, asDateTime, attrs){ + url <- drURL(base.name = "NGWMN", access = pkg.env$access, request = "GetObservation", + service = "SOS", version = "2.0.0", observedProperty = "urn:ogc:def:property:OGC:GroundWaterLevel", + responseFormat = "text/xml", featureOfInterest = paste("VW_GWDP_GEOSERVER", featureID, sep = ".")) + + 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 + attr(returnData, "generationDate") <- NA + } + + #mutate removes the attributes, need to save and append + attribs <- saveAttrs(attrs, returnData) + if(nrow(returnData) > 0){ + #tack on site number + siteNum <- rep(sub('.*\\.', '', featureID), nrow(returnData)) + returnData <- mutate(returnData, site = siteNum) + numCol <- ncol(returnData) + returnData <- returnData[,c(numCol,1:(numCol - 1))] #move siteNum to the left + } + attributes(returnData) <- c(attributes(returnData), as.list(attribs)) + + return(returnData) +} + +#retrieve feature of interest +#could allow pass through srsName - needs to be worked in higher-up in dots +retrieveFeatureOfInterest <- function(..., asDateTime, srsName="urn:ogc:def:crs:EPSG::4269"){ + dots <- list(...) + values <- gsub(x = convertDots(dots), pattern = ",", replacement = "%2C") + + url <- drURL(base.name = "NGWMN", access = pkg.env$access, request = "GetFeatureOfInterest", + service = "SOS", version = "2.0.0", responseFormat = "text/xml") + + if("featureID" %in% names(values)){ + url <- appendDrURL(url, featureOfInterest = paste("VW_GWDP_GEOSERVER", + values[['featureID']], sep = ".")) + + }else if("bbox" %in% names(values)){ + url <- appendDrURL(url, bbox = paste(values[['bbox']], collapse=","), + srsName = srsName) + }else{ + stop() + } + siteDF <- importNGWMN_wml2(url, asDateTime) + attr(siteDF, "url") <- url + attr(siteDF, "queryTime") <- Sys.time() + return(siteDF) +} + + +#save specified attributes from a data frame +saveAttrs <- function(attrs, df){ + attribs <- sapply(attrs, function(x) attr(df, x)) + if(is.vector(attribs)){ + toReturn <- as.data.frame(t(attribs), stringsAsFactors = FALSE) + }else{ #don't need to transpose + toReturn <- as.data.frame(attribs, stringsAsFactors = FALSE) + } + return(toReturn) +} + +#strip specified attributes from a data frame +removeAttrs <- function(attrs, df){ + for(a in attrs){ + attr(df, a) <- NULL + } + return(df) +} \ No newline at end of file diff --git a/R/readNWISdata.r b/R/readNWISdata.r index ce4cebd4ab69311882c1f79757881a58fa566e18..cc5d19c45160b0b7466da02041fe561d3f2dc818 100644 --- a/R/readNWISdata.r +++ b/R/readNWISdata.r @@ -137,7 +137,7 @@ readNWISdata <- function(..., asDateTime=TRUE,convertType=TRUE,tz="UTC"){ stop("Only one service call allowed.") } - values <- sapply(matchReturn, function(x) as.character(paste(eval(x),collapse=",",sep=""))) + values <- convertDots(matchReturn) names(values)[names(values) == "startDate"] <- "startDT" names(values)[names(values) == "endDate"] <- "endDT" @@ -347,3 +347,11 @@ countyCdLookup <- function(state, county, outputType = "id"){ return(retVal) } + + +# convert variables in dots to usable format +convertDots <- function(matchReturn){ + retVal <- sapply(matchReturn, function(x) as.character(paste(eval(x),collapse=",",sep=""))) + return(retVal) +} + diff --git a/R/setAccess.R b/R/setAccess.R index 631c9c6ea05ca8df97b84b27365751fa9dce081e..2d541ad05d3304b4154738749740680bdeeed15b 100644 --- a/R/setAccess.R +++ b/R/setAccess.R @@ -57,6 +57,8 @@ access = match.arg(access, c('public','internal','cooperator','USGS')) pkg.env$wqpActivity = "https://www.waterqualitydata.us/Activity/search" pkg.env$wqpMetrics = "https://www.waterqualitydata.us/ActivityMetric/search" + pkg.env$NGWMN = "https://cida.usgs.gov/ngwmn_cache/sos" + options(Access.dataRetrieval = access) } diff --git a/man/importNGWMN_wml2.Rd b/man/importNGWMN_wml2.Rd new file mode 100644 index 0000000000000000000000000000000000000000..ba103b18e80e319274d49f1ce41fcdaf1fbe9062 --- /dev/null +++ b/man/importNGWMN_wml2.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/importNGWMN_wml2.R +\name{importNGWMN_wml2} +\alias{importNGWMN_wml2} +\title{Function to return data from the National Ground Water Monitoring Network waterML2 format} +\usage{ +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.} + +\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 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"} +} +\value{ +mergedDF a data frame source, time, value, uom, uomTitle, comment, gmlID +} +\description{ +This function accepts a url parameter for a WaterML2 getObservation. This function is still under development, +but the general functionality is correct. +} +\examples{ +\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&featureOf +Interest=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&featureOf +Interest=VW_GWDP_GEOSERVER.USGS.474011117072901" +data <- importNGWMN_wml2(url) +} + + +} diff --git a/man/readNGWMNdata.Rd b/man/readNGWMNdata.Rd new file mode 100644 index 0000000000000000000000000000000000000000..b87830c1ff91b3dda0084054d35e9c941e9663d5 --- /dev/null +++ b/man/readNGWMNdata.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/readNGWMNdata.R +\name{readNGWMNdata} +\alias{readNGWMNdata} +\title{import data from the National Groundwater Monitoring Network \url{http://cida.usgs.gov/ngwmn/}.} +\usage{ +readNGWMNdata(..., service = "observation", asDateTime = TRUE, tz = "") +} +\arguments{ +\item{\dots}{Other parameters to supply, namely \code{featureID} or \code{bbox}} + +\item{service}{character Identifies which web service to access. \code{observation} retrieves all water level for each site, +and \code{featureOfInterest} retrieves a data frame of site information, including description, latitude, and longitude.} + +\item{asDateTime}{logical if \code{TRUE}, will convert times to POSIXct format. Currently defaults to +\code{FALSE} since time zone information is not included.} + +\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 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"} +} +\description{ +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) +attributes(multiSiteData) + +#non-USGS site +#accepts colon or period between agency and ID +site <- "MBMG:892195" +data <- readNGWMNdata(featureID = site) + +#site with no data returns empty data frame +noDataSite <- "UTGS.401544112060301" +noDataSite <- readNGWMNdata(featureID = noDataSite, service = "observation") + +#bounding box +bboxSites <- readNGWMNdata(service = "featureOfInterest", bbox = c(30, -99, 31, 102)) +} + +} diff --git a/man/readNGWMNlevels.Rd b/man/readNGWMNlevels.Rd new file mode 100644 index 0000000000000000000000000000000000000000..8cca195ff9c9626f07337e43ed2f0a51541ed817 --- /dev/null +++ b/man/readNGWMNlevels.Rd @@ -0,0 +1,38 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/readNGWMNdata.R +\name{readNGWMNlevels} +\alias{readNGWMNlevels} +\title{Retrieve groundwater levels from the National Ground Water Monitoring Network \url{http://cida.usgs.gov/ngwmn/}.} +\usage{ +readNGWMNlevels(featureID, asDateTime = TRUE) +} +\arguments{ +\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{asDateTime}{logical Should dates and times be converted to date/time objects, +or returned as character? Defaults to \code{TRUE}. Must be set to \code{FALSE} if a site +contains non-standard dates.} +} +\description{ +Retrieve groundwater levels from the National Ground Water Monitoring Network \url{http://cida.usgs.gov/ngwmn/}. +} +\examples{ +\dontrun{ +#one site +site <- "USGS.430427089284901" +oneSite <- readNGWMNlevels(featureID = site) + +#multiple sites +sites <- c("USGS.272838082142201","USGS.404159100494601", "USGS.401216080362703") +multiSiteData <- readNGWMNlevels(sites) + +#non-USGS site +site <- "MBMG.892195" +data <- readNGWMNlevels(featureID = site) + +#site with no data returns empty data frame +noDataSite <- "UTGS.401544112060301" +noDataSite <- readNGWMNlevels(featureID = noDataSite) +} +} diff --git a/man/readNGWMNsites.Rd b/man/readNGWMNsites.Rd new file mode 100644 index 0000000000000000000000000000000000000000..c253b2d52721267ef9f0b46ecfdb29313ed05747 --- /dev/null +++ b/man/readNGWMNsites.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/readNGWMNdata.R +\name{readNGWMNsites} +\alias{readNGWMNsites} +\title{Retrieve site data from the National Ground Water Monitoring Network \url{http://cida.usgs.gov/ngwmn/}.} +\usage{ +readNGWMNsites(featureID) +} +\arguments{ +\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}.} +} +\value{ +A data frame the following columns: +#' \tabular{lll}{ +Name \tab Type \tab Description \cr +site \tab char \tab Site FID \cr +description \tab char \tab Site description \cr +dec_lat_va, dec_lon_va \tab numeric \tab Site latitude and longitude \cr +} +} +\description{ +Retrieve site data from the National Ground Water Monitoring Network \url{http://cida.usgs.gov/ngwmn/}. +} +\examples{ +\dontrun{ +#one site +site <- "USGS.430427089284901" +oneSite <- readNGWMNsites(featureID = site) + +#multiple sites +sites <- c("USGS.272838082142201","USGS.404159100494601", "USGS.401216080362703") +multiSiteInfo <- readNGWMNsites(sites) + +#non-USGS site +site <- "MBMG.892195" +siteInfo <- readNGWMNsites(featureID = site) + +} +} diff --git a/tests/testthat/tests_userFriendly_fxns.R b/tests/testthat/tests_userFriendly_fxns.R index d2a072a9f91db5e4e9ee06fbbff87c43d5756cc6..d7c5d59ccabe56dcefe685c46c2391eba0804618 100644 --- a/tests/testthat/tests_userFriendly_fxns.R +++ b/tests/testthat/tests_userFriendly_fxns.R @@ -224,6 +224,45 @@ test_that("state county tests",{ expect_equal(fromIDs, "Bacon County") }) +test_that("NGWMN functions working", { + testthat::skip_on_cran() + noDataSite <- "UTGS.401544112060301" + noDataSite <- readNGWMNlevels(featureID = noDataSite) + expect_true(is.data.frame(noDataSite)) + + #bounding box and a bigger request + bboxSites <- readNGWMNdata(service = "featureOfInterest", bbox = c(30, -99, 31, 102)) + #siteInfo <- readNGWMNsites(bboxSites$site[1:100]) + + #one site + site <- "USGS.430427089284901" + oneSite <- readNGWMNlevels(featureID = site) + siteInfo <- readNGWMNsites(site) + expect_true(is.numeric(oneSite$value)) + expect_true(is.character(oneSite$site)) + expect_true(is.data.frame(siteInfo)) + expect_true(nrow(siteInfo) > 0) + expect_true(nrow(oneSite) > 0) + + #non-USGS site + data <- readNGWMNlevels(featureID = "MBMG.1388") + expect_true(nrow(data) > 1) + expect_true(is.numeric(oneSite$value)) + + #sites with colons and NAs work + na_colons <- c(NA, bboxSites$site[202], NA, NA) + returnDF <- readNGWMNdata(service = "observation", featureID = na_colons) + expect_is(returnDF, "data.frame") + expect_true(nrow(returnDF) > 1) + expect_true(!is.null(attributes(returnDF)$siteInfo)) + + # sites <- c("USGS:424427089494701", NA) + # siteInfo <- readNGWMNsites(sites) + # expect_is(siteInfo, "data.frame") + # expect_true(nrow(siteInfo) == 1) + +}) + context("water year column") df_test <- data.frame(site_no = as.character(1:13),