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

Get chirps monthly #1

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions data-raw/chirps_monthly_afg_adm1_historical.R
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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was having this issue authenticating, so used this workaround, just FYI

Copy link
Collaborator Author

@zackarno zackarno Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! yeah it seems the {rgee} authentication procedures are in some state of flux at the moment. I personally am not having issues with it, but seems alot are.

A couple questions

  1. Are you using Rstudio?
  2. Also have you followed the {rgee} install/authentication instructions? I think it sets up a conda env w/ miniconda (also downloaded/set up gcloud?). Unfortunately {rgee} has never been a simple install.packages("rgee") situation

It's probably an annoying default set up if you are using pyenv for everything else. But so far has been working for me at least


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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep getting Error in value[[3L]](cond) : The ee$FeatureCollection (y) must be not higher than 10 000., even though the feature collection should just be 34 long. Could it be related to your issue here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah seems related, but oddly this is not causing issues for me.
Probably it's calculating length(y) as 34 (provinces) *12 (months) * 43 (years).

Did you download the dev version of {tidyrgee} (devtools::install_github("r-tidy-remote-sensing/tidyrgee")). Maybe I implemented a work-around for the issue there, but I thought it would be on CRAN anyway

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