readNWISpCode.r 3.37 KB
Newer Older
Laura A DeCicco's avatar
Laura A DeCicco committed
1
2
#' USGS Parameter Data Retrieval
#'
3
#' Imports data from NWIS about meaured parameter based on user-supplied parameter code or codes.
Laura A DeCicco's avatar
Laura A DeCicco committed
4
#' This function gets the data from here: \url{https://nwis.waterdata.usgs.gov/nwis/pmcodes}
Laura A DeCicco's avatar
Laura A DeCicco committed
5
#'
6
#' @param parameterCd character of USGS parameter codes (or multiple parameter codes).  These are 5 digit number codes,
Laura A DeCicco's avatar
Laura A DeCicco committed
7
#' more information can be found here: \url{https://help.waterdata.usgs.gov/}. To get a 
8
#' complete list of all current parameter codes in the USGS, use "all" as the input.
Laura A DeCicco's avatar
Laura A DeCicco committed
9
#' @keywords data import USGS web service
Laura A DeCicco's avatar
Laura A DeCicco committed
10
#' @return parameterData data frame with the following information: 
Laura A DeCicco's avatar
Laura A DeCicco committed
11
12
13
14
15
16
17
18
19
20
#' \tabular{lll}{
#'   Name \tab Type \tab Description\cr
#'   parameter_cd \tab character \tab 5-digit USGS parameter code \cr
#'   parameter_group_nm \tab character \tab USGS parameter group name\cr
#'   parameter_nm \tab character \tab USGS parameter name\cr
#'   casrn \tab character \tab Chemical Abstracts Service (CAS) Registry Number\cr
#'   srsname \tab character \tab Substance Registry Services Name\cr
#'   parameter_units \tab character \tab Parameter units\cr
#' }
#' 
Laura A DeCicco's avatar
Laura A DeCicco committed
21
#' @export
Laura A DeCicco's avatar
Laura A DeCicco committed
22
#' @seealso \code{\link{importRDB1}}
Laura A DeCicco's avatar
Laura A DeCicco committed
23
#' @examples
Laura A DeCicco's avatar
Laura A DeCicco committed
24
#' paramINFO <- readNWISpCode(c('01075','00060','00931'))
Laura A DeCicco's avatar
Laura A DeCicco committed
25
#' paramINFO <- readNWISpCode(c('01075','00060','00931', NA))
Laura A DeCicco's avatar
Laura A DeCicco committed
26
readNWISpCode <- function(parameterCd){
Laura A DeCicco's avatar
Laura A DeCicco committed
27
 
Laura A DeCicco's avatar
Laura A DeCicco committed
28
29
30
  parameterCd.orig <- parameterCd
  parameterCd <- parameterCd[!is.na(parameterCd)]
  
31
  if(any(parameterCd == "all")){
Laura A DeCicco's avatar
Laura A DeCicco committed
32
    fullURL <- "https://nwis.waterdata.usgs.gov/nwis/pmcodes/pmcodes?radio_pm_search=param_group&pm_group=All+--+include+all+parameter+groups&format=rdb&show=parameter_group_nm&show=parameter_nm&show=casrn&show=srsname&show=parameter_units"
Laura A DeCicco's avatar
Laura A DeCicco committed
33
    parameterData <- importRDB1(fullURL, asDateTime = FALSE)
34
35
36
37
38
39
  } else {
    pcodeCheck <- all(nchar(parameterCd) == 5) & all(!is.na(suppressWarnings(as.numeric(parameterCd))))
    parameterData <- parameterCdFile[parameterCdFile$parameter_cd %in% parameterCd,]
  
    if(nrow(parameterData) != length(parameterCd)){
      if(length(parameterCd) == 1){
Laura A DeCicco's avatar
Laura A DeCicco committed
40
        suburl <- paste0("https://nwis.waterdata.usgs.gov/nwis/pmcodes/pmcodes?radio_pm_search=pm_search",
41
42
43
44
                     "&pm_search=", parameterCd,
                     "&format=rdb", "&show=parameter_group_nm",
                     "&show=parameter_nm", "&show=casrn",
                     "&show=srsname", "&show=parameter_units")
Laura A DeCicco's avatar
Laura A DeCicco committed
45
        parameterData <- importRDB1(suburl,asDateTime = FALSE)
46
      } else {
Laura A DeCicco's avatar
Laura A DeCicco committed
47
        fullURL <- "https://nwis.waterdata.usgs.gov/nwis/pmcodes/pmcodes?radio_pm_search=param_group&pm_group=All+--+include+all+parameter+groups&format=rdb&show=parameter_group_nm&show=parameter_nm&show=casrn&show=srsname&show=parameter_units"
48
        fullPcodeDownload <- importRDB1(fullURL)
Laura A DeCicco's avatar
Laura A DeCicco committed
49
        parameterData <- fullPcodeDownload[fullPcodeDownload$parameter_cd %in% parameterCd,]
50
      }
Laura A DeCicco's avatar
Laura A DeCicco committed
51
      
Laura A DeCicco's avatar
Laura A DeCicco committed
52
      if(nrow(parameterData) != length(parameterCd)){
Laura A DeCicco's avatar
Laura A DeCicco committed
53
        badPcode <- parameterCd[!(parameterCd %in% parameterData$parameter_cd)]
Laura A DeCicco's avatar
Laura A DeCicco committed
54
55
        warning("The following pCodes seem mistyped, and no information was returned: ",paste(badPcode,collapse=","))
      }
Laura A DeCicco's avatar
Laura A DeCicco committed
56
    } 
57
58
  }
  
Laura A DeCicco's avatar
Laura A DeCicco committed
59
60
61
62
63
64
65
66
67
  if(nrow(parameterData) != sum(is.na(parameterCd.orig))){
    na.params <- data.frame(matrix(ncol = ncol(parameterData), nrow = sum(is.na(parameterCd.orig))))
    names(na.params) <- names(parameterData)
    parameterData <- rbind(parameterData, na.params)
  }
  
  
  return(parameterData)
  
Laura A DeCicco's avatar
Laura A DeCicco committed
68
}