Skip to content

Commit 6a2f042

Browse files
committed
cleanup
1 parent 8bc2c7a commit 6a2f042

File tree

6 files changed

+42
-173
lines changed

6 files changed

+42
-173
lines changed

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ Authors@R:
88
Description: Create geographically referenced rasters of NASA Black Marble nighttime lights data <https://blackmarble.gsfc.nasa.gov/>.
99
License: MIT + file LICENSE
1010
Encoding: UTF-8
11-
VignetteBuilder: utils
1211
Roxygen: list(markdown = TRUE)
1312
RoxygenNote: 7.2.1
1413
URL: https://worldbank.github.io/blackmarbler/
1514
BugReports: https://github.com/worldbank/blackmarbler/issues
1615
Imports:
1716
readr,
17+
hdf5r,
1818
dplyr,
1919
purrr,
2020
lubridate,
2121
raster,
2222
sf,
2323
exactextractr,
2424
stringr,
25-
hdf5r,
2625
httr
2726
Suggests:
2827
geodata,
2928
ggplot2,
30-
tidyr
29+
tidyr,
30+
knitr

NAMESPACE

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import(exactextractr)
77
import(hdf5r)
88
import(httr)
99
import(lubridate)
10-
import(purrr)
11-
import(raster, except = c(union, select, intersect, origin, tail, head))
10+
import(purrr, except = c(flatten_df, values))
11+
import(raster, except = c(union, select, intersect, origin, tail, head, values))
12+
1213
import(readr)
1314
import(sf)
1415
import(stringr)

R/blackmarbler.R

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,21 @@ file_to_raster <- function(f,
9898
if(variable %in% c("DNB_BRDF-Corrected_NTL",
9999
"Gap_Filled_DNB_BRDF-Corrected_NTL",
100100
"Latest_High_Quality_Retrieval")){
101+
101102
for(val in quality_flag_rm){ # out[qf %in% quality_flag_rm] doesn't work, so loop
102103
out[qf == val] <- NA
103104
}
104105
}
105106
}
106107

108+
# Above doesn't fully capture
109+
if(variable %in% "Latest_High_Quality_Retrieval"){
110+
out[out == 255] <- NA
111+
}
112+
107113
#### Monthly/Annually
108114
} else{
109-
115+
110116
lat <- h5_data[["HDFEOS/GRIDS/VIIRS_Grid_DNB_2d/Data Fields/lat"]][]
111117
lon <- h5_data[["HDFEOS/GRIDS/VIIRS_Grid_DNB_2d/Data Fields/lon"]][]
112118

@@ -275,10 +281,10 @@ create_dataset_name_df <- function(product_id,
275281
}
276282

277283
#### Create data
278-
files_df <- map2_dfr(param_df$year,
279-
param_df$day,
280-
read_bm_csv,
281-
product_id)
284+
files_df <- purrr::map2_dfr(param_df$year,
285+
param_df$day,
286+
read_bm_csv,
287+
product_id)
282288

283289
return(files_df)
284290
}
@@ -301,11 +307,11 @@ download_raster <- function(file_name,
301307
download_path <- file.path(temp_dir, file_name)
302308

303309
if(quiet == FALSE) message(paste0("Processing: ", file_name))
304-
305-
response <- GET(url,
306-
add_headers(headers),
307-
write_disk(download_path, overwrite = TRUE),
308-
progress())
310+
311+
response <- httr::GET(url,
312+
add_headers(headers),
313+
write_disk(download_path, overwrite = TRUE),
314+
progress())
309315

310316
if(response$status_code != 200){
311317
message("Error in downloading data")
@@ -660,17 +666,17 @@ bm_extract <- function(roi_sf,
660666
#'
661667
#' @export
662668
#'
663-
#' @import purrr
664-
#' @import stringr
669+
#' @import readr
665670
#' @import hdf5r
666671
#' @import dplyr
667672
#' @import sf
668-
#' @import lubridate
669-
#' @import readr
670673
#' @import exactextractr
674+
#' @import stringr
671675
#' @import httr
672-
#' @rawNamespace import(raster, except = c(union, select, intersect, origin, tail, head))
673-
676+
#' @import lubridate
677+
#' @rawNamespace import(purrr, except = c(flatten_df, values))
678+
#' @rawNamespace import(raster, except = c(union, select, intersect, origin, tail, head, values))
679+
#'
674680
# @rawNamespace import(utils, except = c(stack, unstack))
675681
bm_raster <- function(roi_sf,
676682
product_id,

man/bm_tiles_sf.Rd

Lines changed: 0 additions & 24 deletions
This file was deleted.

vignettes/assess-quality.Rmd

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ This page illustrates how to examine the quality of nighttime lights data.
4545

4646
We first load packages and obtain a polygon for a region of interest; for this example, we use Switzerland.
4747

48-
```{r}
48+
```{r, results='hide'}
4949
library(blackmarbler)
5050
library(geodata)
5151
library(sf)
@@ -56,6 +56,7 @@ library(exactextractr)
5656
library(lubridate)
5757
library(tidyr)
5858
library(geodata)
59+
library(knitr)
5960
6061
bearer <- "BEARER-TOKEN-HERE"
6162
```
@@ -77,7 +78,7 @@ Below shows an example examining quality for daily data (`VNP46A2`).
7778

7879
We download data for January 1st, 2023. When the `variable` parameter is not specified, `bm_raster` creates a raster using the `Gap_Filled_DNB_BRDF-Corrected_NTL` variable for daily data.
7980

80-
```{r}
81+
```{r, results='hide'}
8182
ntl_r <- bm_raster(roi_sf = roi_sf,
8283
product_id = "VNP46A2",
8384
date = "2023-01-01",
@@ -137,7 +138,7 @@ print(n_non_na_pixel_num / n_pixel_num)
137138

138139
By default, the `bm_extract` function computes these values:
139140

140-
```{r}
141+
```{r, results='hide'}
141142
ntl_df <- bm_extract(roi_sf = roi_sf,
142143
product_id = "VNP46A2",
143144
date = seq.Date(from = ymd("2023-01-01"),
@@ -184,7 +185,7 @@ For daily data, the quality values are:
184185

185186
We can map quality by using the `Mandatory_Quality_Flag` variable.
186187

187-
```{r}
188+
```{r, results='hide'}
188189
quality_r <- bm_raster(roi_sf = roi_sf,
189190
product_id = "VNP46A2",
190191
date = "2023-01-01",
@@ -229,7 +230,7 @@ ggplot() +
229230

230231
The `quality_flag_rm` parameter determines which pixels are set to `NA` based on the quality indicator. By default, only pixels with a value of `255` are filtered out. However, if we only want data for good quality pixels, we can adjust the `quality_flag_rm` parameter.
231232

232-
```{r}
233+
```{r, results='hide'}
233234
ntl_good_qual_r <- bm_raster(roi_sf = roi_sf,
234235
product_id = "VNP46A2",
235236
date = "2023-01-01",
@@ -273,7 +274,7 @@ ggplot() +
273274

274275
By default, the `bm_raster` function uses the `Gap_Filled_DNB_BRDF-Corrected_NTL` variable for daily data. Gap filling indicates that some poor quality pixels use data from a previous date; the `Latest_High_Quality_Retrieval` indicates the date the nighttime lights value came from.
275276

276-
```{r}
277+
```{r, results='hide'}
277278
ntl_tmp_gap_r <- bm_raster(roi_sf = roi_sf,
278279
product_id = "VNP46A2",
279280
date = "2023-01-01",
@@ -290,8 +291,6 @@ ntl_tmp_gap_r <- ntl_tmp_gap_r |> mask(roi_sf)
290291
ntl_tmp_gap_df <- rasterToPoints(ntl_tmp_gap_r, spatial = TRUE) |> as.data.frame()
291292
names(ntl_tmp_gap_df) <- c("value", "x", "y")
292293
293-
#ntl_tmp_gap_df$value[ntl_tmp_gap_df$value == 255] <- NA
294-
295294
##### Map
296295
ggplot() +
297296
geom_raster(data = ntl_tmp_gap_df,
@@ -311,7 +310,7 @@ ggplot() +
311310

312311
Instead of using `Gap_Filled_DNB_BRDF-Corrected_NTL`, we could ignore gap filled observations---using the `DNB_BRDF-Corrected_NTL` variable. Here, we also remove poor quality pixels.
313312

314-
```{r}
313+
```{r, results='hide'}
315314
ntl_r <- bm_raster(roi_sf = roi_sf,
316315
product_id = "VNP46A2",
317316
date = "2023-01-01",
@@ -359,7 +358,7 @@ Below shows an example examining quality for monthly data (`VNP46A3`). The same
359358

360359
We download data for January 2023. When the `variable` parameter is not specified, `bm_raster` creates a raster using the `NearNadir_Composite_Snow_Free` variable for monthly and annual data---which is nighttime lights, removing effects from snow cover.
361360

362-
```{r}
361+
```{r, results='hide'}
363362
ntl_r <- bm_raster(roi_sf = roi_sf,
364363
product_id = "VNP46A3",
365364
date = "2023-01-01",
@@ -402,7 +401,7 @@ ggplot() +
402401

403402
Black Marble removes poor quality observations, such as pixels covered by clouds. To determine the number of observations used to generate nighttime light values for each pixel, we add `_Num` to the variable name.
404403

405-
```{r}
404+
```{r, results='hide'}
406405
cf_r <- bm_raster(roi_sf = roi_sf,
407406
product_id = "VNP46A3",
408407
date = "2023-01-01",
@@ -451,7 +450,7 @@ For monthly and annual data, the quality values are:
451450

452451
We can map quality by adding `_Quality` to the variable name.
453452

454-
```{r}
453+
```{r, results='hide'}
455454
quality_r <- bm_raster(roi_sf = roi_sf,
456455
product_id = "VNP46A3",
457456
date = "2023-01-01",
@@ -496,7 +495,7 @@ ggplot() +
496495

497496
The `quality_flag_rm` parameter determines which pixels are set to `NA` based on the quality indicator. By default, only pixels with a value of `255` are filtered out. However, if we also want to remove poor quality pixels, we can adjust the `quality_flag_rm` parameter.
498497

499-
```{r}
498+
```{r, results='hide'}
500499
ntl_good_qual_r <- bm_raster(roi_sf = roi_sf,
501500
product_id = "VNP46A3",
502501
date = "2023-01-01",

vignettes/assess-quality.html

Lines changed: 2 additions & 115 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)