---
title: "PEPRMT-Tidal_example_run"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{PEPRMT-Tidal_example_run}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

This vignette uses an example dataset from Oikawa et al. (2023) to run the PEPRMT-Tidal modules. This dataset includes data from 5 eddy covariance towers located in tidal wetlands across the 3 coasts of the USA. For more details about the sites and data sources see Oikawa et al. (2023).

```{r setup, echo=FALSE, message=FALSE}
library(PEPRMT)
library(ggplot2)
```

```{r data_structure}
# All data were sourced from the Ameriflux database or directly from site PIs,
# details about the data and acquisition are in Oikawa et al. 2023
# Note: site column needs to be numeric, there is also a char site name column
# to help keep track

summary(example_data)
str(example_data)
```

First, run GPP Module

```{r gpp}
# Run model using default parameters
GPP_mod <- PEPRMT_GPP(
  data = example_data
)

# Create a new dataset that includes model results
example_data_results <- example_data
example_data_results$GPP_mod <- GPP_mod$GPP_mod

# Plot
example_data_results |>
  ggplot(aes(DOY)) +
  geom_line(aes(x = DOY, y = GPP_mod, color = "Modeled")) +
  geom_line(aes(x = DOY, y = GPP_gC_m2_day, color = "Observed")) +
  scale_color_manual(
    breaks = c("Observed", "Modeled"),
    values = c("black", "red")
  ) +
  theme_bw(base_size = 12) +
  ylab(expression("GPP (g C " ~ CO[2] ~ m^-2 ~ day^-1 * ")")) +
  theme(
    axis.line = element_line(colour = "black"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_rect(colour = "black"),
    panel.background = element_blank()
  ) +
  facet_wrap(~site, ncol = 1, scales = "free")
```

Second, run Reco Module

```{r reco}
# Run model using default parameters
Reco_mod <- PEPRMT_Reco(
  data = example_data_results,
  wetland_type = 2
)

# Create a new dataset that includes model results
example_data_results$Reco_mod <- Reco_mod$Reco_mod

# Plot
example_data_results |>
  ggplot(aes(DOY)) +
  geom_line(aes(x = DOY, y = Reco_mod, color = "Modeled")) +
  geom_line(aes(x = DOY, y = Reco_gC_m2_day, color = "Observed")) +
  scale_color_manual(
    breaks = c("Observed", "Modeled"),
    values = c("black", "red")
  ) +
  theme_bw(base_size = 12) +
  ylab(expression("Reco (g C " ~ CO[2] ~ m^-2 ~ day^-1 * ")")) +
  theme(
    axis.line = element_line(colour = "black"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_rect(colour = "black"),
    panel.background = element_blank()
  ) +
  facet_wrap(~site, ncol = 1, scales = "free")
```

Finally, run CH4 module

```{r ch4}
# Add modeled S1, S2 into data before running CH4 module (17th & 18th columns)
example_data_results$SOM_total <- Reco_mod$S1
example_data_results$SOM_labile <- Reco_mod$S2

# Run model using default parameters
CH4_mod <- PEPRMT_CH4(
  data = example_data_results,
  wetland_type = 2
)

# Create a new dataset that includes model results
example_data_results$CH4_mod <- CH4_mod$CH4_mod

# Plot
example_data_results |>
  ggplot(aes(DOY)) +
  geom_line(aes(x = DOY, y = CH4_mod * 1000, color = "Modeled")) +
  geom_line(aes(x = DOY, y = CH4_gC_m2_day * 1000, color = "Observed")) +
  scale_color_manual(
    breaks = c("Observed", "Modeled"),
    values = c("black", "red")
  ) +
  labs(x = "", y = bquote("CH4 " ~ (mgC ~ m^-2 ~ d^-1))) +
  theme_bw(base_size = 12) +
  theme(
    axis.line = element_line(colour = "black"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_rect(colour = "black"),
    panel.background = element_blank(),
    legend.position = "center"
  ) +
  facet_wrap(~site, ncol = 1, scales = "free")
```

Alternatively, all modules can be run in combination using run_PEPRMT().

```{r}
# Run model using default parameters
run_PEPRMT(example_data,
  wetland_type = 2
)
```

