Skip to content

Commit

Permalink
refactor weather historical means for issue #64. still need to incorp…
Browse files Browse the repository at this point in the history
…orate into weather and forecast anomalies.
  • Loading branch information
emmamendelsohn committed Nov 1, 2023
1 parent 7711001 commit 580b816
Show file tree
Hide file tree
Showing 3 changed files with 394 additions and 377 deletions.
29 changes: 22 additions & 7 deletions R/calculate_weather_historical_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,30 @@
#' @return
#' @author Emma Mendelsohn
#' @export
calculate_weather_historical_means <- function(nasa_weather_transformed,
calculate_weather_historical_means <- function(nasa_weather_transformed, # enforce dependency
nasa_weather_transformed_directory,
weather_historical_means_directory,
days_of_year,
lag_intervals,
lead_intervals,
overwrite = FALSE) {

interval_length <- unique(c(diff(lag_intervals), diff(lead_intervals)))
assertthat::are_equal(length(interval_length), 1)

# Set filename
doy <- days_of_year
doy_frmt <- str_pad(doy,width = 3, side = "left", pad = "0")
save_filename <- glue::glue("historical_weather_mean_doy_{doy_frmt}.gz.parquet")
message(paste("calculating historical weather means and standard deviations for doy", doy_frmt))

# use dummy dates to keep date logic
doy_start <- days_of_year
dummy_date_start <- ymd("20200101") + doy_start - 1
dummy_date_end <- dummy_date_start + interval_length - 1
doy_end <- yday(dummy_date_end)

doy_start_frmt <- str_pad(doy_start, width = 3, side = "left", pad = "0")
doy_end_frmt <- str_pad(doy_end, width = 3, side = "left", pad = "0")

save_filename <- glue::glue("historical_weather_mean_doy_{doy_start_frmt}_to_{doy_end_frmt}.gz.parquet")
message(paste("calculating historical weather means and standard deviations for doy", doy_start_frmt, "to", doy_end_frmt))

# Check if file already exists
existing_files <- list.files(weather_historical_means_directory)
Expand All @@ -31,9 +44,11 @@ calculate_weather_historical_means <- function(nasa_weather_transformed,
weather_transformed_dataset <- open_dataset(nasa_weather_transformed_directory)

# Filter for day of year and calculate historical means and standard deviations
doy_select <- yday(seq(dummy_date_start, dummy_date_end, by = "day"))

historical_means <- weather_transformed_dataset |>
filter(day_of_year == doy) |>
group_by(x, y, day_of_year) |>
filter(day_of_year %in% doy_select) |>
group_by(x, y) |>
summarize(historical_relative_humidity_mean = mean(relative_humidity),
historical_temperature_mean = mean(temperature),
historical_precipitation_mean = mean(precipitation),
Expand Down
2 changes: 2 additions & 0 deletions _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ data_targets <- tar_plan(
nasa_weather_transformed_directory,
weather_historical_means_directory,
days_of_year,
lag_intervals,
lead_intervals,
overwrite = FALSE),
pattern = days_of_year,
format = "file",
Expand Down
Loading

0 comments on commit 580b816

Please sign in to comment.