Skip to content

Commit 321b7b9

Browse files
committed
update fill values
1 parent 77eb203 commit 321b7b9

File tree

4 files changed

+87
-45
lines changed

4 files changed

+87
-45
lines changed

R/blackmarbler.R

Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,42 +59,93 @@ pad3 <- function(x){
5959
pad3 <- Vectorize(pad3)
6060

6161
remove_fill_value <- function(x, variable){
62-
# Apply scaling factor to variables according to Black Marble user guide
62+
# Remove fill values
6363

6464
# https://viirsland.gsfc.nasa.gov/PDF/BlackMarbleUserGuide_v1.2_20220916.pdf
6565
# * Table 3 (page 12)
6666
# * Table 6 (page 16)
6767
# * Table 9 (page 18)
6868

69+
#### 255
6970
if(variable %in% c(
70-
71-
# VNP46A1
72-
"DNB_At_Sensor_Radiance",
73-
74-
# VNP46A2
71+
"Granule",
72+
"Mandatory_Quality_Flag",
73+
"Latest_High_Quality_Retrieval",
74+
"Snow_Flag",
75+
"DNB_Platform",
76+
"Land_Water_Mask",
77+
"AllAngle_Composite_Snow_Covered_Quality",
78+
"AllAngle_Composite_Snow_Free_Quality",
79+
"NearNadir_Composite_Snow_Covered_Quality",
80+
"NearNadir_Composite_Snow_Free_Quality",
81+
"OffNadir_Composite_Snow_Covered_Quality",
82+
"OffNadir_Composite_Snow_Free_Quality"
83+
)){
84+
x[][x[] == 255] <- NA
85+
}
86+
87+
#### -999.9
88+
if(variable %in% c("UTC_Time")){
89+
x[][x[] == -999.9] <- NA
90+
}
91+
92+
#### -32768
93+
if(variable %in% c("Sensor_Azimuth",
94+
"Sensor_Zenith",
95+
"Solar_Azimuth",
96+
"Solar_Zenith",
97+
"Lunar_Azimuth",
98+
"Lunar_Zenith",
99+
"Glint_Angle",
100+
"Moon_Illumination_Fraction",
101+
"Moon_Phase_Angle")){
102+
x[][x[] == -32768] <- NA
103+
}
104+
105+
106+
#### 65535
107+
if(variable %in% c(
108+
"DNB_At_Sensor_Radiance_500m",
109+
"BrightnessTemperature_M12",
110+
"BrightnessTemperature_M13",
111+
"BrightnessTemperature_M15",
112+
"BrightnessTemperature_M16",
113+
"QF_Cloud_Mask",
114+
"QF_DNB",
115+
"QF_VIIRS_M10",
116+
"QF_VIIRS_M11",
117+
"QF_VIIRS_M12",
118+
"QF_VIIRS_M13",
119+
"QF_VIIRS_M15",
120+
"QF_VIIRS_M16",
121+
"Radiance_M10",
122+
"Radiance_M11",
123+
"QF_Cloud_Mask",
75124
"DNB_BRDF-Corrected_NTL",
76-
"Gap_Filled_DNB_BRDF-Corrected_NTL",
77125
"DNB_Lunar_Irradiance",
78-
79-
# VNP46A3/4
126+
"Gap_Filled_DNB_BRDF-Corrected_NTL",
80127
"AllAngle_Composite_Snow_Covered",
81-
"AllAngle_Composite_Snow_Covered_Std",
128+
"AllAngle_Composite_Snow_Covered_Num",
82129
"AllAngle_Composite_Snow_Free",
83-
"AllAngle_Composite_Snow_Free_Std",
130+
"AllAngle_Composite_Snow_Free_Num",
84131
"NearNadir_Composite_Snow_Covered",
85-
"NearNadir_Composite_Snow_Covered_Std",
132+
"NearNadir_Composite_Snow_Covered_Num",
86133
"NearNadir_Composite_Snow_Free",
87-
"NearNadir_Composite_Snow_Free_Std",
134+
"NearNadir_Composite_Snow_Free_Num",
88135
"OffNadir_Composite_Snow_Covered",
89-
"OffNadir_Composite_Snow_Covered_Std",
136+
"OffNadir_Composite_Snow_Covered_Num",
90137
"OffNadir_Composite_Snow_Free",
91-
"OffNadir_Composite_Snow_Free_Std")
92-
){
93-
138+
"OffNadir_Composite_Snow_Free_Num",
139+
"AllAngle_Composite_Snow_Covered_Std",
140+
"AllAngle_Composite_Snow_Free_Std",
141+
"NearNadir_Composite_Snow_Covered_Std",
142+
"NearNadir_Composite_Snow_Free_Std",
143+
"OffNadir_Composite_Snow_Covered_Std",
144+
"OffNadir_Composite_Snow_Free_Std"
145+
)){
94146
x[][x[] == 65535] <- NA
95-
96147
}
97-
148+
98149
return(x)
99150
}
100151

@@ -187,10 +238,10 @@ file_to_raster <- function(f,
187238
}
188239
}
189240

190-
# Above doesn't fully capture
191-
if(variable %in% "Latest_High_Quality_Retrieval"){
192-
out[out == 255] <- NA
193-
}
241+
# # Above doesn't fully capture
242+
# if(variable %in% "Latest_High_Quality_Retrieval"){
243+
# out[out == 255] <- NA
244+
# }
194245

195246
#### Monthly/Annually
196247
} else{
@@ -476,21 +527,19 @@ count_n_obs <- function(values, coverage_fraction) {
476527
#' * For `product_id` `"VNP46A2"`, uses `Gap_Filled_DNB_BRDF-Corrected_NTL`.
477528
#' * For `product_id`s `"VNP46A3"` and `"VNP46A4"`, uses `NearNadir_Composite_Snow_Free`.
478529
#' For information on other variable choices, see [here](https://ladsweb.modaps.eosdis.nasa.gov/api/v2/content/archives/Document%20Archive/Science%20Data%20Product%20Documentation/VIIRS_Black_Marble_UG_v1.2_April_2021.pdf); for `VNP46A1`, see Table 3; for `VNP46A2` see Table 6; for `VNP46A3` and `VNP46A4`, see Table 9.
479-
#' @param 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)`).
530+
#' @param 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`).
480531
#'
481532
#'
482533
#' For `VNP46A1` and `VNP46A2` (daily data):
483534
#' - `0`: High-quality, Persistent nighttime lights
484535
#' - `1`: High-quality, Ephemeral nighttime Lights
485536
#' - `2`: Poor-quality, Outlier, potential cloud contamination, or other issues
486-
#' - `255`: No retrieval, Fill value (masked out on ingestion)
487537
#'
488538
#'
489539
#' For `VNP46A3` and `VNP46A4` (monthly and annual data):
490540
#' - `0`: Good-quality, The number of observations used for the composite is larger than 3
491541
#' - `1`: Poor-quality, The number of observations used for the composite is less than or equal to 3
492542
#' - `2`: Gap filled NTL based on historical data
493-
#' - `255`: Fill value
494543
#' @param 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`).
495544
#' @param interpol_na When data for more than one date is downloaded, whether to interpolate `NA` values in rasters using the `raster::approxNA` function. Additional arguments for the `raster::approxNA` function can also be passed into `bm_extract` (eg, `method`, `rule`, `f`, `ties`, `z`, `NA_rule`). (Default: `FALSE`).
496545
#' @param output_location_type Where to produce output; either `memory` or `file`. If `memory`, functions returns a dataframe in R. If `file`, function exports a `.csv` file and returns `NULL`.
@@ -539,7 +588,7 @@ bm_extract <- function(roi_sf,
539588
aggregation_fun = c("mean"),
540589
add_n_pixels = TRUE,
541590
variable = NULL,
542-
quality_flag_rm = 255,
591+
quality_flag_rm = NULL,
543592
check_all_tiles_exist = TRUE,
544593
interpol_na = FALSE,
545594
output_location_type = "memory", # memory, file
@@ -784,21 +833,19 @@ bm_extract <- function(roi_sf,
784833
#' * For `product_id` `"VNP46A2"`, uses `Gap_Filled_DNB_BRDF-Corrected_NTL`.
785834
#' * For `product_id`s `"VNP46A3"` and `"VNP46A4"`, uses `NearNadir_Composite_Snow_Free`.
786835
#' For information on other variable choices, see [here](https://ladsweb.modaps.eosdis.nasa.gov/api/v2/content/archives/Document%20Archive/Science%20Data%20Product%20Documentation/VIIRS_Black_Marble_UG_v1.2_April_2021.pdf); for `VNP46A1`, see Table 3; for `VNP46A2` see Table 6; for `VNP46A3` and `VNP46A4`, see Table 9.
787-
#' @param 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)`).
836+
#' @param 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`).
788837
#'
789838
#'
790839
#' For `VNP46A1` and `VNP46A2` (daily data):
791840
#' - `0`: High-quality, Persistent nighttime lights
792841
#' - `1`: High-quality, Ephemeral nighttime Lights
793842
#' - `2`: Poor-quality, Outlier, potential cloud contamination, or other issues
794-
#' - `255`: No retrieval, Fill value (masked out on ingestion)
795843
#'
796844
#'
797845
#' For `VNP46A3` and `VNP46A4` (monthly and annual data):
798846
#' - `0`: Good-quality, The number of observations used for the composite is larger than 3
799847
#' - `1`: Poor-quality, The number of observations used for the composite is less than or equal to 3
800848
#' - `2`: Gap filled NTL based on historical data
801-
#' - `255`: Fill value
802849
#' @param 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`).
803850
#' @param interpol_na When data for more than one date is downloaded, whether to interpolate `NA` values using the `raster::approxNA` function. Additional arguments for the `raster::approxNA` function can also be passed into `bm_raster` (eg, `method`, `rule`, `f`, `ties`, `z`, `NA_rule`). (Default: `FALSE`).
804851
#' @param output_location_type Where to produce output; either `memory` or `file`. If `memory`, functions returns a raster in R. If `file`, function exports a `.tif` file and returns `NULL`.
@@ -859,7 +906,7 @@ bm_raster <- function(roi_sf,
859906
date,
860907
bearer,
861908
variable = NULL,
862-
quality_flag_rm = 255,
909+
quality_flag_rm = NULL,
863910
check_all_tiles_exist = TRUE,
864911
interpol_na = FALSE,
865912
output_location_type = "memory", # memory, file

vignettes/assess-quality.Rmd

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ 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")
86+
variable = "Gap_Filled_DNB_BRDF-Corrected_NTL",
87+
quality = NULL)
8788
```
8889

8990
<details>
@@ -181,8 +182,6 @@ For daily data, the quality values are:
181182

182183
* 2: Poor-quality, Outlier, potential cloud contamination, or other issues
183184

184-
* 255: No retrieval, Fill value (masked out on ingestion)
185-
186185
We can map quality by using the `Mandatory_Quality_Flag` variable.
187186

188187
```{r, results='hide'}
@@ -206,8 +205,7 @@ quality_df <- quality_df %>%
206205
dplyr::mutate(value_str = case_when(
207206
value == 0 ~ "0: High-quality, persistent",
208207
value == 1 ~ "1: High-quality, ephemeral",
209-
value == 2 ~ "2: Poor-quality",
210-
value == 255 ~ "255: Fill value"
208+
value == 2 ~ "2: Poor-quality"
211209
))
212210
213211
##### Map
@@ -228,15 +226,15 @@ ggplot() +
228226

229227
#### Nighttime lights for good quality observations <a name="daily-goodq"></a>
230228

231-
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.
229+
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.
232230

233231
```{r, results='hide'}
234232
ntl_good_qual_r <- bm_raster(roi_sf = roi_sf,
235233
product_id = "VNP46A2",
236234
date = "2023-01-01",
237235
bearer = bearer,
238236
variable = "Gap_Filled_DNB_BRDF-Corrected_NTL",
239-
quality_flag_rm = c(2, 255))
237+
quality_flag_rm = 2)
240238
```
241239

242240
<details>
@@ -316,7 +314,7 @@ ntl_r <- bm_raster(roi_sf = roi_sf,
316314
date = "2023-01-01",
317315
bearer = bearer,
318316
variable = "DNB_BRDF-Corrected_NTL",
319-
quality_flag_rm = c(2, 255))
317+
quality_flag_rm = 2)
320318
```
321319

322320
<details>
@@ -446,8 +444,6 @@ For monthly and annual data, the quality values are:
446444

447445
* 2: Gap filled NTL based on historical data
448446

449-
* 255: Fill value
450-
451447
We can map quality by adding `_Quality` to the variable name.
452448

453449
```{r, results='hide'}
@@ -471,8 +467,7 @@ quality_df <- quality_df %>%
471467
dplyr::mutate(value_str = case_when(
472468
value == 0 ~ "0: Good quality",
473469
value == 1 ~ "1: Poor quality",
474-
value == 2 ~ "2: Gap filled",
475-
value == 255 ~ "255: Fill value"
470+
value == 2 ~ "2: Gap filled"
476471
))
477472
478473
##### Map
@@ -493,15 +488,15 @@ ggplot() +
493488

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

496-
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.
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.
497492

498493
```{r, results='hide'}
499494
ntl_good_qual_r <- bm_raster(roi_sf = roi_sf,
500495
product_id = "VNP46A3",
501496
date = "2023-01-01",
502497
bearer = bearer,
503498
variable = "NearNadir_Composite_Snow_Free",
504-
quality_flag_rm = c(1, 255))
499+
quality_flag_rm = 1)
505500
```
506501

507502
<details>
113 KB
Loading
398 KB
Loading

0 commit comments

Comments
 (0)