Skip to content

Commit

Permalink
better handling of day of year 366
Browse files Browse the repository at this point in the history
  • Loading branch information
emmamendelsohn committed Nov 3, 2023
1 parent f0a8804 commit debc55d
Show file tree
Hide file tree
Showing 3 changed files with 790 additions and 423 deletions.
8 changes: 5 additions & 3 deletions R/calculate_weather_anomalies.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ calculate_weather_anomalies <- function(nasa_weather_transformed,

anomalies <- map2(lag_intervals_start, lag_intervals_end, function(start, end){

# get lag dates
# 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])

# Get historical means for lag period
doy_start <- yday(lag_dates[1])
doy_end <- yday(lag_dates[length(lag_dates)])
doy_start <- head(lag_doys, 1)
doy_end <- tail(lag_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}")
Expand Down
22 changes: 21 additions & 1 deletion _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ data_targets <- tar_plan(

tar_target(lag_intervals, c(30, 60, 90)),
tar_target(lead_intervals, c(30, 60, 90, 120, 150)),
tar_target(days_of_year, 1:366),
tar_target(days_of_year, 1:365),
tar_target(model_dates_selected, set_model_dates(start_year = 2005,
end_year = 2022,
n_per_month = 2,
Expand All @@ -318,6 +318,16 @@ data_targets <- tar_plan(
format = "file",
repository = "local"),

# save historical means to AWS bucket
tar_target(weather_historical_means_upload_aws_s3,
aws_s3_upload(path = weather_historical_means,
bucket = aws_bucket,
key = weather_historical_means,
check = TRUE),
pattern = weather_historical_means,
cue = tar_cue("thorough")), # only run this if you need to upload new data


tar_target(weather_anomalies_directory,
create_data_directory(directory_path = "data/weather_anomalies")),

Expand Down Expand Up @@ -377,6 +387,16 @@ data_targets <- tar_plan(
format = "file",
repository = "local"),

# save historical means to AWS bucket
tar_target(ndvi_historical_means_upload_aws_s3,
aws_s3_upload(path = ndvi_historical_means,
bucket = aws_bucket,
key = ndvi_historical_means,
check = TRUE),
pattern = ndvi_historical_means,
cue = tar_cue("thorough")), # only run this if you need to upload new data


tar_target(ndvi_anomalies_directory,
create_data_directory(directory_path = "data/ndvi_anomalies")),

Expand Down
Loading

0 comments on commit debc55d

Please sign in to comment.