From 543a758b75a1eb2f6e95481b1e3be2b5b8f9ab2d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Christoph=20Sch=C3=BCrz?= <christoph.schuerz@ufz.de>
Date: Wed, 14 Dec 2022 11:53:38 +0100
Subject: [PATCH] Update printing triggered mgt, implement qtile printing

---
 DESCRIPTION                 |  2 +-
 NAMESPACE                   |  6 ++++++
 R/plot_hru_pw.R             | 37 ++++++++++++++++++++++++++++++++++++-
 R/plot_mgt_harv.R           | 11 ++++++-----
 R/print_mgt.R               |  7 +++----
 R/run_swat_verify.R         |  4 +++-
 man/as_mtx_null.Rd          | 15 +++++++++++++++
 man/print_avannual_qtile.Rd | 27 +++++++++++++++++++++++++++
 man/print_triggered_mgt.Rd  |  6 ++----
 man/report_mgt.Rd           | 23 +++++++++++++++++++++++
 10 files changed, 122 insertions(+), 16 deletions(-)
 create mode 100644 man/as_mtx_null.Rd
 create mode 100644 man/print_avannual_qtile.Rd
 create mode 100644 man/report_mgt.Rd

diff --git a/DESCRIPTION b/DESCRIPTION
index 30fdd8b..4c847d7 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.2
+Version: 0.1.3
 Author: c(person("Svajunas", "Plunge",
              email = "svajunas_plunge@sggw.edu.pl",
              role = c("aut")),
diff --git a/NAMESPACE b/NAMESPACE
index 88abee4..ccf6cd2 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -5,19 +5,25 @@ export(plot_climate_annual)
 export(plot_hru_pw_day)
 export(plot_monthly_snow)
 export(plot_variable_at_harvkill)
+export(print_avannual_qtile)
 export(print_triggered_mgt)
+export(report_mgt)
 export(run_swat_verification)
 import(ggplot2)
 import(patchwork)
 importFrom(data.table,fread)
 importFrom(dplyr,"%>%")
+importFrom(dplyr,arrange)
 importFrom(dplyr,filter)
 importFrom(dplyr,group_by)
+importFrom(dplyr,group_split)
+importFrom(dplyr,lead)
 importFrom(dplyr,left_join)
 importFrom(dplyr,mutate)
 importFrom(dplyr,n)
 importFrom(dplyr,rename)
 importFrom(dplyr,select)
+importFrom(dplyr,slice_sample)
 importFrom(dplyr,summarise)
 importFrom(dplyr,ungroup)
 importFrom(ggplot2,aes)
diff --git a/R/plot_hru_pw.R b/R/plot_hru_pw.R
index f2c0eba..e6d42ef 100644
--- a/R/plot_hru_pw.R
+++ b/R/plot_hru_pw.R
@@ -46,7 +46,7 @@ get_hru_id_by_attribute <- function(sim_verify, lum = NULL, mgt = NULL, soil = N
 #' @param var Character vector that defines the variable names that are plotted
 #' @param years Years of the simulated data for which varaibles are plotted.
 #'
-#' @importFrom dplyr filter mutate select
+#' @importFrom dplyr filter mutate select %>%
 #' @importFrom lubridate ymd
 #' @importFrom tidyr pivot_longer
 #' @importFrom tidyselect all_of
@@ -77,3 +77,38 @@ plot_hru_pw_day <- function(sim_verify, hru_id, var, years = 1900:2100) {
           strip.text = element_text(face = 'bold'),
           axis.title.y = element_blank())
 }
+
+
+#' Print the average annual qtile for HRUs
+#'
+#' print_avannual_qtile prints a table with the average annual qtile in mm
+#' for HRUs that used a tile flow parametrization in landuse.lum
+#'
+#' @param sim_verify Simulation output of the function \code{run_swat_verification()}.
+#'   To plot the heat units at least the output option \code{outputs = 'wb'} must
+#'   be set in  \code{run_swat_verification()}
+#' @param exclude_lum Character vector to define land uses which are excluded
+#'   in the printed table.
+#'
+#' @importFrom dplyr arrange filter left_join rename select %>%
+#'
+#' @return Returns a table with hru ids average annual qtile and attributes.
+#'
+#' @export
+#'
+print_avannual_qtile <- function(sim_verify,
+                                 exclude_lum = c(
+                                   "urhd_lum", "urmd_lum", "urml_lum",
+                                   "urld_lum", "ucom_lum", "uidu_lum",
+                                   "utrn_lum", "uins_lum", "urbn_lum"
+                                 )) {
+
+  sim_verify$hru_wb_aa %>%
+    rename(id = unit) %>%
+    left_join(., sim_verify$lum_mgt, by = "id") %>%
+    filter(tile != 'null') %>%
+    filter(!lu_mgt %in% exclude_lum) %>%
+    select(id, qtile, lu_mgt, mgt, soil) %>%
+    arrange(qtile, id)
+}
+
diff --git a/R/plot_mgt_harv.R b/R/plot_mgt_harv.R
index 81f7af1..b70941c 100644
--- a/R/plot_mgt_harv.R
+++ b/R/plot_mgt_harv.R
@@ -1,14 +1,15 @@
-
 #' Boxplot for relevant variables at harvest-kill
 #'
 #' plot_variable_at_harvkill plots boxplots of one of the variables crop heat unit
 #' fractions ('phu'), crop yields ('yield'), or plant biomass ('bioms') for crops
 #' at harvest-kill of a crop separated for all identified crops.
 #'
-#' @param sim_verify Simulation output of the function \code{run_swat_verification()}.
-#'   To plot the heat units at least the output option \code{outputs = 'mgt'} must
-#'   be set in  \code{run_swat_verification()}
-#' @param variable Selected variable to be plotted. Must be one of: 'phu', 'yield', 'bioms'
+#' @param sim_verify Simulation output of the function
+#'   \code{run_swat_verification()}.
+#'   To plot the heat units at least the output option \code{outputs = 'mgt'}
+#'   must be set in \code{run_swat_verification()}
+#' @param variable Selected variable to be plotted. Must be one of: 'phu',
+#'   'yield', 'bioms'
 #' @param years Simulated years which are aggregated in the boxplot
 #'
 #' @return ggplot boxplot the selected variable at harvest-kill.
diff --git a/R/print_mgt.R b/R/print_mgt.R
index f6742aa..1b610ac 100644
--- a/R/print_mgt.R
+++ b/R/print_mgt.R
@@ -9,8 +9,7 @@
 #'   be set in  \code{run_swat_verification()}
 #' @param hru_id id of the HRU for which the triggered management operations should
 #'   be printed
-#' @param start_year Integer value to define the first year of printing.
-#' @param end_year Integer value to define the last year of printing.
+#' @param years Integer vector to define the years to be printed.
 #'
 #' @return Prints a tibble with triggered operations to the R console.
 #'
@@ -18,12 +17,12 @@
 #'
 #' @export
 #'
-print_triggered_mgt <- function(sim_verify, hru_id, start_year = 1900, end_year = 2100) {
+print_triggered_mgt <- function(sim_verify, hru_id, years = 1900:2100) {
   cat('Triggered managament for\n', ' hru:       ', hru_id, '\n',
       ' management:', sim_verify$lum_mgt$mgt[sim_verify$lum_mgt$id == hru_id], '\n\n')
   sim_verify$mgt_out %>%
     filter(hru == hru_id) %>%
-    filter(year %in% start_year:end_year) %>%
+    filter(year %in% years) %>%
     rename(op_data1 = op_typ,
            op_data3 = var1) %>%
     mutate(op_data3 = ifelse(operation != 'FERT', 0, op_data3)) %>%
diff --git a/R/run_swat_verify.R b/R/run_swat_verify.R
index 8624cb5..cfef867 100644
--- a/R/run_swat_verify.R
+++ b/R/run_swat_verify.R
@@ -77,6 +77,7 @@ run_swat_verification <- function(project_path, outputs = c('wb', 'mgt', 'plt'),
     if ('wb' %in% outputs) {
       model_output$basin_wb_day <- read_tbl('basin_wb_day.txt', run_path, 3)
       model_output$basin_pw_day <- read_tbl('basin_pw_day.txt', run_path, 3)
+      model_output$hru_wb_aa <- read_tbl('hru_wb_aa.txt', run_path, 3)
     }
     if ('mgt' %in% outputs) {
       model_output$mgt_out <- read_mgt(run_path)
@@ -86,7 +87,7 @@ run_swat_verification <- function(project_path, outputs = c('wb', 'mgt', 'plt'),
       landuse_lum <- read_tbl('landuse.lum', run_path, 2)
       model_output$lum_mgt <- left_join(hru_data, landuse_lum,
                                         by = c("lu_mgt" = 'name')) %>%
-        select(id, topo, hydro, soil, lu_mgt, mgt)
+        select(id, topo, hydro, soil, lu_mgt, plnt_com, mgt, tile)
     }
   }
 
@@ -329,6 +330,7 @@ set_print_prt <- function(project_path, run_path, outputs, years_skip) {
   if ('wb' %in% outputs) {
     print_prt[11] <- "basin_wb                     y             n             n             n  "
     print_prt[14] <- "basin_pw                     y             n             n             n  "
+    print_prt[33] <- "hru_wb                       n             n             n             y  "
   }
   if ('mgt' %in% outputs) {
     print_prt[9]  <- "n             y             n             n             "
diff --git a/man/as_mtx_null.Rd b/man/as_mtx_null.Rd
new file mode 100644
index 0000000..b730813
--- /dev/null
+++ b/man/as_mtx_null.Rd
@@ -0,0 +1,15 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/print_mgt.R
+\name{as_mtx_null}
+\alias{as_mtx_null}
+\title{Transform x to a matrix with 7 columns and fill up with NA values}
+\usage{
+as_mtx_null(x)
+}
+\arguments{
+\item{x}{character vector or NULL}
+}
+\description{
+Transform x to a matrix with 7 columns and fill up with NA values
+}
+\keyword{internal}
diff --git a/man/print_avannual_qtile.Rd b/man/print_avannual_qtile.Rd
new file mode 100644
index 0000000..4474d8a
--- /dev/null
+++ b/man/print_avannual_qtile.Rd
@@ -0,0 +1,27 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/plot_hru_pw.R
+\name{print_avannual_qtile}
+\alias{print_avannual_qtile}
+\title{Print the average annual qtile for HRUs}
+\usage{
+print_avannual_qtile(
+  sim_verify,
+  exclude_lum = c("urhd_lum", "urmd_lum", "urml_lum", "urld_lum", "ucom_lum", "uidu_lum",
+    "utrn_lum", "uins_lum", "urbn_lum")
+)
+}
+\arguments{
+\item{sim_verify}{Simulation output of the function \code{run_swat_verification()}.
+To plot the heat units at least the output option \code{outputs = 'wb'} must
+be set in  \code{run_swat_verification()}}
+
+\item{exclude_lum}{Character vector to define land uses which are excluded
+in the printed table.}
+}
+\value{
+Returns a table with hru ids average annual qtile and attributes.
+}
+\description{
+print_avannual_qtile prints a table with the average annual qtile in mm
+for HRUs that used a tile flow parametrization in landuse.lum
+}
diff --git a/man/print_triggered_mgt.Rd b/man/print_triggered_mgt.Rd
index 0f7ae32..ff02488 100644
--- a/man/print_triggered_mgt.Rd
+++ b/man/print_triggered_mgt.Rd
@@ -4,7 +4,7 @@
 \alias{print_triggered_mgt}
 \title{Print the triggered management operations from the mgt outputs for a certain HRU}
 \usage{
-print_triggered_mgt(sim_verify, hru_id, start_year = 1900, end_year = 2100)
+print_triggered_mgt(sim_verify, hru_id, years = 1900:2100)
 }
 \arguments{
 \item{sim_verify}{Simulation output of the function \code{run_swat_verification()}.
@@ -14,9 +14,7 @@ be set in  \code{run_swat_verification()}}
 \item{hru_id}{id of the HRU for which the triggered management operations should
 be printed}
 
-\item{start_year}{Integer value to define the first year of printing.}
-
-\item{end_year}{Integer value to define the last year of printing.}
+\item{years}{Integer vector to define the years to be printed.}
 }
 \value{
 Prints a tibble with triggered operations to the R console.
diff --git a/man/report_mgt.Rd b/man/report_mgt.Rd
new file mode 100644
index 0000000..467be71
--- /dev/null
+++ b/man/report_mgt.Rd
@@ -0,0 +1,23 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/print_mgt.R
+\name{report_mgt}
+\alias{report_mgt}
+\title{Generate a report table that compares the scheduled and triggered managements}
+\usage{
+report_mgt(sim_verify)
+}
+\arguments{
+\item{sim_verify}{Simulation output of the function \code{run_swat_verification()}.
+To print the management at least the output option \code{outputs = 'mgt'} must
+be set in  \code{run_swat_verification()}}
+}
+\value{
+Returns a tibble that summarises all management schedules for
+  which operations where scheduled, that were either not triggered of
+  for which operation properties differ..
+}
+\description{
+report_mgt compares the scheduled management operations for all schedules
+with the triggered management operations. Therefore HRUs are randomly
+selected for each schedule where one of the schedules is implemented.
+}
-- 
GitLab