Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ramarty committed Dec 7, 2023
1 parent 8bc2c7a commit 6a2f042
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 173 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ Authors@R:
Description: Create geographically referenced rasters of NASA Black Marble nighttime lights data <https://blackmarble.gsfc.nasa.gov/>.
License: MIT + file LICENSE
Encoding: UTF-8
VignetteBuilder: utils
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
URL: https://worldbank.github.io/blackmarbler/
BugReports: https://github.com/worldbank/blackmarbler/issues
Imports:
readr,
hdf5r,
dplyr,
purrr,
lubridate,
raster,
sf,
exactextractr,
stringr,
hdf5r,
httr
Suggests:
geodata,
ggplot2,
tidyr
tidyr,
knitr
5 changes: 3 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import(exactextractr)
import(hdf5r)
import(httr)
import(lubridate)
import(purrr)
import(raster, except = c(union, select, intersect, origin, tail, head))
import(purrr, except = c(flatten_df, values))
import(raster, except = c(union, select, intersect, origin, tail, head, values))

import(readr)
import(sf)
import(stringr)
38 changes: 22 additions & 16 deletions R/blackmarbler.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,21 @@ file_to_raster <- function(f,
if(variable %in% c("DNB_BRDF-Corrected_NTL",
"Gap_Filled_DNB_BRDF-Corrected_NTL",
"Latest_High_Quality_Retrieval")){

for(val in quality_flag_rm){ # out[qf %in% quality_flag_rm] doesn't work, so loop
out[qf == val] <- NA
}
}
}

# Above doesn't fully capture
if(variable %in% "Latest_High_Quality_Retrieval"){
out[out == 255] <- NA
}

#### Monthly/Annually
} else{

lat <- h5_data[["HDFEOS/GRIDS/VIIRS_Grid_DNB_2d/Data Fields/lat"]][]
lon <- h5_data[["HDFEOS/GRIDS/VIIRS_Grid_DNB_2d/Data Fields/lon"]][]

Expand Down Expand Up @@ -275,10 +281,10 @@ create_dataset_name_df <- function(product_id,
}

#### Create data
files_df <- map2_dfr(param_df$year,
param_df$day,
read_bm_csv,
product_id)
files_df <- purrr::map2_dfr(param_df$year,
param_df$day,
read_bm_csv,
product_id)

return(files_df)
}
Expand All @@ -301,11 +307,11 @@ download_raster <- function(file_name,
download_path <- file.path(temp_dir, file_name)

if(quiet == FALSE) message(paste0("Processing: ", file_name))

response <- GET(url,
add_headers(headers),
write_disk(download_path, overwrite = TRUE),
progress())
response <- httr::GET(url,
add_headers(headers),
write_disk(download_path, overwrite = TRUE),
progress())

if(response$status_code != 200){
message("Error in downloading data")
Expand Down Expand Up @@ -660,17 +666,17 @@ bm_extract <- function(roi_sf,
#'
#' @export
#'
#' @import purrr
#' @import stringr
#' @import readr
#' @import hdf5r
#' @import dplyr
#' @import sf
#' @import lubridate
#' @import readr
#' @import exactextractr
#' @import stringr
#' @import httr
#' @rawNamespace import(raster, except = c(union, select, intersect, origin, tail, head))

#' @import lubridate
#' @rawNamespace import(purrr, except = c(flatten_df, values))
#' @rawNamespace import(raster, except = c(union, select, intersect, origin, tail, head, values))
#'
# @rawNamespace import(utils, except = c(stack, unstack))
bm_raster <- function(roi_sf,
product_id,
Expand Down
24 changes: 0 additions & 24 deletions man/bm_tiles_sf.Rd

This file was deleted.

25 changes: 12 additions & 13 deletions vignettes/assess-quality.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This page illustrates how to examine the quality of nighttime lights data.

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

```{r}
```{r, results='hide'}
library(blackmarbler)
library(geodata)
library(sf)
Expand All @@ -56,6 +56,7 @@ library(exactextractr)
library(lubridate)
library(tidyr)
library(geodata)
library(knitr)
bearer <- "BEARER-TOKEN-HERE"
```
Expand All @@ -77,7 +78,7 @@ Below shows an example examining quality for daily data (`VNP46A2`).

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.

```{r}
```{r, results='hide'}
ntl_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A2",
date = "2023-01-01",
Expand Down Expand Up @@ -137,7 +138,7 @@ print(n_non_na_pixel_num / n_pixel_num)

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

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

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

```{r}
```{r, results='hide'}
quality_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A2",
date = "2023-01-01",
Expand Down Expand Up @@ -229,7 +230,7 @@ ggplot() +

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.

```{r}
```{r, results='hide'}
ntl_good_qual_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A2",
date = "2023-01-01",
Expand Down Expand Up @@ -273,7 +274,7 @@ ggplot() +

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.

```{r}
```{r, results='hide'}
ntl_tmp_gap_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A2",
date = "2023-01-01",
Expand All @@ -290,8 +291,6 @@ ntl_tmp_gap_r <- ntl_tmp_gap_r |> mask(roi_sf)
ntl_tmp_gap_df <- rasterToPoints(ntl_tmp_gap_r, spatial = TRUE) |> as.data.frame()
names(ntl_tmp_gap_df) <- c("value", "x", "y")
#ntl_tmp_gap_df$value[ntl_tmp_gap_df$value == 255] <- NA
##### Map
ggplot() +
geom_raster(data = ntl_tmp_gap_df,
Expand All @@ -311,7 +310,7 @@ ggplot() +

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.

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

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}
```{r, results='hide'}
ntl_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A3",
date = "2023-01-01",
Expand Down Expand Up @@ -402,7 +401,7 @@ ggplot() +

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}
```{r, results='hide'}
cf_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A3",
date = "2023-01-01",
Expand Down Expand Up @@ -451,7 +450,7 @@ For monthly and annual data, the quality values are:

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

```{r}
```{r, results='hide'}
quality_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A3",
date = "2023-01-01",
Expand Down Expand Up @@ -496,7 +495,7 @@ ggplot() +

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.

```{r}
```{r, results='hide'}
ntl_good_qual_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A3",
date = "2023-01-01",
Expand Down
117 changes: 2 additions & 115 deletions vignettes/assess-quality.html

Large diffs are not rendered by default.

0 comments on commit 6a2f042

Please sign in to comment.