Skip to content

Commit

Permalink
Rename functions, add argument
Browse files Browse the repository at this point in the history
- download_if_not_there()
  -> download()
- check_or_download_shapes_germany()
  -> download_shapes_germany()
- add arg. "download" to default_projection_file()
  • Loading branch information
hsonne committed May 28, 2023
1 parent fde728b commit c9e0768
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 61 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Generated by roxygen2: do not edit by hand

export(calculate_masked_grid_stats)
export(check_or_download_shapes_germany)
export(convert_bin_to_raster_file)
export(coordinates_to_EPSG_4326)
export(crop_radolan_radial)
export(download_radolan)
export(download_shapes_germany)
export(extract_radolan_zip_files)
export(get_berlin_dwd_mask)
export(get_dwd_urls_metadata)
Expand Down
2 changes: 1 addition & 1 deletion R/download_daily_grids_germany.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ download_and_extract <- function(url, quiet = FALSE)
target_dir <- temp_dir(template = url)

# Download the file into the dedicated folder
file <- download_if_not_there(
file <- download(
url,
target_dir = target_dir,
quiet = quiet,
Expand Down
2 changes: 1 addition & 1 deletion R/download_into_folder_structure.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ download_into_folder_structure <- function(

# Download the files that are not yet in the target directory structure
mapply(
FUN = download_if_not_there,
FUN = download,
urls,
target_files,
MoreArgs = list(...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ if (FALSE)

RCurl::url.exists(url)

download.file(url, file.path(tempdir(), basename(url)), mode = "wb")
file <- kwb.dwd:::download(url, mode = "wb")
}

# check_or_download_shapes_germany ---------------------------------------------
# download_shapes_germany ------------------------------------------------------

#' Check Local Availability or Download Shape Files
#'
Expand All @@ -28,7 +28,7 @@ if (FALSE)
#' * [get_shapes_of_germany],
#' * [get_example_grid_germany],
#' * [download_radolan].
check_or_download_shapes_germany <- function(
download_shapes_germany <- function(
url = "https://geodata.ucdavis.edu/gadm/gadm4.0/shp/gadm40_DEU_shp.zip",
quiet = FALSE,
timeout = 60
Expand All @@ -47,7 +47,7 @@ check_or_download_shapes_germany <- function(

# If <shape_dir> does not contain .shp files, download the zip-archive from
# <url> and extract it into <shape_dir>
file <- try(silent = TRUE, suppressWarnings(download_if_not_there(
file <- try(silent = TRUE, suppressWarnings(download(
url,
target_dir = download_dir("shapes_germany"),
quiet = quiet,
Expand Down
4 changes: 2 additions & 2 deletions R/get_shapes_of_germany.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @export
#' @seealso
#' * [get_example_grid_germany],
#' * [check_or_download_shapes_germany].
#' * [download_shapes_germany].
get_shapes_of_germany <- function(recreate = FALSE, use_sf = FALSE)
{
if (!recreate) {
Expand Down Expand Up @@ -40,7 +40,7 @@ get_shapes_of_germany <- function(recreate = FALSE, use_sf = FALSE)
# List shape files. If required, the shape files are downloaded, unzipped and
# stored locally. They are downloaded from:
# https://geodata.ucdavis.edu/gadm/gadm4.0/shp/gadm40_DEU_shp.zip
shape_dir <- check_or_download_shapes_germany()
shape_dir <- download_shapes_germany()
files <- list_local_shape_files(shape_dir)

# Read shapes at different levels of detail
Expand Down
8 changes: 6 additions & 2 deletions R/provide_projection_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ provide_projection_file <- function(file, dbg = FALSE)
}

# default_projection_file ------------------------------------------------------
default_projection_file <- function(quiet = TRUE)
default_projection_file <- function(quiet = TRUE, download = TRUE)
{
url <- "https://opendata.dwd.de/climate_environment/CDC/help/gk3.prj"

download_if_not_there(url = url, quiet = quiet, file = file.path(
if (!download) {
return(url)
}

download(url = url, quiet = quiet, file = file.path(
system.file("extdata", package = "kwb.dwd"),
basename(url)
))
Expand Down
2 changes: 1 addition & 1 deletion R/read_asc_gz_file_into_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ read_lines_from_gz_file <- function(
assert_url(file, final_slash = FALSE)

# Download file from URL to temporary directory
file <- download_if_not_there(
file <- download(
file,
target_dir = temp_dir(),
quiet = TRUE,
Expand Down
46 changes: 24 additions & 22 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ download_dir <- function(...)
kwb.utils::createDirectory(dbg = FALSE)
}

# download_if_not_there --------------------------------------------------------
download_if_not_there <- function(
# download ---------------------------------------------------------------------
download <- function(
url,
file = file.path(target_dir, basename(url)),
target_dir = download_dir(),
Expand All @@ -97,40 +97,42 @@ download_if_not_there <- function(
timeout = getOption("timeout")
)
{

if (file.exists(file)) {

kwb.utils::catIf(!quiet, "\nFile already there:", file, "\n")
return(file)
}

} else {

# Temporarily set the timeout option
old_options <- options(timeout = timeout)
on.exit(options(old_options))
# Temporarily set the timeout option
old_options <- options(timeout = timeout)
on.exit(options(old_options))

result <- kwb.utils::catAndRun(
sprintf("\nDownloading\n %s\nto\n %s", url, file),
dbg = !quiet,
expr = try(download.file(
result <- kwb.utils::catAndRun(
sprintf("\nDownloading\n %s\nto\n %s", url, file),
dbg = !quiet,
expr = try(
silent = TRUE,
download.file(
url = url,
destfile = file,
method = "auto",
quiet = TRUE,
mode = mode
))
)
)
)

if (kwb.utils::isTryError(result) || !identical(result, 0L)) {

if (kwb.utils::isTryError(result) || !identical(result, 0L)) {
if (file.exists(file)) {
if (!identical(unlink(file), 0L)) {
message("Could not delete incompletely downloaded file: ", file)
}
if (file.exists(file)) {
if (!identical(unlink(file), 0L)) {
message("Could not delete incompletely downloaded file: ", file)
}
kwb.utils::stopFormatted(
"Could not download %s within %d seconds.", url, timeout
)
}

kwb.utils::stopFormatted(
"Could not download %s within %d seconds.\n%s",
url, timeout, as.character(result)
)
}

file
Expand Down
2 changes: 1 addition & 1 deletion inst/extdata/mask-any-shape.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ if (FALSE)
))

# Rainfall in the different Berliner Bezirke
zip_file_bezirke_berlin <- kwb.dwd:::download_if_not_there(
zip_file_bezirke_berlin <- kwb.dwd:::download(
"https://tsb-opendata.s3.eu-central-1.amazonaws.com/bezirksgrenzen/bezirksgrenzen.shp.zip",
target_dir = kwb.dwd:::download_dir("tsb"),
mode = "wb"
Expand Down

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

2 changes: 1 addition & 1 deletion man/get_shapes_of_germany.Rd

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

23 changes: 23 additions & 0 deletions tests/testthat/test-function-download.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#library(testthat)

test_that("download() works", {

f <- kwb.dwd:::download

expect_error(f())
expect_error(expect_output(suppressWarnings(f("no-such-url"))))

url <- default_projection_file(download = FALSE)

target_dir <- tempdir()

expect_output(file <- f(url, target_dir = target_dir))
expect_output(file <- f(url, target_dir = target_dir), "already there")

expect_true(file.exists(file))

unlink(file)

expect_silent(file <- f(url, target_dir = target_dir, quiet = TRUE))

})
17 changes: 0 additions & 17 deletions tests/testthat/test-function-download_if_not_there.R

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#library(testthat)

test_that("check_or_download_shapes_germany() works", {
test_that("download_shapes_germany() works", {

f <- kwb.dwd:::check_or_download_shapes_germany
f <- kwb.dwd:::download_shapes_germany

result <- try(f(quiet = FALSE, timeout = 1))

Expand Down
4 changes: 2 additions & 2 deletions vignettes/overview.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ head(dir(kwb.dwd:::download_dir("radolan/daily/historical")))

### Download Shape Files for Germany

Function: `check_or_download_shapes_germany()`
Function: `download_shapes_germany()`

This function downloads shape files for Germany. They are not provided by DWD
so this package may not be the best place for this function.

```{r}
target_path <- kwb.dwd::check_or_download_shapes_germany()
target_path <- kwb.dwd::download_shapes_germany()
```

Shape files have been downloaded and extracted to the target path:
Expand Down

0 comments on commit c9e0768

Please sign in to comment.