You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/daily_data_statistics.Rmd
+30-24Lines changed: 30 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -15,11 +15,14 @@ knitr::opts_chunk$set(
15
15
```
16
16
17
17
The `read_waterdata_stats_por` and `read_waterdata_stats_daterange` functions replace the legacy `readNWISstat` function.
18
-
This replacement is necessary because the legacy API service that `readNWISstat` will be decommissioned and replaced with a [modernized API](https://api.waterdata.usgs.gov/statistics/v0/docs).
18
+
This replacement is necessary because the legacy API service that `readNWISstat`uses will be decommissioned and replaced with a [modernized API](https://api.waterdata.usgs.gov/statistics/v0/docs).
19
19
This new API has two available endpoints, `observationNormals` and `observationIntervals`, that appear similar at first yet have important differences we want to highlight here.
20
20
21
21
```{r setup}
22
22
library(dataRetrieval)
23
+
library(ggplot2)
24
+
library(tidyr)
25
+
library(dplyr)
23
26
24
27
site1 <- "USGS-05428500"
25
28
```
@@ -53,11 +56,11 @@ You can filter these rows out of the data if you don't want them in downstream a
Before we go, let's look at an example that illustrates the benefits of the statistics API.
59
+
Let's now look at an example that illustrates the benefits of the statistics API.
57
60
In the example below, we pull all day-of-year discharge percentiles for our site.
58
61
Keep in mind that doing so *without* the statistics API would require us to download the **entire** daily period of record for this site and hand-compute these percentiles ourselves, a time- and resource-intensive process indeed.
59
62
60
-
For demonstration, we filter to the output to the January 1 day-of-year percentiles, which include a set of percentiles commonly used on WDFN webpages (e.g., [Wisconsin water conditions](https://waterdata.usgs.gov/state/wisconsin/)).
63
+
For demonstration, we filter the output to the January 1 day-of-year percentiles, which include a set of percentiles commonly used on WDFN webpages (e.g., [Wisconsin water conditions](https://waterdata.usgs.gov/state/wisconsin/)).
After a bit of data manipulation, we can then visualize the percentiles as "ribbons" on a plot.
86
-
The final visual shows the percentile bands as progressively darker ribbons, where the minima and maxima are shown as thin dashed curves and the median values as a solid gray curve.
89
+
Each ribbon spans between two percentiles returned by the /statistics API (e.g., minimum to 5th, 5th to 10th, etc).
Instead of `time_of_year` and `time_of_year_type` columns, this output contains `start_date`, `end_date`, and `interval_type` columns representing the daterange over which the average was calculated.
152
-
The first row shows the average January, 2025 discharge was about 219 cubic feet per second.
153
-
We again have extra rows: the second row contains the **calendar** year 2025 average and the third contains the **water** year 2025 average.
156
+
The first row shows the average January, 2024 discharge was about 112 cubic feet per second.
157
+
We again have extra rows: the second row contains the **calendar** year 2024 average and the third contains the **water** year 2024 average.
154
158
155
159
Annual statistics will be returned for any calendar/water years than intersect with the specified date range.
156
160
Consider the example below, where the `start_date` to `end_date` range is only 93 days yet happens to intersect with calendar **and** water years 2023 and 2024.
The `sample_count` column indicates that there were 22 observations used to compute these averages, suggesting the site's period of record is (at least) 22 years long.
222
-
We can verify this using the timeseries-metadata API endpoint, passing in the "parent" timeseries ID used to compute the mean:
From this output, we see the begin and end dates of the POR at indeed at least 22 years apart.
225
+
The statistics API does not follow the same OGC standards as the <https://api.waterdata.usgs.gov/ogcapi/v0/> endpoints.
226
+
This section will focus on important differences between the statistics and OGC-compliant APIs and other tips for working with the endpoint.
229
227
228
+
***No request limit or API token**: at time of writing, the statistics API does not limit the number of requests that can be made per hour. It also does not require you sign up for an API token. Requesting data from the statistics API does not count against your total request limit to the OGC-compliant APIs.
229
+
***The API always returns all columns**: compared to the OGC-compliant endpoints, which come with `skipGeometry` and `properties` arguments to limit the number of columns returned by the API, there is no way to request a subset of columns from the API.
230
230
231
+
***Month-of-year statistics**: to return month-of-year statistics using `read_waterdata_stats_por`, make sure the `start_date` to `end_date` range overlaps with the first day of the month for which you want to data. For example, `start_date = "01-01"` and `end_date = "03-01"` will return the month-of-year statistics for January, February, and March (in addition to the day-of-year statistics for each month-day in this range).
232
+
***Monthly and annual statistics**: when using `read_waterdata_stats_daterange`, the output will return monthly and annual summaries for every calendar month, calendar year, and water year that intersects with the `start_date` to `end_date` range. For example, `start_date = 2023-12-31` and `end_date = 2024-10-01` will return monthly statistics for each month between December, 2023 through October, 2024 **and** calendar year statistics for 2023 and 2024 **and** water year statistics for WY2024 and WY2025.
233
+
***Median comes with percentiles**: you never need to set `computation = c("median", "percentile")` as the median is returned as the 50th percentile. If you do ask for both median and percentiles, your data set will have two rows containing the median for each `parent_time_series_id`.
234
+
***Minimum and maximum do *not* come with percentiles**: minimum and maximum statistics are not returned as percentiles so use `computation = c("minimum", "maximum", "percentile")` if you want a "complete" set of order statistics.
235
+
***The API returns specific percentiles**: for `computation = "percentile"`, the API will only ever return the following percentiles: 5th, 10th, 25th, 50th, 75th, 90th, and 95th. If you want other percentiles, you'll need to pull the daily data period of record using `read_waterdata_daily` and compute them yourself.
236
+
***Pay attention to `sample_count`**: the `sample_count` column represents the number of observations used to compute the statistic. As stated in the [statistics documentation](https://waterdata.usgs.gov/statistics-documentation/#minimum-period-of-record-number-of-observations), there is no minimum requirement for the number of observations to calculate a statistic. This means reported monthly and annual statistics can be based on *one* daily observation from that month/year. In the case of a single observation, the reported `minimum`, `maximum`, `median`, and `arithmetic_mean` will all be equal to the value of that observation and any other percentiles will be `NA`.
0 commit comments