Skip to content

Commit 4fbb68f

Browse files
committed
fill value
1 parent 321b7b9 commit 4fbb68f

File tree

8 files changed

+196
-210
lines changed

8 files changed

+196
-210
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Version: 0.1.1
44
Authors@R:
55
c(person("Robert", "Marty", , "[email protected]", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0002-3164-3813")),
7-
person("Gabriel", "Stefanini Vicente", , "[email protected]", role = c("aut")))
7+
person("Gabriel", "Stefanini Vicente", , "[email protected]", role = c("aut"),
8+
comment = c(ORCID = "0000-0001-6530-3780")))
89
Description: Geographically referenced data and statistics of nighttime lights from NASA Black Marble <https://blackmarble.gsfc.nasa.gov/>.
910
License: MIT + file LICENSE
1011
Encoding: UTF-8

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,19 +276,17 @@ Both functions take the following arguments:
276276
* For `product_id` `"VNP46A2"`, uses `Gap_Filled_DNB_BRDF-Corrected_NTL`.
277277
* For `product_id`s `"VNP46A3"` and `"VNP46A4"`, uses `NearNadir_Composite_Snow_Free`.
278278

279-
* **quality_flag_rm:** Quality flag values to use to set values to `NA`. Each pixel has a quality flag value, where low quality values can be removed. Values are set to `NA` for each value in ther `quality_flag_rm` vector. (Default: `c(255)`).
279+
* **quality_flag_rm:** Quality flag values to use to set values to `NA`. Each pixel has a quality flag value, where low quality values can be removed. Values are set to `NA` for each value in ther `quality_flag_rm` vector. (Default: `NULL`).
280280

281281
* For `VNP46A1` and `VNP46A2` (daily data):
282282
* `0`: High-quality, Persistent nighttime lights
283283
* `1`: High-quality, Ephemeral nighttime Lights
284284
* `2`: Poor-quality, Outlier, potential cloud contamination, or other issues
285-
* `255`: No retrieval, Fill value (masked out on ingestion)
286285

287286
* For `VNP46A3` and `VNP46A4` (monthly and annual data):
288287
* `0`: Good-quality, The number of observations used for the composite is larger than 3
289288
* `1`: Poor-quality, The number of observations used for the composite is less than or equal to 3
290289
* `2`: Gap filled NTL based on historical data
291-
* `255`: Fill value
292290

293291
* **check_all_tiles_exist:** Check whether all Black Marble nighttime light tiles exist for the region of interest. Sometimes not all tiles are available, so the full region of interest may not be covered. If `TRUE`, skips cases where not all tiles are available. (Default: `TRUE`).
294292
* **interpol_na:** When data for more than one date is downloaded, whether to interpolate `NA` values in rasters using the [`raster::approxNA`](https://www.rdocumentation.org/packages/raster/versions/3.6-26/topics/approxNA) function. Additional arguments for the [`raster::approxNA`](https://www.rdocumentation.org/packages/raster/versions/3.6-26/topics/approxNA) function can also be passed into `bm_raster`/`bm_extract` (eg, `method`, `rule`, `f`, `ties`, `z`, `NA_rule`). (Default: `FALSE`).

man/bm_extract.Rd

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/bm_raster.Rd

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/assess-quality.Rmd

Lines changed: 89 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ This page illustrates how to examine the quality of nighttime lights data.
3131

3232
* [Setup](#setup)
3333
* [Daily data](#daily)
34-
* [Nighttime lights](#daily-ntl)
34+
* [Nighttime lights: Gap Filled](#daily-ntl-gap)
35+
* [Nighttime lights: Non Gap Filled](#daily-ntl-nongap)
3536
* [Quality flag](#daily-quality)
3637
* [Nighttime lights using good quality observations](#daily-goodq)
37-
* [Nighttime lights using good quality observations without gap filling](#daily-nogap)
3838
* [Monthly/annual data](#ma)
3939
* [Nighttime lights](#ma-ntl)
4040
* [Number of observations](#ma-numobs)
@@ -74,17 +74,95 @@ roi_sf <- gadm(country = "CHE", level=0, path = tempdir()) |> st_as_sf()
7474

7575
Below shows an example examining quality for daily data (`VNP46A2`).
7676

77-
#### Nighttime Lights <a name="daily-ntl"></a>
77+
#### Gap filled nighttime lights <a name="daily-ntl-gap"></a>
7878

79-
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.
79+
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. This variable "gap fills" poor quality observations (ie, pixels with cloud cover) using data from previous days.
8080

8181
```{r, results='hide'}
8282
ntl_r <- bm_raster(roi_sf = roi_sf,
8383
product_id = "VNP46A2",
8484
date = "2023-01-01",
8585
bearer = bearer,
86-
variable = "Gap_Filled_DNB_BRDF-Corrected_NTL",
87-
quality = NULL)
86+
variable = "Gap_Filled_DNB_BRDF-Corrected_NTL")
87+
```
88+
89+
<details>
90+
<summary>Show code to produce map</summary>
91+
```{r, ntl_gap_daily_r_map, eval=FALSE}
92+
#### Prep data
93+
ntl_m_r <- ntl_r |> raster::mask(roi_sf)
94+
95+
ntl_df <- rasterToPoints(ntl_m_r, spatial = TRUE) |> as.data.frame()
96+
names(ntl_df) <- c("value", "x", "y")
97+
98+
## Distribution is skewed, so log
99+
ntl_df$value_adj <- log(ntl_df$value+1)
100+
101+
##### Map
102+
ggplot() +
103+
geom_raster(data = ntl_df,
104+
aes(x = x, y = y,
105+
fill = value_adj)) +
106+
scale_fill_gradient2(low = "black",
107+
mid = "yellow",
108+
high = "red",
109+
midpoint = 4) +
110+
coord_quickmap() +
111+
theme_void() +
112+
theme(plot.title = element_text(face = "bold", hjust = 0.5),
113+
legend.position = "none")
114+
```
115+
</details>
116+
117+
```{r, ntl_gap_daily_r_map, echo=FALSE}
118+
```
119+
120+
The `Latest_High_Quality_Retrieval` indicates the number of days since the current date that the nighttime lights value comes from for gap filling.
121+
122+
```{r, results='hide'}
123+
ntl_tmp_gap_r <- bm_raster(roi_sf = roi_sf,
124+
product_id = "VNP46A2",
125+
date = "2023-01-01",
126+
bearer = bearer,
127+
variable = "Latest_High_Quality_Retrieval")
128+
```
129+
130+
<details>
131+
<summary>Show code to produce map</summary>
132+
```{r, ntl_tmp_gap_map, eval=FALSE}
133+
#### Prep data
134+
ntl_tmp_gap_r <- ntl_tmp_gap_r |> mask(roi_sf)
135+
136+
ntl_tmp_gap_df <- rasterToPoints(ntl_tmp_gap_r, spatial = TRUE) |> as.data.frame()
137+
names(ntl_tmp_gap_df) <- c("value", "x", "y")
138+
139+
##### Map
140+
ggplot() +
141+
geom_raster(data = ntl_tmp_gap_df,
142+
aes(x = x, y = y,
143+
fill = value)) +
144+
scale_fill_distiller(palette = "Spectral") +
145+
coord_quickmap() +
146+
theme_void() +
147+
labs(fill = "Temporal\nGap\n(Days)",
148+
title = "Temporal gap between date (Jan 1, 2023)\nand date of high quality pixel used") +
149+
theme(plot.title = element_text(face = "bold", hjust = 0.5))
150+
```
151+
</details>
152+
153+
```{r, ntl_tmp_gap_map, echo=FALSE}
154+
```
155+
156+
#### Non gap filled nighttime lights <a name="daily-ntl-nongap"></a>
157+
158+
Instead of using gap-filled data, we could also just use nighttime light values from the date selected using the `DNB_BRDF-Corrected_NTL` variable.
159+
160+
```{r, results='hide'}
161+
ntl_r <- bm_raster(roi_sf = roi_sf,
162+
product_id = "VNP46A2",
163+
date = "2023-01-01",
164+
bearer = bearer,
165+
variable = "DNB_BRDF-Corrected_NTL")
88166
```
89167

90168
<details>
@@ -118,7 +196,7 @@ ggplot() +
118196
```{r, ntl_daily_r_map, echo=FALSE}
119197
```
120198

121-
We notice that a number of observations are missing, that are poor quality and are not gap-filled. To understand the extent of missing date, we can use the following code to determine (1) the total number of pixels that cover Switzerland, (2) the total number of non-`NA` nighttime light pixels, and (3) the proportion of non-`NA` pixels.
199+
We notice that a number of observations are missing. To understand the extent of missing date, we can use the following code to determine (1) the total number of pixels that cover Switzerland, (2) the total number of non-`NA` nighttime light pixels, and (3) the proportion of non-`NA` pixels.
122200

123201
```{r}
124202
n_pixel <- function(values, coverage_fraction){
@@ -146,7 +224,7 @@ ntl_df <- bm_extract(roi_sf = roi_sf,
146224
to = ymd("2023-01-10"),
147225
by = 1),
148226
bearer = bearer,
149-
variable = "Gap_Filled_DNB_BRDF-Corrected_NTL")
227+
variable = "DNB_BRDF-Corrected_NTL")
150228
151229
knitr::kable(ntl_df)
152230
```
@@ -233,7 +311,7 @@ ntl_good_qual_r <- bm_raster(roi_sf = roi_sf,
233311
product_id = "VNP46A2",
234312
date = "2023-01-01",
235313
bearer = bearer,
236-
variable = "Gap_Filled_DNB_BRDF-Corrected_NTL",
314+
variable = "DNB_BRDF-Corrected_NTL",
237315
quality_flag_rm = 2)
238316
```
239317

@@ -268,86 +346,6 @@ ggplot() +
268346
```{r, ntl_daily_good_qual_map, echo=FALSE}
269347
```
270348

271-
#### Nighttime lights for good quality observations, without gap filling <a name="daily-nogap"></a>
272-
273-
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.
274-
275-
```{r, results='hide'}
276-
ntl_tmp_gap_r <- bm_raster(roi_sf = roi_sf,
277-
product_id = "VNP46A2",
278-
date = "2023-01-01",
279-
bearer = bearer,
280-
variable = "Latest_High_Quality_Retrieval")
281-
```
282-
283-
<details>
284-
<summary>Show code to produce map</summary>
285-
```{r, ntl_tmp_gap_map, eval=FALSE}
286-
#### Prep data
287-
ntl_tmp_gap_r <- ntl_tmp_gap_r |> mask(roi_sf)
288-
289-
ntl_tmp_gap_df <- rasterToPoints(ntl_tmp_gap_r, spatial = TRUE) |> as.data.frame()
290-
names(ntl_tmp_gap_df) <- c("value", "x", "y")
291-
292-
##### Map
293-
ggplot() +
294-
geom_raster(data = ntl_tmp_gap_df,
295-
aes(x = x, y = y,
296-
fill = value)) +
297-
scale_fill_distiller(palette = "Spectral") +
298-
coord_quickmap() +
299-
theme_void() +
300-
labs(fill = "Temporal\nGap\n(Days)",
301-
title = "Temporal gap between date (Jan 1, 2023)\nand date of high quality pixel used") +
302-
theme(plot.title = element_text(face = "bold", hjust = 0.5))
303-
```
304-
</details>
305-
306-
```{r, ntl_tmp_gap_map, echo=FALSE}
307-
```
308-
309-
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.
310-
311-
```{r, results='hide'}
312-
ntl_r <- bm_raster(roi_sf = roi_sf,
313-
product_id = "VNP46A2",
314-
date = "2023-01-01",
315-
bearer = bearer,
316-
variable = "DNB_BRDF-Corrected_NTL",
317-
quality_flag_rm = 2)
318-
```
319-
320-
<details>
321-
<summary>Show code to produce map</summary>
322-
```{r, ntl_daily_nogap_r_map, eval=FALSE}
323-
#### Prep data
324-
ntl_r <- ntl_r |> mask(roi_sf)
325-
326-
ntl_df <- rasterToPoints(ntl_r, spatial = TRUE) |> as.data.frame()
327-
names(ntl_df) <- c("value", "x", "y")
328-
329-
## Distribution is skewed, so log
330-
ntl_df$value_adj <- log(ntl_df$value+1)
331-
332-
##### Map
333-
ggplot() +
334-
geom_raster(data = ntl_df,
335-
aes(x = x, y = y,
336-
fill = value_adj)) +
337-
scale_fill_gradient2(low = "black",
338-
mid = "yellow",
339-
high = "red",
340-
midpoint = 4) +
341-
coord_quickmap() +
342-
theme_void() +
343-
theme(plot.title = element_text(face = "bold", hjust = 0.5),
344-
legend.position = "none")
345-
```
346-
</details>
347-
348-
```{r, ntl_daily_nogap_r_map, echo=FALSE}
349-
```
350-
351349
### Monthly/Annual Data <a name="ma"></a>
352350

353351
Below shows an example examining quality for monthly data (`VNP46A3`). The same approach can be used for annual data (`VNP46A4`); the variables are the same for both monthly and annual data.
@@ -488,15 +486,15 @@ ggplot() +
488486

489487
#### Nighttime lights for good quality observations <a name="ma-ntl_gq"></a>
490488

491-
The `quality_flag_rm` parameter determines which pixels are set to `NA` based on the quality indicator. By default, no pixels are filtered out (except for those that are assigned a "fill value" by BlackMarble, which are always removed). However, if we also want to remove poor quality pixels, we can adjust the `quality_flag_rm` parameter.
489+
The `quality_flag_rm` parameter determines which pixels are set to `NA` based on the quality indicator. By default, no pixels are filtered out (except for those that are assigned a "fill value" by BlackMarble, which are always removed). However, if we also want to remove poor quality pixels and remove pixels that are gap filled, we can adjust the `quality_flag_rm` parameter.
492490

493491
```{r, results='hide'}
494492
ntl_good_qual_r <- bm_raster(roi_sf = roi_sf,
495493
product_id = "VNP46A3",
496494
date = "2023-01-01",
497495
bearer = bearer,
498496
variable = "NearNadir_Composite_Snow_Free",
499-
quality_flag_rm = 1)
497+
quality_flag_rm = c(1,2)) # 1 = poor quality; 2 = gap filled based on historical data
500498
```
501499

502500
<details>

vignettes/assess-quality.html

Lines changed: 100 additions & 107 deletions
Large diffs are not rendered by default.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)