From fc4a9d56edde5c8e9a293449cb1b827e9b14352e Mon Sep 17 00:00:00 2001 From: Emma Mendelsohn Date: Fri, 3 Nov 2023 21:49:50 -0400 Subject: [PATCH] handling 366 --- R/calculate_forecasts_anomalies.R | 13 ++++++++++--- R/calculate_weather_anomalies.R | 5 ++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/R/calculate_forecasts_anomalies.R b/R/calculate_forecasts_anomalies.R index aee0806..490716a 100644 --- a/R/calculate_forecasts_anomalies.R +++ b/R/calculate_forecasts_anomalies.R @@ -69,9 +69,16 @@ calculate_forecasts_anomalies <- function(ecmwf_forecasts_transformed, summarize(lead_mean = sum(mean * weight)/ sum(weight)) |> ungroup() - # get historical means for lead period - doy_start <- yday(lead_start_date) - doy_end <- yday(lead_end_date) + # get historical means for lead period, removing doy 366 + lead_dates <- seq(lead_start_date, lead_end_date, by = "day") + lead_doys <- yday(lead_dates) + if(366 %in% lead_doys) { + lead_doys <- lead_doys[lead_doys!=366] + lead_doys <- c(lead_doys, tail(lead_doys, 1) + 1) + } + + doy_start <- head(lead_doys, 1) + doy_end <- tail(lead_doys, 1) 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") doy_range <- glue::glue("{doy_start_frmt}_to_{doy_end_frmt}") diff --git a/R/calculate_weather_anomalies.R b/R/calculate_weather_anomalies.R index 79593a4..bc6e79e 100644 --- a/R/calculate_weather_anomalies.R +++ b/R/calculate_weather_anomalies.R @@ -40,7 +40,10 @@ calculate_weather_anomalies <- function(nasa_weather_transformed, # get lag dates, removing doy 366 lag_dates <- seq(date_selected - end, date_selected - start, by = "day") lag_doys <- yday(lag_dates) - if(366 %in% lag_doys) lag_doys <- c(head(lag_doys, 1) - 1, lag_doys[lag_doys!=366]) + if(366 %in% lag_doys){ + lag_doys <- lag_doys[lag_doys!=366] + lag_doys <- c(head(lag_doys, 1) - 1, lag_doys) + } # Get historical means for lag period doy_start <- head(lag_doys, 1)