header_tag.html

Skip to contents

Returns data from the NWIS web service. Arguments to the function should be based on https://waterservices.usgs.gov service calls. See examples below for ideas of constructing queries.

Usage

readNWISdata(..., asDateTime = TRUE, convertType = TRUE, tz = "UTC")

Arguments

...

see https://waterservices.usgs.gov/docs/site-service/ for a complete list of options. A list of arguments can also be supplied. One important argument to include is "service". Possible values are "iv" (for instantaneous), "dv" (for daily values), "gwlevels" (for groundwater levels), "site" (for site service), "measurement", and "stat" (for statistics service). Note: "measurement" calls go to: https://nwis.waterdata.usgs.gov/usa/nwis for data requests, and use different call requests schemes. The statistics service has a limited selection of arguments (see https://waterservices.usgs.gov/docs/site-service/).

asDateTime

logical, if TRUE returns date and time as POSIXct, if FALSE, Date

convertType

logical, defaults to TRUE. If TRUE, the function will convert the data to dates, datetimes, numerics based on a standard algorithm. If false, everything is returned as a character

tz

character to set timezone attribute of dateTime. Default is "UTC", and converts the date times to UTC, properly accounting for daylight savings times based on the data's provided tz_cd column. Possible values to provide are "America/New_York", "America/Chicago", "America/Denver", "America/Los_Angeles", "America/Anchorage", as well as the following which do not use daylight savings time: "America/Honolulu", "America/Jamaica", "America/Managua", "America/Phoenix", and "America/Metlakatla". See also OlsonNames() for more information on time zones.

Value

A data frame with the following columns:

NameTypeDescription
agencycharacterThe NWIS code for the agency reporting the data
sitecharacterThe USGS site number
dateTimePOSIXctThe date and time (if applicable) of the measurement, converted to UTC for unit value data. R only allows one time zone attribute per column. For unit data spanning a time zone change, converting the data to UTC solves this problem. For daily data, the time zone attribute is the time zone of the first returned measurement.
tz_cdcharacterThe time zone code for dateTime column
codecharacterAny codes that qualify the corresponding value
valuenumericThe numeric value for the parameter

Note that code and value are repeated for the parameters requested. The names are of the form X_D_P_S, where X is literal, D is an option description of the parameter, P is the parameter code, and S is the statistic code (if applicable).

There are also several useful attributes attached to the data frame:

NameTypeDescription
urlcharacterThe url used to generate the data
siteInfodata.frameA data frame containing information on the requested sites
variableInfodata.frameA data frame containing information on the requested parameters
statisticInfodata.frameA data frame containing information on the requested statistics on the data
queryTimePOSIXctThe time the data was returned

Details

This function requires users to create their own arguments based on the NWIS web services. It is a more complicated function to use compared to other NWIS functions such as readNWISdv, readNWISuv, readNWISgwl, etc. However, this function adds a lot of flexibility to the possible queries. This function will also behave exactly as NWIS when it comes to date queries. NWIS by default will only return the latest value for the daily and instantaneous services. So if you do not provide a starting date, you will only get back the latest value. If you want the full period of record, you can use "startDate = '1900-01-01'". Other options for dates are periods, such as "period = 'P7D'" which translates to a period of 7 days. For period, use only a positive ISO-8601 duration format, which should not be expressed in periods of less than a day, or in increments of months M or years Y. period returns data for a site generally from now to a time in the past. Note that when period is used all data up to the most recent value are returned.

Examples

# \donttest{
# Examples not run for time considerations

dataTemp <- readNWISdata(stateCd = "OH", parameterCd = "00010", service = "dv")
#> GET: https://waterservices.usgs.gov/nwis/dv/?stateCd=OH&parameterCd=00010&format=waterml,1.1&
instFlow <- readNWISdata(
  sites = "05114000", service = "iv",
  parameterCd = "00060",
  startDate = "2014-05-01T00:00Z", endDate = "2014-05-01T12:00Z"
)
#> GET: https://nwis.waterservices.usgs.gov/nwis/iv/?sites=05114000&parameterCd=00060&startDT=2014-05-01T00:00Z&endDT=2014-05-01T12:00Z&format=waterml,1.1&

instFlowCDT <- readNWISdata(
  sites = "05114000", service = "iv",
  parameterCd = "00060",
  startDate = "2014-05-01T00:00", endDate = "2014-05-01T12:00",
  tz = "America/Chicago"
)
#> GET: https://nwis.waterservices.usgs.gov/nwis/iv/?sites=05114000&parameterCd=00060&startDT=2014-05-01T00:00&endDT=2014-05-01T12:00&format=waterml,1.1&

multiSite <- readNWISdata(
  sites = c("04025500", "040263491"),
  service = "iv", parameterCd = "00060"
)
#> GET: https://nwis.waterservices.usgs.gov/nwis/iv/?sites=04025500,040263491&parameterCd=00060&format=waterml,1.1&

bBoxEx <- readNWISdata(bBox = c(-83, 36.5, -81, 38.5), parameterCd = "00010")
#> GET: https://waterservices.usgs.gov/nwis/dv/?bBox=-83,36.5,-81,38.5&parameterCd=00010&format=waterml,1.1&

startDate <- as.Date("2013-10-01")
endDate <- as.Date("2014-09-30")
waterYear <- readNWISdata(
  bBox = c(-83, 36.5, -82.5, 36.75),
  parameterCd = "00010",
  service = "dv",
  startDate = startDate,
  endDate = endDate
)
#> GET: https://waterservices.usgs.gov/nwis/dv/?bBox=-83,36.5,-82.5,36.75&parameterCd=00010&startDT=2013-10-01&endDT=2014-09-30&format=waterml,1.1&

siteInfo <- readNWISdata(
  stateCd = "WI", parameterCd = "00010",
  hasDataTypeCd = "iv", service = "site"
)
#> GET: https://waterservices.usgs.gov/nwis/site/?stateCd=WI&parameterCd=00010&hasDataTypeCd=iv&format=rdb&
temp <- readNWISdata(
  bBox = c(-83, 36.5, -82.5, 36.75), parameterCd = "00010", service = "site",
  seriesCatalogOutput = TRUE
)
#> GET: https://waterservices.usgs.gov/nwis/site/?bBox=-83,36.5,-82.5,36.75&parameterCd=00010&seriesCatalogOutput=TRUE&format=rdb&
GWL <- readNWISdata(site_no = c("392725077582401", 
                                "375907091432201"),
                    parameterCd = "62610",
                    service = "gwlevels")
#> GET: https://nwis.waterdata.usgs.gov/nwis/gwlevels?site_no=392725077582401,375907091432201&parameterCd=62610&date_format=YYYY-MM-DD&rdb_inventory_output=file&TZoutput=0&format=rdb
                    
levels <- readNWISdata(stateCd = "WI", 
                       service = "gwlevels",
                       startDate = "2024-05-01",
                       endDate = "2024-05-30") 
#> GET: https://nwis.waterdata.usgs.gov/nwis/gwlevels?state_cd=WI&begin_date=2024-05-01&end_date=2024-05-30&date_format=YYYY-MM-DD&rdb_inventory_output=file&TZoutput=0&range_selection=date_range&list_of_search_criteria=state_cd&format=rdb
                    
meas <- readNWISdata(
  state_cd = "WI", service = "measurements",
  format = "rdb_expanded"
)
#> GET: https://waterdata.usgs.gov/nwis/measurements/?state_cd=WI&format=rdb_expanded&date_format=YYYY-MM-DD&rdb_inventory_output=file&TZoutput=0

waterYearStat <- readNWISdata(
  site = c("01646500"),
  service = "stat",
  statReportType = "annual",
  statYearType = "water",
  missingData = "on"
)
#> Please be aware the NWIS data service feeding this function is in BETA.
#> 
#>             Data formatting could be changed at any time, and is not guaranteed
#> GET: https://waterservices.usgs.gov/nwis/stat/?site=01646500&statReportType=annual&statYearType=water&missingData=on&format=rdb
monthlyStat <- readNWISdata(
  site = c("01646500"),
  service = "stat",
  statReportType = "monthly"
)
#> Please be aware the NWIS data service feeding this function is in BETA.
#> 
#>             Data formatting could be changed at any time, and is not guaranteed
#> GET: https://waterservices.usgs.gov/nwis/stat/?site=01646500&statReportType=monthly&format=rdb

dailyStat <- readNWISdata(
  site = c("01646500"),
  service = "stat",
  statReportType = "daily",
  statType = c("p25", "p50", "p75", "min", "max"),
  parameterCd = "00060"
)
#> Please be aware the NWIS data service feeding this function is in BETA.
#> 
#>             Data formatting could be changed at any time, and is not guaranteed
#> GET: https://waterservices.usgs.gov/nwis/stat/?site=01646500&statReportType=daily&statType=p25,p50,p75,min,max&parameterCd=00060&format=rdb

arg.list <- list(
  site = "03111548",
  statReportType = "daily",
  statType = c("p25", "p50", "p75", "min", "max"),
  parameterCd = "00060"
)
allDailyStats_2 <- readNWISdata(arg.list, service = "stat")
#> Please be aware the NWIS data service feeding this function is in BETA.
#> 
#>             Data formatting could be changed at any time, and is not guaranteed
#> GET: https://waterservices.usgs.gov/nwis/stat/?site=03111548&statReportType=daily&statType=p25,p50,p75,min,max&parameterCd=00060&format=rdb

# use county names to get data
dailyStaffordVA <- readNWISdata(
  stateCd = "Virginia",
  countyCd = "Stafford",
  parameterCd = "00060",
  startDate = "2015-01-01",
  endDate = "2015-01-30"
)
#> GET: https://waterservices.usgs.gov/nwis/dv/?countyCd=51179&parameterCd=00060&startDT=2015-01-01&endDT=2015-01-30&format=waterml,1.1&
va_counties <- c("51001", "51003", "51005", "51007", "51009", "51011", "51013", "51015")
va_counties_data <- readNWISdata(
  startDate = "2015-01-01", endDate = "2015-12-31",
  parameterCd = "00060", countycode = va_counties
)
#> GET: https://waterservices.usgs.gov/nwis/dv/?startDT=2015-01-01&endDT=2015-12-31&parameterCd=00060&countyCd=51001,51003,51005,51007,51009,51011,51013,51015&format=waterml,1.1&

site_id <- "01594440"
rating_curve <- readNWISdata(service = "rating", site_no = site_id, file_type = "base")
#> GET: https://waterdata.usgs.gov/nwisweb/get_ratings/?site_no=01594440&file_type=base
all_sites_base <- readNWISdata(service = "rating", file_type = "base")
#> GET: https://waterdata.usgs.gov/nwisweb/get_ratings/?file_type=base
all_sites_core <- readNWISdata(service = "rating", file_type = "corr")
#> GET: https://waterdata.usgs.gov/nwisweb/get_ratings/?file_type=corr
all_sites_exsa <- readNWISdata(service = "rating", file_type = "exsa")
#> GET: https://waterdata.usgs.gov/nwisweb/get_ratings/?file_type=exsa
all_sites_24hrs <- readNWISdata(service = "rating", file_type = "exsa", period = 24)
#> GET: https://waterdata.usgs.gov/nwisweb/get_ratings/?file_type=exsa&period=24

peak_data <- readNWISdata(
  service = "peak",
  site_no = c("01594440", "040851325"),
  range_selection = "data_range"
)
#> GET: https://nwis.waterdata.usgs.gov/usa/nwis/peak/?site_no=01594440,040851325&range_selection=data_range&date_format=YYYY-MM-DD&rdb_inventory_output=file&TZoutput=0&format=rdb

peak_data <- readNWISdata(
  service = "peak",
  state_cd = "PA"
)
#> GET: https://nwis.waterdata.usgs.gov/usa/nwis/peak/?state_cd=PA&date_format=YYYY-MM-DD&rdb_inventory_output=file&TZoutput=0&format=rdb

peak_data <- readNWISdata(
  service = "peak",
  huc2_cd = "20"
)
#> GET: https://nwis.waterdata.usgs.gov/usa/nwis/peak/?huc2_cd=20&date_format=YYYY-MM-DD&rdb_inventory_output=file&TZoutput=0&list_of_search_criteria=huc2_cd&format=rdb
# }