Skip to content

Commit

Permalink
Merge pull request #25 from datapartnership/ntl
Browse files Browse the repository at this point in the history
Ntl
  • Loading branch information
ramarty authored Jun 25, 2024
2 parents 97b8466 + 3291464 commit f7322f8
Show file tree
Hide file tree
Showing 24 changed files with 3,622 additions and 808 deletions.
20 changes: 11 additions & 9 deletions notebooks/nighttime-lights/01_download_ntl.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"))

24 changes: 20 additions & 4 deletions notebooks/nighttime-lights/02_extract_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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")))
}
}

11 changes: 10 additions & 1 deletion notebooks/nighttime-lights/_main.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
library(blackmarbler)
library(sparkline)
library(sparklyr)
library(slider)
942 changes: 478 additions & 464 deletions notebooks/nighttime-lights/ntl_analysis.html

Large diffs are not rendered by default.

Loading

0 comments on commit f7322f8

Please sign in to comment.