Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Included re-setting CWD by day-of-year #6

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ Suggests:
ggplot2,
patchwork,
extRemes,
tidyr
tidyr,
visdat,
recipes
License: AGPL-3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Expand Down
11 changes: 8 additions & 3 deletions R/cwd.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#'
#' @export
#'
cwd <- function(df, varname_wbal, varname_date, thresh_terminate = 0.0, thresh_drop = 0.9, doy_reset = NA){
cwd <- function(df, varname_wbal, varname_date, thresh_terminate = 0.0, thresh_drop = 0.9, doy_reset = 999){

# corresponds to mct2.R

Expand All @@ -43,6 +43,9 @@ cwd <- function(df, varname_wbal, varname_date, thresh_terminate = 0.0, thresh_d
thresh_terminate <- thresh_drop
}

# create day-of-year column
df$doy <- lubridate::yday(df[[ varname_date ]])

inst <- tibble()
idx <- 0
iinst <- 1
Expand All @@ -66,8 +69,10 @@ cwd <- function(df, varname_wbal, varname_date, thresh_terminate = 0.0, thresh_d
done_finding_dropday <- FALSE

# continue accumulating deficit as long as the deficit has not fallen below (thresh_terminate) times the maximum deficit attained in this event
while (iidx <= (nrow(df)-1) &&
(deficit - df[[ varname_wbal ]][iidx] > thresh_terminate * max_deficit)
# optionally
while (iidx <= (nrow(df)-1) && # avoid going over row length
(deficit - df[[ varname_wbal ]][iidx] > thresh_terminate * max_deficit) &&
df$doy[iidx] < doy_reset
){

dday <- dday + 1
Expand Down
4 changes: 3 additions & 1 deletion data-raw/get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ saveRDS(df, file = paste0(here(), "/data/df_ch-lae.rds"))

# get fluxnet data for FR-Pue site
df <- read_csv(paste0(here(), "/inst/extdata/FLX_FR-Pue_FLUXDATAKIT_FULLSET_DD_2000_2014_2-3.csv")) |>
select(TIMESTAMP, P_F, TA_F_MDS, PA_F, LE_F_MDS, NETRAD)
select(TIMESTAMP, P_F, TA_F_MDS, PA_F, LE_F_MDS, SW_IN_F_MDS, LW_IN_F_MDS, NETRAD)

# visdat::vis_miss(df)

saveRDS(df, file = paste0(here(), "/data/df_fr-pue.rds"))
Binary file modified data/df_fr-pue.rds
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ pkgdown_sha: ~
articles:
cwd_example: cwd_example.html
potential_cwd: potential_cwd.html
last_built: 2024-05-07T13:22Z
last_built: 2024-05-13T13:11Z

2 changes: 1 addition & 1 deletion docs/reference/cwd.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/cwd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions vignettes/cwd_example.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ gg5 / gg6 / gg7

Get CWD and events.
```{r}
out_cwd <- cwd(df,
varname_wbal = "wbal",
varname_date = "TIMESTAMP",
thresh_terminate = 0.0,
thresh_drop = 0.0)
out_cwd <- cwd(
df,
varname_wbal = "wbal",
varname_date = "TIMESTAMP",
thresh_terminate = 0.0,
thresh_drop = 0.0
)
```

Retain only events of a minimum length of 20 days.
Expand Down
148 changes: 147 additions & 1 deletion vignettes/potential_cwd.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ knitr::opts_chunk$set(echo = TRUE)
```{r message=FALSE}
library(readr)
library(dplyr)
library(tidyr)
library(here)
library(lubridate)
library(patchwork)
library(extRemes)
library(ggplot2)
library(cwd)
library(visdat)
library(recipes)
```

A potential cumulative water deficit can be calculated using net radiation and the *potential* evapotranspiration (PET). Here, we calculate PET based on net radiation and the approach by Priestley & Taylor (1972) as implemented by Davis et al. (2017). Necessary functions are part of the {cwd} package.
Expand All @@ -31,8 +34,60 @@ A potential cumulative water deficit can be calculated using net radiation and t
Read data from the example FLUNXET file contained in this repository.
```{r}
df <- readRDS(file = paste0(here(), "/data/df_fr-pue.rds"))

visdat::vis_miss(df)
```

Some net radiation data is missing. Impute missing values by KNN.
```{r}
pp <- recipes::recipe(
NETRAD ~ SW_IN_F_MDS + LW_IN_F_MDS + TA_F_MDS,
data = df |>
drop_na(SW_IN_F_MDS, LW_IN_F_MDS, TA_F_MDS)
) |>
recipes::step_center(
recipes::all_numeric(),
-recipes::all_outcomes()
) |>
recipes::step_scale(
recipes::all_numeric(),
-recipes::all_outcomes()
) |>
recipes::step_impute_knn(
recipes::all_outcomes(),
neighbors = 5
)

pp_prep <- recipes::prep(
pp,
training = df |>
drop_na(SW_IN_F_MDS, LW_IN_F_MDS, TA_F_MDS)
)

df_baked <- recipes::bake(
pp_prep,
new_data = df
)

# fill missing with gap-filled
df <- df |>
dplyr::bind_cols(
df_baked |>
dplyr::select(
NETRAD_filled = NETRAD)
) |>
dplyr::mutate(
NETRAD = ifelse(is.na(NETRAD), NETRAD_filled, NETRAD)
#qc = ifelse(is.na(netrad), TRUE, FALSE)
) |>
dplyr::select(
-NETRAD_filled
)

visdat::vis_miss(df)
```


## Apply PET function

... and convert from units of mm s-1 to mm d-1.
Expand Down Expand Up @@ -68,8 +123,99 @@ df |>
) |>
ggplot() +
geom_line(aes(doy, et, color = "ET")) +
geom_line(aes(doy, pet, color = "PET"))
geom_line(aes(doy, pet, color = "PET")) +
labs(
x = "Day of year",
y = expression(paste("Water vapour mass flux (mm d"^-1, ")"))
) +
theme_classic()
```
## Cumulating PET - *P*

Check annual totals.
```{r}
adf <- df |>
mutate(year = year(TIMESTAMP)) |>
group_by(year) |>
summarise(pet = sum(pet), prec = sum(P_F))

adf |>
tidyr::pivot_longer(cols = c(pet, prec), names_to = "Flux") |>
ggplot(aes(x = year, y = value, color = Flux)) +
geom_line() +
labs(y = "Flux (mm/yr)") +
theme_classic()
```

In some cases, the mean annual PET may be larger than the mean annual precipitation (*P*), leading to a steady long-term increase of a *potential* cumulative water deficit. This is not the case here (see plot above). For demonstration, let's assume precipitation was 30% of its actual value and calculate the running sum of (PET - *P*) - the cumulative potential evapotranspiration.
```{r}
df |>
mutate(P_F = 0.3 * P_F) |>
mutate(pcwd = cumsum(pet - P_F)) |>
ggplot(aes(TIMESTAMP, pcwd)) +
geom_line() +
theme_classic()
```

This indicates a need to re-set the potential cumulative water deficit calculation. Let's determine the wettest month from the available years of data and reset the cumulative water deficit each year in that month. The plot below shows the average *P* - PET for each month. November is the wettest month at this site.

```{r}
mdf_mean <- df |>
mutate(month = lubridate::month(TIMESTAMP),
pwbal = P_F - pet) |>
group_by(month) |>
summarise(
pwbal = sum(pwbal)
)

mdf_mean |>
ggplot(aes(as.factor(month), pwbal)) +
geom_bar(stat = "identity") +
theme_classic()
```

Therefore, we re-set the cumulation of the water deficit each year in November.
```{r}
# determine the day-of-year of the first day of the month after the wettest month
doy_reset <- lubridate::yday(lubridate::ymd("2000-11-01") + lubridate::dmonths(1))
```

Get potential CWD and events.
```{r}
df <- df |>
mutate(pwbal = P_F - pet)

out_cwd <- cwd(
df,
varname_wbal = "pwbal",
varname_date = "TIMESTAMP",
thresh_terminate = 0.0,
thresh_drop = 0.0,
doy_reset = doy_reset
)
```

Retain only events of a minimum length of 20 days.
```{r}
out_cwd$inst <- out_cwd$inst |>
filter(len >= 20)
```

Plot CWD time series.
```{r}
ggplot() +
geom_rect(
data = out_cwd$inst,
aes(xmin = date_start, xmax = date_end, ymin = 0, ymax = max( out_cwd$df$deficit)),
fill = rgb(0,0,0,0.3),
color = NA) +
geom_line(data = out_cwd$df, aes(TIMESTAMP, deficit), color = "tomato") +
theme_classic() +
ylim(0, max( out_cwd$df$deficit)) +
labs(
x = "Date",
y = "Potential cumulative water deficit (mm)"
)
```

## References
Expand Down
Loading