Skip to content

Commit 529e497

Browse files
Merge pull request #836 from jzemmels/statistics
Add statistics functions
2 parents 43e569a + cc213fc commit 529e497

24 files changed

Lines changed: 1213 additions & 189 deletions

‎.Rbuildignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,4 @@ vignettes/wqpData.rds
108108
_quarto.yml
109109
vignettes/basic_slides.Rmd
110110
vignettes/changes_slides.Rmd
111+
vignettes/daily_data_statistics.Rmd

‎DESCRIPTION‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: dataRetrieval
22
Type: Package
33
Title: Retrieval Functions for USGS and EPA Hydrology and Water Quality Data
4-
Version: 2.7.22
4+
Version: 2.7.22.9000
55
Authors@R: c(
66
person("Laura", "DeCicco", role = c("aut","cre"),
77
email = "ldecicco@usgs.gov",
@@ -26,7 +26,7 @@ Authors@R: c(
2626
comment=c(ORCID = "0000-0003-2521-5043")),
2727
person("Lee", "Stanish", role="ctb",
2828
comment=c(ORCID = "0000-0002-9775-6861")),
29-
person("Joeseph", "Zemmels", role="ctb",
29+
person("Joseph", "Zemmels", role="ctb",
3030
email = "jzemmels@usgs.gov",
3131
comment=c(ORCID = "0009-0008-1463-6313")),
3232
person("Elise", "Hinman", role="aut",
@@ -55,7 +55,8 @@ Imports:
5555
jsonlite,
5656
httr2,
5757
whisker,
58-
sf
58+
sf,
59+
data.table
5960
Suggests:
6061
covr,
6162
dplyr,

‎NAMESPACE‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export(checkWQPdates)
66
export(check_OGC_requests)
77
export(check_waterdata_sample_params)
88
export(constructNWISURL)
9-
export(constructUseURL)
109
export(constructWQPURL)
1110
export(construct_api_requests)
1211
export(construct_waterdata_sample_request)
@@ -56,6 +55,8 @@ export(read_waterdata_metadata)
5655
export(read_waterdata_monitoring_location)
5756
export(read_waterdata_parameter_codes)
5857
export(read_waterdata_samples)
58+
export(read_waterdata_stats_daterange)
59+
export(read_waterdata_stats_por)
5960
export(read_waterdata_ts_meta)
6061
export(renameNWISColumns)
6162
export(setAccess)
@@ -71,3 +72,12 @@ export(whatWQPsamples)
7172
export(whatWQPsites)
7273
export(wqp_check_status)
7374
export(zeroPad)
75+
importFrom(data.table,":=")
76+
importFrom(data.table,.BY)
77+
importFrom(data.table,.EACHI)
78+
importFrom(data.table,.GRP)
79+
importFrom(data.table,.I)
80+
importFrom(data.table,.N)
81+
importFrom(data.table,.NGRP)
82+
importFrom(data.table,.SD)
83+
importFrom(data.table,data.table)

‎NEWS‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
dataRetrieval 2.7.22.9000
2+
===================
3+
* Added data.table to Imports
4+
* Added data.table importFrom entries to NAMESPACE
5+
* Added read_waterdata_stats_por and read_waterdata_stats_daterange to access USGS daily data statistics.
6+
* Updated vignettes, examples, and README to reflect new stats functions.
7+
* Added tests for read_waterdata_stats functions
8+
* Updated next_req_url to allow paging through /statistics API output
9+
* Added deprecate message to readNWISstat function.
10+
11+
112
dataRetrieval 2.7.22
213
===================
314
* Added read_waterdata_latest_daily to access latest daily USGS water data.

‎R/constructNWISURL.R‎

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
#' provide statistics for each month and year within the range individually.
3636
#' @param statType character Only used for statistics service requests. Type(s)
3737
#' of statistics to output for daily values. Default is mean, which is the only
38-
#' option for monthly and yearly report types. See the statistics service documentation
39-
#' at <https://waterservices.usgs.gov/docs/statistics/> for a
40-
#' full list of codes.
38+
#' option for monthly and yearly report types.
4139
#' @keywords data import USGS web service
4240
#' @return url string
4341
#' @export
@@ -431,63 +429,4 @@ constructWQPURL <- function(siteNumbers,
431429
return(baseURL)
432430
}
433431

434-
#' Construct URL for NWIS water use data service
435-
#'
436-
#' Reconstructs URLs to retrieve data from here: <https://waterdata.usgs.gov/nwis/wu>
437-
#'
438-
#' @param years integer Years for data retrieval. Must be years ending in 0 or 5,
439-
#' or "ALL", which retrieves all available years.
440-
#' @param stateCd could be character (full name, abbreviation, id), or numeric (id)
441-
#' @param countyCd could be numeric (County IDs from countyCdLookup) or character ("ALL")
442-
#' @param categories character Two-letter cateogory abbreviation(s)
443-
#' @return url string
444-
#' @export
445-
#' @examples
446-
#' url <- constructUseURL(
447-
#' years = c(1990, 1995),
448-
#' stateCd = "Ohio",
449-
#' countyCd = c(1, 3),
450-
#' categories = "ALL"
451-
#' )
452-
#'
453-
constructUseURL <- function(years, stateCd, countyCd, categories) {
454-
455-
456-
if (is.null(stateCd)) {
457-
baseURL <- httr2::request(pkg.env[["useNat"]])
458-
baseURL <- httr2::req_url_query(baseURL,
459-
format = "rdb",
460-
rdb_compression = "value")
461-
} else {
462-
463-
stateCd <- stateCdLookup(input = stateCd, outputType = "postal")
464-
baseURL <- httr2::request("https://waterdata.usgs.gov/")
465-
baseURL <- httr2::req_url_path_append(baseURL, stateCd)
466-
baseURL <- httr2::req_url_path_append(baseURL,
467-
"nwis", "water_use")
468-
baseURL <- httr2::req_url_query(baseURL,
469-
format = "rdb",
470-
rdb_compression = "value")
471-
472-
if (!(is.null(countyCd) )) {
473-
474-
baseURL <- httr2::req_url_query(baseURL,
475-
wu_area = "county")
476-
baseURL <- httr2::req_url_query(baseURL,
477-
wu_county = countyCd,
478-
.multi = "comma")
479-
} else {
480-
baseURL <- httr2::req_url_query(baseURL,
481-
wu_area = "State Total")
482-
}
483-
}
484432

485-
baseURL <- httr2::req_url_query(baseURL,
486-
wu_year = years,
487-
.multi = "comma")
488-
baseURL <- httr2::req_url_query(baseURL,
489-
wu_category = categories,
490-
.multi = "comma")
491-
492-
return(baseURL)
493-
}
Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,8 @@ token_message)
5858
#'
5959
#' @docType data
6060
#' @export parameterCdFile
61-
#' @examplesIf is_dataRetrieval_user()
62-
#'
63-
#' \donttest{
64-
#' # Please migrate to:
65-
#' parameterCds <- read_waterdata_metadata("parameter-codes")
66-
#'
67-
#' }
61+
#' @examples
62+
#' head(parameterCdFile[, 1:2])
6863
NULL
6964

7065

@@ -118,13 +113,8 @@ NULL
118113
#' @docType data
119114
#' @export stateCd
120115
#' @keywords USGS stateCd
121-
#' @examplesIf is_dataRetrieval_user()
122-
#'
123-
#' \donttest{
124-
#' # Please migrate to:
125-
#' stateCd <- read_waterdata_metadata("states")
126-
#'
127-
#' }
116+
#' @examples
117+
#' head(stateCd)
128118
NULL
129119

130120
#' US County Code Lookup Table
@@ -146,15 +136,24 @@ NULL
146136
#' @docType data
147137
#' @export countyCd
148138
#' @keywords USGS countyCd
149-
#' @examplesIf is_dataRetrieval_user()
150-
#'
151-
#' \donttest{
152-
#' # Please migrate to:
153-
#' countyCd <- read_waterdata_metadata("counties")
154-
#'
155-
#' }
139+
#' @examples
140+
#' head(countyCd)
141+
NULL
142+
143+
## usethis namespace: start
144+
#' @importFrom data.table data.table
145+
#' @importFrom data.table :=
146+
#' @importFrom data.table .SD
147+
#' @importFrom data.table .BY
148+
#' @importFrom data.table .N
149+
#' @importFrom data.table .I
150+
#' @importFrom data.table .GRP
151+
#' @importFrom data.table .NGRP
152+
#' @importFrom data.table .EACHI
153+
## usethis namespace: end
156154
NULL
157155

156+
158157
# nolint start: commented_code_linter
159158
# Here's how to incorporate the state_county.json into the historic
160159
# sysdata.rda. The original data included some IDs that aren't in the json
@@ -205,21 +204,4 @@ NULL
205204
#
206205
# save(countyCd, stateCd, parameterCdFile, pCodeToName,
207206
# file = "R/sysdata.rda", compress = "xz")
208-
#
209-
# services <- c("daily", "time-series-metadata",
210-
# "monitoring-locations", "latest-continuous",
211-
# "field-measurements", "latest-daily",
212-
# "continuous")
213-
#
214-
# property_list <- list()
215-
#
216-
# for(i in services){
217-
# schema <- check_OGC_requests(endpoint = i, type = "schema")
218-
# properties <- names(schema$properties)
219-
# property_list[[i]] <- properties
220-
# }
221-
# rm(schema, i, services, properties)
222-
# save(countyCd, stateCd, parameterCdFile, pCodeToName, property_list, offsetLibrary,
223-
# file = "R/sysdata.rda", compress = "xz")
224-
#
225-
# # nolint end
207+
# nolint end

‎R/readNWISunit.R‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,10 @@ readNWISgwl <- function(siteNumbers,
610610
#' }
611611
readNWISstat <- function(siteNumbers, parameterCd, startDate = "", endDate = "", convertType = TRUE,
612612
statReportType = "daily", statType = "mean") {
613+
614+
.Deprecated(new = "read_waterdata_stats_por",
615+
package = "dataRetrieval",
616+
msg = "NWIS servers are slated for decommission. Please begin to migrate to either read_waterdata_stats_por or read_waterdata_stats_daterange.")
613617

614618
message(new_nwis_message())
615619
# check for NAs in site numbers

0 commit comments

Comments
 (0)