From 82089ca83f7a9a3e015482f4a989fc75113cc55c Mon Sep 17 00:00:00 2001 From: Zack Date: Mon, 22 Apr 2024 13:10:36 -0400 Subject: [PATCH 1/2] gee code - chirps monthly at admin 1 --- data-raw/chirps_monthly_afg_adm1_historical.R | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 data-raw/chirps_monthly_afg_adm1_historical.R diff --git a/data-raw/chirps_monthly_afg_adm1_historical.R b/data-raw/chirps_monthly_afg_adm1_historical.R new file mode 100644 index 0000000..24c6733 --- /dev/null +++ b/data-raw/chirps_monthly_afg_adm1_historical.R @@ -0,0 +1,60 @@ +#' `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() + +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 +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" + ) + +write_csv(x = df_chirps_monthly, + file = df_csv_outpath) From 28a324f5528d80f8023cde6331a1ac29104aac9e Mon Sep 17 00:00:00 2001 From: Zack Date: Mon, 22 Apr 2024 13:13:21 -0400 Subject: [PATCH 2/2] add overwrite_csv conditional parameter --- data-raw/chirps_monthly_afg_adm1_historical.R | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/data-raw/chirps_monthly_afg_adm1_historical.R b/data-raw/chirps_monthly_afg_adm1_historical.R index 24c6733..0d3f0aa 100644 --- a/data-raw/chirps_monthly_afg_adm1_historical.R +++ b/data-raw/chirps_monthly_afg_adm1_historical.R @@ -17,6 +17,8 @@ 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) @@ -25,8 +27,8 @@ 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 -Map$addLayer(adm1_afghanistan, list(color = "red"), "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) @@ -56,5 +58,8 @@ df_csv_outpath <-file.path( "chirps_monthly_afg_adm1_historical.csv" ) -write_csv(x = df_chirps_monthly, - file = df_csv_outpath) +if(overwrite_csv){ + write_csv(x = df_chirps_monthly, + file = df_csv_outpath) +} +