## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE,
  fig.width = 7,
  fig.height = 4.5,
  fig.alt = "ggplot2 chart showing daily weather aggregation."
)

## ----packages, message=FALSE, warning=FALSE-----------------------------------
library(windcut)
library(dplyr)
library(ggplot2)
library(tidyr)
library(cowplot)

## -----------------------------------------------------------------------------
data(window_pane_demo_data)

hourly_weather <- window_pane_demo_data$weather_hourly

hourly_weather %>%
  filter(site_id == site_id[1]) %>%
  slice_head(n = 8) %>%
  knitr::kable()

## -----------------------------------------------------------------------------
daily_weather <- aggregate_weather_daily(
  weather = hourly_weather,
  id_col = "site_id",
  weather_cols = c("temp", "rh", "rain", "leaf_wetness"),
  statistics = list(
    temp = "mean",
    rh = "mean",
    rain = "sum",
    leaf_wetness = "sum"
  )
)

daily_weather %>%
  filter(site_id == site_id[1]) %>%
  slice_head(n = 8) %>%
  knitr::kable()

## -----------------------------------------------------------------------------
daily_weather %>%
  count(site_id, name = "n_days") %>%
  knitr::kable()

## -----------------------------------------------------------------------------
timestamp_weather <- hourly_weather %>%
  rename(timestamp = time)

daily_from_timestamp <- aggregate_weather_daily(
  weather = timestamp_weather,
  id_col = "site_id",
  time_col = "timestamp",
  date_col = "weather_date",
  weather_cols = c("temp", "rh", "rain", "leaf_wetness"),
  statistics = list(
    temp = "mean",
    rh = "mean",
    rain = "sum",
    leaf_wetness = "sum"
  )
)

daily_from_timestamp %>%
  filter(site_id == site_id[1]) %>%
  select(site_id, weather_date, timestamp, daily_mean_temp, daily_mean_rh, daily_sum_rain) %>%
  slice_head(n = 6) %>%
  knitr::kable()

## -----------------------------------------------------------------------------
weather_with_day <- hourly_weather %>%
  mutate(weather_day = as.Date(time))

daily_from_existing_day <- aggregate_weather_daily(
  weather = weather_with_day,
  id_col = "site_id",
  time_col = "time",
  date_col = "weather_day",
  weather_cols = c("temp", "rh", "rain", "leaf_wetness"),
  statistics = list(
    temp = "mean",
    rh = "mean",
    rain = "sum",
    leaf_wetness = "sum"
  )
)

daily_from_existing_day %>%
  filter(site_id == site_id[1]) %>%
  select(site_id, weather_day, time, daily_mean_temp, daily_mean_rh, daily_sum_rain) %>%
  slice_head(n = 6) %>%
  knitr::kable()

## -----------------------------------------------------------------------------
already_dated <- hourly_weather %>%
  mutate(weather_day = as.Date(time)) %>%
  select(site_id, weather_day, temp, rain)

daily_from_date_only <- aggregate_weather_daily(
  weather = already_dated,
  id_col = "site_id",
  time_col = NULL,
  date_col = "weather_day",
  weather_cols = c("temp", "rain"),
  statistics = list(temp = "mean", rain = "sum"),
  keep_time = FALSE
)

daily_from_date_only %>%
  filter(site_id == site_id[1]) %>%
  slice_head(n = 6) %>%
  knitr::kable()

## -----------------------------------------------------------------------------
daily_many_stats <- aggregate_weather_daily(
  weather = hourly_weather,
  id_col = "site_id",
  weather_cols = c("temp", "rh", "rain", "leaf_wetness"),
  statistics = c("mean", "min", "max")
)

daily_many_stats %>%
  filter(site_id == site_id[1]) %>%
  select(site_id, date, time, daily_mean_temp, daily_min_temp, daily_max_temp, daily_mean_rh, daily_min_rh, daily_max_rh) %>%
  slice_head(n = 6) %>%
  knitr::kable()

## -----------------------------------------------------------------------------
daily_selected_stats <- aggregate_weather_daily(
  weather = hourly_weather,
  id_col = "site_id",
  weather_cols = c("temp", "rh", "rain", "leaf_wetness"),
  statistics = list(
    temp = c("mean", "min", "max"),
    rh = c("mean", "median"),
    rain = c("sum", "max"),
    leaf_wetness = list(wet_hours = "sum")
  )
)

daily_selected_stats %>%
  filter(site_id == site_id[1]) %>%
  select(site_id, date, starts_with("daily_mean_temp"), starts_with("daily_sum_rain"), daily_wet_hours_leaf_wetness) %>%
  slice_head(n = 6) %>%
  knitr::kable()

## -----------------------------------------------------------------------------
daily_condition_stats <- aggregate_weather_daily(
  weather = hourly_weather,
  id_col = "site_id",
  weather_cols = c("temp", "rh"),
  statistics = list(
    temp = "mean",
    rh = "mean",
    .conditions = list(
      favorable_hours = count_when(temp >= 18 & temp <= 26 & rh >= 90)
    )
  )
)

daily_condition_stats %>%
  filter(site_id == site_id[1]) %>%
  select(site_id, date, daily_mean_temp, daily_mean_rh, daily_favorable_hours) %>%
  slice_head(n = 8) %>%
  knitr::kable()

## -----------------------------------------------------------------------------
daily_conditions_only <- aggregate_weather_daily(
  weather = hourly_weather,
  id_col = "site_id",
  weather_cols = character(),
  statistics = list(
    .conditions = list(
      favorable_hours = count_when(temp >= 18 & temp <= 26 & rh >= 90)
    )
  )
)

stopifnot(identical(
  daily_conditions_only$daily_favorable_hours,
  daily_condition_stats$daily_favorable_hours
))

## -----------------------------------------------------------------------------
daily_condition_stats %>%
  filter(site_id == site_id[1]) %>%
  ggplot(aes(date, daily_favorable_hours)) +
  geom_col(fill = "#3f7d58", width = 0.75) +
  labs(
    title = "Daily favorable-hour counts from hourly records",
    x = NULL,
    y = "Favorable hours per day"
  ) +
  cowplot::theme_half_open()

## -----------------------------------------------------------------------------
custom_hourly <- hourly_weather %>%
  rename(
    temp2m = temp,
    relhum = rh,
    prectot = rain
  ) %>%
  mutate(sradiation = pmax(0, 600 * sin(as.numeric(format(time, "%H")) / 24 * pi)))

custom_daily <- aggregate_weather_daily(
  weather = custom_hourly,
  id_col = "site_id",
  weather_cols = c("temp2m", "relhum", "prectot", "sradiation"),
  statistics = list(
    temp2m = c("mean", "max"),
    relhum = "mean",
    prectot = "sum",
    sradiation = "sum"
  )
)

custom_daily %>%
  filter(site_id == site_id[1]) %>%
  slice_head(n = 6) %>%
  knitr::kable()

## -----------------------------------------------------------------------------
custom_daily_short_names <- aggregate_weather_daily(
  weather = custom_hourly,
  id_col = "site_id",
  weather_cols = c(
    air_temp = "temp2m",
    humidity = "relhum",
    rain = "prectot",
    solar = "sradiation"
  ),
  statistics = list(
    air_temp = c("mean", "max"),
    humidity = "mean",
    rain = "sum",
    solar = "sum"
  )
)

custom_daily_short_names %>%
  filter(site_id == site_id[1]) %>%
  slice_head(n = 6) %>%
  knitr::kable()

## -----------------------------------------------------------------------------
plot_daily <- daily_weather %>%
  filter(site_id == site_id[1]) %>%
  select(site_id, date, daily_mean_temp, daily_mean_rh, daily_sum_rain, daily_sum_leaf_wetness) %>%
  pivot_longer(
    cols = c(daily_mean_temp, daily_mean_rh, daily_sum_rain, daily_sum_leaf_wetness),
    names_to = "variable",
    values_to = "value"
  )

ggplot(plot_daily, aes(date, value)) +
  geom_line(color = "#3f7d58", linewidth = 0.7) +
  facet_wrap(~ variable, scales = "free_y", ncol = 1) +
  labs(
    title = "Daily weather after aggregation",
    x = NULL,
    y = NULL
  ) +
  cowplot::theme_half_open()

## -----------------------------------------------------------------------------
windows <- make_windows(
  min_offset = -21,
  max_offset = -1,
  width = 5,
  reference_col = "assessment_time"
)

features <- window_pane(
  weather = daily_weather,
  assessments = window_pane_demo_data$assessments,
  windows = windows,
  id_col = "site_id",
  response_col = "disease_intensity",
  weather_cols = c(
    temp = "daily_mean_temp",
    rh = "daily_mean_rh",
    rain = "daily_sum_rain",
    leaf_wetness = "daily_sum_leaf_wetness"
  ),
  statistics = list(
    temp = c("mean", "min", "max"),
    rh = "mean",
    rain = "sum",
    leaf_wetness = "sum"
  ),
  unit = "days"
)

features %>%
  select(1:10) %>%
  slice_head(n = 6) %>%
  knitr::kable()

