From 69a3f9a2a5bdaac2d201622e45b42abf8344d603 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Christoph=20Sch=C3=BCrz?= <christoph.schuerz@ufz.de>
Date: Mon, 12 Dec 2022 14:36:41 +0100
Subject: [PATCH] Add plot_monthly_snow

---
 DESCRIPTION                  |  2 +-
 NAMESPACE                    |  2 ++
 R/plot_climate.R             | 68 +++++++++++++++++++++++++++++++-----
 R/run_swat_verify.R          |  4 +--
 man/plot_monthly_snow.Rd     | 24 +++++++++++++
 man/plot_stat_text.Rd        |  2 +-
 man/run_swat_verification.Rd |  4 +--
 7 files changed, 91 insertions(+), 15 deletions(-)
 create mode 100644 man/plot_monthly_snow.Rd

diff --git a/DESCRIPTION b/DESCRIPTION
index 1f1d0d6..7861527 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,7 +1,7 @@
 Package: SWATdoctR
 Type: Package
 Title: Finding the right diagnoses and treatments for SWAT+ models
-Version: 0.1.0
+Version: 0.1.1
 Author: c(person("Svajunas", "Plunge",
              email = "svajunas_plunge@sggw.edu.pl",
              role = c("aut")),
diff --git a/NAMESPACE b/NAMESPACE
index db4f695..d80c339 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -3,6 +3,7 @@
 export(get_hru_id_by_attribute)
 export(plot_climate_annual)
 export(plot_hru_pw_day)
+export(plot_monthly_snow)
 export(plot_variable_at_harvkill)
 export(print_triggered_mgt)
 export(run_swat_verification)
@@ -31,6 +32,7 @@ importFrom(ggplot2,theme_void)
 importFrom(lubridate,int_end)
 importFrom(lubridate,int_start)
 importFrom(lubridate,interval)
+importFrom(lubridate,month)
 importFrom(lubridate,yday)
 importFrom(lubridate,year)
 importFrom(lubridate,ymd)
diff --git a/R/plot_climate.R b/R/plot_climate.R
index dcd4dde..6770466 100644
--- a/R/plot_climate.R
+++ b/R/plot_climate.R
@@ -96,14 +96,14 @@ plot_climate_annual <- function(sim_verify) {
     group_by(yr) %>%
     summarise(tmn_d = min(tmn),
               tmx_d = max(tmx),
-              tmn_a = mean(tmn),
-              tmx_a = mean(tmx),
+              # tmn_a = mean(tmn),
+              # tmx_a = mean(tmx),
               tmpav = mean(tmpav)) %>%
     pivot_longer(cols = -yr)
 
   gg_tmp <- ggplot() +
     geom_errorbarh(data = tmp_tbl, aes(xmax = yr + 0.5, xmin = yr - 0.5, y = value, height = 0, col = name), lwd = 1) +
-    scale_color_manual(values = c('dodgerblue2', 'dodgerblue4', 'black', 'tomato1', 'tomato3')) +
+    scale_color_manual(values = c('dodgerblue4', 'black', 'tomato3')) +
     # geom_step(data = pet_tbl, aes(x = yr, y = value_sum), direction = 'mid') +
     labs(y = 'Temperature (°C)') +
     theme_bw() +
@@ -144,9 +144,18 @@ plot_climate_annual <- function(sim_verify) {
   gg_pcp_stat <- plot_stat_text(clim_data_annual,
                                 vars = c('precip', 'rainfall', 'snofall'),
                                 c(5,4,3), 'mm', F)
-  gg_tmp_stat <- plot_stat_text(rename(clim_data, value_sum = value),
-                                vars = c('tmn', 'tmx', 'tmpav'),
-                                c(5,4,3), '°C', F)
+  tmp_stat <- clim_data %>%
+    filter(name %in% c('tmn', 'tmx', 'tmpav')) %>%
+    group_by(name) %>%
+    summarise(val_min = min(value),
+              val_max = max(value),
+              val_mean = mean(value)) %>%
+    mutate(name = c('tmn day', 'tmpav', 'tmx day'),
+           value_sum = c(val_min[1], val_mean[2], val_max[3])) %>%
+    select(name, value_sum)
+  gg_tmp_stat <- plot_stat_text(tmp_stat,
+                                vars = c('tmn day', 'tmpav', 'tmx day'),
+                                c(5,4,3), '°C', F, digit = 1)
   gg_slr_stat <- plot_stat_text(clim_data_annual, vars = c('solarad'),
                                 c(5), 'MJ m^-2', F)
 
@@ -166,12 +175,12 @@ plot_climate_annual <- function(sim_verify) {
 #'
 #' @keywords internal
 #'
-plot_stat_text <- function(tbl, vars, pos, unit_add, add_title) {
+plot_stat_text <- function(tbl, vars, pos, unit_add, add_title, digit = 0) {
   tbl_mean <- tbl %>%
     filter(name %in% vars) %>%
-    select(name, yr, value_sum) %>%
+    select(name, value_sum) %>%
     group_by(name) %>%
-    summarise(value_sum = round(mean(value_sum))) %>%
+    summarise(value_sum = round(mean(value_sum), digits = digit)) %>%
     mutate(value_sum = paste(value_sum, unit_add),
            y = pos)
 
@@ -187,3 +196,44 @@ plot_stat_text <- function(tbl, vars, pos, unit_add, add_title) {
 
   return(gg)
 }
+
+#' Plot average monthly precipitation components rainfall and snofall and the snow melt
+#'
+#' plot_snow_monthly uses the simulated basin averages of precip, and snofall,
+#' and plots average monthly sums of rainfall, snofall and snomlt.
+#' This is plot can be helpful to verify the dominant snow processes and
+#' can be useful in catchments where large spring runoff through snowmelt
+#' plays a relevant role.
+#'
+#' @param sim_verify Simulation output of the function \code{run_swat_verification()}.
+#'   To plot the climate outputs at least the output option \code{outputs = 'wb'} must
+#'   be set in  \code{run_swat_verification()}.
+#'
+#' @return Returns a ggplots for the monthly average values of the simulated
+#'   precip, snofall and snomlt.
+#'
+#' @importFrom dplyr group_by mutate select summarise %>%
+#' @importFrom lubridate month
+#' @importFrom tidyr pivot_longer
+#' @import ggplot2
+#' @export
+#'
+plot_monthly_snow <- function(sim_verify) {
+  sno_wb <- sim_verify$basin_wb_day %>%
+    mutate(rainfall = precip - snofall) %>%
+    select(yr, mon, rainfall, snofall, snomlt) %>%
+    pivot_longer(., cols = - c(yr, mon)) %>%
+    group_by(name, mon, yr) %>%
+    summarise(value = sum(value), .groups = 'drop_last') %>%
+    group_by(name, mon) %>%
+    summarise(., value = mean(value), .groups = 'drop') %>%
+    mutate(process = ifelse(name == 'snomlt', 'Snow melt', 'Precipitation'),
+           mon = month(mon, label = T))
+
+  ggplot(data = sno_wb) +
+    geom_col(aes(x = mon, y = value, fill = name)) +
+    labs(x = 'Month', y = 'Average monthly sum (mm)', fill = 'Process') +
+    scale_fill_manual(values = c('dodgerblue4','slategray2', 'skyblue4')) +
+    facet_wrap(.~process, ncol = 1) +
+    theme_bw()
+}
diff --git a/R/run_swat_verify.R b/R/run_swat_verify.R
index 0a9d5ad..d5f96a0 100644
--- a/R/run_swat_verify.R
+++ b/R/run_swat_verify.R
@@ -21,8 +21,8 @@
 #' @param years_skip (optional) Integer value to define the number of simulation
 #'   years that are skipped before writing SWAT model outputs.
 #' @param nostress nostress parameter in the 'codes.bsn' file to activate/deactivate
-#'   plant stresses for plant growth. Set \code{nostress = 1} to activate all stress
-#'   factors, \code{nostress = 1} to deactivate all stress factors, and \code{nostress = 1}
+#'   plant stresses for plant growth. Set \code{nostress = 0} to activate all stress
+#'   factors, \code{nostress = 1} to deactivate all stress factors, and \code{nostress = 2}
 #'   to only activate nutrient plant stress.
 #' @param keep_folder (optional) If \code{keep_folder = TRUE}
 #'   '.model_run/verification' is kept and not deleted after finishing model runs.
diff --git a/man/plot_monthly_snow.Rd b/man/plot_monthly_snow.Rd
new file mode 100644
index 0000000..d2f4b19
--- /dev/null
+++ b/man/plot_monthly_snow.Rd
@@ -0,0 +1,24 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/plot_climate.R
+\name{plot_monthly_snow}
+\alias{plot_monthly_snow}
+\title{Plot average monthly precipitation components rainfall and snofall and the snow melt}
+\usage{
+plot_monthly_snow(sim_verify)
+}
+\arguments{
+\item{sim_verify}{Simulation output of the function \code{run_swat_verification()}.
+To plot the climate outputs at least the output option \code{outputs = 'wb'} must
+be set in  \code{run_swat_verification()}.}
+}
+\value{
+Returns a ggplots for the monthly average values of the simulated
+  precip, snofall and snomlt.
+}
+\description{
+plot_snow_monthly uses the simulated basin averages of precip, and snofall,
+and plots average monthly sums of rainfall, snofall and snomlt.
+This is plot can be helpful to verify the dominant snow processes and
+can be useful in catchments where large spring runoff through snowmelt
+plays a relevant role.
+}
diff --git a/man/plot_stat_text.Rd b/man/plot_stat_text.Rd
index 95c7d8a..4cdbc87 100644
--- a/man/plot_stat_text.Rd
+++ b/man/plot_stat_text.Rd
@@ -4,7 +4,7 @@
 \alias{plot_stat_text}
 \title{Plot annual average values in tabular text form as a ggplot}
 \usage{
-plot_stat_text(tbl, vars, pos, unit_add, add_title)
+plot_stat_text(tbl, vars, pos, unit_add, add_title, digit = 0)
 }
 \arguments{
 \item{tbl}{Processed climate variable table}
diff --git a/man/run_swat_verification.Rd b/man/run_swat_verification.Rd
index d3781df..522767b 100644
--- a/man/run_swat_verification.Rd
+++ b/man/run_swat_verification.Rd
@@ -34,8 +34,8 @@ in the form yyyymmdd, or in Date format.}
 years that are skipped before writing SWAT model outputs.}
 
 \item{nostress}{nostress parameter in the 'codes.bsn' file to activate/deactivate
-plant stresses for plant growth. Set \code{nostress = 1} to activate all stress
-factors, \code{nostress = 1} to deactivate all stress factors, and \code{nostress = 1}
+plant stresses for plant growth. Set \code{nostress = 0} to activate all stress
+factors, \code{nostress = 1} to deactivate all stress factors, and \code{nostress = 2}
 to only activate nutrient plant stress.}
 
 \item{keep_folder}{(optional) If \code{keep_folder = TRUE}
-- 
GitLab