## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  fig.width = 7, fig.height = 5,
  message = FALSE, warning = FALSE
)

## ----run----------------------------------------------------------------------
library(cumba)
library(dplyr)
library(ggplot2)

data(tomatoFoggia)
data(cumbaParameters)

weather <- tomatoFoggia$weather

out <- cumba_scenario(
  weather          = weather,
  param            = cumbaParameters,
  estimateRad      = TRUE,
  estimateET0      = TRUE,
  transplantingDOY = 120,
  irrigationStrategy = list(
    vegetative   = list(wsLevel = 0.5, turnMin = 4),
    reproductive = list(wsLevel = 0.5, turnMin = 4),
    ripening     = list(wsLevel = 0.5, turnMin = 4)
  ),
  fullOut = FALSE
)

head(out)

## ----compare------------------------------------------------------------------
full_irr <- list(
  vegetative   = list(wsLevel = 1.0, turnMin = 4),
  reproductive = list(wsLevel = 1.0, turnMin = 4),
  ripening     = list(wsLevel = 1.0, turnMin = 4)
)

deficit <- list(
  vegetative   = list(wsLevel = 0.4, turnMin = 4),
  reproductive = list(wsLevel = 0.4, turnMin = 4),
  ripening     = list(wsLevel = 0.3, turnMin = 4)
)

run_strategy <- function(strategy, label) {
  cumba_scenario(
    weather            = weather,
    param              = cumbaParameters,
    irrigationStrategy = strategy,
    transplantingDOY   = 120,
    estimateRad        = TRUE,
    estimateET0        = TRUE,
    fullOut            = FALSE
  ) |>
    mutate(strategy_label = label)
}

scen <- bind_rows(
  run_strategy(full_irr, "Full irrigation"),
  run_strategy(deficit,  "Deficit irrigation")
)

# Last row per (strategy × site × year) = harvest day
harvest <- scen |>
  group_by(strategy_label, site, year) |>
  slice_tail(n = 1) |>
  ungroup()

pal <- c("Full irrigation" = "#2166ac", "Deficit irrigation" = "#d6604d")

ggplot(harvest, aes(strategy_label, yield, fill = strategy_label)) +
  geom_boxplot(width = 0.45, outlier.shape = 21, outlier.size = 2,
               colour = "grey30", linewidth = 0.6) +
  scale_fill_manual(values = pal) +
  labs(y = "Yield (t ha⁻¹)", x = NULL,
       title = "Yield response to irrigation strategy") +
  theme_bw(base_size = 12) +
  theme(legend.position = "none")

ggplot(harvest, aes(strategy_label, brix, fill = strategy_label)) +
  geom_boxplot(width = 0.45, outlier.shape = 21, outlier.size = 2,
               colour = "grey30", linewidth = 0.6) +
  scale_fill_manual(values = pal) +
  labs(y = "Brix (°Brix)", x = NULL,
       title = "Brix response to irrigation strategy") +
  theme_bw(base_size = 12) +
  theme(legend.position = "none")

## ----daily, fig.height = 4----------------------------------------------------
scen_yr1 <- scen |> filter(year == min(year))

ggplot(scen_yr1, aes(doy)) +
  geom_col(aes(y = irrigation, fill = strategy_label),
           position = "dodge", width = 0.9, alpha = 0.5) +
  geom_line(aes(y = swc, colour = strategy_label), linewidth = 0.9) +
  scale_colour_manual(values = pal, name = NULL) +
  scale_fill_manual(values = pal, name = NULL) +
  labs(x = "Day of year",
       y = "Soil water content (%) / Irrigation (mm)",
       title = "Soil water and irrigation events — first year") +
  theme_bw(base_size = 12) +
  theme(legend.position = "bottom")

