Skip to content
Snippets Groups Projects
Commit 33a4e943 authored by Christoph Schürz's avatar Christoph Schürz
Browse files

Implement daily plot function for hru_pw

parent 238faaab
No related branches found
No related tags found
No related merge requests found
Package: SWATdoctR
Type: Package
Title: Finding the right diagnoses and treatments for SWAT+ models
Version: 0.1.0.9003
Version: 0.1.0.9004
Author: c(person("Svajunas", "Plunge",
email = "svajunas_plunge@sggw.edu.pl",
role = c("aut")),
......
# Generated by roxygen2: do not edit by hand
export(get_hru_id_by_attribute)
export(plot_climate_annual)
export(plot_hru_pw_day)
export(plot_phuplant_harvest)
export(print_triggered_mgt)
export(run_swat_verification)
import(ggplot2)
import(patchwork)
......@@ -13,10 +17,15 @@ importFrom(dplyr,mutate)
importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(dplyr,summarise)
importFrom(dplyr,ungroup)
importFrom(ggplot2,aes)
importFrom(ggplot2,geom_boxplot)
importFrom(ggplot2,geom_hline)
importFrom(ggplot2,geom_text)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,labs)
importFrom(ggplot2,lims)
importFrom(ggplot2,theme_bw)
importFrom(ggplot2,theme_void)
importFrom(lubridate,int_end)
importFrom(lubridate,int_start)
......@@ -26,6 +35,7 @@ importFrom(lubridate,year)
importFrom(lubridate,ymd)
importFrom(processx,run)
importFrom(purrr,map)
importFrom(purrr,map_df)
importFrom(purrr,set_names)
importFrom(readr,read_lines)
importFrom(readr,write_lines)
......@@ -36,4 +46,5 @@ importFrom(stringr,str_trim)
importFrom(tibble,as_tibble)
importFrom(tibble,tibble)
importFrom(tidyr,pivot_longer)
importFrom(tidyselect,all_of)
importFrom(vroom,vroom_lines)
#' Get HRU ids by filtering for HRU attributes
#'
#' get_hru_id_by_attribute Uses the hru-data and landuse_lum saved after running
#' \code{run_swat_verification()} and filters the attributes by the defined
#' filter attributes.
#'
#' @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 lum Optional character vector with landuse.lum labels
#' @param mgt Optional character vector with management labels as defined in management.sch.
#' @param soil Optional character vector with soil type labels as defined in the soil data.
#'
#' @return Returns a table with hru ids and attributes.
#'
#' @export
#'
get_hru_id_by_attribute <- function(sim_verify, lum = NULL, mgt = NULL, soil = NULL) {
hru_attr <- sim_verify$lum_mgt[,c('id', 'lu_mgt', 'mgt', 'soil')]
if(!is.null(lum)) {
hru_attr <- hru_attr[hru_attr$lu_mgt %in% lum,]
} else {
hru_attr$lu_mgt <- NULL
}
if(!is.null(mgt)) {
hru_attr <- hru_attr[hru_attr$mgt %in% mgt,]
} else {
hru_attr$mgt <- NULL
}
if(!is.null(soil)) {
hru_attr <- hru_attr[hru_attr$soil %in% soil,]
} else {
hru_attr$soil <- NULL
}
return(hru_attr)
}
#' Plot daily simulated variables which are saved in hru_pw_day
#'
#' plot_hru_pw_day plots daily time series of variables from the output file hru_pw_day
#'
#' @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 hru_id Numeric vector with HRU ids for which variables should be plotted
#' @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 lubridate ymd
#' @importFrom tidyr pivot_longer
#' @importFrom tidyselect all_of
#' @import ggplot2
#'
#' @return Returns a table with hru ids and attributes.
#'
#' @export
#'
plot_hru_pw_day <- function(sim_verify, hru_id, var, years = 1900:2100) {
plot_data <- sim_verify$hru_pw_day %>%
filter(unit %in% hru_id) %>%
filter(yr %in% years) %>%
mutate(date = ymd(paste(yr, mon, day, sep = '-')),
unit = paste('hru:', unit)) %>%
select(date, unit, all_of(var)) %>%
pivot_longer(., cols = - c(date, unit), names_to = 'var', values_to = 'value')
ggplot(plot_data) +
geom_line(aes(x = date, y = value, color = unit, lty = unit)) +
labs(x = 'Date', color = 'HRU', lty = 'HRU') +
scale_x_date(date_labels = '%Y-%m-%d') +
facet_grid(rows = vars(all_of(var)), scales = 'free_y', switch = 'y') +
theme_bw() +
theme(legend.position = 'bottom',
strip.background = element_blank(),
strip.placement = 'outside',
strip.text = element_text(face = 'bold'),
axis.title.y = element_blank())
}
......@@ -224,10 +224,12 @@ read_mgt <- function(run_path) {
}
#' Generate folder structure for parallel SWAT execution
#' Generate folder structure for SWAT execution
#'
#' @param project_path Path to the SWAT project folder (i.e. TxtInOut)
#'
#' @keywords internal
#'
build_model_run <- function(project_path){
run_path <- paste0(project_path, '/.run_verify')
swat_files <- dir(project_path, full.names = TRUE)
......
......@@ -2,7 +2,7 @@
% Please edit documentation in R/run_swat_verify.R
\name{build_model_run}
\alias{build_model_run}
\title{Generate folder structure for parallel SWAT execution}
\title{Generate folder structure for SWAT execution}
\usage{
build_model_run(project_path)
}
......@@ -10,5 +10,6 @@ build_model_run(project_path)
\item{project_path}{Path to the SWAT project folder (i.e. TxtInOut)}
}
\description{
Generate folder structure for parallel SWAT execution
Generate folder structure for SWAT execution
}
\keyword{internal}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plot_hru_pw.R
\name{get_hru_id_by_attribute}
\alias{get_hru_id_by_attribute}
\title{Get HRU ids by filtering for HRU attributes}
\usage{
get_hru_id_by_attribute(sim_verify, lum = NULL, mgt = NULL, soil = NULL)
}
\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 = 'mgt'} must
be set in \code{run_swat_verification()}}
\item{lum}{Optional character vector with landuse.lum labels}
\item{mgt}{Optional character vector with management labels as defined in management.sch.}
\item{soil}{Optional character vector with soil type labels as defined in the soil data.}
}
\value{
Returns a table with hru ids and attributes.
}
\description{
get_hru_id_by_attribute Uses the hru-data and landuse_lum saved after running
\code{run_swat_verification()} and filters the attributes by the defined
filter attributes.
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plot_hru_pw.R
\name{plot_hru_pw_day}
\alias{plot_hru_pw_day}
\title{Plot daily simulated variables which are saved in hru_pw_day}
\usage{
plot_hru_pw_day(sim_verify, hru_id, var, years = 1900:2100)
}
\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 = 'mgt'} must
be set in \code{run_swat_verification()}}
\item{hru_id}{Numeric vector with HRU ids for which variables should be plotted}
\item{var}{Character vector that defines the variable names that are plotted}
\item{years}{Years of the simulated data for which varaibles are plotted.}
}
\value{
Returns a table with hru ids and attributes.
}
\description{
plot_hru_pw_day plots daily time series of variables from the output file hru_pw_day
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment