-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from OCHA-DAP/get-chirps-monthly
Get chirps monthly
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#' `chirps_monthly_afg_adm1_historical.csv` | ||
#' `Date:` 2024-04-22 | ||
#' This script generates the chirps_monthly_afg_adm1_historical.csv | ||
#' | ||
#' **Description:** | ||
#' Using GEE temporally aggregate CHIRPS daily data to monthly. | ||
#' We then run zonal means for each yr_mo combination at the admin-1 level | ||
#' For all of Afghanistan. | ||
#' | ||
#' **Tip:* | ||
#' Script can be run in the background (on Mac with terminal call) | ||
#' `caffeinate -i -s Rscript data-raw/chirps_monthly_afg_adm1.R` | ||
|
||
library(rgee) | ||
library(tidyrgee) | ||
library(rhdx) | ||
library(tidyverse) | ||
ee_Initialize() | ||
|
||
overwrite_csv <- c(T,F)[2] | ||
|
||
chirps_daily_url <- "UCSB-CHG/CHIRPS/DAILY" | ||
chirps_ic <- ee$ImageCollection(chirps_daily_url) | ||
|
||
adm1_fc <- ee$FeatureCollection("FAO/GAUL_SIMPLIFIED_500m/2015/level1") | ||
|
||
# filter adm1 to get only those in Afghanistan | ||
adm1_afghanistan <- adm1_fc$filter(ee$Filter$eq("ADM0_NAME", "Afghanistan")) | ||
|
||
# Add to map to quickly check - only for interactive viewing (not backaground job) | ||
# Map$addLayer(adm1_afghanistan, list(color = "red"), "Afghanistan") | ||
|
||
# convert to tidyee IC for easy temporal aggregation | ||
chirps_tic <- as_tidyee(chirps_ic) | ||
|
||
chirps_monthly_tic <- chirps_tic |> | ||
group_by(year, month) |> | ||
summarise( | ||
stat= "sum" | ||
) | ||
|
||
|
||
# zonal means for each year-month at admin 1 | ||
df_chirps_monthly <- ee_extract_tidy( | ||
x=chirps_monthly_tic, | ||
y= adm1_afghanistan, | ||
scale = 5566, | ||
stat = "mean", | ||
via = "drive" | ||
) | ||
|
||
# write as csv | ||
df_csv_outpath <-file.path( | ||
Sys.getenv("AA_DATA_DIR_NEW"), | ||
"public", | ||
"processed", | ||
"afg", | ||
"chirps_monthly_afg_adm1_historical.csv" | ||
) | ||
|
||
if(overwrite_csv){ | ||
write_csv(x = df_chirps_monthly, | ||
file = df_csv_outpath) | ||
} | ||
|