Skip to content

Commit

Permalink
Weather data extraction more robust
Browse files Browse the repository at this point in the history
- tests and example code was failing when external data sources were offline or changed their delivery format
- now failures are captured correctly
  • Loading branch information
dschlaep committed May 17, 2024
1 parent a44279b commit ca2234c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
36 changes: 25 additions & 11 deletions R/sw_WeatherExtract.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,21 @@ sw_meteo_obtain_DayMet <- function(

#--- Download NRCS station data (if needed) ------
if (is.null(rawdata)) {
rawdata <- sw_download_DayMet(
longitude = x[["longitude"]],
latitude = x[["latitude"]],
years = start_year:end_year
rawdata <- try(
sw_download_DayMet(
longitude = x[["longitude"]],
latitude = x[["latitude"]],
years = start_year:end_year
),
silent = TRUE
)
}

#--- Prepare requested station data ------

#--- * Daily weather data (update with additional variables) ------
stopifnot(
!inherits(rawdata, "try-error"),
length(rawdata[["data"]][["tmax..deg.c."]]) > 0L
)

Expand Down Expand Up @@ -194,7 +198,7 @@ sw_download_SCAN <- function(nrcs_site_code, years) {
silent = TRUE
)

if (inherits(res, "try-error")) {
if (inherits(res, "try-error") || is.null(res)) {
stop("Download NRCS station data failed: ", res)
}

Expand All @@ -216,14 +220,20 @@ sw_download_SCAN <- function(nrcs_site_code, years) {
#' @examples
#' ## Example: SCAN station "Mccracken Mesa"
#' if (requireNamespace("curl") && curl::has_internet()) {
#' mm_scan <- rSOILWAT2::sw_meteo_obtain_SCAN(
#' x = 2140, # SCAN station code
#' start_year = 2015,
#' end_year = 2023
#' mm_scan <- try(
#' rSOILWAT2::sw_meteo_obtain_SCAN(
#' x = 2140, # SCAN station code
#' start_year = 2015,
#' end_year = 2023
#' )
#' )
#' }
#'
#' if (exists("mm_scan") && exists("mm_dm") && requireNamespace("graphics")) {
#' if (
#' exists("mm_scan") && !inherits(mm_scan, "try-error") &&
#' exists("mm_dm") &&
#' requireNamespace("graphics")
#' ) {
#' vars <- c("Tmax_C", "Tmin_C", "PPT_cm")
#' par_prev <- graphics::par(mfrow = grDevices::n2mfrow(length(vars)))
#'
Expand Down Expand Up @@ -252,13 +262,17 @@ sw_meteo_obtain_SCAN <- function(

#--- Download NRCS station data (if needed) ------
if (is.null(rawdata)) {
rawdata <- sw_download_SCAN(x, years = start_year:end_year)
rawdata <- try(
sw_download_SCAN(x, years = start_year:end_year),
silent = TRUE
)
}

#--- Prepare requested station data ------

#--- * Daily weather data (update with additional variables) ------
stopifnot(
!inherits(rawdata, "try-error"),
nrow(rawdata[["TMAX"]]) > 0L,
nrow(rawdata[["TMIN"]]) > 0L,
nrow(rawdata[["PRCP"]]) > 0L
Expand Down
16 changes: 11 additions & 5 deletions man/sw_meteo_obtain.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tests/testthat/test_WeatherExtraction.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ test_that("Weather data extraction", {
)
)

expect_obtained_meteo(mm_scan)
if (!inherits(mm_scan, "try-error")) {
expect_obtained_meteo(mm_scan)
}
}
})

0 comments on commit ca2234c

Please sign in to comment.