From 7f348fd7c6b49351d8025d35ccdc2667fbb3e654 Mon Sep 17 00:00:00 2001 From: Rob Marty Date: Tue, 26 Nov 2024 18:58:05 -0500 Subject: [PATCH] catch --- R/blackmarbler.R | 114 +++++++++++++++++++++------------------ readme_figures/testing.R | 4 +- 2 files changed, 64 insertions(+), 54 deletions(-) diff --git a/R/blackmarbler.R b/R/blackmarbler.R index b361281..abc2218 100644 --- a/R/blackmarbler.R +++ b/R/blackmarbler.R @@ -1091,62 +1091,72 @@ bm_raster <- function(roi_sf, # Download data -------------------------------------------------------------- r_list <- lapply(date, function(date_i){ - #### Make name for raster based on date - date_name_i <- define_date_name(date_i, product_id) - - #### If save as tif format - if(output_location_type == "file"){ - - ## Output path - out_name_end <- paste0("_", - date_name_i, - ".tif") - out_name <- paste0(out_name_begin, out_name_end) - - out_path <- file.path(file_dir, out_name) - - make_raster <- TRUE - if(file_skip_if_exists & file.exists(out_path)) make_raster <- FALSE - - if(make_raster){ + out <- tryCatch( + { - r <- bm_raster_i(roi_sf = roi_sf, - product_id = product_id, - date = date_i, - bearer = bearer, - variable = variable, - quality_flag_rm = quality_flag_rm, - check_all_tiles_exist = check_all_tiles_exist, - h5_dir = h5_dir, - quiet = quiet, - temp_dir = temp_dir) - names(r) <- date_name_i - writeRaster(r, out_path) + #### Make name for raster based on date + date_name_i <- define_date_name(date_i, product_id) - } else{ - message(paste0('"', out_path, '" already exists; skipping.\n')) + #### If save as tif format + if(output_location_type == "file"){ + + ## Output path + out_name_end <- paste0("_", + date_name_i, + ".tif") + out_name <- paste0(out_name_begin, out_name_end) + + out_path <- file.path(file_dir, out_name) + + make_raster <- TRUE + if(file_skip_if_exists & file.exists(out_path)) make_raster <- FALSE + + if(make_raster){ + + r <- bm_raster_i(roi_sf = roi_sf, + product_id = product_id, + date = date_i, + bearer = bearer, + variable = variable, + quality_flag_rm = quality_flag_rm, + check_all_tiles_exist = check_all_tiles_exist, + h5_dir = h5_dir, + quiet = quiet, + temp_dir = temp_dir) + names(r) <- date_name_i + + writeRaster(r, out_path) + + } else{ + message(paste0('"', out_path, '" already exists; skipping.\n')) + } + + r_out <- NULL # Saving as tif file, so output from function should be NULL + + } else{ + + r_out <- bm_raster_i(roi_sf = roi_sf, + product_id = product_id, + date = date_i, + bearer = bearer, + variable = variable, + quality_flag_rm = quality_flag_rm, + check_all_tiles_exist = check_all_tiles_exist, + h5_dir = h5_dir, + quiet = quiet, + temp_dir = temp_dir) + names(r_out) <- date_name_i + + } + + return(r_out) + + }, + error=function(e) { + return(NULL) } - - r_out <- NULL # Saving as tif file, so output from function should be NULL - - } else{ - - r_out <- bm_raster_i(roi_sf = roi_sf, - product_id = product_id, - date = date_i, - bearer = bearer, - variable = variable, - quality_flag_rm = quality_flag_rm, - check_all_tiles_exist = check_all_tiles_exist, - h5_dir = h5_dir, - quiet = quiet, - temp_dir = temp_dir) - names(r_out) <- date_name_i - - } - - return(r_out) + ) #) diff --git a/readme_figures/testing.R b/readme_figures/testing.R index 3727dcd..263fb88 100644 --- a/readme_figures/testing.R +++ b/readme_figures/testing.R @@ -11,8 +11,8 @@ roi_sf <- data.frame(lat = -1.943889, lon = 30.059444, id = 1) |> st_buffer(dist = 20000) r_20210205 <- bm_raster(roi_sf = roi_sf, - product_id = "VNP46A3", - date = "2021-02-05", + product_id = "VNP46A4", + date = 2023:2024, bearer = bearer) library(readr)