Skip to content

Commit

Permalink
Fix no-merge downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemahoney218 committed Nov 2, 2023
1 parent c3dfd60 commit 1ee5178
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions R/get_stac_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ get_stac_data <- function(aoi,
p <- function(...) NULL
}

if (is.null(mask_function) && !rescale_bands && composite_function == "merge") {
if (is.null(mask_function) && !rescale_bands && !is.null(composite_function) && composite_function == "merge") {
download_results <- simple_download(
items,
sign_function,
Expand Down Expand Up @@ -315,7 +315,6 @@ get_stac_data <- function(aoi,
on.exit(file.remove(unlist(download_results[["final_bands"]])), add = TRUE)
}


mapply(
function(in_bands, vrt) {
stack_rasters(
Expand All @@ -330,6 +329,20 @@ get_stac_data <- function(aoi,

on.exit(file.remove(download_results[["out_vrt"]]), add = TRUE)

if (is.null(composite_function)) {
app <- tryCatch(rstac::items_datetime(items), error = function(e) NA)
if (any(is.na(app))) app <- NULL
app <- app %||% seq_along(download_results[["final_bands"]])

output_filename <- paste0(
tools::file_path_sans_ext(output_filename),
"_",
app,
".",
tools::file_ext(output_filename)
)
}

out <- mapply(
function(vrt, out) {
sf::gdal_utils(
Expand Down Expand Up @@ -402,7 +415,7 @@ simple_download <- function(items,
future.seed = TRUE
)
list(
final_bands = out,
final_bands = list(out),
out_vrt = tempfile(fileext = ".vrt")
)
}
Expand Down Expand Up @@ -456,25 +469,12 @@ complex_download <- function(items,
}
)
download_locations <- stats::na.omit(download_locations)
on.exit(file.remove(unlist(download_locations)))
names(download_locations) <- names(items_urls)

if (!is.null(mask_band)) apply_masks(mask_band, mask_function, download_locations, p)

if (is.null(composite_function)) {
out_vrt <- replicate(nrow(download_locations), tempfile(fileext = ".tif"))

app <- tryCatch(rstac::items_datetime(items), error = function(e) NA)
if (any(is.na(app))) app <- NULL
app <- app %||% seq_along(out_vrt)

output_filename <- paste0(
tools::file_path_sans_ext(output_filename),
"_",
app,
".",
tools::file_ext(output_filename)
)
final_bands <- apply(download_locations, 1, identity, simplify = FALSE)
} else {
out_vrt <- tempfile(fileext = ".vrt")
Expand Down

0 comments on commit 1ee5178

Please sign in to comment.