Skip to content

Commit e0353be

Browse files
committed
warning message
1 parent 196ee6f commit e0353be

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

R/blackmarbler.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,20 @@ file_to_raster <- function(f,
169169
str_replace_all("_Std", "")
170170

171171
qf_name <- paste0(variable_short, "_Quality")
172+
172173
if(qf_name %in% var_names){
173174

174175
#qf <- h5_data$HDFEOS$GRIDS$VIIRS_Grid_DNB_2d$`Data Fields`[[paste0(variable, "_Quality")]]
175-
qf <- h5_data[[paste0("HDFEOS/GRIDS/VIIRS_Grid_DNB_2d/Data Fields/", variable, "_Quality")]][,]
176+
qf <- h5_data[[paste0("HDFEOS/GRIDS/VIIRS_Grid_DNB_2d/Data Fields/", qf_name)]][,]
176177

177178
for(val in quality_flag_rm){ # out[qf %in% quality_flag_rm] doesn't work, so loop
178179
out[qf == val] <- NA
179180
}
181+
180182
}
181183

182184
}
183-
185+
184186
if(class(out[1,1])[1] != "numeric"){
185187
out <- matrix(as.numeric(out), # Convert to numeric matrix
186188
ncol = ncol(out))
@@ -544,7 +546,7 @@ bm_extract <- function(roi_sf,
544546
saveRDS(r_agg, out_path)
545547

546548
} else{
547-
cat(paste0('"', out_path, '" already exists; skipping.\n'))
549+
warning(paste0('"', out_path, '" already exists; skipping.\n'))
548550
}
549551

550552
r_out <- NULL # Saving as file, so output from function should be NULL
@@ -743,7 +745,7 @@ bm_raster <- function(roi_sf,
743745
writeRaster(r, out_path)
744746

745747
} else{
746-
cat(paste0('"', out_path, '" already exists; skipping.\n'))
748+
warning(paste0('"', out_path, '" already exists; skipping.\n'))
747749
}
748750

749751
r_out <- NULL # Saving as tif file, so output from function should be NULL
@@ -863,10 +865,9 @@ bm_raster_i <- function(roi_sf,
863865
#stop(st_intersects(bm_tiles_sf, roi_sf, sparse = F))
864866
}
865867
)
866-
867-
868+
868869
grid_use_sf <- bm_tiles_sf[inter>0,]
869-
870+
870871
# Make Raster ----------------------------------------------------------------
871872
tile_ids_rx <- grid_use_sf$TileID %>% paste(collapse = "|")
872873
bm_files_df <- bm_files_df[bm_files_df$name %>% str_detect(tile_ids_rx),]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Additional Analysis for Readme
2+
3+
#### Setup
4+
# Load packages
5+
library(blackmarbler)
6+
library(geodata)
7+
library(sf)
8+
library(raster)
9+
library(ggplot2)
10+
11+
#### Define NASA bearer token
12+
bearer <- "BEARER-TOKEN-HERE"
13+
bearer <- read.csv("~/Desktop/bearer_bm.csv")$token
14+
15+
### ROI
16+
# Define region of interest (roi). The roi must be (1) an sf polygon and (2)
17+
# in the WGS84 (epsg:4326) coordinate reference system. Here, we use the
18+
# getData function to load a polygon of Ghana
19+
roi_sf <- gadm(country = "NER", level=0, path = tempdir()) |> st_as_sf()
20+
21+
### Daily data: raster for February 5, 2021
22+
r <- bm_raster(roi_sf = roi_sf,
23+
product_id = "VNP46A4",
24+
date = "2021",
25+
bearer = bearer,
26+
variable = "NearNadir_Composite_Snow_Free")
27+
28+
r_cf <- bm_raster(roi_sf = roi_sf,
29+
product_id = "VNP46A2",
30+
date = "2023-07-23",
31+
bearer = bearer,
32+
variable = "Gap_Filled_DNB_BRDF-Corrected_NTL")
33+
r_cf[] <- as.numeric(r_cf[] == 0)
34+
plot(r_cf)
35+
36+
37+
r <- bm_raster(roi_sf = roi_sf,
38+
product_id = "VNP46A2",
39+
date = "2023-07-23",
40+
bearer = bearer,
41+
variable = "Gap_Filled_DNB_BRDF-Corrected_NTL",
42+
quality_flag_rm = c(255))
43+
44+
r[] <- log(r[] + 1)
45+
plot(r)

0 commit comments

Comments
 (0)