Skip to content

Commit b4c2436

Browse files
committed
h5 dir
1 parent a0463c0 commit b4c2436

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

R/blackmarbler.R

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ download_raster <- function(file_name,
435435
variable,
436436
bearer,
437437
quality_flag_rm,
438+
h5_dir,
438439
quiet){
439440

440441
year <- file_name %>% substring(10,13)
@@ -445,28 +446,36 @@ download_raster <- function(file_name,
445446
product_id, '/', year, '/', day, '/', file_name)
446447

447448
headers <- c('Authorization' = paste('Bearer', bearer))
448-
download_path <- file.path(temp_dir, file_name)
449449

450-
if(quiet == FALSE) message(paste0("Processing: ", file_name))
451-
452-
if(quiet == TRUE){
453-
response <- httr::GET(url,
454-
add_headers(headers),
455-
write_disk(download_path, overwrite = TRUE))
450+
if(is.null(h5_dir)){
451+
download_path <- file.path(temp_dir, file_name)
456452
} else{
457-
response <- httr::GET(url,
458-
add_headers(headers),
459-
write_disk(download_path, overwrite = TRUE),
460-
progress())
453+
download_path <- file.path(h5_dir, file_name)
461454
}
462455

463-
464-
if(response$status_code != 200){
465-
message("Error in downloading data")
466-
message(response)
456+
if(!file.exists(download_path)){
457+
458+
if(quiet == FALSE) message(paste0("Processing: ", file_name))
459+
460+
if(quiet == TRUE){
461+
response <- httr::GET(url,
462+
add_headers(headers),
463+
write_disk(download_path, overwrite = TRUE))
464+
} else{
465+
response <- httr::GET(url,
466+
add_headers(headers),
467+
write_disk(download_path, overwrite = TRUE),
468+
progress())
469+
}
470+
471+
if(response$status_code != 200){
472+
message("Error in downloading data")
473+
message(response)
474+
}
475+
467476
}
468477

469-
r <- file_to_raster(file.path(temp_dir, file_name),
478+
r <- file_to_raster(download_path,
470479
variable,
471480
quality_flag_rm)
472481

@@ -555,6 +564,7 @@ count_n_obs <- function(values, coverage_fraction) {
555564
#' @param file_dir (If `output_location_type = file`). The directory where data should be exported (default: `NULL`, so the working directory will be used)
556565
#' @param file_prefix (If `output_location_type = file`). Prefix to add to the file to be saved. The file will be saved as the following: `[file_prefix][product_id]_t[date].csv`
557566
#' @param file_skip_if_exists (If `output_location_type = file`). Whether the function should first check wither the file already exists, and to skip downloading or extracting data if the data for that date if the file already exists (default: `TRUE`).
567+
#' @param h5_dir Black Marble data are originally downloaded as `h5` files. If `h5_dir = NULL`, the function downloads to a temporary directory then deletes the directory. If `h5_dir` is set to a path, `h5` files are saved to that directory and not deleted. The function will then check if the needed `h5` file already exists in the directory; if it exists, the function will not re-download the `h5` file.
558568
#' @param quiet Suppress output that show downloading progress and other messages. (Default: `FALSE`).
559569
#'
560570
#' @param ... Additional arguments for `raster::approxNA`, if `interpol_na = TRUE`
@@ -604,6 +614,7 @@ bm_extract <- function(roi_sf,
604614
file_dir = NULL,
605615
file_prefix = NULL,
606616
file_skip_if_exists = TRUE,
617+
h5_dir = NULL,
607618
quiet = FALSE,
608619
...){
609620

@@ -661,6 +672,7 @@ bm_extract <- function(roi_sf,
661672
quality_flag_rm = quality_flag_rm,
662673
check_all_tiles_exist = check_all_tiles_exist,
663674
interpol_na = F,
675+
h5_dir = h5_dir,
664676
quiet = quiet,
665677
temp_dir = temp_dir)
666678

@@ -726,6 +738,7 @@ bm_extract <- function(roi_sf,
726738
variable = variable,
727739
quality_flag_rm = quality_flag_rm,
728740
check_all_tiles_exist = check_all_tiles_exist,
741+
h5_dir = h5_dir,
729742
quiet = quiet,
730743
temp_dir = temp_dir)
731744
names(r) <- date_name_i
@@ -778,6 +791,7 @@ bm_extract <- function(roi_sf,
778791
variable = variable,
779792
quality_flag_rm = quality_flag_rm,
780793
check_all_tiles_exist = check_all_tiles_exist,
794+
h5_dir = h5_dir,
781795
quiet = quiet,
782796
temp_dir = temp_dir)
783797
names(r_out) <- date_name_i
@@ -887,6 +901,7 @@ bm_extract <- function(roi_sf,
887901
#' @param file_dir The directory where data should be exported (default: `NULL`, so the working directory will be used)
888902
#' @param file_prefix Prefix to add to the file to be saved. The file will be saved as the following: `[file_prefix][product_id]_t[date].tif`
889903
#' @param file_skip_if_exists Whether the function should first check wither the file already exists, and to skip downloading or extracting data if the data for that date if the file already exists (default: `TRUE`).
904+
#' @param h5_dir Black Marble data are originally downloaded as `h5` files. If `h5_dir = NULL`, the function downloads to a temporary directory then deletes the directory. If `h5_dir` is set to a path, `h5` files are saved to that directory and not deleted. The function will then check if the needed `h5` file already exists in the directory; if it exists, the function will not re-download the `h5` file.
890905
#' @param quiet Suppress output that show downloading progress and other messages. (Default: `FALSE`).
891906
#' @param ... Additional arguments for `raster::approxNA`, if `interpol_na = TRUE`
892907
#'
@@ -947,6 +962,7 @@ bm_raster <- function(roi_sf,
947962
file_dir = NULL,
948963
file_prefix = NULL,
949964
file_skip_if_exists = TRUE,
965+
h5_dir = NULL,
950966
quiet = FALSE,
951967
...){
952968

@@ -1024,6 +1040,7 @@ bm_raster <- function(roi_sf,
10241040
variable = variable,
10251041
quality_flag_rm = quality_flag_rm,
10261042
check_all_tiles_exist = check_all_tiles_exist,
1043+
h5_dir = h5_dir,
10271044
quiet = quiet,
10281045
temp_dir = temp_dir)
10291046
names(r) <- date_name_i
@@ -1044,6 +1061,7 @@ bm_raster <- function(roi_sf,
10441061
variable = variable,
10451062
quality_flag_rm = quality_flag_rm,
10461063
check_all_tiles_exist = check_all_tiles_exist,
1064+
h5_dir = h5_dir,
10471065
quiet = quiet,
10481066
temp_dir = temp_dir)
10491067
names(r_out) <- date_name_i
@@ -1104,6 +1122,7 @@ bm_raster_i <- function(roi_sf,
11041122
variable,
11051123
quality_flag_rm,
11061124
check_all_tiles_exist,
1125+
h5_dir,
11071126
quiet,
11081127
temp_dir){
11091128

@@ -1181,7 +1200,7 @@ bm_raster_i <- function(roi_sf,
11811200
}
11821201

11831202
r_list <- lapply(bm_files_df$name, function(name_i){
1184-
download_raster(name_i, temp_dir, variable, bearer, quality_flag_rm, quiet)
1203+
download_raster(name_i, temp_dir, variable, bearer, quality_flag_rm, h5_dir, quiet)
11851204
})
11861205

11871206
if(length(r_list) == 1){

readme_figures/figures_for_readme.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ if(F){
1313
bearer <- read_csv("~/Desktop/bearer_bm.csv") %>%
1414
pull(token)
1515

16+
roi_sf <- read_sf("https://www.geoboundaries.org/api/current/gbOpen/GHA/ADM1/")
1617
roi_sf <- gadm(country = "GHA", level=1, path = tempdir()) %>% st_as_sf()
1718
product_id <- "VNP46A3"
1819
year <- 2018

readme_figures/readme_test_1.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
#### Setup
3+
# Load packages
4+
library(blackmarbler)
5+
library(geodata)
6+
library(sf)
7+
library(raster)
8+
library(ggplot2)
9+
10+
#### Define NASA bearer token
11+
bearer <- "BEARER-TOKEN-HERE"
12+
13+
### ROI
14+
# Define region of interest (roi). The roi must be (1) an sf polygon and (2)
15+
# in the WGS84 (epsg:4326) coordinate reference system. Here, we use the
16+
# getData function to load a polygon of Ghana
17+
roi_sf <- gadm(country = "GHA", level=1, path = tempdir()) |> st_as_sf()

readme_figures/testing.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ quiet = FALSE
3737

3838
r_202110 <- bm_raster(roi_sf = roi_sf,
3939
product_id = "VNP46A3",
40+
variable = "NearNadir_Composite_Snow_Free",
4041
date = "2021-10-01",
41-
bearer = bearer)
42+
bearer = bearer,
43+
h5_dir = "~/Desktop/h5_test",
44+
quiet = T)
4245

4346
e_202110 <- bm_raster(roi_sf = roi_sf,
4447
product_id = "VNP46A3",

0 commit comments

Comments
 (0)