# Individual Files -------------------------------------------------------------
dir.create (file.path (ntl_bm_dir, "aggregated_individual_files" ))
# "adm3",
# "cities5km",
# "cities10km",
# "citiesbuff"
#### Setup loops
for (unit in c ("adm0" ,
"adm1" ,
"adm2" )){
for (time_type in c ("annual" ,
"monthly" )){
if (time_type == "annual" ){
time_vec <- 2012 : 2024
}
if (time_type == "monthly" ){
time_vec <- file.path (ntl_bm_dir, "rasters" , "monthly" ) %>%
list.files () %>%
str_replace_all (".tif" , "" ) %>%
str_replace_all ("VNP46A3_NearNadir_Composite_Snow_Free_qflag_t" , "" ) %>%
str_replace_all ("_" , "-" ) %>%
paste0 ("-01" )
}
if (time_type == "daily" ){
time_vec <- file.path (ntl_bm_dir, "rasters" , "daily" ) %>%
list.files () %>%
str_replace_all (".tif" , "" ) %>%
str_replace_all ("VNP46A2_Gap_Filled_DNB_BRDF-Corrected_NTL_qflag_t" , "" ) %>%
str_replace_all ("_" , "-" )
}
for (time_i in time_vec){
#### Only process if file doesn't exist
dir.create (file.path (ntl_bm_dir, "aggregated_individual_files" , unit))
dir.create (file.path (ntl_bm_dir, "aggregated_individual_files" , unit, time_type))
OUT_FILE <- file.path (ntl_bm_dir, "aggregated_individual_files" , unit, time_type,
paste0 ("ntl_" , time_i, ".Rds" ))
if (! file.exists (OUT_FILE)){
#### Load Unit
if (unit == "adm0" ){
roi_sf <- load_gadm (0 )
}
if (unit == "adm1" ){
roi_sf <- load_gadm (1 )
}
if (unit == "adm2" ){
roi_sf <- load_gadm (2 )
}
if (unit == "cities5km" ){
roi_sf <- readRDS (file.path (data_dir, "cities" , "cities_country_5km_sf.Rds" ))
}
if (unit == "cities10km" ){
roi_sf <- readRDS (file.path (data_dir, "cities" , "cities_country_10km_sf.Rds" ))
}
if (unit == "citiesbuff" ){
roi_sf <- readRDS (file.path (data_dir, "cities" , "cities_country_buff_sf.Rds" ))
}
#### Load Raster
if (time_type == "annual" ){
bm_product <- "VNP46A4"
time_raster_i <- time_i
}
if (time_type == "monthly" ){
bm_product <- "VNP46A3"
time_raster_i <- time_i %>%
substring (1 , 7 ) %>%
str_replace_all ("-" , "_" )
}
if (time_type == "daily" ){
bm_product <- "VNP46A2"
time_raster_i <- time_i %>%
str_replace_all ("-" , "_" )
}
if (time_type %in% c ("annual" , "monthly" )){
ntl_r <- rast (file.path (ntl_bm_dir, "rasters" , time_type,
paste0 (bm_product,"_NearNadir_Composite_Snow_Free_qflag_t" ,time_raster_i,".tif" )))
} else {
ntl_r <- rast (file.path (ntl_bm_dir, "rasters" , "daily" ,
paste0 ("VNP46A2_Gap_Filled_DNB_BRDF-Corrected_NTL_qflag_t" ,time_raster_i,".tif" )))
}
# Replace low values with 0
ntl_r_1na <- ntl_r
ntl_r_1na[ntl_r_1na < 1 ] <- 0
ntl_r_0_5na <- ntl_r
ntl_r_0_5na[ntl_r_0_5na < 0.5 ] <- 0
ntl_r_0_1na <- ntl_r
ntl_r_0_1na[ntl_r_0_1na < 0.1 ] <- 0
#### Gas Flaring Mask
gs_5km_sf <- readRDS (file.path (gas_flare_dir, "finaldata" , "gas_flare_locations_5km.Rds" ))
gs_10km_sf <- readRDS (file.path (gas_flare_dir, "finaldata" , "gas_flare_locations_10km.Rds" ))
if (nrow (gs_5km_sf) > 0 ){
ntl_gf_5km_r <- mask (ntl_r, gs_5km_sf)
ntl_nogf_5km_r <- mask (ntl_r, gs_5km_sf, inverse = T)
ntl_gf_10km_r <- mask (ntl_r, gs_10km_sf)
ntl_nogf_10km_r <- mask (ntl_r, gs_10km_sf, inverse = T)
}
#### Extract NTL
roi_sf$ ntl_sum <- exact_extract (ntl_r, roi_sf, "sum" )
roi_sf$ ntl_mean <- exact_extract (ntl_r, roi_sf, "mean" )
roi_sf$ ntl_sum_l1na <- exact_extract (ntl_r_1na, roi_sf, "sum" )
roi_sf$ ntl_sum_l0_5na <- exact_extract (ntl_r_0_5na, roi_sf, "sum" )
roi_sf$ ntl_sum_l0_1na <- exact_extract (ntl_r_0_1na, roi_sf, "sum" )
if (time_type == "monthly" ){
qual_r <- rast (file.path (ntl_bm_dir, "rasters" , "monthly_quality" ,
paste0 (bm_product,"_NearNadir_Composite_Snow_Free_Quality_qflag_t" ,time_raster_i,".tif" )))
tmp_r <- ntl_r
tmp_r[] <- 1
tmp_r <- mask (tmp_r, roi_sf)
roi_sf$ n_pixel <- exact_extract (tmp_r, roi_sf, "sum" )
roi_sf$ n_qual_0 <- exact_extract (qual_r == 0 , roi_sf, "sum" )
roi_sf$ n_qual_1 <- exact_extract (qual_r == 1 , roi_sf, "sum" )
roi_sf$ n_qual_2 <- exact_extract (qual_r == 2 , roi_sf, "sum" )
roi_sf <- roi_sf %>%
dplyr:: mutate (prop_qual_0 = n_qual_0 / n_pixel,
prop_qual_1 = n_qual_1 / n_pixel,
prop_qual_2 = n_qual_2 / n_pixel) %>%
dplyr:: select (- c (n_pixel, prop_qual_0, prop_qual_1, prop_qual_2))
}
if (nrow (gs_5km_sf) > 0 ){
roi_sf$ ntl_gf_5km_sum <- exact_extract (ntl_gf_5km_r, roi_sf, "sum" )
roi_sf$ ntl_nogf_5km_sum <- exact_extract (ntl_nogf_5km_r, roi_sf, "sum" )
roi_sf$ ntl_gf_10km_sum <- exact_extract (ntl_gf_10km_r, roi_sf, "sum" )
roi_sf$ ntl_nogf_10km_sum <- exact_extract (ntl_nogf_10km_r, roi_sf, "sum" )
roi_sf$ ntl_gf_5km_mean <- exact_extract (ntl_gf_5km_r, roi_sf, "mean" )
roi_sf$ ntl_nogf_5km_mean <- exact_extract (ntl_nogf_5km_r, roi_sf, "mean" )
roi_sf$ ntl_gf_10km_mean <- exact_extract (ntl_gf_10km_r, roi_sf, "mean" )
roi_sf$ ntl_nogf_10km_mean <- exact_extract (ntl_nogf_10km_r, roi_sf, "mean" )
}
#### NA Values
r_n_obs <- exact_extract (ntl_r, roi_sf, function (values, coverage_fraction)
sum (! is.na (values)),
progress = F)
r_n_obs_poss <- exact_extract (ntl_r, roi_sf, function (values, coverage_fraction)
length (values),
progress = F)
roi_sf$ n_pixels <- r_n_obs_poss
roi_sf$ n_non_na_pixels <- r_n_obs
roi_sf$ prop_non_na_pixels <- roi_sf$ n_non_na_pixels / roi_sf$ n_pixels
#### Cleanup
roi_sf$ date <- time_i
roi_df <- roi_sf %>%
st_drop_geometry ()
#### Export
saveRDS (roi_df, OUT_FILE)
}
}
}
}
#### Append Files
dir.create (file.path (ntl_bm_dir, "aggregated" ))
for (unit in c ("adm0" ,
"adm1" ,
"adm2" )){
for (time_type in c ("annual" , "monthly" )){
ntl_df <- file.path (ntl_bm_dir, "aggregated_individual_files" , unit, time_type) %>%
list.files (full.names = T,
pattern = ".Rds" ) %>%
map_df (readRDS)
saveRDS (ntl_df, file.path (ntl_bm_dir, "aggregated" , paste0 ("ntl_" , unit, "_" , time_type, ".Rds" )))
write_csv (ntl_df, file.path (ntl_bm_dir, "aggregated" , paste0 ("ntl_" , unit, "_" , time_type, ".csv" )))
write_dta (ntl_df, file.path (ntl_bm_dir, "aggregated" , paste0 ("ntl_" , unit, "_" , time_type, ".dta" )))
}
}