---
title: "Fitting CO2 response curves"
author: "Joseph R. Stinziano and Christopher D. Muir"
date: "`r Sys.Date()`"
output: rmarkdown::html_document
vignette: >
 %\VignetteIndexEntry{co2-response}
 %\VignetteEngine{knitr::rmarkdown}
 %\VignetteEncoding{UTF-8}
---

This package currently implements a Gu-type fitting procedure for CO2 response 
curves similar to the Duursma (2015) implementation. There is ongoing work to 
implement a full Gu-type method whereby mesophyll conductance, Km, and GammaStar 
could all be fit (Gu *et al.* 2010).

```{r, message = FALSE}
#| fig.alt: >
#|   Example of fitted A-Ci curve

library(dplyr)
library(photosynthesis)

# Read in your data
dat = system.file("extdata", "A_Ci_Q_data_1.csv", package = "photosynthesis") |>
  read.csv() |>
  mutate(
    # Set grouping variable
    group = as.factor(round(Qin, digits = 0)),
    # Convert data temperature to K
    T_leaf = Tleaf + 273.15
  ) |>
  rename(A_net = A, PPFD = Qin, C_i = Ci)

# Fit ACi curve. 
# Note that we are filtering the data.frame to fit for a single light value
fit = fit_aci_response(filter(dat, group == 1500))

# View fitted parameters
fit[[1]]

# View graph
fit[[2]]

# View data with modeled parameters attached
# fit[[3]]

# Fit many curves
fits = fit_many(
  data = dat, 
  funct = fit_aci_response, 
  group = "group", 
  progress = FALSE
)

# Print the parameters
# First set of double parentheses selects an individual group value
# Second set selects an element of the sublist
fits[[3]][[1]]

# Print the graph
fits[[3]][[2]]

# Compile graphs into a list for plotting
fits_graphs = compile_data(fits, list_element = 2)

# Print graphs to jpeg
# print_graphs(data = fits_graphs, path = tempdir(), output_type = "jpeg")

# Compile parameters into data.frame for analysis
fits_pars = compile_data(fits, output_type = "dataframe", list_element = 1)

```

# References

Duursma R. 2015. Plantecophys - an R package for analysing and modeling leaf gas exchange data. *PLoS ONE* 10:e0143346

Gu L, Pallardy SG, Tu K, Law BE, Wullschleger SD. 2010. Reliable estimation of biochemical parameters from C3 leaf photosynthesis-intercellular carbon dioxide response curves. *Plant, Cell & Environment* 33:1582-1874.