Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: resourcecode
Title: Access to the 'RESOURCECODE' Hindcast Database
Version: 0.5.1.9000
Date: 2025-12-16
Version: 0.5.2
Date: 2026-01-05
Authors@R:
person("Nicolas", "Raillard", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-3385-5104"))
Expand Down Expand Up @@ -42,8 +42,7 @@ Suggests:
knitr,
mockery,
rmarkdown,
testthat,
vdiffr
testthat
LinkingTo:
Rcpp,
RcppArmadillo
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# resourcecode (development version)
# resourcecode 0.5.2

- Give informative message when remote database is not available

# resourcecode 0.5.1

Expand Down
32 changes: 25 additions & 7 deletions R/spectral_data_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@
#' @noRd
#' @keywords internal
download_nc_data <- function(url, destfile) {

# Ensure destfile exists only if successful
success <- tryCatch(
{
curl::curl_download(url, destfile = destfile, mode = "wb")
TRUE
},
error = function(e) {
message("Could not download spectral data.
The remote server may be unavailable or the URL may have changed.")
message(
"Could not download spectral data.
The remote server may be unavailable or the URL may have changed."
)
FALSE
},
warning = function(w) {
message("A warning occurred while downloading the spectral data.
The resource may have changed.\n", w)
message(
"A warning occurred while downloading the spectral data.
The resource may have changed.\n",
w
)
FALSE
}
)
Expand Down Expand Up @@ -62,11 +66,18 @@ get_2d_spectrum_raw <- function(point, year, month) {
"_spec.nc"
)


temp <- tempfile(fileext = ".nc")

file <- download_nc_data(url, temp)

if (is.null(file)) {
message(
"Could not download spectral data.
The remote server may be unavailable or the URL may have changed."
)
return(NULL)
}

nc <- ncdf4::nc_open(file)

on.exit({
Expand Down Expand Up @@ -136,6 +147,14 @@ get_1d_spectrum_raw <- function(point, year, month) {

file <- download_nc_data(url, temp)

if (is.null(file)) {
message(
"Could not download spectral data.
The remote server may be unavailable or the URL may have changed."
)
return(NULL)
}

nc <- ncdf4::nc_open(file)

on.exit({
Expand Down Expand Up @@ -339,7 +358,6 @@ get_2d_spectrum <- function(point, start = "1994-01-01", end = "1994-02-28") {
get_1d_spectrum <- function(point, start = "1994-01-01", end = "1994-02-28") {
stopifnot(length(point) == 1)


if (is.numeric(point)) {
point <- resourcecodedata::rscd_spectral[point, "name"]
}
Expand Down
40 changes: 40 additions & 0 deletions tests/testthat/test-data_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,43 @@ test_that("download_nc_data() fails gracefully when FTP not available", {

expect_null(result)
})

test_that("get_1d_spectrum() fails gracefully when FTP not available", {
download_nc_data <- getFromNamespace("download_nc_data", "resourcecode")
# Mock curl_download to throw an error (simulating network failure)
mock_download <- function(...) stop("FTP connection failed")

# Replace curl_download inside the function
testthat::local_mocked_bindings(download_nc_data = function(...) NULL)

expect_message(
result <- get_1d_spectrum(
"SEMREVO",
start = "1994-01-01",
end = "1994-02-28"
),
"Could not download spectral data"
)

expect_null(result)
})

test_that("get_2d_spectrum() fails gracefully when FTP not available", {
download_nc_data <- getFromNamespace("download_nc_data", "resourcecode")
# Mock curl_download to throw an error (simulating network failure)
mock_download <- function(...) stop("FTP connection failed")

# Replace curl_download inside the function
testthat::local_mocked_bindings(download_nc_data = function(...) NULL)

expect_message(
result <- get_2d_spectrum(
"SEMREVO",
start = "1994-01-01",
end = "1994-02-28"
),
"Could not download spectral data"
)

expect_null(result)
})
Loading