Extract and aggregate nighttime lights data from NASA Black Marble data
Usage
bm_extract(
roi_sf,
product_id,
date,
bearer,
aggregation_fun = c("sum"),
add_n_pixels = TRUE,
variable = NULL,
quality_flag_rm = NULL,
check_all_tiles_exist = TRUE,
interpol_na = FALSE,
output_location_type = "memory",
file_dir = NULL,
file_prefix = NULL,
file_skip_if_exists = TRUE,
file_return_null = FALSE,
h5_dir = NULL,
download_method = "httr",
quiet = FALSE,
...
)Arguments
- roi_sf
Region of interest; sf polygon. Must be in the WGS 84 (epsg:4326) coordinate reference system.
- product_id
One of the following:
"VNP46A1": Daily (raw)"VNP46A2": Daily (corrected)"VNP46A3": Monthly"VNP46A4": Annual
- date
Date of raster data. Entering one date will produce a
SpatRasterobject. Entering multiple dates will produce aSpatRasterobject with multiple bands; one band per date.For
product_ids"VNP46A1"and"VNP46A2", a date (eg,"2021-10-03").For
product_id"VNP46A3", a date or year-month (e.g.,"2021-10-01", where the day will be ignored, or"2021-10").For
product_id"VNP46A4", year or date (e.g.,"2021-10-01", where the month and day will be ignored, or2021).
- bearer
NASA bearer token. For instructions on how to create a token, see here.
- aggregation_fun
Function used to aggregate nighttime lights data to polygons; this values is passed to the
funargument in exactextractr::exact_extract (Default:sum).- add_n_pixels
Whether to add a variable indicating the number of nighttime light pixels used to compute nighttime lights statistics (eg, number of pixels used to compute average of nighttime lights). When
TRUE, it adds three values:n_non_na_pixels(the number of non-NApixels used for computing nighttime light statistics);n_pixels(the total number of pixels); andprop_non_na_pixelsthe proportion of the two. (Default:TRUE).- variable
Variable to used to create raster (default:
NULL). IfNULL, uses the following default variables:For
product_id:VNP46A1", usesDNB_At_Sensor_Radiance_500m.For
product_id"VNP46A2", usesGap_Filled_DNB_BRDF-Corrected_NTL.For
product_ids"VNP46A3"and"VNP46A4", usesNearNadir_Composite_Snow_Free. To see all variable choices, setvariable = ""(this will create an error message that lists all valid variables). For additional information on variable choices, see here; forVNP46A1, see Table 3; forVNP46A2see Table 6; forVNP46A3andVNP46A4, see Table 9.
- 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 toNAfor each value in thequality_flag_rmvector. Note thatquality_flag_rmdoes not apply forVNP46A1. (Default:NULL).For
VNP46A2(daily data):0: High-quality, Persistent nighttime lights1: High-quality, Ephemeral nighttime Lights2: Poor-quality, Outlier, potential cloud contamination, or other issues
For
VNP46A3andVNP46A4(monthly and annual data):0: Good-quality, The number of observations used for the composite is larger than 31: Poor-quality, The number of observations used for the composite is less than or equal to 32: Gap filled NTL based on historical data
- 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).- interpol_na
When data for more than one date is downloaded, whether to interpolate
NAvalues in rasters using theterra::approximatefunction. Additional arguments for theterra::approximatefunction can also be passed intobm_extract(eg,method,rule,f,ties,z,NA_rule). (Default:FALSE).- output_location_type
Where to produce output; either
memoryorfile. Ifmemory, functions returns a dataframe in R. Iffile, function exports a.csvfile and returnsNULL.- file_dir
(If
output_location_type = file). The directory where data should be exported (default:NULL, so the working directory will be used)- file_prefix
(If
output_location_type = file). Prefix to add to the file to be saved. The file will be saved as the following:[file_prefix][product_id]_t[date].csv- file_skip_if_exists
(If
output_location_type = file). Whether the function should first check wither the file already exists, and to skip downloading or extracting data if the data for that date if the file already exists (default:TRUE).- file_return_null
Whether to return
NULLinstead of adataframe. Whenoutput_location_type = 'file', the function will export data to thefile_dirdirectory. Whenfile_return_null = FALSE, the function will also return adataframeof the queried data—so the data is available in R memory. Settingfile_return_null = TRUE, data will be saved tofile_dirbut no data will be returned by the function to R memory (default:FALSE).- h5_dir
Black Marble data are originally downloaded as
h5files. Ifh5_dir = NULL, the function downloads to a temporary directory then deletes the directory. Ifh5_diris set to a path,h5files are saved to that directory and not deleted. The function will then check if the neededh5file already exists in the directory; if it exists, the function will not re-download theh5file.- download_method
Method to download data (h5 files) from NASA LAADS Archive: "
httr" or "wget". Ifhttr, uses thehttr2R package to download data. Ifwget, uses thewgetcommand line tool.httris fully integrated in R, whilewgetrequires thewgetsystem command.wgetcan be more efficient and can help avoid network issues. (Default:"httr").- quiet
Suppress output that show downloading progress and other messages. (Default:
FALSE).- ...
Additional arguments for
terra::approximate, ifinterpol_na = TRUE
Author
Robert Marty rmarty@worldbank.org
Examples
if (FALSE) { # \dontrun{
# Define bearer token
bearer <- "BEARER-TOKEN-HERE"
# sf polygon of Ghana
library(geodata)
roi_sf <- gadm(country = "GHA", level=1, path = tempdir()) %>% st_as_sf()
# Daily data: raster for October 3, 2021
ken_20210205_r <- bm_extract(roi_sf = roi_sf,
product_id = "VNP46A2",
date = "2021-10-03",
bearer = bearer)
# Monthly data: raster for March 2021
ken_202103_r <- bm_extract(roi_sf = roi_sf,
product_id = "VNP46A3",
date = "2021-03-01",
bearer = bearer)
# Annual data: raster for 2021
ken_2021_r <- bm_extract(roi_sf = roi_sf,
product_id = "VNP46A4",
date = 2021,
bearer = bearer)
} # }
