From 87f62ccbccde096c7878875476aa60ce7285f6d3 Mon Sep 17 00:00:00 2001 From: biopsichas <svajunas.plunge@gmail.com> Date: Thu, 9 Feb 2023 16:14:16 +0200 Subject: [PATCH] added function plot water partition --- DESCRIPTION | 3 +- NAMESPACE | 8 ++++ R/globals.R | 3 +- R/plot_hru_pw.R | 91 +++++++++++++++++++++++++++++++++++++ R/run_soft_cal.R | 3 +- man/build_model_run.Rd | 2 +- man/find_swat_exe.Rd | 2 +- man/get_os.Rd | 2 +- man/plot_water_partition.Rd | 43 ++++++++++++++++++ man/run_os.Rd | 2 +- man/set_codes_bsn.Rd | 2 +- man/set_print_prt.Rd | 2 +- man/set_time_sim.Rd | 2 +- 13 files changed, 155 insertions(+), 10 deletions(-) create mode 100644 man/plot_water_partition.Rd diff --git a/DESCRIPTION b/DESCRIPTION index f233591..251b0e1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,7 +23,8 @@ Imports: tibble, tidyr, tidyselect, - plotly + plotly, + utils Suggests: datasets, stats diff --git a/NAMESPACE b/NAMESPACE index 7cee7f6..6156934 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ export(plot_hru_var) export(plot_hru_var_aa) export(plot_monthly_snow) export(plot_variable_at_harvkill) +export(plot_water_partition) export(print_avannual_qtile) export(print_triggered_mgt) export(report_mgt) @@ -27,12 +28,17 @@ importFrom(dplyr,lead) importFrom(dplyr,left_join) importFrom(dplyr,mutate) importFrom(dplyr,mutate_all) +importFrom(dplyr,mutate_at) importFrom(dplyr,n) importFrom(dplyr,rename) +importFrom(dplyr,row_number) importFrom(dplyr,select) importFrom(dplyr,slice) importFrom(dplyr,slice_sample) importFrom(dplyr,summarise) +importFrom(dplyr,summarise_all) +importFrom(dplyr,summarize) +importFrom(dplyr,tibble) importFrom(dplyr,ungroup) importFrom(ggplot2,aes) importFrom(ggplot2,geom_boxplot) @@ -51,6 +57,7 @@ importFrom(lubridate,month) importFrom(lubridate,yday) importFrom(lubridate,year) importFrom(lubridate,ymd) +importFrom(plotly,add_pie) importFrom(plotly,layout) importFrom(plotly,plot_ly) importFrom(plotly,plotly_build) @@ -76,3 +83,4 @@ importFrom(tibble,tibble) importFrom(tidyr,pivot_longer) importFrom(tidyr,unite) importFrom(tidyselect,all_of) +importFrom(utils,download.file) diff --git a/R/globals.R b/R/globals.R index c47b288..9487d50 100644 --- a/R/globals.R +++ b/R/globals.R @@ -5,4 +5,5 @@ utils::globalVariables(c("%&&%", "%//%", ".", "crop", "day", "ecanopy", "eplant" "rhum rm_skp", "schedule", "snofall", "snomlt", "soil", "solarad", "starts_with", "tile", "time_out", "tmn", "tmpav", "tmx", "topo", "val_max", "val_mean", "val_min", "value", "value_sum", "var", "var1", "var2", "var3", "var4", "var5", - "wndspd", "yr", "rhum", "rm_skp", "Date", "Values")) + "wndspd", "yr", "rhum", "rm_skp", "Date", "Values", "surq_gen", "latq", "perc", + "description")) diff --git a/R/plot_hru_pw.R b/R/plot_hru_pw.R index 77b00d4..8b28bd2 100644 --- a/R/plot_hru_pw.R +++ b/R/plot_hru_pw.R @@ -186,3 +186,94 @@ plot_hru_var_aa <- function(sim_verify, lum = NULL, mgt = NULL, soil = NULL){ return(fig) } +#' Plot simulated variables of water partition of filtered HRUs saved in hru_wb_aa +#' +#' @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 tile Optional Boolean TRUE for selecting HRUs with working tiles, FALSE - without working tiles and NULL for selecting all HRUs. +#' @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. +#' @param exclude_lum Character vector to define land uses which are excluded +#' in the printed table. +#' @return plotly figure object +#' @importFrom dplyr %>% mutate group_by rename left_join summarise_all filter select +#' @importFrom tidyr pivot_longer +#' @importFrom plotly plot_ly layout subplot add_pie +#' @export +#' +#' @examples +#' \dontrun{ +#' plot_water_partition(sim_nostress, tile = TRUE) +#' } + +plot_water_partition <- function(sim_verify, tile = TRUE, lum = NULL, mgt = NULL, soil = NULL, exclude_lum = c( + "urhd_lum", "urmd_lum", "urml_lum", + "urld_lum", "ucom_lum", "uidu_lum", + "utrn_lum", "uins_lum", "urbn_lum")){ + df <- sim_verify$hru_wb_aa %>% + rename(id = unit) %>% + left_join(., sim_verify$lum_mgt, by = "id") %>% + filter(!lu_mgt %in% exclude_lum) %>% + select(id, et, surq_gen, latq, perc, qtile, lu_mgt, mgt, soil) + ##Filtering for selected tile, lum, mgt and soil options + if(!is.null(lum)) { + df <- df[df$lu_mgt %in% lum,] + } + if(!is.null(mgt)) { + df <- df[df$mgt %in% mgt,] + } + if(!is.null(soil)) { + df <- df[df$soil %in% soil,] + } + if(!is.null(tile)) { + if(tile == TRUE){ + df <- df[df$qtile > 0,] + } else if(tile == FALSE){ + df <- df[df$qtile == 0,] + } else { + stop("Wrong input!!! Valid 'tile' parameter could be only TRUE, FALSE or Null.") + } + } + ##Selecting only required variables + df <- df[c('et', 'surq_gen', 'latq', 'perc', 'qtile')] %>% + mutate(units = "mm") %>% + pivot_longer(!units, names_to = "var", values_to = "Values") + ##Setting colors for variables + pal <- c("blue", "green", "brown", "grey", "black") + df$var <- factor(df$var, levels = c("et", "surq_gen", "latq", "qtile", "perc")) + ##Preparing pie chart + pie_pl <- plot_ly(df[c("var", "Values")] %>% + group_by(var) %>% + summarise_all(mean) %>% + mutate(Values = round(Values, 1), + pal = factor(var, labels = pal)), + values=~Values, labels = ~var, + hoverinfo = 'text', + textinfo = 'percent', + text = ~paste(var, Values, "mm/year"), + marker = list(colors = pal, + line = list(color = '#FFFFFF', width = 1)), + domain = list(x = c(0.6, 0.9), + y = c(0.0, 1)), + showlegend = F) %>% + add_pie(hole = 0.3) + ##Preparing box plot + box_pl <- plot_ly(df[c("var", "Values")], x=~Values, color = ~var, type = "box", colors = pal, + showlegend = F) %>% + layout(yaxis = list(autorange = "reversed")) + ##Putting into one figure and annotations + fig <- subplot(box_pl, pie_pl, nrows = 1, margin = 0.05) %>% + layout(title = paste("Selected HRUs |", + if(!is.null(tile)){paste0("tile drains ", tile, ",")}, + if(!is.null(lum)){paste0("lum - ", lum, ",")}, + if(!is.null(mgt)){paste0("mgt - ", mgt, ",")}, + if(!is.null(soil)){paste0("soil - ", soil)},"|"), + annotations = list( + list(x = 0.1 , y = 1, text = "a) Box plot for selected HRU's (mm/year)", showarrow = F, xref='paper', yref='paper'), + list(x = 0.8 , y = 1, text = "b) Mean results for selected HRU's", showarrow = F, xref='paper', yref='paper')) + ) + options(warn = -1) + return(fig) +} diff --git a/R/run_soft_cal.R b/R/run_soft_cal.R index 5f251e5..fc32375 100644 --- a/R/run_soft_cal.R +++ b/R/run_soft_cal.R @@ -165,6 +165,7 @@ build_wb_softcal_input <- function(run_path, wateryield_ratio, baseflow_ratio) { #' @param path text string to (temporary) directory #' @keywords internal #' @importFrom dplyr %>% +#' @importFrom utils download.file #' download_sft_files <- function(path) { # "water_balance.sft" currently does not exist on bitbucket but needs to be downloaded!! @@ -293,7 +294,7 @@ toggle_sft <- function(path, switch) { #' @keywords internal #' @importFrom data.table fread #' @importFrom tidyr unite -#' @importFrom dplyr %>% slice filter mutate_all tibble +#' @importFrom dplyr %>% slice filter mutate_all tibble row_number mutate_at #' #' @return returns a tibble of the formatted output of the soft-cal routine #' diff --git a/man/build_model_run.Rd b/man/build_model_run.Rd index 72b1faf..0d783ea 100644 --- a/man/build_model_run.Rd +++ b/man/build_model_run.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/run_swat_verify.R +% Please edit documentation in R/update_input_files.R \name{build_model_run} \alias{build_model_run} \title{Generate folder structure for SWAT execution} diff --git a/man/find_swat_exe.Rd b/man/find_swat_exe.Rd index 12ba01b..1cbf9d9 100644 --- a/man/find_swat_exe.Rd +++ b/man/find_swat_exe.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/run_swat_verify.R +% Please edit documentation in R/utils.R \name{find_swat_exe} \alias{find_swat_exe} \title{Find the SWAT+ executable file and trigger error if 0 or more than 1 diff --git a/man/get_os.Rd b/man/get_os.Rd index 6ef853e..c9f0cb9 100644 --- a/man/get_os.Rd +++ b/man/get_os.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/run_swat_verify.R +% Please edit documentation in R/utils.R \name{get_os} \alias{get_os} \title{Identify the operating system. diff --git a/man/plot_water_partition.Rd b/man/plot_water_partition.Rd new file mode 100644 index 0000000..5a065b7 --- /dev/null +++ b/man/plot_water_partition.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot_hru_pw.R +\name{plot_water_partition} +\alias{plot_water_partition} +\title{Plot simulated variables of water partition of filtered HRUs saved in hru_wb_aa} +\usage{ +plot_water_partition( + sim_verify, + tile = TRUE, + lum = NULL, + mgt = NULL, + soil = NULL, + 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{tile}{Optional Boolean TRUE for selecting HRUs with working tiles, FALSE - without working tiles and NULL for selecting all HRUs.} + +\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.} + +\item{exclude_lum}{Character vector to define land uses which are excluded +in the printed table.} +} +\value{ +plotly figure object +} +\description{ +Plot simulated variables of water partition of filtered HRUs saved in hru_wb_aa +} +\examples{ +\dontrun{ +plot_water_partition(sim_nostress, tile = TRUE) +} +} diff --git a/man/run_os.Rd b/man/run_os.Rd index 084a2a1..0d0acb1 100644 --- a/man/run_os.Rd +++ b/man/run_os.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/run_swat_verify.R +% Please edit documentation in R/utils.R \name{run_os} \alias{run_os} \title{Add './' to run the exe on unix systems} diff --git a/man/set_codes_bsn.Rd b/man/set_codes_bsn.Rd index b97997a..3286278 100644 --- a/man/set_codes_bsn.Rd +++ b/man/set_codes_bsn.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/run_swat_verify.R +% Please edit documentation in R/update_input_files.R \name{set_codes_bsn} \alias{set_codes_bsn} \title{Set the nostress value in the code.bsn file} diff --git a/man/set_print_prt.Rd b/man/set_print_prt.Rd index f2aca66..351470a 100644 --- a/man/set_print_prt.Rd +++ b/man/set_print_prt.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/run_swat_verify.R +% Please edit documentation in R/update_input_files.R \name{set_print_prt} \alias{set_print_prt} \title{Read and set SWAT+ print.prt file write the updated print.prt and the original file diff --git a/man/set_time_sim.Rd b/man/set_time_sim.Rd index 84fcbad..3c42a5d 100644 --- a/man/set_time_sim.Rd +++ b/man/set_time_sim.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/run_swat_verify.R +% Please edit documentation in R/update_input_files.R \name{set_time_sim} \alias{set_time_sim} \title{Read and set SWAT+ time.sim file write the updated time.sim and the original file -- GitLab