Skip to content

Commit cfb4e51

Browse files
avpozzidramanica
andauthored
fix multi download issue worldclim (#76)
* fix multi download issue worldclim for future dataset with resolution higher or equal than 5min, there seems to be a limit for multi-dopwnload. Implemented a way to use simple single file download. * fix lints --------- Co-authored-by: Andrea Manica <[email protected]>
1 parent 0cf2bbe commit cfb4e51

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

R/download_worldclim_future.R

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,34 @@ download_worldclim_future <- function(dataset, bio_var, filename = NULL) {
4343

4444
# and now download the files (so the url is actually a local file)
4545
worldclim_url <- file.path(worldclim_dir, basename(download_url))
46-
download_res <- curl::multi_download(download_url,
47-
destfiles = worldclim_url
48-
)
46+
# extract the resolution from the dataset name
47+
resolution <- sub(".*_(\\d+\\.?\\d*)m$", "\\1", dataset)
48+
49+
if (as.numeric(resolution) > 5) {
50+
download_res <- curl::multi_download(download_url,
51+
destfiles = worldclim_url
52+
)
53+
} else {
54+
# create a list to store the results
55+
download_res <- data.frame(
56+
url = character(length(download_url)),
57+
success = logical(length(download_url))
58+
)
59+
60+
61+
for (i in seq_along(download_url)) {
62+
download_res$url[i] <- curl::curl_download(download_url[i],
63+
destfile = worldclim_url[i],
64+
quiet = FALSE
65+
)
66+
download_res$success[i] <- file.exists(worldclim_url[i])
67+
}
68+
}
69+
# nolint start
70+
# download_res <- curl::multi_download(download_url,
71+
# destfiles = worldclim_url
72+
# )
73+
# nolint end
4974
if (any(!download_res$success)) {
5075
print(download_res[!download_res$success, ])
5176
stop("something went wrong downloading the data; try again")

data-raw/notes_on_ecoregions.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#path <- "/media/andrea/Elements/resolve2017/"
1+
# path <- "/media/andrea/Elements/resolve2017/"
22
path <- normalizePath("~/project_temp/resolve2017/")
33
# create the path if it does not exist
44
if (!dir.exists(path)) {
@@ -22,8 +22,8 @@ worldclim_pres <- pastclim::region_slice(
2222
dataset = "WorldClim_2.1_10m"
2323
)
2424
ecoregions_rast <- terra::rast(terra::ext(worldclim_pres),
25-
resolution = terra::res(worldclim_pres),
26-
crs = terra::crs(worldclim_pres)
25+
resolution = terra::res(worldclim_pres),
26+
crs = terra::crs(worldclim_pres)
2727
)
2828

2929
# rasterise the biomes at the resolutions of Worldclim
@@ -44,10 +44,10 @@ time(ecoregions_rast, "years") <- time(worldclim_pres)
4444
# now save this
4545
ncdf_filename <- file.path(path, "ecoregions_1985.nc")
4646
terra::writeCDF(ecoregions_rast,
47-
varname = "biome",
48-
longname = "biome from RESOLVE Ecoregions 2017",
49-
filename = ncdf_filename, prec = "integer",
50-
overwrite = TRUE
47+
varname = "biome",
48+
longname = "biome from RESOLVE Ecoregions 2017",
49+
filename = ncdf_filename, prec = "integer",
50+
overwrite = TRUE
5151
)
5252
# fix a few things in the netcdf file
5353
nc_in <- ncdf4::nc_open(ncdf_filename, write = TRUE)
@@ -58,8 +58,8 @@ format_line <- function(x) {
5858
paste0(paste(x, collapse = " "), "; ")
5959
}
6060
ncdf4::ncatt_put(nc_in,
61-
varid = "biome", attname = "biomes",
62-
attval = paste(apply(ecoregions_meta[[1]], 1, format_line), collapse = "")
61+
varid = "biome", attname = "biomes",
62+
attval = paste(apply(ecoregions_meta[[1]], 1, format_line), collapse = "")
6363
)
6464
ncdf4::nc_close(nc_in)
6565

tests/testthat/test_distance_from_sea.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ set_data_path(
1212
################################################################################
1313

1414
testthat::test_that("get biome classes", {
15-
1615
distance_spatrast <- distance_from_sea(
1716
time_bp = -10000,
1817
dataset = "Example"

0 commit comments

Comments
 (0)