diff --git a/DESCRIPTION b/DESCRIPTION index f23359157ce67a7ae96f0c68bb3008a2adeb291d..251b0e146136931d7fedbacd24560a3443dd56d1 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 7cee7f6250c88389f5ab403463037d0bd3e9d2e7..615693495dd07e850dd776c42551302ebb3cbdaa 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 c47b288df6d77e565dd5c3466464c7701f17cc53..9487d505feeed880fc388bdf318311852da0fa52 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 77b00d440f4458ac0ac3eb8b280488e809b3f3ea..8b28bd2b2210680d421a4a5b6dc4f8103238a65e 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 5f251e5911889eae1c2d1dcf7b3c6cfc7dae2d23..fc32375b45a59077db6e926a7158867c1fd1120e 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 72b1fafe6a5d075f6c73767ce878d846a2246c95..0d783ea561e7b2fc8a9a6f73e7d0f69781112692 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 12ba01b0b9eb67323f8fd062ec72be0c4113cfd4..1cbf9d9eff9dee9b380dc5776e3c921d212ab611 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 6ef853e69df1e3acb22932d7ea727a63f5a7265e..c9f0cb9f7dc0e733b394a2e8a59db690fe223872 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 0000000000000000000000000000000000000000..5a065b7fc9e0cbc439cf983fea8c033e52b02657 --- /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 084a2a128718e720558dfab908545df48f1df664..0d0acb1b9aefcb575dff1442c4d8fb1672674917 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 b97997a94b3d2dedcbd8f36282ccfa4809ac5de0..3286278e1663b84490fabd10b87bdd2daffa033e 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 f2aca66cc37608c6d9d4137f10c10b48f7da677e..351470a3f42efde2d6d32475bf51d2eb33ffbd56 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 84fcbad1e8edb73b33c459abad31b7a5191664f9..3c42a5d5e8e5d257f68e37dd4a084cf51ec3678e 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