Skip to content

Commit 28790e6

Browse files
committed
Merge branch 'master' of https://github.com/tbep-tech/tbeptools
2 parents 8586de0 + 73ebeec commit 28790e6

8 files changed

Lines changed: 297 additions & 125 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: tbeptools
22
Title: Data and Indicators for the Tampa Bay Estuary Program
3-
Version: 3.1.0.9022
4-
Date: 2026-01-29
3+
Version: 3.1.0.9023
4+
Date: 2026-02-01
55
Authors@R: c(
66
person(given = "Marcus",
77
family = "Beck",
@@ -74,7 +74,6 @@ Imports:
7474
purrr,
7575
reactable,
7676
readxl,
77-
rnoaa,
7877
rvest,
7978
sf,
8079
terra,
@@ -95,8 +94,6 @@ Suggests:
9594
rmarkdown,
9695
testthat (>= 2.1.0),
9796
covr
98-
Remotes:
99-
ropensci/rnoaa
10097
License: MIT + file LICENSE
10198
Encoding: UTF-8
10299
VignetteBuilder: knitr

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export(util_importwqwa)
104104
export(util_importwqwin)
105105
export(util_map)
106106
export(util_orgin)
107+
export(util_rain)
107108
import(dplyr)
108109
import(ggplot2)
109110
import(patchwork)
@@ -163,7 +164,6 @@ importFrom(plotly,subplot)
163164
importFrom(purrr,map)
164165
importFrom(purrr,pwalk)
165166
importFrom(reactable,colDef)
166-
importFrom(rnoaa,ncdc)
167167
importFrom(rvest,html_node)
168168
importFrom(rvest,html_table)
169169
importFrom(rvest,read_html)

R/anlz_hydroload.R

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
#' @param trace logical indicating if function progress is printed in the consol
88
#'
99
#' @details
10-
#' This function uses rainfall and streamflow data from NOAA and USGS and requires an API key. See the "Authentication" section under the help file for ncdc in the defunct rnoaa package. This key can be added to the R environment file and called for later use, see the examples.
10+
#' This function uses rainfall and streamflow data from NOAA and USGS and requires an API key. See the webpage <https://www.ncdc.noaa.gov/cdo-web/token> for retrieving a NOAA token. This key can be added to the R environment file and called for later use, see the examples.
1111
#'
1212
#' These estimates are used in annual compliance assessment reports produced by the Tampa Bay Nitrogen Management Consortium. Load estimates and adjustment factors are based on regression models in https://drive.google.com/file/d/11NT0NQ2WbPO6pVZaD7P7Z6qjcwO1jxHw/view?usp=drivesdk
1313
#'
1414
#' @concept analyze
1515
#'
16-
#' @importFrom rnoaa ncdc
17-
#'
1816
#' @return A data frame with hydrological load estimates by bay segments for the requested years
1917
#' @export
2018
#'
@@ -36,9 +34,6 @@
3634
#' }
3735
anlz_hydroload <- function(yrs, noaa_key = NULL, trace = FALSE){
3836

39-
if(!requireNamespace('rnoaa', quietly = TRUE))
40-
stop("Package \"noaa\" needed for this function to work. Please install it.", call. = FALSE)
41-
4237
res <- yrs %>%
4338
tibble::enframe('name', 'year') %>%
4439
dplyr::group_by(name) %>%
@@ -55,17 +50,10 @@ anlz_hydroload <- function(yrs, noaa_key = NULL, trace = FALSE){
5550
end <- paste0(yr, "-12-31")
5651

5752
# download NOAA UWS rainfall station data
58-
sp_rainfall <- ncdc(datasetid = "GHCND", stationid = "GHCND:USW00092806",
59-
datatypeid = "PRCP", startdate = start, enddate = end,
60-
limit = 500, add_units = TRUE, token = noaa_key)
61-
sp_rain <- sp_rainfall$data %>%
62-
dplyr::summarise(sum = sum(value)/254)
63-
64-
tia_rainfall <- ncdc(datasetid = "GHCND", stationid = "GHCND:USW00012842",
65-
datatypeid = "PRCP", startdate = start, enddate = end,
66-
limit = 500, add_units = TRUE, token = noaa_key)
67-
tia_rain <- tia_rainfall$data %>%
68-
dplyr::summarise(sum = sum(value)/254)
53+
sp_rain <- util_rain(station = 'GHCND:USW00092806', start = start, end = end,
54+
token = noaa_key, quiet = T)
55+
tia_rain <- util_rain(station = 'GHCND:USW00012842', start = start, end = end,
56+
token = noaa_key, quiet = T)
6957

7058
# download USGS streamflow data
7159
hr <- dataRetrieval::readNWISdv("02303000", "00060", start, end) %>%

R/util_rain.R

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#' Get rainfall data at NOAA NCDC sites
2+
#'
3+
#' Get rainfall data at NOAA NCDC sites
4+
#'
5+
#' @param station character for the station id to retrieve
6+
#' @param start character for start of time period as YYYY-MM-DD
7+
#' @param end character for end of time period as YYYY-MM-DD
8+
#' @param token character for the NOAA API key
9+
#' @param ntry numeric for the number of times to try to download the data
10+
#' @param quiet logical to print progress in the console
11+
#'
12+
#' @details This function is used to retrieve a long-term record of rainfall for the requested station. A NOAA API key is required to use the function.
13+
#'
14+
#' @return a single value data frame with the rainfall sum in inches
15+
#'
16+
#' @export
17+
#'
18+
#' @seealso \code{\link{anlz_hydroload}}
19+
#'
20+
#' @examples
21+
#' \dontrun{
22+
#' noaa_key <- Sys.getenv('NOAA_KEY')
23+
#' util_rain('GHCND:USW00092806', start = '2021-01-01', end = '2021-12-31', noaa_key)
24+
#' }
25+
util_rain <- function(station = NULL, start, end, token, ntry = 5, quiet = FALSE){
26+
27+
# empty data frame
28+
empt <- data.frame(
29+
sum = NA_real_
30+
)
31+
32+
# Build the API request URL
33+
base_url <- "https://www.ncei.noaa.gov/cdo-web/api/v2/data"
34+
query_params <- list(
35+
datasetid = 'GHCND',
36+
stationid = station,
37+
datatypeid = 'PRCP',
38+
startdate = start,
39+
enddate = end,
40+
limit = 1000, # Increased from 400 to get more data per request
41+
units = 'metric'
42+
)
43+
44+
# Make the API request
45+
dat <- try({
46+
response <- httr::GET(
47+
url = base_url,
48+
query = query_params,
49+
httr::add_headers(token = token)
50+
)
51+
52+
# Check if request was successful
53+
if(httr::status_code(response) != 200) {
54+
stop("API request failed with status code: ", httr::status_code(response))
55+
}
56+
57+
# Parse the JSON response
58+
content <- httr::content(response, as = "text", encoding = "UTF-8")
59+
parsed_data <- jsonlite::fromJSON(content)
60+
61+
# Return the results data frame
62+
if(!length(parsed_data$results) == 0) {
63+
parsed_data$results
64+
} else {
65+
empt
66+
}
67+
68+
}, silent = TRUE)
69+
70+
tryi <- 0
71+
while(inherits(dat, 'try-error') & tryi < ntry) {
72+
73+
if(!quiet)
74+
cat('Retrying...\n')
75+
76+
dat <- try({
77+
response <- httr::GET(
78+
url = base_url,
79+
query = query_params,
80+
httr::add_headers(token = token)
81+
)
82+
83+
if(httr::status_code(response) != 200) {
84+
stop("API request failed with status code: ", httr::status_code(response))
85+
}
86+
87+
content <- httr::content(response, as = "text", encoding = "UTF-8")
88+
parsed_data <- jsonlite::fromJSON(content)
89+
90+
if(!length(parsed_data$results) == 0) {
91+
parsed_data$results
92+
} else {
93+
empt
94+
}
95+
96+
}, silent = TRUE)
97+
98+
tryi <- tryi + 1
99+
}
100+
101+
if(tryi == ntry){
102+
if(!quiet) cat('Failed...\n')
103+
stop()
104+
}
105+
106+
# Return empty data frame with expected structure if no data
107+
if(nrow(dat) == 0)
108+
return(empt)
109+
110+
# convert mm to inches, sum across time period,
111+
out <- dat |>
112+
dplyr::mutate(
113+
value = value / 25.4
114+
) |>
115+
dplyr::summarise(sum = sum(value, na.rm = TRUE))
116+
117+
return(out)
118+
}

man/anlz_hydroload.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/util_rain.Rd

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)