Skip to content

Commit

Permalink
fix quiet param
Browse files Browse the repository at this point in the history
  • Loading branch information
ramarty committed Feb 19, 2024
1 parent 4fbb68f commit 3574568
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: blackmarbler
Title: Black Marble Data and Statistics
Version: 0.1.1
Version: 0.1.2
Authors@R:
c(person("Robert", "Marty", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-3164-3813")),
Expand Down
37 changes: 25 additions & 12 deletions R/blackmarbler.R
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,17 @@ download_raster <- function(file_name,

if(quiet == FALSE) message(paste0("Processing: ", file_name))

response <- httr::GET(url,
add_headers(headers),
write_disk(download_path, overwrite = TRUE),
progress())
if(quiet == TRUE){
response <- httr::GET(url,
add_headers(headers),
write_disk(download_path, overwrite = TRUE))
} else{
response <- httr::GET(url,
add_headers(headers),
write_disk(download_path, overwrite = TRUE),
progress())
}


if(response$status_code != 200){
message("Error in downloading data")
Expand Down Expand Up @@ -656,14 +663,14 @@ bm_extract <- function(roi_sf,
roi_df <- roi_sf %>% st_drop_geometry()
roi_df$date <- NULL

n_obs_df <- exact_extract(bm_r, roi_sf, count_n_obs) %>%
n_obs_df <- exact_extract(bm_r, roi_sf, count_n_obs, progress = !quiet) %>%
bind_cols(roi_df) %>%
tidyr::pivot_longer(cols = -c(names(roi_df)),
names_to = c(".value", "date"),
names_sep = "\\.t") %>%
dplyr::mutate(prop_non_na_pixels = .data$n_non_na_pixels / .data$n_pixels)

ntl_df <- exact_extract(bm_r, roi_sf, aggregation_fun) %>%
ntl_df <- exact_extract(bm_r, roi_sf, aggregation_fun, progress = !quiet) %>%
tidyr::pivot_longer(cols = everything(),
names_to = c(".value", "date"),
names_sep = "\\.t")
Expand Down Expand Up @@ -712,7 +719,8 @@ bm_extract <- function(roi_sf,
names(r) <- date_name_i

#### Extract
r_agg <- exact_extract(x = r, y = roi_sf, fun = aggregation_fun)
r_agg <- exact_extract(x = r, y = roi_sf, fun = aggregation_fun,
progress = !quiet)
roi_df <- roi_sf
roi_df$geometry <- NULL

Expand All @@ -727,10 +735,12 @@ bm_extract <- function(roi_sf,
if(add_n_pixels){

r_n_obs <- exact_extract(r_out, roi_sf, function(values, coverage_fraction)
sum(!is.na(values)))
sum(!is.na(values)),
progress = !quiet)

r_n_obs_poss <- exact_extract(r_out, roi_sf, function(values, coverage_fraction)
length(values))
length(values),
progress = !quiet)

r_agg$n_pixels <- r_n_obs_poss
r_agg$n_non_na_pixels <- r_n_obs
Expand Down Expand Up @@ -763,17 +773,20 @@ bm_extract <- function(roi_sf,
if(add_n_pixels){

r_n_obs <- exact_extract(r_out, roi_sf, function(values, coverage_fraction)
sum(!is.na(values)))
sum(!is.na(values)),
progress = !quiet)

r_n_obs_poss <- exact_extract(r_out, roi_sf, function(values, coverage_fraction)
length(values))
length(values),
progress = !quiet)

roi_sf$n_pixels <- r_n_obs_poss
roi_sf$n_non_na_pixels <- r_n_obs
roi_sf$prop_non_na_pixels <- roi_sf$n_non_na_pixels / roi_sf$n_pixels
}

r_out <- exact_extract(x = r_out, y = roi_sf, fun = aggregation_fun)
r_out <- exact_extract(x = r_out, y = roi_sf, fun = aggregation_fun,
progress = !quiet)

roi_df <- roi_sf
roi_df$geometry <- NULL
Expand Down
2 changes: 1 addition & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
url: https://worldbank.github.io/blackmarbler/
url: ~
template:
bootstrap: 5

33 changes: 25 additions & 8 deletions readme_figures/readme_test.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
#### Setup
# Load packages
library(blackmarbler)
#library(blackmarbler)
library(geodata)
library(sf)
library(raster)
library(ggplot2)

devtools::install_github("worldbank/blackmarbler@new-version")
library(readr)
library(hdf5r)
library(dplyr)
library(purrr)
library(lubridate)
library(tidyr)
library(raster)
library(sf)
library(exactextractr)
library(stringr)
library(httr)

#devtools::install_github("worldbank/blackmarbler@new-version")

#### Define NASA bearer token
bearer <- "BEARER-TOKEN-HERE"
Expand All @@ -16,18 +28,23 @@ bearer <- read.csv("~/Desktop/bearer_bm.csv")$token
# Define region of interest (roi). The roi must be (1) an sf polygon and (2)
# in the WGS84 (epsg:4326) coordinate reference system. Here, we use the
# getData function to load a polygon of Ghana
roi_sf <- gadm(country = "GHA", level=1, path = tempdir()) |> st_as_sf()
#roi_sf <- gadm(country = "GHA", level=1, path = tempdir()) |> st_as_sf()

#########
roi_sf <- gadm(country = "CHE", level=1, path = tempdir()) |> st_as_sf()

ro <- bm_raster(roi_sf = roi_sf,
########
r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A3",
date = c("2021-01-01",
"2021-02-01",
"2021-03-01"),
date = c("2021-01-01"),
bearer = bearer,
quality_flag_rm = c(255,2))
quiet = T)

r <- bm_extract(roi_sf = roi_sf,
product_id = "VNP46A3",
date = c("2021-01-01"),
bearer = bearer,
quiet = F)

r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A3",
Expand Down

0 comments on commit 3574568

Please sign in to comment.