Skip to content

Commit

Permalink
catch
Browse files Browse the repository at this point in the history
  • Loading branch information
ramarty committed Nov 26, 2024
1 parent 7b6617d commit 7f348fd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 54 deletions.
114 changes: 62 additions & 52 deletions R/blackmarbler.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)


#)
Expand Down
4 changes: 2 additions & 2 deletions readme_figures/testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7f348fd

Please sign in to comment.