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

Added a static table of parameter codes to improve efficiency: parameterCdFile.

parent 59f62054
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
#' @examples #' @examples
#' # These examples require an internet connection to run #' # These examples require an internet connection to run
#' availableData <- getDataAvailability('05114000') #' availableData <- getDataAvailability('05114000')
getDataAvailability <- function(siteNumber="",interactive=TRUE){ getDataAvailability <- function(siteNumber,interactive=TRUE){
# Checking for 8 digit site ID: # Checking for 8 digit site ID:
siteNumber <- formatCheckSiteNumber(siteNumber,interactive=interactive) siteNumber <- formatCheckSiteNumber(siteNumber,interactive=interactive)
...@@ -41,6 +41,7 @@ getDataAvailability <- function(siteNumber="",interactive=TRUE){ ...@@ -41,6 +41,7 @@ getDataAvailability <- function(siteNumber="",interactive=TRUE){
pcodeINFO <- getParameterInfo(pCodes,interactive) pcodeINFO <- getParameterInfo(pCodes,interactive)
SiteFile <- merge(SiteFile,pcodeINFO,by="parameter_cd") SiteFile <- merge(SiteFile,pcodeINFO,by="parameter_cd")
# SiteFile <- merge(SiteFile,pcodeINFO,by.x="parameter_cd",by.y="parm_cd")
return(SiteFile) return(SiteFile)
} }
...@@ -14,21 +14,22 @@ ...@@ -14,21 +14,22 @@
getParameterInfo <- function(parameterCd,interactive=TRUE){ getParameterInfo <- function(parameterCd,interactive=TRUE){
parameterCd <- formatCheckParameterCd(parameterCd, interactive=interactive) parameterCd <- formatCheckParameterCd(parameterCd, interactive=interactive)
urlParameterCd <- "http://nwis.waterdata.usgs.gov/nwis/pmcodes/pmcodes?radio_pm_search=param_group&pm_group=All+--+include+all+parameter+groups&pm_search=&casrn_search=&srsname_search=&format=rdb&show=parameter_group_nm&show=parameter_nm&show=casrn&show=srsname&show=parameter_units" # urlParameterCd <- "http://nwis.waterdata.usgs.gov/nwis/pmcodes/pmcodes?radio_pm_search=param_group&pm_group=All+--+include+all+parameter+groups&pm_search=&casrn_search=&srsname_search=&format=rdb&show=parameter_group_nm&show=parameter_nm&show=casrn&show=srsname&show=parameter_units"
# # urlParameterCd <- "http://help.waterdata.usgs.gov/code/parameter_cd_query?fmt=rdb&group_cd=%"
parameterCdFile <- read.delim( # parameterCdFile <- read.delim(
urlParameterCd, # urlParameterCd,
header = TRUE, # header = TRUE,
quote="\"", # quote="\"",
dec=".", # dec=".",
sep='\t', # sep='\t',
colClasses=c('character'), # colClasses=c('character'),
fill = TRUE, # fill = TRUE,
comment.char="#") # comment.char="#")
dataType <- parameterCdFile[1,] # dataType <- parameterCdFile[1,]
parameterCdFile <- parameterCdFile[-1,] # parameterCdFile <- parameterCdFile[-1,]
parameterData <- parameterCdFile[parameterCdFile$parameter_cd %in% parameterCd,] parameterData <- parameterCdFile[parameterCdFile$parameter_cd %in% parameterCd,]
# parameterData <- parameterCdFile[parameterCdFile$parm_cd %in% parameterCd,]
return(parameterData) return(parameterData)
} }
...@@ -11,6 +11,9 @@ ...@@ -11,6 +11,9 @@
#' siteNumber <- '05114000' #' siteNumber <- '05114000'
#' rawData <- retrieveNWISData(siteNumber,c("00010","00060","00300"),"2001-01-01","2002-01-01",StatCd=c("00001","00003")) #' rawData <- retrieveNWISData(siteNumber,c("00010","00060","00300"),"2001-01-01","2002-01-01",StatCd=c("00001","00003"))
#' rawData <- renameColumns(rawData) #' rawData <- renameColumns(rawData)
#' today <- as.character(Sys.Date())
#' rawData2 <- retrieveUnitNWISData(siteNumber,c("00010","00060"),today,today)
#' rawData2 <- renameColumns(rawData2)
renameColumns <- function(rawData){ renameColumns <- function(rawData){
columnNames <- names(rawData) columnNames <- names(rawData)
...@@ -34,12 +37,14 @@ renameColumns <- function(rawData){ ...@@ -34,12 +37,14 @@ renameColumns <- function(rawData){
DDnum <- gsub("X","",DDnum) DDnum <- gsub("X","",DDnum)
if (!any(duplicated(pCodes))){ if (!any(duplicated(pCodes))){
dataColNames <- pcodeINFO$srsname[which(pcodeINFO$parameter_cd %in% pCodes)] dataColNames <- pcodeINFO$parameter_nm[which(pcodeINFO$parameter_cd %in% pCodes)]
# dataColNames <- pcodeINFO$srsname[which(pcodeINFO$parameter_cd %in% pCodes)]
dataColNames <- paste(dataColNames,statCd,sep="") dataColNames <- paste(dataColNames,statCd,sep="")
} else { } else {
dataColNames <- rep(NA,length(dataCol_names)) dataColNames <- rep(NA,length(dataCol_names))
for (i in 1:length(dataCol_names)){ for (i in 1:length(dataCol_names)){
dataColNames[i] <- pcodeINFO$srsname[which(pcodeINFO$parameter_cd %in% pCodes[i])] dataColNames[i] <- pcodeINFO$parameter_nm[which(pcodeINFO$parameter_cd %in% pCodes[i])]
# dataColNames[i] <- pcodeINFO$srsname[which(pcodeINFO$parameter_cd %in% pCodes[i])]
if((!(pCodes[i] %in% duplicated(pCodes))) && (pCodes[i] != pCodes[anyDuplicated(pCodes)])){ if((!(pCodes[i] %in% duplicated(pCodes))) && (pCodes[i] != pCodes[anyDuplicated(pCodes)])){
dataColNames[i] <- paste(dataColNames[i],statCd[i],sep="") dataColNames[i] <- paste(dataColNames[i],statCd[i],sep="")
} else { } else {
......
#' Raw Data Import for USGS NWIS Data #' Raw Data Import for USGS NWIS Data
#' #'
#' Imports data from NWIS web service. This function gets the data from here: \url{http://waterservices.usgs.gov/} #' Imports data from NWIS web service. This function gets the data from here: \url{http://waterservices.usgs.gov/}
#' A list of parameter codes can be found here: \url{http://nwis.waterdata.usgs.gov/nwis/pmcodes/} #' A list of parameter codes can be found here: \url{http://help.waterdata.usgs.gov/codes-and-parameters/parameters}
#' A list of statistic codes can be found here: \url{http://nwis.waterdata.usgs.gov/nwis/help/?read_file=stat&format=table} #' A list of statistic codes can be found here: \url{http://help.waterdata.usgs.gov/code/stat_code_query?fmt=html}
#' #'
#' @param siteNumber string USGS site number. This is usually an 8 digit number #' @param siteNumber string USGS site number. This is usually an 8 digit number
#' @param ParameterCd string USGS parameter code. This is usually an 5 digit number. #' @param ParameterCd string USGS parameter code. This is usually an 5 digit number.
#' @param StartDate string starting date for data retrieval in the form YYYY-MM-DD. #' @param StartDate string starting date for data retrieval in the form YYYY-MM-DD.
#' @param EndDate string ending date for data retrieval in the form YYYY-MM-DD. #' @param EndDate string ending date for data retrieval in the form YYYY-MM-DD.
#' @param StatCd string USGS statistic code. This is usually 5 digits. Daily mean (00003) is the default. #' @param StatCd string USGS statistic code. This is usually 5 digits. Daily mean (00003) is the default.
#' @param format string, can be "tsv" or "xml", and is only applicable for daily and unit value requests. "tsv" returns results faster, but there is a possiblitiy that an incomplete file is returned without warning. XML is slower, #' @param format string, can be 'tsv' or 'xml', and is only applicable for daily and unit value requests. 'tsv' returns results faster, but there is a possiblitiy that an incomplete file is returned without warning. XML is slower,
#' but will offer a warning if the file was incomplete (for example, if there was a momentary problem with the internet connection). It is possible to safely use the "tsv" option, #' but will offer a warning if the file was incomplete (for example, if there was a momentary problem with the internet connection). It is possible to safely use the 'tsv' option,
#' but the user must carefully check the results to see if the data returns matches what is expected. The default is "tsv". #' but the user must carefully check the results to see if the data returns matches what is expected. The default is 'tsv'.
#' @param interactive logical Option for interactive mode. If true, there is user interaction for error handling and data checks. #' @param interactive logical Option for interactive mode. If true, there is user interaction for error handling and data checks.
#' @keywords data import USGS web service
#' @return data dataframe with agency, site, dateTime, value, and code columns #' @return data dataframe with agency, site, dateTime, value, and code columns
#' @export #' @export
#' @keywords data import USGS web service
#' @examples #' @examples
#' # These examples require an internet connection to run #' # These examples require an internet connection to run
#' siteNumber <- '04085427' #' siteNumber <- '04085427'
#' startDate <- '2012-01-01' #' startDate <- '2012-01-01'
#' endDate <- '2012-06-30' #' endDate <- '2012-06-30'
#' pCode <- "00060" #' pCode <- '00060'
#' rawDailyQ <- retrieveNWISData(siteNumber,pCode, startDate, endDate) #' rawDailyQ <- retrieveNWISData(siteNumber,pCode, startDate, endDate)
#' rawDailyTemperature <- retrieveNWISData(siteNumber,'00010', startDate, endDate, StatCd='00001') #' rawDailyTemperature <- retrieveNWISData(siteNumber,'00010', startDate, endDate, StatCd='00001')
#' rawDailyTemperatureTSV <- retrieveNWISData(siteNumber,'00010', startDate, endDate, StatCd='00001',format="tsv") #' rawDailyTemperatureTSV <- retrieveNWISData(siteNumber,'00010', startDate, endDate, StatCd='00001',format='tsv')
#' rawDailyQAndTempMeanMax <- retrieveNWISData(siteNumber,c('00010','00060'), startDate, endDate, StatCd=c('00001','00003')) #' rawDailyQAndTempMeanMax <- retrieveNWISData(siteNumber,c('00010','00060'), startDate, endDate, StatCd=c('00001','00003'))
retrieveNWISData <- function (siteNumber,ParameterCd,StartDate,EndDate,StatCd="00003",format="tsv",interactive=TRUE){ retrieveNWISData <- function (siteNumber,ParameterCd,StartDate,EndDate,StatCd="00003",format="tsv",interactive=TRUE){
......
...@@ -45,3 +45,12 @@ NULL ...@@ -45,3 +45,12 @@ NULL
#' @author Robert M. Hirsch \email{rhirsch@@usgs.gov} #' @author Robert M. Hirsch \email{rhirsch@@usgs.gov}
#' @keywords water flow data #' @keywords water flow data
NULL NULL
#' List of USGS parameter codes
#'
#' Complete list of USGS parameter codes as of September 25, 2013.
#'
#' @name parameterCdFile
#' @docType data
#' @keywords USGS parameterCd
NULL
File added
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
\alias{getDataAvailability} \alias{getDataAvailability}
\title{USGS data availability} \title{USGS data availability}
\usage{ \usage{
getDataAvailability(siteNumber = "", interactive = TRUE) getDataAvailability(siteNumber, interactive = TRUE)
} }
\arguments{ \arguments{
\item{siteNumber}{string USGS site number. This is \item{siteNumber}{string USGS site number. This is
......
\docType{data}
\name{parameterCdFile}
\alias{parameterCdFile}
\title{List of USGS parameter codes}
\description{
Complete list of USGS parameter codes as of September 25,
2013.
}
\keyword{USGS}
\keyword{datasets}
\keyword{parameterCd}
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
siteNumber <- '05114000' siteNumber <- '05114000'
rawData <- retrieveNWISData(siteNumber,c("00010","00060","00300"),"2001-01-01","2002-01-01",StatCd=c("00001","00003")) rawData <- retrieveNWISData(siteNumber,c("00010","00060","00300"),"2001-01-01","2002-01-01",StatCd=c("00001","00003"))
rawData <- renameColumns(rawData) rawData <- renameColumns(rawData)
today <- as.character(Sys.Date())
rawData2 <- retrieveUnitNWISData(siteNumber,c("00010","00060"),today,today)
rawData2 <- renameColumns(rawData2)
} }
\keyword{USGS} \keyword{USGS}
\keyword{data} \keyword{data}
......
...@@ -22,16 +22,16 @@ ...@@ -22,16 +22,16 @@
\item{StatCd}{string USGS statistic code. This is usually \item{StatCd}{string USGS statistic code. This is usually
5 digits. Daily mean (00003) is the default.} 5 digits. Daily mean (00003) is the default.}
\item{format}{string, can be "tsv" or "xml", and is only \item{format}{string, can be 'tsv' or 'xml', and is only
applicable for daily and unit value requests. "tsv" applicable for daily and unit value requests. 'tsv'
returns results faster, but there is a possiblitiy that returns results faster, but there is a possiblitiy that
an incomplete file is returned without warning. XML is an incomplete file is returned without warning. XML is
slower, but will offer a warning if the file was slower, but will offer a warning if the file was
incomplete (for example, if there was a momentary problem incomplete (for example, if there was a momentary problem
with the internet connection). It is possible to safely with the internet connection). It is possible to safely
use the "tsv" option, but the user must carefully check use the 'tsv' option, but the user must carefully check
the results to see if the data returns matches what is the results to see if the data returns matches what is
expected. The default is "tsv".} expected. The default is 'tsv'.}
\item{interactive}{logical Option for interactive mode. \item{interactive}{logical Option for interactive mode.
If true, there is user interaction for error handling and If true, there is user interaction for error handling and
...@@ -45,19 +45,19 @@ ...@@ -45,19 +45,19 @@
Imports data from NWIS web service. This function gets Imports data from NWIS web service. This function gets
the data from here: \url{http://waterservices.usgs.gov/} the data from here: \url{http://waterservices.usgs.gov/}
A list of parameter codes can be found here: A list of parameter codes can be found here:
\url{http://nwis.waterdata.usgs.gov/nwis/pmcodes/} A list \url{http://help.waterdata.usgs.gov/codes-and-parameters/parameters}
of statistic codes can be found here: A list of statistic codes can be found here:
\url{http://nwis.waterdata.usgs.gov/nwis/help/?read_file=stat&format=table} \url{http://help.waterdata.usgs.gov/code/stat_code_query?fmt=html}
} }
\examples{ \examples{
# These examples require an internet connection to run # These examples require an internet connection to run
siteNumber <- '04085427' siteNumber <- '04085427'
startDate <- '2012-01-01' startDate <- '2012-01-01'
endDate <- '2012-06-30' endDate <- '2012-06-30'
pCode <- "00060" pCode <- '00060'
rawDailyQ <- retrieveNWISData(siteNumber,pCode, startDate, endDate) rawDailyQ <- retrieveNWISData(siteNumber,pCode, startDate, endDate)
rawDailyTemperature <- retrieveNWISData(siteNumber,'00010', startDate, endDate, StatCd='00001') rawDailyTemperature <- retrieveNWISData(siteNumber,'00010', startDate, endDate, StatCd='00001')
rawDailyTemperatureTSV <- retrieveNWISData(siteNumber,'00010', startDate, endDate, StatCd='00001',format="tsv") rawDailyTemperatureTSV <- retrieveNWISData(siteNumber,'00010', startDate, endDate, StatCd='00001',format='tsv')
rawDailyQAndTempMeanMax <- retrieveNWISData(siteNumber,c('00010','00060'), startDate, endDate, StatCd=c('00001','00003')) rawDailyQAndTempMeanMax <- retrieveNWISData(siteNumber,c('00010','00060'), startDate, endDate, StatCd=c('00001','00003'))
} }
\keyword{USGS} \keyword{USGS}
......
...@@ -301,12 +301,12 @@ An example of plotting the above data (Figure \ref{fig:getNWIStemperaturePlot}): ...@@ -301,12 +301,12 @@ An example of plotting the above data (Figure \ref{fig:getNWIStemperaturePlot}):
par(mar=c(5,5,5,5)) par(mar=c(5,5,5,5))
with(temperatureAndFlow, plot( with(temperatureAndFlow, plot(
datetime, Temperature_water_01, datetime, Temperature_water_degrees_Celsius_Max_01,
xlab="Date",ylab="Temperature [C]" xlab="Date",ylab="Temperature [C]"
)) ))
par(new=TRUE) par(new=TRUE)
with(temperatureAndFlow, plot( with(temperatureAndFlow, plot(
datetime, Stream_flow_mean_daily, datetime, Discharge_cubic_feet_per_second,
col="red",type="l",xaxt="n",yaxt="n",xlab="",ylab="",axes=FALSE col="red",type="l",xaxt="n",yaxt="n",xlab="",ylab="",axes=FALSE
)) ))
axis(4,col="red",col.axis="red") axis(4,col="red",col.axis="red")
......
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