---
title: "Internal radiation and ET₀ estimators"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Internal radiation and ET₀ estimators}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse  = TRUE,
  comment   = "#>",
  fig.width = 8, fig.height = 4,
  message   = FALSE, warning = FALSE
)
```

# Overview

`cumba_scenario()` and `cumba_experiment()` can internally estimate two
key driving variables from basic weather inputs:

- **Solar radiation** — Hargreaves–Samani formula, requiring only daily
  Tmax, Tmin, and site latitude (`estimateRad = TRUE`).
- **Reference evapotranspiration ET₀** — a temperature-based
  approximation of the FAO-56 Penman–Monteith equation, requiring Tmax,
  Tmin, and latitude (`estimateET0 = TRUE`).

This vignette runs the model in scenario mode over all years in
`tomatoFoggia` and inspects the seasonal behaviour of both estimators.

# Load data and run the model

```{r run}
library(cumba)
library(dplyr)
library(ggplot2)
library(lubridate)

data(tomatoFoggia)
data(cumbaParameters)

weather_obs <- tomatoFoggia$weather

# Run over all site-years; fullOut = TRUE exposes radiation and et0 columns
out <- cumba_scenario(
  weather          = weather_obs,
  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 = TRUE
)

sim <- out |> select(year, doy, radiation, et0)
glimpse(sim)
```

# Radiation estimates — seasonal dynamics

The Hargreaves–Samani method approximates solar radiation from the
daily temperature range, which correlates with cloud cover. The
estimates track the expected bell-shaped seasonal pattern for a
Mediterranean site at latitude ~41°N.

```{r rad_season, fig.height = 5}
ggplot(sim, aes(doy, radiation)) +
  geom_line(colour = "#d6604d", linewidth = 0.8) +
  facet_wrap(~year, ncol = 3) +
  labs(
    x     = "Day of year",
    y     = "Radiation (MJ m⁻² d⁻¹)",
    title = "Hargreaves–Samani radiation estimates"
  ) +
  theme_bw(base_size = 11) +
  theme(
    strip.background = element_rect(fill = "#f0f0f0"),
    panel.grid.minor = element_blank()
  )
```

# ET₀ estimates — seasonal dynamics

The temperature-based ET₀ follows the same seasonal trajectory as
radiation, rising through summer and falling toward harvest. Values in
the 4–9 mm d⁻¹ range are typical for Foggia in June–August.

```{r et0_season, fig.height = 5}
ggplot(sim, aes(doy, et0)) +
  geom_area(fill = "#1b7837", alpha = 0.15) +
  geom_line(colour = "#1b7837", linewidth = 0.8) +
  facet_wrap(~year, ncol = 3) +
  labs(
    x     = "Day of year",
    y     = expression(ET[0]~~(mm~d^{-1})),
    title = expression("FAO-56 Penman–Monteith"~~ET[0]~~"estimates")
  ) +
  theme_bw(base_size = 11) +
  theme(
    strip.background = element_rect(fill = "#f0f0f0"),
    panel.grid.minor = element_blank()
  )
```

# Summary statistics by year

```{r metrics}
sim |>
  group_by(year) |>
  summarise(
    n_days           = n(),
    mean_rad         = round(mean(radiation, na.rm = TRUE), 1),
    max_rad          = round(max(radiation,  na.rm = TRUE), 1),
    mean_et0         = round(mean(et0,       na.rm = TRUE), 1),
    season_total_et0 = round(sum(et0,        na.rm = TRUE), 0)
  ) |>
  rename(
    Year                        = year,
    `Days simulated`            = n_days,
    `Mean radiation (MJ m⁻² d⁻¹)` = mean_rad,
    `Max radiation (MJ m⁻² d⁻¹)`  = max_rad,
    `Mean ET₀ (mm d⁻¹)`                = mean_et0,
    `Season total ET₀ (mm)`                      = season_total_et0
  )
```
