-
Notifications
You must be signed in to change notification settings - Fork 3
/
assess-quality.Rmd
492 lines (393 loc) · 15.4 KB
/
assess-quality.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
---
title: "Assessing Quality of Nighttime Lights Data"
#author: "Robert Marty"
#date: "`r Sys.Date()`"
#output: rmarkdown::html_vignette
output:
html_document:
toc: true
toc_depth: 3
toc_float: true
vignette: >
%\VignetteEngine{knitr::knitr}
%\VignetteIndexEntry{Assess Quality}
%\VignetteEncoding{UTF-8}
---
```{r init, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
warning = FALSE,
message = FALSE,
comment = "#>"
)
```
### Overview <a name="overview"></a>
The quality of nighttime lights data can be impacted by a number of factors, particularly cloud cover. To facilitate analysis using high quality data, Black Marble (1) marks the quality of each pixel and (2) in some cases, uses data from a previous date to fill the value---using a temporally-gap filled NTL value.
This page illustrates how to examine the quality of nighttime lights data.
* [Setup](#setup)
* [Daily data](#daily)
* [Nighttime lights: Gap Filled](#daily-ntl-gap)
* [Nighttime lights: Non Gap Filled](#daily-ntl-nongap)
* [Quality flag](#daily-quality)
* [Nighttime lights using good quality observations](#daily-goodq)
* [Monthly/annual data](#ma)
* [Nighttime lights](#ma-ntl)
* [Number of observations](#ma-numobs)
* [Quality](#ma-quality)
* [Nighttime lights using good quality observations](#ma-ntl_gq)
### Setup <a name="setup"></a>
We first load packages and obtain a polygon for a region of interest; for this example, we use Switzerland.
```{r, results='hide'}
library(blackmarbler)
library(geodata)
library(sf)
library(raster)
library(ggplot2)
library(tidyterra)
library(dplyr)
library(exactextractr)
library(lubridate)
library(tidyr)
library(geodata)
library(knitr)
bearer <- "BEARER-TOKEN-HERE"
```
```{r bearer, include = FALSE}
#bearer <- read.csv("~/Desktop/bearer_bm.csv")$token
bearer <- read.csv("https://www.dropbox.com/scl/fi/pipze9nvak5qo7pedvwb4/bearer_bm.csv?rlkey=bkpv62s657c5w9jbchpg2vvr7&dl=1")$token
```
```{r}
roi_sf <- gadm(country = "CHE", level=0, path = tempdir()) |> st_as_sf()
```
### Daily Data <a name="daily"></a>
Below shows an example examining quality for daily data (`VNP46A2`).
#### Gap filled nighttime lights <a name="daily-ntl-gap"></a>
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.
```{r, results='hide'}
ntl_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A2",
date = "2023-01-01",
bearer = bearer,
variable = "Gap_Filled_DNB_BRDF-Corrected_NTL")
```
<details>
<summary>Show code to produce map</summary>
```{r, ntl_gap_daily_r_map, eval=FALSE}
#### Prep data
ntl_m_r <- ntl_r |> terra::mask(roi_sf)
## Distribution is skewed, so log
ntl_m_r[] <- log(ntl_m_r[]+1)
##### Map
ggplot() +
geom_spatraster(data = ntl_m_r) +
scale_fill_gradient2(low = "black",
mid = "yellow",
high = "red",
midpoint = 4,
na.value = "transparent") +
coord_sf() +
theme_void() +
theme(plot.title = element_text(face = "bold", hjust = 0.5),
legend.position = "none")
```
</details>
```{r, ntl_gap_daily_r_map, echo=FALSE}
```
The `Latest_High_Quality_Retrieval` indicates the number of days since the current date that the nighttime lights value comes from for gap filling.
```{r, results='hide'}
ntl_tmp_gap_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A2",
date = "2023-01-01",
bearer = bearer,
variable = "Latest_High_Quality_Retrieval")
```
<details>
<summary>Show code to produce map</summary>
```{r, ntl_tmp_gap_map, eval=FALSE}
#### Prep data
ntl_tmp_gap_r <- ntl_tmp_gap_r |> terra::mask(roi_sf)
##### Map
ggplot() +
geom_spatraster(data = ntl_tmp_gap_r) +
scale_fill_distiller(palette = "Spectral",
na.value = "transparent") +
coord_sf() +
theme_void() +
labs(fill = "Temporal\nGap\n(Days)",
title = "Temporal gap between date (Jan 1, 2023)\nand date of high quality pixel used") +
theme(plot.title = element_text(face = "bold", hjust = 0.5))
```
</details>
```{r, ntl_tmp_gap_map, echo=FALSE}
```
#### Non gap filled nighttime lights <a name="daily-ntl-nongap"></a>
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.
```{r, results='hide'}
ntl_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A2",
date = "2023-01-01",
bearer = bearer,
variable = "DNB_BRDF-Corrected_NTL")
```
<details>
<summary>Show code to produce map</summary>
```{r, ntl_daily_r_map, eval=FALSE}
#### Prep data
ntl_m_r <- ntl_r |> terra::mask(roi_sf)
## Distribution is skewed, so log
ntl_m_r[] <- log(ntl_m_r[] + 1)
##### Map
ggplot() +
geom_spatraster(data = ntl_m_r) +
scale_fill_gradient2(low = "black",
mid = "yellow",
high = "red",
midpoint = 4,
na.value = "transparent") +
coord_sf() +
theme_void() +
theme(plot.title = element_text(face = "bold", hjust = 0.5),
legend.position = "none")
```
</details>
```{r, ntl_daily_r_map, echo=FALSE}
```
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.
```{r}
n_pixel <- function(values, coverage_fraction){
length(values)
}
n_non_na_pixel <- function(values, coverage_fraction){
sum(!is.na(values))
}
n_pixel_num <- exact_extract(ntl_r, roi_sf, n_pixel)
n_non_na_pixel_num <- exact_extract(ntl_r, roi_sf, n_non_na_pixel)
print(n_pixel_num)
print(n_non_na_pixel_num)
print(n_non_na_pixel_num / n_pixel_num)
```
By default, the `bm_extract` function computes these values:
```{r, results='hide'}
ntl_df <- bm_extract(roi_sf = roi_sf,
product_id = "VNP46A2",
date = seq.Date(from = ymd("2023-01-01"),
to = ymd("2023-01-10"),
by = 1),
bearer = bearer,
variable = "DNB_BRDF-Corrected_NTL")
knitr::kable(ntl_df)
```
The below figure shows trends in average nighttime lights (left) and the proportion of the country with a value for nighttime lights (right). For some days, low number of pixels corresponds to low nighttime lights (eg, January 3 and 5th); however, for other days, low number of pixels corresponds to higher nighttime lights (eg, January 9 and 10). On January 3 and 5, missing pixels could have been over typically high-lit areas (eg, cities)---while on January 9 and 10, missing pixels could have been over typically lower-lit areas.
<details>
<summary>Show code to produce figure</summary>
```{r, n_obs_figure, eval=FALSE}
ntl_df %>%
dplyr::select(date, ntl_mean, prop_non_na_pixels) %>%
pivot_longer(cols = -date) %>%
ggplot(aes(x = date,
y = value)) +
geom_line() +
facet_wrap(~name,
scales = "free")
```
</details>
```{r, n_obs_figure, echo=FALSE}
```
#### Quality <a name="daily-quality"></a>
For daily data, the quality values are:
* 0: High-quality, Persistent nighttime lights
* 1: High-quality, Ephemeral nighttime Lights
* 2: Poor-quality, Outlier, potential cloud contamination, or other issues
We can map quality by using the `Mandatory_Quality_Flag` variable.
```{r, results='hide'}
quality_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A2",
date = "2023-01-01",
bearer = bearer,
variable = "Mandatory_Quality_Flag")
```
<details>
<summary>Show code to produce map</summary>
```{r, quality_daily_r_map, eval=FALSE}
#### Prep data
quality_r <- quality_r |> terra::mask(roi_sf)
qual_levels <- data.frame(id=0:2, cover=c("0: High-quality, persistent",
"1: High-quality, ephemeral",
"2: Poor-quality"))
levels(quality_r) <- qual_levels
##### Map
ggplot() +
geom_spatraster(data = quality_r) +
scale_fill_brewer(palette = "Spectral",
direction = -1,
na.value = "transparent") +
labs(fill = "Quality") +
coord_sf() +
theme_void() +
theme(plot.title = element_text(face = "bold", hjust = 0.5))
```
</details>
```{r, quality_daily_r_map, echo=FALSE}
```
#### Nighttime lights for good quality observations <a name="daily-goodq"></a>
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 only want data for good quality pixels, we can adjust the `quality_flag_rm` parameter.
```{r, results='hide'}
ntl_good_qual_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A2",
date = "2023-01-01",
bearer = bearer,
variable = "DNB_BRDF-Corrected_NTL",
quality_flag_rm = 2)
```
<details>
<summary>Show code to produce map</summary>
```{r, ntl_daily_good_qual_map, eval=FALSE}
#### Prep data
ntl_good_qual_r <- ntl_good_qual_r |> terra::mask(roi_sf)
## Distribution is skewed, so log
ntl_good_qual_r[] <- log(ntl_good_qual_r[]+1)
##### Map
ggplot() +
geom_spatraster(data = ntl_good_qual_r) +
scale_fill_gradient2(low = "black",
mid = "yellow",
high = "red",
midpoint = 4,
na.value = "transparent") +
coord_sf() +
theme_void() +
theme(plot.title = element_text(face = "bold", hjust = 0.5),
legend.position = "none")
```
</details>
```{r, ntl_daily_good_qual_map, echo=FALSE}
```
### Monthly/Annual Data <a name="ma"></a>
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.
#### Nighttime Lights <a name="ma-ntl"></a>
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.
```{r, results='hide'}
ntl_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A3",
date = "2023-01-01",
bearer = bearer,
variable = "NearNadir_Composite_Snow_Free")
```
<details>
<summary>Show code to produce map</summary>
```{r, ntl_r_map, eval=FALSE}
#### Prep data
ntl_r <- ntl_r |> terra::mask(roi_sf)
## Distribution is skewed, so log
ntl_r[] <- log(ntl_r[] + 1)
##### Map
ggplot() +
geom_spatraster(data = ntl_r) +
scale_fill_gradient2(low = "black",
mid = "yellow",
high = "red",
midpoint = 4,
na.value = "transparent") +
coord_sf() +
theme_void() +
theme(plot.title = element_text(face = "bold", hjust = 0.5),
legend.position = "none")
```
</details>
```{r, ntl_r_map, echo=FALSE}
```
#### Number of Observations <a name="ma-numobs"></a>
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.
```{r, results='hide'}
cf_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A3",
date = "2023-01-01",
bearer = bearer,
variable = "NearNadir_Composite_Snow_Free_Num")
```
<details>
<summary>Show code to produce map</summary>
```{r, cf_r_map, eval=FALSE}
#### Prep data
cf_r <- cf_r |> terra::mask(roi_sf)
##### Map
ggplot() +
geom_spatraster(data = cf_r) +
scale_fill_viridis_c(na.value = "transparent") +
labs(fill = "Number of\nObservations") +
coord_sf() +
theme_void() +
theme(plot.title = element_text(face = "bold", hjust = 0.5))
```
</details>
```{r, cf_r_map, echo=FALSE}
```
#### Quality <a name="ma-quality"></a>
For monthly and annual data, the quality values are:
* 0: Good-quality, The number of observations used for the composite is larger than 3
* 1: Poor-quality, The number of observations used for the composite is less than or equal to 3
* 2: Gap filled NTL based on historical data
We can map quality by adding `_Quality` to the variable name.
```{r, results='hide'}
quality_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A3",
date = "2023-01-01",
bearer = bearer,
variable = "NearNadir_Composite_Snow_Free_Quality")
```
<details>
<summary>Show code to produce map</summary>
```{r, quality_r_map, eval=FALSE}
#### Prep data
quality_r <- quality_r |> terra::mask(roi_sf)
qual_levels <- data.frame(id=0:2, cover=c("0: Good quality",
"1: Poor quality",
"2: Gap filled"))
levels(quality_r) <- qual_levels
##### Map
ggplot() +
geom_spatraster(data = quality_r) +
scale_fill_brewer(palette = "Spectral",
direction = -1,
na.value = "transparent") +
labs(fill = "Quality") +
coord_sf() +
theme_void() +
theme(plot.title = element_text(face = "bold", hjust = 0.5))
```
</details>
```{r, quality_r_map, echo=FALSE}
```
#### Nighttime lights for good quality observations <a name="ma-ntl_gq"></a>
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.
```{r, results='hide'}
ntl_good_qual_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A3",
date = "2023-01-01",
bearer = bearer,
variable = "NearNadir_Composite_Snow_Free",
quality_flag_rm = c(1,2)) # 1 = poor quality; 2 = gap filled based on historical data
```
<details>
<summary>Show code to produce map</summary>
```{r, ntl_good_qual_map, eval=FALSE}
#### Prep data
ntl_good_qual_r <- ntl_good_qual_r |> terra::mask(roi_sf)
## Distribution is skewed, so log
ntl_good_qual_r[] <- log(ntl_good_qual_r[] + 1)
##### Map
ggplot() +
geom_spatraster(data = ntl_good_qual_r) +
scale_fill_gradient2(low = "black",
mid = "yellow",
high = "red",
midpoint = 4,
na.value = "transparent") +
coord_sf() +
theme_void() +
theme(plot.title = element_text(face = "bold", hjust = 0.5),
legend.position = "none")
```
</details>
```{r, ntl_good_qual_map, echo=FALSE}
```