From 73c3c7a068d6ae08b733235457c3668c93986723 Mon Sep 17 00:00:00 2001 From: Rob Marty Date: Mon, 4 Nov 2024 11:35:51 -0500 Subject: [PATCH] make rast of select files --- R/blackmarbler.R | 31 ++++++++++++++++--------------- readme_figures/testing.R | 21 +++++++++++++++++---- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/R/blackmarbler.R b/R/blackmarbler.R index f10b570..17ace4e 100644 --- a/R/blackmarbler.R +++ b/R/blackmarbler.R @@ -450,13 +450,12 @@ download_raster <- function(file_name, download_path <- file.path(h5_dir, file_name) } - - if(!file.exists(download_path)){ if(quiet == FALSE) message(paste0("Processing: ", file_name)) if(quiet == TRUE){ + response <- httr::GET(url, httr::timeout(60), httr::add_headers(headers), @@ -1013,10 +1012,10 @@ bm_raster <- function(roi_sf, # Required parameters used in try statement, so error not generated when used, # so use them here - roi_sf <- roi_sf + roi_sf <- roi_sf product_id <- product_id - date <- date - bearer <- bearer + date <- date + bearer <- bearer # Assign interpolation variables --------------------------------------------- if(interpol_na == T){ @@ -1149,11 +1148,18 @@ bm_raster <- function(roi_sf, # Output raster when output_location_type = "file" --------------------------- if(output_location_type == "file"){ if(!file_return_null){ - r <- file_dir %>% - list.files(full.names = T, - pattern = paste0("*.tif")) %>% - str_subset(out_name_begin) %>% + + ## Output path + date_names <- define_date_name(date, product_id) + + out_name_end <- paste0("_", + date_names, + ".tif") + out_name <- paste0(out_name_begin, out_name_end) + + r <- file.path(file_dir, out_name) %>% rast() + } else{ r <- NULL } @@ -1199,9 +1205,7 @@ bm_raster_i <- function(roi_sf, year <- date %>% year() month <- date %>% month() day <- date %>% yday() - - - + bm_files_df <- create_dataset_name_df(product_id = product_id, all = T, years = year, @@ -1209,9 +1213,6 @@ bm_raster_i <- function(roi_sf, days = day) - - - # Intersecting tiles --------------------------------------------------------- # Remove grid along edges, which causes st_intersects to fail bm_tiles_sf <- bm_tiles_sf[!(bm_tiles_sf$TileID %>% str_detect("h00")),] diff --git a/readme_figures/testing.R b/readme_figures/testing.R index 9d1fe98..cdbae94 100644 --- a/readme_figures/testing.R +++ b/readme_figures/testing.R @@ -12,9 +12,22 @@ library(exactextractr) library(stringr) library(httr) +bearer <- read.csv("~/Dropbox/bearer_bm.csv")$token +bearer <- "BEARER HERE" +library(blackmarbler) +library(geodata) +roi_sf <- gadm(country = "GHA", level=1, path = tempdir()) + +r_20210205 <- bm_raster(roi_sf = roi_sf, + product_id = "VNP46A2", + date = "2021-02-05", + bearer = bearer) + + + # Setup ------------------------------------------------------------------------ source("~/Documents/Github/blackmarbler/R/blackmarbler.R") -#library(blackmarbler) +library(blackmarbler) library(geodata) bearer <- read.csv("~/Desktop/bearer_bm.csv")$token @@ -28,9 +41,9 @@ r1 <- bm_raster(roi_sf = roi_sf, bearer = bearer) r2 <- bm_raster(roi_sf = roi_sf, - product_id = "VNP46A3", - date = "2021-10-01", - bearer = bearer) + product_id = "VNP46A3", + date = "2021-10-01", + bearer = bearer) r3 <- bm_raster(roi_sf = roi_sf, product_id = "VNP46A4",