Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge 'dev' into 'main' #8

Merged
merged 5 commits into from
Jun 23, 2023
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Description: Environmental ocean data modeled both for the present and for futur
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
RoxygenNote: 7.2.0
Suggests:
httr,
testthat (>= 3.0.0)
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(download_griddap_dataset)
export(download_dataset)
35 changes: 24 additions & 11 deletions R/draft.R → R/main.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#' Downloads a griddap dataset from an ERDDAP server
#'
#' @param erddap_server
#' @param url
#' @param dataset
#' @param variables
#' @param constraints
#' @param response
#' @param fmt
#' @param directory
#' @param verbose
#'
Expand All @@ -21,15 +21,16 @@
#' longitude = c(120, 130)
#' variables = c("o2_mean")
#' constraints = list(time, latitude, longitude)
#' fmt = "nc"
#' names(constraints) = c("time", "latitude", "longitude")
#' # Test call
#' download_griddap_dataset(url, datasetid, variables, constraints)
download_griddap_dataset = function(erddap_server,
dataset,
#' download_dataset(datasetid, variables, constraints)
download_dataset = function(dataset,
variables,
constraints,
response="nc",
directory="./",
url="https://erddap-test.emodnet.eu/erddap/",
fmt="nc",
directory=FALSE,
verbose=TRUE,
debug=FALSE
) {
Expand All @@ -39,7 +40,8 @@ download_griddap_dataset = function(erddap_server,
}
# Args to be passed to griddap call later on
docallargs = list()
out = rerddap::info(datasetid=dataset, url=erddap_server)
docallargs[["fmt"]] = fmt
out = rerddap::info(datasetid=dataset, url=url)
docallargs[["x"]] = out

printer(sprintf("Selected dataset %s.", dataset))
Expand All @@ -65,9 +67,12 @@ download_griddap_dataset = function(erddap_server,
printer(sprintf("Selected %s variables: %s", length(valid_variables), toString(valid_variables)))

# Set directory for storing data
cache_dir <- hoardr::hoard()
cache_dir$cache_path_set(full_path=directory)
docallargs[["store"]] = rerddap::disk(cache_dir$cache_path_get())

if (!isFALSE(directory)) {
cache_dir <- hoardr::hoard()
cache_dir$cache_path_set(full_path=directory)
docallargs[["store"]] = rerddap::disk(cache_dir$cache_path_get())
}

# Debug flag to check args if needed
if (debug) {
Expand All @@ -80,3 +85,11 @@ download_griddap_dataset = function(erddap_server,
return(res)
}


list_layers = function(url = "https://erddap-test.emodnet.eu/erddap/", filter_biooracle=TRUE) {
response = rerddap::ed_datasets("griddap", url=url)
if (isTRUE(filter_biooracle)) {
response = response %>% filter(grepl("biooracle",Dataset.ID))
}
return(response)
}
23 changes: 11 additions & 12 deletions man/download_griddap_dataset.Rd → man/download_dataset.Rd

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

14 changes: 9 additions & 5 deletions tests/testthat/test-download.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ test_that("download dataset works", {
# Test call
server_is_working <- httr::status_code(httr::HEAD(url)) == 200
if(server_is_working){
test <- download_griddap_dataset(url, datasetid, variables, constraints)
test <- download_dataset(datasetid, variables, constraints, url, fmt="csv")

expect_type(test, "list")
expect_s3_class(test, "griddap_nc")
expect_s3_class(test$data, "data.frame")
expect_s3_class(test, "griddap_csv")
expect_s3_class(test, "data.frame")

# Test download to custom dir
# Test download to custom dir, and csv response
temp_dir <- normalizePath(tempdir())
test_dir <- download_griddap_dataset(directory = temp_dir, url, datasetid, variables, constraints)
test_dir <- download_dataset(datasetid, variables, constraints, url, fmt="nc", directory = temp_dir)
expect_s3_class(test_dir, "griddap_nc")
expect_s3_class(test_dir$data, "data.frame")

print(test_dir$summary$filename)
print(dirname(test_dir$summary$filename))
out_dir <- normalizePath(dirname(test_dir$summary$filename))
expect_equal(out_dir, temp_dir)
}
Expand Down