Skip to content

Commit 0d998c4

Browse files
Merge pull request #830 from ldecicco-USGS/latest_daily
Latest daily
2 parents ea1d6d3 + 1f35505 commit 0d998c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+546
-162
lines changed

.Rbuildignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ README.Rmd
4040
^codecov\.yml$
4141
^\.github$
4242
^\.gitlab$
43+
^\.quarto$
4344
vignettes/tutorial.Rmd
4445
vignettes/phosData.rds
4546
vignettes/waterservices.png
@@ -104,3 +105,6 @@ tests/manual/*
104105
vignettes/qwdata_changes.Rmd
105106
vignettes/nwisData.rds
106107
vignettes/wqpData.rds
108+
_quarto.yml
109+
vignettes/basic_slides.Rmd
110+
vignettes/changes_slides.Rmd

DESCRIPTION

Lines changed: 3 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.21
4+
Version: 2.7.21.9000
55
Authors@R: c(
66
person("Laura", "DeCicco", role = c("aut","cre"),
77
email = "[email protected]",
@@ -47,7 +47,7 @@ Copyright: This software is in the public domain because it contains materials
4747
Depends:
4848
R (>= 4.1.0)
4949
Imports:
50-
curl (>= 6.0.0),
50+
curl (>= 7.0.0),
5151
lubridate (>= 1.5.0),
5252
stats,
5353
utils,
@@ -67,5 +67,5 @@ Encoding: UTF-8
6767
BuildVignettes: true
6868
VignetteBuilder: knitr
6969
BugReports: https://github.com/DOI-USGS/dataRetrieval/issues
70-
RoxygenNote: 7.3.2
70+
RoxygenNote: 7.3.3
7171
Roxygen: list(markdown = TRUE)

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export(read_waterdata)
4949
export(read_waterdata_daily)
5050
export(read_waterdata_field_measurements)
5151
export(read_waterdata_latest_continuous)
52+
export(read_waterdata_latest_daily)
5253
export(read_waterdata_metadata)
5354
export(read_waterdata_monitoring_location)
5455
export(read_waterdata_parameter_codes)

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
dataRetrieval 2.7.22
2+
===================
3+
* Added read_waterdata_latest_daily to access latest daily USGS water data.
4+
* Added state_name and hydrologic_unit_code to read_waterdata_ts_meta
5+
16
dataRetrieval 2.7.21
27
===================
38
* Added read_waterdata_field_measurements to access new USGS water data API.
49
* Added deprecation warning to readNWISgwl and readNWISmeas.
510
* Added parent_time_series_id to read_waterdata_ts_meta.
611
* Updated some documentation.
12+
* Handle waterdata empty returns better.
713

814
dataRetrieval 2.7.20
915
===================

R/AAA.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pkg.env <- new.env()
88

99
services <- c("server", "daily", "time-series-metadata",
1010
"monitoring-locations", "latest-continuous",
11-
"field-measurements")
11+
"field-measurements", "latest-daily")
1212
collections <- c("parameter-codes", "agency-codes", "altitude-datums", "aquifer-codes",
1313
"aquifer-types", "coordinate-accuracy-codes", "coordinate-datum-codes",
1414
"coordinate-method-codes", "medium-codes",

R/read_waterdata.R

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#' for more information.
66
#'
77
#' @export
8-
#' @param service character, can be any existing collection such
9-
#' as "daily", "monitoring-locations", "time-series-metadata"
8+
#' @param service character, can be any existing collection.
109
#' @param CQL A string in a Common Query Language format.
1110
#' @param convertType logical, defaults to `TRUE`. If `TRUE`, the function
1211
#' will convert the data to dates and qualifier to string vector.
@@ -64,9 +63,12 @@ read_waterdata <- function(service,
6463

6564
output_id <- switch(service,
6665
"daily" = "daily_id",
66+
"latest-daily" = "latest_daily_id",
6767
"time-series-metadata" = "time_series_id",
6868
"monitoring-locations" = "monitoring_location_id",
6969
"latest-continuous" = "latest_continuous_id",
70+
"continuous" = "continuous_id",
71+
"field-measurements" = "field_measurement_id",
7072
service)
7173

7274
if(!"properties" %in% names(args)){
@@ -87,7 +89,18 @@ read_waterdata <- function(service,
8789

8890
return_list <- walk_pages(data_req, max_results)
8991

90-
return_list <- deal_with_empty(return_list, args[["properties"]], service)
92+
if(is.null(args[["skipGeometry"]])){
93+
skipGeometry <- FALSE
94+
} else if (is.na(args[["skipGeometry"]])){
95+
skipGeometry <- FALSE
96+
} else {
97+
skipGeometry <- args[["skipGeometry"]]
98+
}
99+
100+
return_list <- deal_with_empty(return_list, args[["properties"]],
101+
service,
102+
skipGeometry,
103+
convertType)
91104

92105
if(convertType) return_list <- cleanup_cols(return_list)
93106

R/read_waterdata_field_measurements.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161
#'
6262
#' old_df <- read_waterdata_field_measurements(monitoring_location_id = "USGS-425957088141001",
6363
#' time = c("1980-01-01", NA))
64+
#'
65+
#' surface_water <- read_waterdata_field_measurements(
66+
#' monitoring_location_id = c("USGS-07069000",
67+
#' "USGS-07064000",
68+
#' "USGS-07068000"),
69+
#' time = "2024-07-01T00:00:00Z/..",
70+
#' parameter_code = "00060")
6471
#'
6572
#'
6673
#' }

R/read_waterdata_latest_daily.R

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#' Get Latest USGS Daily Data
2+
#'
3+
#' @description `r get_description("latest-daily")`
4+
#'
5+
#' @export
6+
#' @param monitoring_location_id `r get_params("latest-daily")$monitoring_location_id`
7+
#' @param parameter_code `r get_params("latest-daily")$parameter_code`
8+
#' @param statistic_id `r get_params("latest-daily")$statistic_id`
9+
#' @param time `r get_params("latest-daily")$time`
10+
#' @param value `r get_params("latest-daily")$value`
11+
#' @param unit_of_measure `r get_params("latest-daily")$unit_of_measure`
12+
#' @param approval_status `r get_params("latest-daily")$approval_status`
13+
#' @param last_modified `r get_params("latest-daily")$last_modified`
14+
#' @param time_series_id `r get_params("latest-daily")$time_series_id`
15+
#' @param qualifier `r get_params("latest-daily")$qualifier`
16+
#' @param latest_daily_id `r get_params("latest-daily")$id`
17+
#' @param properties A vector of requested columns to be returned from the query.
18+
#' Available options are:
19+
#' `r schema <- check_OGC_requests(endpoint = "latest-daily", type = "schema"); paste(names(schema$properties), collapse = ", ")`
20+
#' @param bbox Only features that have a geometry that intersects the bounding
21+
#' box are selected.The bounding box is provided as four or six numbers, depending
22+
#' on whether the coordinate reference system includes a vertical axis (height or
23+
#' depth). Coordinates are assumed to be in crs 4326. The expected format is a numeric
24+
#' vector structured: c(xmin,ymin,xmax,ymax). Another way to think of it is c(Western-most longitude,
25+
#' Southern-most latitude, Eastern-most longitude, Northern-most longitude).
26+
#' @param limit The optional limit parameter is used to control the subset of the
27+
#' selected features that should be returned in each page. The maximum allowable
28+
#' limit is 10000. It may be beneficial to set this number lower if your internet
29+
#' connection is spotty. The default (`NA`) will set the limit to the maximum
30+
#' allowable limit for the service.
31+
#' @param max_results The optional maximum number of rows to return. This value
32+
#' must be less than the requested limit.
33+
#' @param skipGeometry This option can be used to skip response geometries for
34+
#' each feature. The returning object will be a data frame with no spatial
35+
#' information.
36+
#' @param convertType logical, defaults to `TRUE`. If `TRUE`, the function
37+
#' will convert the data to dates and qualifier to string vector.
38+
#' @examplesIf is_dataRetrieval_user()
39+
#'
40+
#' \donttest{
41+
#' site <- "USGS-02238500"
42+
#' pcode <- "00060"
43+
#' dv_data_sf <- read_waterdata_latest_daily(monitoring_location_id = site,
44+
#' parameter_code = pcode)
45+
#'
46+
#' dv_data_last_modified <- read_waterdata_latest_daily(monitoring_location_id = site,
47+
#' parameter_code = pcode,
48+
#' last_modified = "P7D")
49+
#'
50+
#' dv_data_trim <- read_waterdata_latest_daily(monitoring_location_id = site,
51+
#' parameter_code = pcode,
52+
#' properties = c("monitoring_location_id",
53+
#' "value",
54+
#' "time"))
55+
#'
56+
#' dv_data <- read_waterdata_latest_daily(monitoring_location_id = site,
57+
#' parameter_code = pcode,
58+
#' skipGeometry = TRUE)
59+
#'
60+
#' multi_site <- read_waterdata_latest_daily(monitoring_location_id = c("USGS-01491000",
61+
#' "USGS-01645000"),
62+
#' parameter_code = c("00060", "00010"))
63+
#'
64+
#' }
65+
read_waterdata_latest_daily <- function(monitoring_location_id = NA_character_,
66+
parameter_code = NA_character_,
67+
statistic_id = NA_character_,
68+
properties = NA_character_,
69+
time_series_id = NA_character_,
70+
latest_daily_id = NA_character_,
71+
approval_status = NA_character_,
72+
unit_of_measure = NA_character_,
73+
qualifier = NA_character_,
74+
value = NA,
75+
last_modified = NA_character_,
76+
skipGeometry = NA,
77+
time = NA_character_,
78+
bbox = NA,
79+
limit = NA,
80+
max_results = NA,
81+
convertType = TRUE){
82+
83+
service <- "latest-daily"
84+
output_id <- "latest_daily_id"
85+
86+
args <- mget(names(formals()))
87+
return_list <- get_ogc_data(args,
88+
output_id,
89+
service)
90+
91+
return_list <- return_list[order(return_list$time, return_list$monitoring_location_id), ]
92+
93+
return(return_list)
94+
}
95+
96+
97+

R/read_waterdata_ts_meta.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#' @param last_modified `r get_params("time-series-metadata")$last_modified`
1414
#' @param begin `r get_params("time-series-metadata")$begin`
1515
#' @param end `r get_params("time-series-metadata")$end`
16+
#' @param hydrologic_unit_code `r get_params("time-series-metadata")$hydrologic_unit_code`
17+
#' @param state_name `r get_params("time-series-metadata")$state_name`
1618
#' @param thresholds `r get_params("time-series-metadata")$thresholds`
1719
#' @param unit_of_measure `r get_params("time-series-metadata")$unit_of_measure`
1820
#' @param primary `r get_params("time-series-metadata")$primary`
@@ -55,6 +57,8 @@
5557
#' "end",
5658
#' "time_series_id"),
5759
#' skipGeometry = TRUE)
60+
#'
61+
#' meta_wi <- read_waterdata_ts_meta(state_name = "Wisconsin")
5862
#' }
5963
read_waterdata_ts_meta <- function(monitoring_location_id = NA_character_,
6064
parameter_code = NA_character_,
@@ -64,6 +68,8 @@ read_waterdata_ts_meta <- function(monitoring_location_id = NA_character_,
6468
last_modified = NA_character_,
6569
begin = NA_character_,
6670
end = NA_character_,
71+
hydrologic_unit_code = NA_character_,
72+
state_name = NA_character_,
6773
unit_of_measure = NA_character_,
6874
computation_period_identifier = NA_character_,
6975
computation_identifier = NA_character_,

R/walk_pages.R

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#' @param properties A vector of requested columns
55
#' @param service character, can be any existing collection such
66
#' as "daily", "monitoring-locations", "time-series-metadata"
7+
#' @param skipGeometry A logical for whether to return geometry
8+
#' @param convertType A logical for whether to convert value to numeric
79
#'
810
#' @return data.frame
911
#' @noRd
@@ -17,17 +19,50 @@
1719
#' properties = NA,
1820
#' service = "daily")
1921
#'
20-
deal_with_empty <- function(return_list, properties, service){
22+
deal_with_empty <- function(return_list, properties, service,
23+
skipGeometry,
24+
convertType){
25+
2126
if(nrow(return_list) == 0){
2227

2328
if(all(is.na(properties))){
2429
schema <- check_OGC_requests(endpoint = service, type = "schema")
2530
properties <- names(schema$properties)
2631
}
27-
return_list <- data.frame(matrix(nrow = 0, ncol = length(properties)))
32+
return_list <- data.frame(matrix(nrow = 0,
33+
ncol = length(properties)))
34+
return_list <- lapply(return_list, as.character)
2835
names(return_list) <- properties
36+
37+
single_params <- c("datetime", "last_modified", "begin", "end", "time")
38+
39+
for(i in single_params){
40+
if(i %in% names(return_list)){
41+
return_list[[i]] <- as.POSIXct(as.character(), origin = "1970-01-01")
42+
}
43+
}
44+
45+
if(convertType && service == "daily"){
46+
return_list$time <- as.Date(as.character())
47+
}
48+
49+
if(convertType && "value" %in% names(return_list)){
50+
return_list$value <- as.numeric()
51+
}
52+
53+
if(convertType && "contributing_drainage_area" %in% names(return_list)){
54+
return_list$contributing_drainage_area <- as.numeric()
55+
}
56+
57+
return_list <- data.frame(return_list)
58+
return_list$geometry <- NULL
59+
60+
if(!skipGeometry){
61+
return_list <- sf::st_as_sf(return_list, geometry = sf::st_sfc())
62+
}
63+
2964
}
30-
65+
3166
return(return_list)
3267
}
3368

@@ -126,6 +161,10 @@ cleanup_cols <- function(df, service = "daily"){
126161

127162
if("contributing_drainage_area" %in% names(df)){
128163
df$contributing_drainage_area <- as.numeric(df$contributing_drainage_area)
164+
}
165+
166+
if("drainage_area" %in% names(df)){
167+
df$drainage_area <- as.numeric(df$drainage_area)
129168
}
130169

131170
df
@@ -254,8 +293,7 @@ get_ogc_data <- function(args,
254293
service){
255294

256295
args[["service"]] <- service
257-
max_results <- args[["max_results"]]
258-
args[["max_results"]] <- NULL
296+
259297
args <- switch_arg_id(args,
260298
id_name = output_id,
261299
service = service)
@@ -269,9 +307,19 @@ get_ogc_data <- function(args,
269307

270308
req <- do.call(construct_api_requests, args)
271309

310+
max_results <- args[["max_results"]]
311+
args[["max_results"]] <- NULL
312+
272313
return_list <- walk_pages(req, max_results)
273314

274-
return_list <- deal_with_empty(return_list, properties, service)
315+
if(is.na(args[["skipGeometry"]])){
316+
skipGeometry <- FALSE
317+
} else {
318+
skipGeometry <- args[["skipGeometry"]]
319+
}
320+
321+
return_list <- deal_with_empty(return_list, properties, service,
322+
skipGeometry, convertType)
275323

276324
if(convertType) return_list <- cleanup_cols(return_list, service = service)
277325

0 commit comments

Comments
 (0)