Skip to content

Commit

Permalink
init ntl
Browse files Browse the repository at this point in the history
  • Loading branch information
ramarty committed Apr 18, 2024
1 parent e2ce950 commit 335cdb5
Show file tree
Hide file tree
Showing 42 changed files with 20,876 additions and 4 deletions.
4 changes: 2 additions & 2 deletions notebooks/nighttime-lights/01_download_ntl.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ roi_sf <- read_sf(file.path(boundaries_dir, "ner_adm_ignn_20230720_ab_shp",
## Download data
annual_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A4",
date = 2012,
date = 2012:2023,
bearer = bearer,
output_location_type = "file",
file_dir = file.path(ntl_dir,
Expand All @@ -29,7 +29,7 @@ month_r <- bm_raster(roi_sf = roi_sf,

day_r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A2",
date = seq.Date(from = ymd("2024-01-01"), to = ymd("2024-01-12"), by = "day"),
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,
Expand Down
109 changes: 109 additions & 0 deletions notebooks/nighttime-lights/02_extract_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Extract data

AGG_FUNS <- c("mean", "sum")

# Load ROIs --------------------------------------------------------------------
adm0_sf <- read_sf(file.path(boundaries_dir, "ner_adm_ignn_20230720_ab_shp",
"NER_admbnda_adm0_IGNN_20230720.shp"))
adm1_sf <- read_sf(file.path(boundaries_dir, "ner_adm_ignn_20230720_ab_shp",
"NER_admbnda_adm1_IGNN_20230720.shp"))
adm2_sf <- read_sf(file.path(boundaries_dir, "ner_adm_ignn_20230720_ab_shp",
"NER_admbnda_adm1_IGNN_20230720.shp"))
adm3_sf <- read_sf(file.path(boundaries_dir, "ner_adm_ignn_20230720_ab_shp",
"NER_admbnda_adm1_IGNN_20230720.shp"))

city_df <- read.csv(file.path(city_dir, "niger_cities.csv"))
city_sf <- st_as_sf(city_df, coords = c("longitude", "latitude"), crs = 4326)
city_buff_sf <- city_sf %>% st_buffer(dist = 10*1000)

# Extract individual files -----------------------------------------------------

#### Loop through unit
for(unit in c("adm0", "adm1", "adm2", "adm3", "city")){

dir.create(file.path(ntl_dir, "aggregated_individual", unit))

if(unit == "adm0") unit_sf <- adm0_sf
if(unit == "adm1") unit_sf <- adm1_sf
if(unit == "adm2") unit_sf <- adm2_sf
if(unit == "adm3") unit_sf <- adm3_sf
if(unit == "city") unit_sf <- city_buff_sf

#### Loop through time period
for(time_period in c("annually", "monthly", "daily")){

dir.create(file.path(ntl_dir, "aggregated_individual", unit, time_period))

files_vec <- file.path(ntl_dir,
"individual_rasters",
time_period) %>%
list.files(full.names = T)

#### Loop through dates
for(file_i in files_vec){

date_i <- file_i %>%
str_replace_all(".*qflag_t", "") %>%
str_replace_all(".tif", "")

OUT_DIR <- file.path(ntl_dir, "aggregated_individual", unit, time_period,
paste0(date_i, ".Rds"))

#### Extract data
if(!file.exists(OUT_DIR)){

r <- terra::rast(file_i)

ntl_df <- exact_extract(r, unit_sf, fun = AGG_FUNS)
names(ntl_df) <- paste0("ntl_", names(ntl_df))
ntl_df$date <- date_i

unit_df <- unit_sf %>%
st_drop_geometry()
unit_df$date <- NULL

ntl_df <- bind_cols(unit_df, ntl_df)

#### Cleanup dates
if(time_period == "annually"){

ntl_df$date <- ntl_df$date %>% as.numeric()

} else if(time_period == "monthly"){

ntl_df$date <- paste0(ntl_df$date, "-01") %>%
str_replace_all("_", "-") %>%
ymd()

} else if(time_period == "daily"){

ntl_df$date <- ntl_df$date %>%
str_replace_all("_", "-") %>%
ymd()

}

saveRDS(ntl_df, OUT_DIR)

}
}
}
}

# Append files -----------------------------------------------------------------
for(unit in c("adm0", "adm1", "adm2", "adm3", "city")){
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)

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")))

}
}

6 changes: 4 additions & 2 deletions notebooks/nighttime-lights/_main.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ data_dir <- file.path("/Users", "rmarty", "Library", "CloudStorage", "OneDrive-S
"Development Data Partnership - Niger Economic Monitor", "Data")

boundaries_dir <- file.path(data_dir, "Boundaries")
ntl_dir <- file.path(data_dir, "Nighttime Lights")
ntl_dir <- file.path(data_dir, "Nighttime Lights")
city_dir <- file.path(data_dir, "Cities")

# Packages ---------------------------------------------------------------------
library(dplyr)
library(lubridate)
library(sf)
library(ggplot2)
library(leaflet)
library(haven)
library(terra)
#library(blackmarbler)
library(blackmarbler)
Loading

0 comments on commit 335cdb5

Please sign in to comment.