Skip to content

Commit

Permalink
add n_tries parameter in sits_cube_copy
Browse files Browse the repository at this point in the history
  • Loading branch information
OldLipe committed Apr 26, 2024
1 parent ade19af commit f9464f6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
73 changes: 48 additions & 25 deletions R/api_download.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#' @title Downloads an asset
#' @noRd
#' @param asset File to be downloaded (with path)
#' @param res Spatial resolution
#' @param sf_roi Region of interest (sf object)
#' @param asset File to be downloaded (with path)
#' @param res Spatial resolution
#' @param sf_roi Region of interest (sf object)
#' @param n_tries Number of tries to download the same image.
#' @param output_dir Directory where file will be saved
#' @param progress Show progress bar?
#' @returns Updated asset
.download_asset <- function(asset, res, sf_roi, output_dir, progress) {
.download_asset <- function(asset, res, sf_roi, n_tries, output_dir,
progress, ...) {
# Get all paths and expand
file <- .file_normalize(.tile_path(asset))
# Create a list of user parameters as gdal format
Expand All @@ -16,19 +18,17 @@
res = res
)
# Create output file
derived_cube <- inherits(asset, "derived_cube")
if (derived_cube) {
out_file <- .file_path(
.tile_satellite(asset),
.download_remove_slash(.tile_sensor(asset)),
.tile_name(asset),
.tile_bands(asset),
.tile_start_date(asset),
output_dir = output_dir,
ext = "tif"
)
if (inherits(asset, "derived_cube")) {
out_file <- paste0(output_dir, "/", basename(file))
} else {
out_file <- .file_path(
.tile_satellite(asset),
.download_remove_slash(.tile_sensor(asset)),
.tile_name(asset),
.tile_bands(asset),
.tile_start_date(asset),
output_dir = output_dir,
ext = "tif"
)
}
# Resume feature
if (.raster_is_valid(out_file, output_dir = output_dir)) {
Expand All @@ -45,7 +45,7 @@
out_file = out_file, gdal_params = gdal_params
)
# Download file
suppressWarnings(download_fn(file))
suppressWarnings(download_fn(file, n_tries, ...))
# Update asset metadata
asset <- .download_update_asset(
asset = asset, roi = sf_roi, res = res, out_file = out_file
Expand Down Expand Up @@ -102,11 +102,25 @@
#' @param gdal_params GDAL parameters
#' @returns Appropriate GDAL download function
.download_gdal <- function(out_file, gdal_params) {
download_fn <- function(file) {
.gdal_translate(
file = out_file, base_file = file,
params = gdal_params, quiet = TRUE
)
# Ellipse is not used in gdal_translate. Defined to keep consistency.
download_fn <- function(file, n_tries, ...) {
# Download file
while (n_tries > 0) {
out <- .try(
.gdal_translate(
file = out_file, base_file = file,
params = gdal_params, quiet = TRUE
), default = NULL
)
if (.has(out)) {
return(out_file)
}
n_tries <- n_tries - 1
}
if (!.has(out)) {
warning(paste("Error in downloading file", file))
}
# Return file name
out_file
}
download_fn
Expand All @@ -116,16 +130,25 @@
#' @param out_file Path where file will be saved
#' @returns Appropriate non-GDAL download function
.download_base <- function(out_file) {
donwload_fn <- function(file) {
donwload_fn <- function(file, n_tries, ...) {
# Remove vsi driver path
file <- .file_remove_vsi(file)
# Add file scheme in local paths
if (.file_is_local(file)) {
file <- .file_path("file:https://", file, sep = "")
}
httr::GET(
url = file, httr::write_disk(path = out_file, overwrite = TRUE)
# Download file
out <- httr::RETRY(
verb = "GET",
url = file,
httr::write_disk(path = out_file, overwrite = TRUE),
times = n_tries,
pause_min = 10,
...
)
if (httr::http_error(out)) {
warning(paste("Error in downloading file", file))
}
# Return file name
out_file
}
Expand Down
10 changes: 8 additions & 2 deletions R/sits_cube_copy.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
#' ("lon_min", "lat_min", "lon_max", "lat_max").
#' @param res An integer value corresponds to the output
#' spatial resolution of the images. Default is NULL.
#' @param n_tries Number of tries to download the same image. Default is 3.
#' @param multicores Number of cores for parallel downloading
#' (integer, min = 1, max = 2048).
#' @param output_dir Output directory where images will be saved.
#' (character vector of length 1).
#' @param progress Logical: show progress bar?
#' @param ... Additional parameters to httr package.
#' @return Copy of input data cube (class "raster cube").
#'
#' @examples
Expand Down Expand Up @@ -54,9 +56,10 @@
sits_cube_copy <- function(cube,
roi = NULL,
res = NULL,
n_tries = 3,
multicores = 2L,
output_dir,
progress = TRUE) {
progress = TRUE, ...) {
# Set caller for error msgs
.check_set_caller("sits_cube_copy")
# Pre-conditions
Expand All @@ -66,6 +69,8 @@ sits_cube_copy <- function(cube,
warning(.conf("messages"), "sits_cube_copy_sar_no_copy")
return(cube)
}
# Check n_tries parameter
.check_num_min_max(x = n_tries, min = 1, max = 50)
# check files
.check_raster_cube_files(cube)
if (.has(roi)) {
Expand Down Expand Up @@ -104,8 +109,9 @@ sits_cube_copy <- function(cube,
asset = asset,
res = res,
sf_roi = sf_roi,
n_tries = n_tries,
output_dir = output_dir,
progress = progress
progress = progress, ...
)
# Return local tile
local_asset
Expand Down
8 changes: 7 additions & 1 deletion man/sits_cube_copy.Rd

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

0 comments on commit f9464f6

Please sign in to comment.