Skip to content

Commit dffb0ef

Browse files
Merge pull request #1477 from rolfsimoes/dev
Bug fixes and improvements
2 parents 2f864b8 + f7338eb commit dffb0ef

12 files changed

Lines changed: 459 additions & 569 deletions

File tree

R/api_data.R

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,23 +179,46 @@
179179
}
180180

181181
# Extract time series
182-
samples <- .ts_get_raster_data(
183-
tile = tile,
184-
points = samples,
185-
bands = band,
186-
impute_fn = impute_fn,
187-
xy = as.matrix(samples[, c("X", "Y")]),
188-
cld_band = cld_band
182+
samples <- .try(
183+
.ts_get_raster_data(
184+
tile = tile,
185+
points = samples,
186+
bands = band,
187+
impute_fn = impute_fn,
188+
xy = as.matrix(samples[, c("X", "Y")]),
189+
cld_band = cld_band
190+
),
191+
.default = NULL
189192
)
193+
if (is.null(samples)) {
194+
warn <- simpleWarning(
195+
sprintf(
196+
.conf("messages", ".data_get_ts_raster_data"),
197+
tile_name,
198+
band
199+
)
200+
)
201+
return(warn)
202+
}
190203
samples[["tile"]] <- tile_name
191204
saveRDS(samples, filename)
192205
samples
193206
}, progress = progress)
194207
# bind rows to get a melted tibble of samples
208+
is_warn <- vapply(ts, inherits, logical(1), "warning")
209+
warns <- unique(ts[is_warn])
210+
if (.has(warns)) {
211+
for (warn in warns[-1]) {
212+
warning(warn, immediate. = TRUE)
213+
}
214+
stop(conditionMessage(warns[[1L]]), call. = FALSE)
215+
}
195216
ts <- dplyr::bind_rows(ts)
196217
if (!.has_ts(ts)) {
197-
warning(.conf("messages", ".data_by_tile"),
198-
immediate. = TRUE, call. = FALSE
218+
warning(
219+
.conf("messages", ".data_by_tile"),
220+
call. = FALSE,
221+
immediate. = TRUE
199222
)
200223
return(.tibble())
201224
}

R/api_gdalcubes.R

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,12 @@
461461
# require gdalcubes package
462462
.check_require_packages("gdalcubes")
463463
# prepare temp_output_dir
464-
temp_output_dir <- file.path(output_dir, ".sits")
464+
temp_output_dir <- file.path(output_dir, ".sits", "tmp")
465465
if (!dir.exists(temp_output_dir)) {
466466
dir.create(temp_output_dir, recursive = TRUE)
467467
}
468468
# set to delete all files in temp dir
469-
on.exit(unlink(list.files(temp_output_dir, full.names = TRUE)), add = TRUE)
469+
on.exit(unlink(temp_output_dir, recursive = TRUE), add = TRUE)
470470
if (.has_not(timeline)) {
471471
# timeline of intersection
472472
timeline <- .gc_get_valid_timeline(cube, period = period)
@@ -520,7 +520,8 @@
520520
cube = cube,
521521
local_cube = local_cube,
522522
processed_cube = processed_cube,
523-
timeline = timeline
523+
timeline = timeline,
524+
output_dir = output_dir
524525
)
525526
# recovery mode
526527
finished <- length(jobs) == 0
@@ -650,7 +651,8 @@
650651
cube = cube,
651652
local_cube = local_cube,
652653
processed_cube = processed_cube,
653-
timeline = timeline
654+
timeline = timeline,
655+
output_dir = output_dir
654656
)
655657

656658
# have we finished?
@@ -752,10 +754,10 @@
752754
#' @param local_cube Regularized local cube (may be missing tiles).
753755
#' @param processed_cube Regularized processed cube.
754756
#' @param timeline Timeline used by gdalcubes for regularized cube
755-
#' @param period Period of timeline regularization.
757+
#' @param output_dir Output directory used to checked files
756758
#'
757759
#' @return Tiles that are missing from the regularized cube.
758-
.gc_missing_tiles <- function(cube, local_cube, processed_cube, timeline) {
760+
.gc_missing_tiles <- function(cube, local_cube, processed_cube, timeline, output_dir) {
759761
# do a cross product on tiles and bands
760762
tiles_bands_times <- unlist(slider::slide(cube, function(tile) {
761763
bands <- .cube_bands(tile, add_cloud = FALSE)
@@ -773,22 +775,39 @@
773775
if (!is.null(local_cube)) {
774776
# do a cross product on tiles and bands
775777
local_tiles_bands_times <- unlist(slider::slide(local_cube, function(tile) {
776-
purrr::pmap(tile$file_info[[1L]][, c("band", "date")], function(band, date) {
777-
list(tile$tile, band, date)
778+
purrr::pmap(tile$file_info[[1L]][, c("band", "date", "path")], function(band, date, path) {
779+
list(tile$tile, band, date, path)
778780
})
779781
}), recursive = FALSE)
780782
}
783+
valids <- vapply(local_tiles_bands_times, function(x) {
784+
valid <- .raster_is_valid(x[[4L]], output_dir)
785+
if (!valid) unlink(x[[4L]])
786+
valid
787+
}, logical(1L))
788+
local_tiles_bands_times <- lapply(local_tiles_bands_times, function(x) {
789+
x[c(1L, 2L, 3L)]
790+
})[valids]
781791

782792
# Get processed cube tiles, bands and times
783793
proc_tiles_bands_times <- NULL
784794
if (!is.null(processed_cube)) {
785795
# do a cross product on tiles and bands
786796
proc_tiles_bands_times <- unlist(slider::slide(processed_cube, function(tile) {
787-
purrr::pmap(tile$file_info[[1L]][, c("band", "date")], function(band, date) {
788-
list(tile$tile, band, date)
797+
purrr::pmap(tile$file_info[[1L]][, c("band", "date", "path")], function(band, date, path) {
798+
list(tile$tile, band, date, path)
789799
})
790800
}), recursive = FALSE)
791801
}
802+
valids <- vapply(proc_tiles_bands_times, function(x) {
803+
valid <- .raster_is_valid(x[[4L]], output_dir)
804+
if (!valid) unlink(x[[4L]])
805+
valid
806+
}, logical(1L))
807+
proc_tiles_bands_times <- lapply(proc_tiles_bands_times, function(x) {
808+
x[c(1L, 2L, 3L)]
809+
})[valids]
810+
792811
# merge local and processed entries
793812
gc_tiles_bands_times <- c(local_tiles_bands_times, proc_tiles_bands_times)
794813

0 commit comments

Comments
 (0)