# Partitioning net flux into GPP and respiration
# https://tidyecology.com/posts/partitioning-net-flux-into-gpp-and-respiration/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-11-04

## ---- setup ----
library(ggplot2)

te_pal <- list(forest = "#275139", green = "#2f8f63", sage = "#93a87f",
               clay = "#b5534e", gold = "#cda23f", line = "#dad9ca",
               ink = "#16241d", paper = "#f5f4ee")

theme_te <- function() {
  theme_minimal(base_size = 12) +
    theme(panel.grid.minor = element_blank(),
          panel.grid.major = element_line(colour = "#e7e6dc"),
          plot.background = element_rect(fill = "#f5f4ee", colour = NA),
          panel.background = element_rect(fill = "#f5f4ee", colour = NA),
          plot.title = element_text(face = "bold", colour = te_pal$ink),
          axis.title = element_text(colour = "#2c3a31"),
          axis.text = element_text(colour = "#2c3a31"))
}

## ---- drivers ----
lat_deg <- 47
n_day <- 365
doy <- rep(seq_len(n_day), each = 48)
hh <- rep(seq(0, 23.5, by = 0.5), times = n_day)
n_hh <- length(doy)

decl <- 23.45 * sin(2 * pi * (284 + doy) / 365) * pi / 180
lat_r <- lat_deg * pi / 180
hang <- 15 * (hh - 12) * pi / 180
sin_elev <- sin(lat_r) * sin(decl) + cos(lat_r) * cos(decl) * cos(hang)

set.seed(20260802)
cloud_day <- pmin(1, pmax(0.22, rbeta(n_day, 5, 1.8)))
cloud <- rep(cloud_day, each = 48) * runif(n_hh, 0.85, 1.05)
ppfd <- pmax(0, 2050 * pmax(0, sin_elev) * pmin(1, cloud))

day_anom <- rep(rnorm(n_day, 0, 2.6), each = 48)
tair <- 9.2 + 10.4 * sin(2 * pi * (doy - 112) / 365) + day_anom +
  (4.2 + 1.8 * sin(2 * pi * (doy - 112) / 365)) *
    sin(2 * pi * (hh - 9.5) / 24) + rnorm(n_hh, 0, 0.7)
tk <- tair + 273.15
lai <- 0.06 + 0.94 * exp(-((doy - 190) / 66)^2)

print(c(half_hours = n_hh, days = n_day, latitude = lat_deg))
print(round(c(min_temp = min(tair), mean_temp = mean(tair),
              max_temp = max(tair), max_ppfd = max(ppfd),
              peak_leaf_area = max(lai)), 2))

## ---- truth ----
tref_k <- 283.15
t0_k <- 227.13
lt_shape <- function(tkv, e0) {
  exp(e0 * (1 / (tref_k - t0_k) - 1 / (tkv - t0_k)))
}

rref_true <- 2.80
e0_true <- 208
seas_r <- 0.72 + 0.58 * lai
reco_pot <- rref_true * seas_r * lt_shape(tk, e0_true)

f_leaf <- 0.50 * lai
kok <- 0.60
light_sw <- ppfd / (ppfd + 25)
inhib <- kok * f_leaf * light_sw
reco_true <- reco_pot * (1 - inhib)

alpha_true <- 0.048
amax_true <- 26
gpp_true <- alpha_true * ppfd * (amax_true * lai) /
  (alpha_true * ppfd + amax_true * lai + 1e-9)
gpp_true[ppfd <= 0] <- 0

nee_true <- reco_true - gpp_true

set.seed(20260803)
sd_hh <- 0.55 + 0.115 * abs(nee_true)
nee_obs <- nee_true + rnorm(n_hh, 0, sd_hh)

gc_hh <- 12.011 * 1e-6 * 1800
ann <- function(x) sum(x) * gc_hh
night <- ppfd < 5
day_i <- which(!night)

gpp_ann <- ann(gpp_true)
reco_ann <- ann(reco_true)
nee_ann <- ann(nee_true)
kok_ann <- ann(reco_pot - reco_true)
inhib_pct <- 100 * sum(inhib[!night] * reco_pot[!night]) /
  sum(reco_pot[!night])

print(round(c(rref = rref_true, e0 = e0_true, alpha = alpha_true,
              amax = amax_true, kok_fraction = kok), 3))
print(round(c(gpp_gc = gpp_ann, reco_gc = reco_ann, nee_gc = nee_ann,
              kok_cost_gc = kok_ann, daytime_inhibition_pct = inhib_pct,
              night_half_hours = sum(night), peak_reco = max(reco_true)), 2))

## ---- nightfit ----
dat_night <- data.frame(nee = nee_obs[night], tk = tk[night])
fit_nt <- nls(nee ~ rref * exp(e0 * (1 / (tref_k - t0_k) -
                                       1 / (tk - t0_k))),
              data = dat_night,
              start = list(rref = 2, e0 = 200))

co_nt <- summary(fit_nt)$coefficients
rref_hat <- unname(co_nt["rref", 1])
e0_hat <- unname(co_nt["e0", 1])
rref_se <- unname(co_nt["rref", 2])
e0_se <- unname(co_nt["e0", 2])

print(round(co_nt, 4))
print(round(c(rref_fitted = rref_hat, rref_true = rref_true,
              rref_error_pct = 100 * (rref_hat / rref_true - 1),
              rref_error_in_se = (rref_hat - rref_true) / rref_se), 3))
print(round(c(e0_fitted = e0_hat, e0_true = e0_true,
              e0_error_pct = 100 * (e0_hat / e0_true - 1),
              e0_error_in_se = (e0_hat - e0_true) / e0_se), 3))

## ---- fig-nightfit ----
tbin <- round(tair[night])
bin_dat <- data.frame(
  tmid = as.numeric(names(tapply(nee_obs[night], tbin, mean))),
  obs = as.vector(tapply(nee_obs[night], tbin, mean)),
  tru = as.vector(tapply(reco_true[night], tbin, mean)),
  nn = as.vector(tapply(nee_obs[night], tbin, length)))
bin_dat <- bin_dat[bin_dat$nn >= 30, ]
curve_dat <- data.frame(tmid = seq(min(bin_dat$tmid), max(bin_dat$tmid),
                                   length.out = 200))
curve_dat$fitted <- rref_hat * lt_shape(curve_dat$tmid + 273.15, e0_hat)
set.seed(20260804)
show_i <- sample(which(night), 2200)

ggplot(bin_dat, aes(tmid, obs)) +
  geom_point(data = data.frame(tmid = tair[show_i], obs = nee_obs[show_i]),
             colour = "#c9c8bb", size = 0.6, alpha = 0.6) +
  geom_line(data = curve_dat, aes(tmid, fitted,
                                  colour = "fitted Lloyd-Taylor curve"),
            linewidth = 1) +
  geom_line(aes(y = tru, colour = "true respiration, bin mean"),
            linetype = "22", linewidth = 1) +
  geom_point(size = 2.2, colour = te_pal$ink) +
  scale_colour_manual(values = c("fitted Lloyd-Taylor curve" = te_pal$clay,
                                 "true respiration, bin mean" = te_pal$forest),
                      name = NULL) +
  coord_cartesian(ylim = c(-1, 9)) +
  labs(x = "air temperature (degrees C)",
       y = "night-time net flux (umol/m2/s)",
       title = "A fit that looks right on the data it was given") +
  theme_te() +
  theme(legend.position = "bottom", plot.margin = margin(8, 14, 4, 8))

## ---- extrapolate ----
reco_nt <- rref_hat * lt_shape(tk, e0_hat)
gpp_nt <- reco_nt - nee_obs

reco_nt_ann <- ann(reco_nt)
gpp_nt_ann <- ann(gpp_nt)
nee_nt_ann <- reco_nt_ann - gpp_nt_ann

err_reco_ann <- reco_nt_ann - reco_ann
err_gpp_ann <- gpp_nt_ann - gpp_ann
err_nee_ann <- nee_nt_ann - nee_ann

print(round(c(gpp_estimated = gpp_nt_ann, gpp_true = gpp_ann, gpp_error = err_gpp_ann,
              gpp_error_pct = 100 * err_gpp_ann / gpp_ann), 2))
print(round(c(reco_estimated = reco_nt_ann, reco_true = reco_ann, reco_error = err_reco_ann,
              reco_error_pct = 100 * err_reco_ann / reco_ann), 2))
print(round(c(nee_estimated = nee_nt_ann, nee_true = nee_ann, nee_error = err_nee_ann,
              nee_error_pct = 100 * err_nee_ann / abs(nee_ann)), 3))
print(round(c(kok_cost = kok_ann, remainder = err_reco_ann - kok_ann,
              kok_share_of_error_pct = 100 * kok_ann / err_reco_ann), 2))

## ---- fig-diurnal ----
jul <- doy >= 182 & doy <= 212
lab_di <- c("true respiration", "potential, no light inhibition",
            "night-time extrapolation")
di_mean <- function(v) as.vector(tapply(v[jul], hh[jul], mean))
di <- do.call(rbind, Map(function(v, lb)
  data.frame(hh = sort(unique(hh)), y = di_mean(v), series = lb),
  list(reco_true, reco_pot, reco_nt), lab_di))
di$series <- factor(di$series, levels = lab_di)
lit <- tapply(ppfd[jul] > 5, hh[jul], mean)
lit_h <- as.numeric(names(lit))[lit > 0.5]

ggplot(di, aes(hh, y, colour = series, linetype = series)) +
  annotate("rect", xmin = -0.2, xmax = min(lit_h), ymin = -Inf, ymax = Inf,
           fill = "#e9e8dd", colour = NA) +
  annotate("rect", xmin = max(lit_h), xmax = 23.7, ymin = -Inf, ymax = Inf,
           fill = "#e9e8dd", colour = NA) +
  geom_line(linewidth = 1) +
  scale_colour_manual(values = c(te_pal$forest, te_pal$gold, te_pal$clay),
                      name = NULL) +
  scale_linetype_manual(values = c("solid", "22", "solid"), name = NULL) +
  scale_x_continuous(breaks = seq(0, 24, by = 4)) +
  labs(x = "hour of day", y = "ecosystem respiration (umol/m2/s)",
       title = "The gap opens where there are no data to see it") +
  theme_te() +
  theme(legend.position = "bottom", plot.margin = margin(8, 14, 4, 8))

## ---- error-identity ----
err_reco_hh <- reco_nt - reco_true
err_gpp_hh <- gpp_nt - gpp_true
noise_hh <- nee_obs - nee_true

identity_gap <- max(abs(err_gpp_hh - (err_reco_hh - noise_hh)))
cor_hh <- cor(err_reco_hh, err_gpp_hh)
slope_hh <- unname(coef(lm(err_gpp_hh ~ err_reco_hh))[2])
cor_pred <- sd(err_reco_hh) / sqrt(var(err_reco_hh) + var(noise_hh))

mon <- as.integer(cut(doy, breaks = c(0, cumsum(
  c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)))))
cor_day <- cor(tapply(err_reco_hh, doy, sum), tapply(err_gpp_hh, doy, sum))
cor_mon <- cor(tapply(err_reco_hh, mon, sum), tapply(err_gpp_hh, mon, sum))

print(c(identity_residual = formatC(identity_gap, format = "e", digits = 1)))
print(round(c(sd_model_error = sd(err_reco_hh), sd_noise = sd(noise_hh),
              regression_slope = slope_hh), 4))
print(round(c(cor_half_hourly = cor_hh, cor_predicted = cor_pred,
              cor_daily = cor_day, cor_monthly = cor_mon), 4))

## ---- replicates ----
n_rep <- 200
rep_out <- matrix(NA_real_, n_rep, 4)
for (b in seq_len(n_rep)) {
  set.seed(770000 + b)
  y_b <- nee_true + rnorm(n_hh, 0, sd_hh)
  fb <- nls(nee ~ rref * exp(e0 * (1 / (tref_k - t0_k) -
                                     1 / (tk - t0_k))),
            data = data.frame(nee = y_b[night], tk = tk[night]),
            start = list(rref = 2, e0 = 200))
  rc <- unname(coef(fb)[1]) * lt_shape(tk, unname(coef(fb)[2]))
  rep_out[b, ] <- c(ann(rc - reco_true), ann(rc - y_b - gpp_true),
                    ann(y_b - nee_true), unname(coef(fb)[2]))
}
colnames(rep_out) <- c("reco_err", "gpp_err", "nee_err", "e0")
rep_out <- as.data.frame(rep_out)

print(round(c(reco_err_mean = mean(rep_out$reco_err), reco_err_sd = sd(rep_out$reco_err),
              gpp_err_mean = mean(rep_out$gpp_err), gpp_err_sd = sd(rep_out$gpp_err)), 3))
print(round(c(nee_err_mean = mean(rep_out$nee_err), nee_err_sd = sd(rep_out$nee_err),
              gpp_err_min = min(rep_out$gpp_err),
              nee_err_max_abs = max(abs(rep_out$nee_err))), 3))
print(round(c(e0_mean = mean(rep_out$e0), e0_sd = sd(rep_out$e0), e0_true = e0_true), 3))

## ---- fig-cumerr ----
lab_cum <- c("gross primary production", "ecosystem respiration",
             "net ecosystem exchange")
run_tot <- function(v) cumsum(as.vector(tapply(v, doy, sum))) * gc_hh
cum_dat <- do.call(rbind, Map(function(v, lb)
  data.frame(doy = seq_len(n_day), y = run_tot(v), series = lb),
  list(err_gpp_hh, err_reco_hh, noise_hh), lab_cum))
cum_dat$series <- factor(cum_dat$series, levels = lab_cum)

ggplot(cum_dat, aes(doy, y, colour = series)) +
  geom_hline(yintercept = 0, colour = te_pal$line, linewidth = 0.6) +
  geom_line(linewidth = 1) +
  scale_colour_manual(values = c(te_pal$forest, te_pal$clay, te_pal$gold),
                      name = NULL) +
  scale_x_continuous(breaks = c(1, 91, 182, 274, 365)) +
  labs(x = "day of year",
       y = "cumulative error (gC/m2)",
       title = "Two gross fluxes drift, the net flux does not") +
  theme_te() +
  theme(legend.position = "bottom", plot.margin = margin(8, 14, 4, 8))

## ---- daytime ----
rd_win <- rep(NA_real_, 12)
conv_win <- logical(12)
for (m in 1:12) {
  idx <- day_i[mon[day_i] == m]
  dd <- data.frame(nee = nee_obs[idx], ppfd = ppfd[idx])
  fm <- tryCatch(nls(nee ~ rd - a * am * ppfd / (a * ppfd + am),
                     data = dd, start = list(rd = 2, a = 0.04, am = 15),
                     algorithm = "port", lower = c(0, 0.001, 0.5),
                     upper = c(20, 0.3, 90)),
                 error = function(e) NULL)
  if (!is.null(fm)) {
    rd_win[m] <- unname(coef(fm)["rd"])
    conv_win[m] <- TRUE
  }
}

reco_dt <- reco_nt
reco_dt[day_i] <- rd_win[mon[day_i]]
gpp_dt <- reco_dt - nee_obs

gpp_dt_ann <- ann(gpp_dt)
reco_dt_ann <- ann(reco_dt)
mon_true <- as.vector(tapply(reco_true[day_i], mon[day_i], mean))
mon_nt <- as.vector(tapply(reco_nt[day_i], mon[day_i], mean))
rmse_nt <- sqrt(mean((mon_nt - mon_true)^2))
rmse_dt <- sqrt(mean((rd_win - mon_true)^2))
worst_m <- which.max(abs(rd_win / mon_true - 1))

low_cut <- 150
n_mon <- 12
low_i <- day_i[ppfd[day_i] < low_cut]
t_low <- mean(tapply(tair[low_i], mon[low_i], mean))
t_day <- mean(tapply(tair[day_i], mon[day_i], mean))
r_low <- mean(tapply(reco_true[low_i], mon[low_i], mean))
r_day <- mean(tapply(reco_true[day_i], mon[day_i], mean))

print(round(rbind(month = 1:12, truth = mon_true,
                  night_extrapolation = mon_nt, daytime_fit = rd_win), 3))
print(round(c(gpp_daytime = gpp_dt_ann, gpp_error = gpp_dt_ann - gpp_ann,
              reco_daytime = reco_dt_ann, reco_error = reco_dt_ann - reco_ann), 2))
print(round(c(monthly_rmse_night = rmse_nt, monthly_rmse_daytime = rmse_dt,
              months_high_night = sum(mon_nt > mon_true), worst_month = worst_m,
              months_high_daytime = sum(rd_win > mon_true),
              windows_converged = sum(conv_win)), 3))
print(round(c(mean_temp_low_light = t_low, mean_temp_all_daytime = t_day,
              true_reco_low_light = r_low, true_reco_all_daytime = r_day), 3))

## ---- fig-daytime ----
lab_md <- c("truth", "night-time extrapolation", "daytime light response")
md <- rbind(data.frame(mon = 1:12, y = mon_true, series = lab_md[1]),
            data.frame(mon = 1:12, y = mon_nt, series = lab_md[2]),
            data.frame(mon = 1:12, y = rd_win, series = lab_md[3]))
md$series <- factor(md$series, levels = lab_md)

ggplot(md, aes(mon, y, colour = series, shape = series)) +
  geom_line(linewidth = 0.85) +
  geom_point(size = 2.6) +
  scale_colour_manual(values = c(te_pal$forest, te_pal$clay, te_pal$gold),
                      name = NULL) +
  scale_shape_manual(values = c(16, 15, 17), name = NULL) +
  scale_x_continuous(breaks = 1:12) +
  labs(x = "month", y = "mean daytime respiration (umol/m2/s)",
       title = "Closer to the truth, and still low") +
  theme_te() +
  theme(legend.position = "bottom", plot.margin = margin(8, 14, 4, 8))

## ---- e0-sensitivity ----
e0_grid <- seq(100, 400, by = 25)
sens <- t(vapply(e0_grid, function(e0v) {
  gshape <- lt_shape(tk, e0v)
  rr <- sum(nee_obs[night] * gshape[night]) / sum(gshape[night]^2)
  rc <- rr * gshape
  gp <- rc - nee_obs
  c(e0 = e0v, rref = rr, gpp = ann(gp), reco = ann(rc),
    nee = ann(rc) - ann(gp))
}, numeric(5)))
sens <- as.data.frame(sens)

gpp_span <- max(sens$gpp) - min(sens$gpp)
nee_span <- max(sens$nee) - min(sens$nee)
gpp_low <- min(sens$gpp) - gpp_ann

print(round(sens, 2))
print(round(c(gpp_span = gpp_span, gpp_span_pct = 100 * gpp_span / gpp_ann,
              reco_span = max(sens$reco) - min(sens$reco), nee_span = nee_span), 3))
print(round(c(closest_gpp_to_truth = min(sens$gpp), truth = gpp_ann,
              smallest_overestimate = gpp_low,
              worst_overestimate = max(sens$gpp) - gpp_ann), 2))

## ---- fig-e0 ----
lab_e0 <- c("gross primary production", "ecosystem respiration",
            "net ecosystem exchange")
pan_e0 <- c("gross fluxes (gC/m2/yr)", "net flux (gC/m2/yr)")
e0_dat <- rbind(
  data.frame(e0 = sens$e0, y = sens$gpp, series = lab_e0[1], panel = pan_e0[1]),
  data.frame(e0 = sens$e0, y = sens$reco, series = lab_e0[2], panel = pan_e0[1]),
  data.frame(e0 = sens$e0, y = sens$nee, series = lab_e0[3], panel = pan_e0[2]))
e0_dat$series <- factor(e0_dat$series, levels = lab_e0)
e0_dat$panel <- factor(e0_dat$panel, levels = pan_e0)
tru_dat <- data.frame(y = c(gpp_ann, reco_ann, nee_ann),
                      series = factor(lab_e0, levels = lab_e0),
                      panel = factor(pan_e0[c(1, 1, 2)], levels = pan_e0))

ggplot(e0_dat, aes(e0, y, colour = series)) +
  geom_vline(xintercept = e0_true, colour = te_pal$line, linewidth = 0.9) +
  geom_hline(data = tru_dat, aes(yintercept = y, colour = series),
             linetype = "22", linewidth = 0.7, show.legend = FALSE) +
  geom_line(linewidth = 1) +
  geom_point(size = 1.9) +
  facet_wrap(~panel, ncol = 1, scales = "free_y") +
  geom_text(data = data.frame(e0 = e0_true + 10, y = min(sens$nee),
                              panel = factor(pan_e0[2], levels = pan_e0)),
            inherit.aes = FALSE, aes(e0, y, label = "the true sensitivity"),
            hjust = 0, vjust = 2.2, colour = "#5d6b61", size = 3.2) +
  scale_colour_manual(values = c(te_pal$forest, te_pal$clay, te_pal$gold),
                      name = NULL) +
  labs(x = "assumed temperature sensitivity parameter", y = NULL,
       title = "The published number inherits the assumed parameter") +
  theme_te() +
  theme(legend.position = "bottom", plot.margin = margin(8, 16, 4, 8))

## ---- short-window ----
win_len <- 14
wid <- pmin(((doy - 1) %/% win_len) + 1, 26)
n_win <- max(wid)
e0_w <- rep(NA_real_, n_win)
rref_w <- rep(NA_real_, n_win)

for (w in seq_len(n_win)) {
  ii <- which(night & wid == w)
  fw <- tryCatch(nls(y ~ rr * exp(ee * (1 / (tref_k - t0_k) -
                                          1 / (x - t0_k))),
                     data = data.frame(y = nee_obs[ii], x = tk[ii]),
                     start = list(rr = 2, ee = 200), algorithm = "port",
                     lower = c(0.01, 0), upper = c(30, 1000)),
                 error = function(e) NULL)
  if (!is.null(fw)) {
    rref_w[w] <- unname(coef(fw)[1])
    e0_w[w] <- unname(coef(fw)[2])
  }
}

reco_sw <- rref_w[wid] * lt_shape(tk, e0_w[wid])
gpp_sw <- reco_sw - nee_obs
rref_w_true <- as.vector(tapply(rref_true * seas_r, wid, mean))

reco_sw_ann <- ann(reco_sw)
gpp_sw_ann <- ann(gpp_sw)
pot_ann <- ann(reco_pot)

rref_cor <- cor(rref_w, rref_w_true)
rref_rmse <- sqrt(mean((rref_w - rref_w_true)^2))

print(round(c(windows = n_win, converged = sum(!is.na(e0_w)), e0_mean = mean(e0_w),
              e0_sd = sd(e0_w), e0_true = e0_true, e0_annual_fit = e0_hat,
              rref_correlation = rref_cor, rref_rmse = rref_rmse), 3))
print(round(c(reco_short_window = reco_sw_ann, reco_error = reco_sw_ann - reco_ann,
              reco_error_annual_fit = err_reco_ann,
              recovered = err_reco_ann - (reco_sw_ann - reco_ann)), 2))
print(round(c(gpp_short_window = gpp_sw_ann, gpp_error = gpp_sw_ann - gpp_ann,
              potential_reco = pot_ann, error_against_potential = reco_sw_ann - pot_ann,
              error_against_potential_pct =
                100 * (reco_sw_ann - pot_ann) / pot_ann), 2))

## ---- fig-window ----
win_mid <- as.vector(tapply(doy, wid, mean))
lab_up <- "basal rate at ten degrees C (umol/m2/s)"
lab_lo <- "temperature sensitivity parameter"
up_dat <- rbind(
  data.frame(x = win_mid, y = rref_w, panel = lab_up, kind = "fortnightly fit"),
  data.frame(x = win_mid, y = rref_w_true, panel = lab_up, kind = "truth"))
lo_dat <- data.frame(x = win_mid, y = e0_w, panel = lab_lo,
                     kind = "fortnightly fit")
wf <- rbind(up_dat, lo_dat)
wf$panel <- factor(wf$panel, levels = c(lab_up, lab_lo))
hl <- data.frame(y = c(e0_true, e0_hat), panel = factor(lab_lo,
                 levels = c(lab_up, lab_lo)),
                 kind = c("truth", "single annual fit"))

ggplot(wf, aes(x, y, colour = kind, linetype = kind)) +
  geom_hline(data = hl, aes(yintercept = y, colour = kind, linetype = kind),
             linewidth = 0.9) +
  geom_line(linewidth = 0.85) +
  geom_point(data = wf[wf$kind == "fortnightly fit", ], size = 2.1,
             show.legend = FALSE) +
  facet_wrap(~panel, ncol = 1, scales = "free_y") +
  scale_colour_manual(values = c("fortnightly fit" = te_pal$gold,
                                 "truth" = te_pal$forest,
                                 "single annual fit" = te_pal$clay),
                      name = NULL) +
  scale_linetype_manual(values = c("fortnightly fit" = "solid",
                                   "truth" = "22",
                                   "single annual fit" = "solid"),
                        name = NULL) +
  scale_x_continuous(breaks = c(1, 91, 182, 274, 365)) +
  labs(x = "day of year", y = NULL,
       title = "Short windows recover the season, not the daytime") +
  theme_te() +
  theme(legend.position = "bottom", plot.margin = margin(8, 16, 4, 8))
