diff --git a/notebooks/nighttime-lights/01_download_ntl.R b/notebooks/nighttime-lights/01_download_ntl.R index 7ee1fee..c535c74 100644 --- a/notebooks/nighttime-lights/01_download_ntl.R +++ b/notebooks/nighttime-lights/01_download_ntl.R @@ -1,7 +1,9 @@ # Download Nighttime Lights ## Bearer token -bearer <- read.csv("/Users/rmarty/Library/CloudStorage/OneDrive-WBG/Webscraping API Keys/bearer_bm.csv") %>% +# bearer <- read.csv("/Users/rmarty/Library/CloudStorage/OneDrive-WBG/Webscraping API Keys/bearer_bm.csv") %>% +# pull(token) +bearer <- read.csv("~/Desktop/bearer_bm.csv") %>% pull(token) ## Define ROI @@ -27,12 +29,12 @@ month_r <- bm_raster(roi_sf = roi_sf, "individual_rasters", "monthly")) -day_r <- bm_raster(roi_sf = roi_sf, - product_id = "VNP46A2", - date = seq.Date(from = ymd("2023-01-01"), to = Sys.Date(), by = "day"), - bearer = bearer, - output_location_type = "file", - file_dir = file.path(ntl_dir, - "individual_rasters", - "daily")) +# day_r <- bm_raster(roi_sf = roi_sf, +# product_id = "VNP46A2", +# date = seq.Date(from = ymd("2023-01-01"), to = Sys.Date(), by = "day"), +# bearer = bearer, +# output_location_type = "file", +# file_dir = file.path(ntl_dir, +# "individual_rasters", +# "daily")) diff --git a/notebooks/nighttime-lights/02_extract_data.R b/notebooks/nighttime-lights/02_extract_data.R index 7cf0c0b..9c4755b 100644 --- a/notebooks/nighttime-lights/02_extract_data.R +++ b/notebooks/nighttime-lights/02_extract_data.R @@ -30,7 +30,7 @@ for(unit in c("adm0", "adm1", "adm2", "adm3", "city")){ if(unit == "city") unit_sf <- city_buff_sf #### Loop through time period - for(time_period in c("annually", "monthly", "daily")){ + for(time_period in c("annually", "monthly")){ # "daily" dir.create(file.path(ntl_dir, "aggregated_individual", unit, time_period)) @@ -92,18 +92,34 @@ for(unit in c("adm0", "adm1", "adm2", "adm3", "city")){ # Append files ----------------------------------------------------------------- for(unit in c("adm0", "adm1", "adm2", "adm3", "city")){ - for(time_period in c("annually", "monthly", "daily")){ + for(time_period in c("annually", "monthly")){ # "daily" ntl_df <- file.path(ntl_dir, "aggregated_individual", unit, time_period) %>% list.files(full.names = T) %>% map_df(readRDS) + # Add moving average for monthly data + if(time_period == "monthly"){ + + if(unit == "adm0") ntl_df$name <- ntl_df$ADM0_FR + if(unit == "adm1") ntl_df$name <- ntl_df$ADM1_PCODE + if(unit == "adm2") ntl_df$name <- ntl_df$ADM2_PCODE + if(unit == "adm3") ntl_df$name <- ntl_df$ADM3_PCODE + + ntl_df <- ntl_df %>% + arrange(date) %>% + group_by(name) %>% + mutate(ntl_mean_3m_ma = slider::slide_dbl(ntl_mean, mean, .before = 3, .after = 3), + ntl_sum_3m_ma = slider::slide_dbl(ntl_sum, mean, .before = 3, .after = 3)) %>% + ungroup() + } + saveRDS(ntl_df, file.path(ntl_dir, "aggregated_appended", paste0(unit, "_", time_period, ".Rds"))) write_dta(ntl_df, file.path(ntl_dir, "aggregated_appended", - paste0(unit, "_", time_period, ".dta"))) - + paste0(unit, "_", time_period, ".dta"))) + } } diff --git a/notebooks/nighttime-lights/_main.R b/notebooks/nighttime-lights/_main.R index f235ad2..4c5b07d 100644 --- a/notebooks/nighttime-lights/_main.R +++ b/notebooks/nighttime-lights/_main.R @@ -7,6 +7,9 @@ git_dir <- file.path("/Users", "rmarty", "Library", "CloudStorage", "OneDrive-WB data_dir <- file.path("/Users", "rmarty", "Library", "CloudStorage", "OneDrive-SharedLibraries-WBG", "Development Data Partnership - Niger Economic Monitor", "Data") +data_dir <- file.path("~/Dropbox/World Bank/Side Work/", + "Development Data Partnership - Niger Economic Monitor", "Data") + boundaries_dir <- file.path(data_dir, "Boundaries") ntl_dir <- file.path(data_dir, "Nighttime Lights") city_dir <- file.path(data_dir, "Cities") @@ -19,7 +22,13 @@ library(ggplot2) library(leaflet) library(haven) library(terra) +library(blackmarbler) +library(stringr) +library(exactextractr) library(purrr) library(stringr) library(exactextractr) -library(blackmarbler) \ No newline at end of file +library(blackmarbler) +library(sparkline) +library(sparklyr) +library(slider) diff --git a/notebooks/nighttime-lights/ntl_analysis.html b/notebooks/nighttime-lights/ntl_analysis.html index 0c0c80e..ccd1f51 100644 --- a/notebooks/nighttime-lights/ntl_analysis.html +++ b/notebooks/nighttime-lights/ntl_analysis.html @@ -69,7 +69,6 @@ - @@ -78,12 +77,9 @@ - + - - - - + @@ -98,12 +94,18 @@

Table of contents

@@ -143,6 +145,7 @@

Nighttime Lights Analysis

Data

We use nighttime lights data from the VIIRS Black Marble dataset. Raw nighttime lights data requires correction due to cloud cover and stray light, such as lunar light. The Black Marble dataset applies advanced algorithms to correct raw nighttime light values and calibrate data so that trends in lights over time can be meaningfully analyzed. From VIIRS Black Marble, we use data from January 2012 through present—where data is available at a 500-meter resolution.

+

The following code downloads and processes data: _main.R Defines filepaths and loads packages 01_download_ntl.R Downloads NTL data 02_extract_data.R Extracts/aggregates nighttime light data to different polygons

Map of nighttime lights

@@ -214,54 +217,133 @@

Map of nighttime lights

)
-
- +
+
-