Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ntl v2 #83

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
24 changes: 18 additions & 6 deletions notebooks/ntl-analysis/01_clean_data/02_download_black_marble.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,33 @@ bearer <- read_csv("~/Desktop/bearer_bm.csv") %>% pull(token)
##### Region of Interest

## Polygon around Syria and Turkey
syr_sp <- getData('GADM', country='SYR', level=0)
tur_sp <- getData('GADM', country='TUR', level=0)
syr_sp <- gadm(country = "SYR", level=1, path = tempdir()) |> st_as_sf()
tur_sp <- gadm(country = "TUR", level=1, path = tempdir()) |> st_as_sf()

# Combine Syria and Turkey into one polygon
roi_sp <- rbind(syr_sp, tur_sp)
roi_sp$id <- 1
roi_sp <- raster::aggregate(roi_sp, by = "id")
roi_sf <- roi_sp %>% st_as_sf()
roi_sf <- roi_sp %>% st_as_sf() #%>% st_combine()

# Download annual data --------------------------------------------------------
dir.create(file.path(data_dir,
"NTL BlackMarble",
"FinalData",
"annual_rasters"))
bm_raster(roi_sf = roi_sf,
product_id = "VNP46A4",
date = 2012:2022,
date = 2012:2023,
bearer = bearer,
output_location_type = "file",
file_dir = file.path(data_dir,
"NTL BlackMarble",
"FinalData",
"annual_rasters"))

dir.create(file.path(data_dir,
"NTL BlackMarble",
"FinalData",
"monthly_rasters"))
bm_raster(roi_sf = roi_sf,
product_id = "VNP46A3",
date = seq.Date(from = ymd("2012-01-01"), to = Sys.Date(), by = "month") %>% rev(),
Expand All @@ -39,15 +46,20 @@ bm_raster(roi_sf = roi_sf,
"FinalData",
"monthly_rasters"))

dir.create(file.path(data_dir,
"NTL BlackMarble",
"FinalData",
"daily_rasters"))
bm_raster(roi_sf = roi_sf,
product_id = "VNP46A2",
date = seq.Date(from = ymd("2022-01-01"), to = Sys.Date(), by = "day") %>% rev(),
bearer = bearer,
variable = "Gap_Filled_DNB_BRDF-Corrected_NTL",
quality = NULL,
output_location_type = "file",
file_dir = file.path(data_dir,
"NTL BlackMarble",
"FinalData",
"daily_rasters"))



79 changes: 5 additions & 74 deletions notebooks/ntl-analysis/01_clean_data/03_border_xing_aggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ ntl_monthly_df <- map_df(monthly_files, function(file_i){

# Add date info
bc_df$date <- file_i %>%
str_replace_all("VNP46A3_t", "") %>%
str_replace_all(".*qflag_t", "") %>%
str_replace_all(".tif", "") %>%
str_replace_all("_", "-") %>%
paste0("-01") %>%
paste0("-01") %>%
ymd()

#### Merge NTL with GADM data
Expand All @@ -47,41 +46,6 @@ ntl_monthly_df <- map_df(monthly_files, function(file_i){
write_csv(ntl_monthly_df, file.path(ntl_bm_dir, "FinalData", "aggregated", "border_xing_monthly.csv"))
saveRDS(ntl_monthly_df, file.path(ntl_bm_dir, "FinalData", "aggregated", "border_xing_monthly.Rds"))

# for(file_i in rev(monthly_files)){
#
# OUT_FILE <- file.path(ntl_bm_dir, "FinalData", "aggregated", "border_xing_temp",
# "monthly_files",
# paste0("syr_border_xing_",
# file_i %>% str_replace_all(".tif", ".Rds")))
#
# if(!file.exists(OUT_FILE)){
#
# print(file_i)
#
# r <- rast(file.path(ntl_bm_dir, "FinalData", paste0("monthly", "_rasters"), file_i))
#
# bc_df <- bc_sf
# bc_df$geometry <- NULL
# bc_df$viirs_bm_mean <- exact_extract(r, bc_sf, 'mean')
# bc_df$viirs_bm_sum <- exact_extract(r, bc_sf, 'sum')
# bc_df$viirs_bm_median <- exact_extract(r, bc_sf, 'median')
#
# # Add date info
# bc_df$date <- file_i %>%
# str_replace_all("VNP46A3_t", "") %>%
# str_replace_all(".tif", "") %>%
# str_replace_all("_", "-") %>%
# paste0("-01") %>%
# ymd()
#
# #### Merge NTL with GADM data
# bc_df$viirs_bm_mean[bc_df$viirs_bm_sum == 0] <- 0
#
# # Export -----------------------------------------------------------------------
# saveRDS(bc_df, OUT_FILE)
# }
# }

# Daily ------------------------------------------------------------------------
daily_files <- list.files(file.path(ntl_bm_dir, "FinalData", paste0("daily", "_rasters")))

Expand All @@ -97,50 +61,17 @@ ntl_daily_df <- map_df(daily_files, function(file_i){

# Add date info
bc_df$date <- file_i %>%
str_replace_all("VNP46A2_t", "") %>%
str_replace_all(".*qflag_t", "") %>%
str_replace_all(".tif", "") %>%
str_replace_all("_", "-") %>%
ymd()

#### Merge NTL with GADM data
bc_df$viirs_bm_mean[bc_df$viirs_bm_sum == 0] <- 0
bc_df$viirs_bm_mean[bc_df$viirs_bm_sum == 0] <- 0

return(bc_df)
})

write_csv(ntl_daily_df, file.path(ntl_bm_dir, "FinalData", "aggregated", "border_xing_daily.csv"))
saveRDS(ntl_daily_df, file.path(ntl_bm_dir, "FinalData", "aggregated", "border_xing_daily.Rds"))
write_dta(ntl_daily_df, file.path(ntl_bm_dir, "FinalData", "aggregated", "border_xing_daily.dta"))

# for(file_i in rev(daily_files)){
#
# OUT_FILE <- file.path(ntl_bm_dir, "FinalData", "aggregated", "border_xing_temp",
# "daily_files",
# paste0("syr_border_xing_",
# file_i %>% str_replace_all(".tif", ".Rds")))
#
# if(!file.exists(OUT_FILE)){
#
# print(file_i)
#
# r <- rast(file.path(ntl_bm_dir, "FinalData", paste0("daily", "_rasters"), file_i))
#
# bc_df <- bc_sf
# bc_df$geometry <- NULL
# bc_df$viirs_bm_mean <- exact_extract(r, bc_sf, 'mean')
# bc_df$viirs_bm_sum <- exact_extract(r, bc_sf, 'sum')
#
# # Add date info
# bc_df$date <- file_i %>%
# str_replace_all("VNP46A2_t", "") %>%
# str_replace_all(".tif", "") %>%
# str_replace_all("_", "-") %>%
# ymd()
#
# #### Merge NTL with GADM data
# bc_df$viirs_bm_mean[bc_df$viirs_bm_sum == 0] <- 0
#
# # Export -----------------------------------------------------------------------
# saveRDS(bc_df, OUT_FILE)
# }
# }
#
Loading