diff --git a/NAMESPACE b/NAMESPACE index f1d2b6d2a24a646aa5a176b6dfe5be2b2e29933b..0a98d2bf9c9d85c35e406542ce00371798c987c7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,6 +15,7 @@ export(getDataAvailability) export(getDataFromFile) export(getGeneralWQPData) export(getMetaData) +export(getNWISData) export(getNWISSites) export(getParameterInfo) export(getQWDataFromFile) diff --git a/R/basicWQPData.R b/R/basicWQPData.R index d23328a53c16eb7089c6db8b234bf284ead38176..6bece887f97d0cc65a3d4987006a63528eb692e3 100644 --- a/R/basicWQPData.R +++ b/R/basicWQPData.R @@ -64,7 +64,7 @@ basicWQPData <- function(url){ if(length(unique(timeZoneStart)) == 1){ retval$ActivityStartDateTime <- with(retval, as.POSIXct(paste(ActivityStartDate, ActivityStartTime.Time),format="%Y-%m-%d %H:%M:%S", tz=unique(timeZoneStart))) } else { - warning("Mixed time zone information") +# warning("Mixed time zone information") if(any(is.na(timeZoneStart))){ warning("Missing time zone information, all dateTimes default to user's local time") retval$ActivityStartDateTime <- with(retval, as.POSIXct(paste(ActivityStartDate, ActivityStartTime.Time), format="%Y-%m-%d %H:%M:%S"),tz=Sys.timezone()) diff --git a/R/getNWISData.r b/R/getNWISData.r new file mode 100644 index 0000000000000000000000000000000000000000..bd1cf61f1f1e26f2a635b7787f39f64f9959e609 --- /dev/null +++ b/R/getNWISData.r @@ -0,0 +1,32 @@ +#' General Data Import from NWIS +#' +#' Returns data from the NWIS web service. +#' Arguments to the function should be based on \url{http://waterservices.usgs.gov} service calls. +#' +#' @param service string. Possible values are "iv" (for instantaneous), "dv" (for daily values), "gwlevels" +#' (for groundwater levels), and "qwdata" (for water quality) +#' @param \dots see \url{http://waterservices.usgs.gov/rest/Site-Service.html#Service} for a complete list of options +#' @keywords data import NWIS web service +#' @return retval dataframe +#' @export +#' @examples +#' dataTemp <- getNWISData(stateCd="OH",parameterCd="00010") +getNWISData <- function(service="dv", ...){ + + matchReturn <- match.call() + + values <- sapply(matchReturn[-1], function(x) URLencode(as.character(paste(eval(x),collapse="",sep="")))) + + urlCall <- paste(paste(names(values),values,sep="="),collapse="&") + + + baseURL <- paste0("http://waterservices.usgs.gov/nwis/",service,"/?format=rdb&") + urlCall <- paste0(baseURL,urlCall) + if(service=="qwdata"){ + urlCall <- paste0(urlCall,"&siteOutput=expanded") + } + + retval <- getRDB1Data(urlCall) + + return(retval) +} diff --git a/R/getRDB1Data.r b/R/getRDB1Data.r index de62aaf106a4a0793ff4e8ea08621ade487018f0..a3053cfa27e833a7d2d321d9c8fc1fdb5514f2bd 100644 --- a/R/getRDB1Data.r +++ b/R/getRDB1Data.r @@ -21,7 +21,8 @@ #' multiData <- getRDB1Data(urlMulti) #' unitDataURL <- constructNWISURL(siteNumber,property, #' as.character(Sys.Date()),as.character(Sys.Date()),'uv',format='tsv') -#' unitData <- getRDB1Data(unitDataURL, asDateT=TRUE) +#' unitData <- getRDB1Data(unitDataURL, asDateTime=TRUE) +#' mulitSites <- getRDB1Data("http://waterservices.usgs.gov/nwis/dv/?format=rdb&stateCd=OH¶meterCd=00010") getRDB1Data <- function(obs_url,asDateTime=FALSE){ retval = tryCatch({ @@ -52,6 +53,16 @@ getRDB1Data <- function(obs_url,asDateTime=FALSE){ dataType <- tmp[1,] data <- tmp[-1,] + multiSiteCorrections <- -which(as.logical(apply(data[,1:2], 1, FUN=function(x) all(x %in% as.character(dataType[,1:2]))))) + if(length(multiSiteCorrections) > 0){ + data <- data[multiSiteCorrections,] + + findRowsWithHeaderInfo <- as.integer(apply(data[,1:2], 1, FUN = function(x) if(x[1] == names(data)[1] & x[2] == names(data)[2]) 1 else 0)) + findRowsWithHeaderInfo <- which(findRowsWithHeaderInfo == 0) + data <- data[findRowsWithHeaderInfo,] + + } + if(sum(regexpr('d$', dataType) > 0) > 0){ if (asDateTime){ @@ -63,7 +74,7 @@ getRDB1Data <- function(obs_url,asDateTime=FALSE){ if(length(unique(timeZone)) == 1){ data[,regexpr('d$', dataType) > 0] <- as.POSIXct(data[,regexpr('d$', dataType) > 0], "%Y-%m-%d %H:%M", tz = unique(timeZone)) } else { - warning("Mixed time zone information") +# warning("Mixed time zone information") for(i in seq_along(row.names(data))){ data[i,regexpr('d$', dataType) > 0] <- as.POSIXct(data[i,regexpr('d$', dataType) > 0], "%Y-%m-%d %H:%M", tz = timeZone[i]) } diff --git a/man/getNWISData.Rd b/man/getNWISData.Rd new file mode 100644 index 0000000000000000000000000000000000000000..75ab364904ab4322007181050d9d51b91780409d --- /dev/null +++ b/man/getNWISData.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2 (4.0.2): do not edit by hand +\name{getNWISData} +\alias{getNWISData} +\title{General Data Import from NWIS} +\usage{ +getNWISData(service = "dv", ...) +} +\arguments{ +\item{service}{string. Possible values are "iv" (for instantaneous), "dv" (for daily values), "gwlevels" +(for groundwater levels), and "qwdata" (for water quality)} + +\item{\dots}{see \url{http://waterservices.usgs.gov/rest/Site-Service.html#Service} for a complete list of options} +} +\value{ +retval dataframe +} +\description{ +Returns data from the NWIS web service. +Arguments to the function should be based on \url{http://waterservices.usgs.gov} service calls. +} +\examples{ +dataTemp <- getNWISData(stateCd="OH",parameterCd="00010") +} +\keyword{NWIS} +\keyword{data} +\keyword{import} +\keyword{service} +\keyword{web} + diff --git a/man/getRDB1Data.Rd b/man/getRDB1Data.Rd index 4ce65b1483b02db6b5f9530b3122c4bbf5e8ebb0..2a48835f8631b5dd4da2d2fd1231ec737925920d 100644 --- a/man/getRDB1Data.Rd +++ b/man/getRDB1Data.Rd @@ -31,6 +31,7 @@ urlMulti <- constructNWISURL("04085427",c("00060","00010"), multiData <- getRDB1Data(urlMulti) unitDataURL <- constructNWISURL(siteNumber,property, as.character(Sys.Date()),as.character(Sys.Date()),'uv',format='tsv') -unitData <- getRDB1Data(unitDataURL, asDateT=TRUE) +unitData <- getRDB1Data(unitDataURL, asDateTime=TRUE) +mulitSites <- getRDB1Data("http://waterservices.usgs.gov/nwis/dv/?format=rdb&stateCd=OH¶meterCd=00010") }