Skip to content

Commit

Permalink
Merge pull request #15 from KWB-R/dev
Browse files Browse the repository at this point in the history
LID scenarios
  • Loading branch information
mrustl authored Jul 7, 2021
2 parents e9307ef + 2d7f1a9 commit 1823e00
Show file tree
Hide file tree
Showing 26 changed files with 1,337 additions and 18 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
^codecov\.yml$
^index\.md$
^README\.md$
^data-raw$
19 changes: 17 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,28 @@ Description: R Package for Simulating the Impact of Different
License: MIT + file LICENSE
URL: https://github.com/KWB-R/keys.lid
BugReports: https://github.com/KWB-R/keys.lid/issues
Depends:
R (>= 2.10)
Imports:
dplyr,
ggplot2,
lubridate,
kwb.event,
kwb.swmm,
kwb.utils,
plotly,
scales,
stringr,
swmmr,
readr,
readxl,
rlang,
tibble,
tidyr,
tidyselect,
xts,
zoo
zoo,
magrittr
Suggests:
car,
covr,
Expand All @@ -44,8 +57,10 @@ VignetteBuilder:
knitr
Remotes:
github::kwb-r/kwb.event,
github::kwb-r/kwb.swmm
github::kwb-r/kwb.swmm,
github::kwb-r/kwb.utils
Encoding: UTF-8
LazyData: true
LazyDataCompression: xz
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
42 changes: 42 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,22 +1,64 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(boxplot_runoff_max)
export(boxplot_runoff_volume)
export(boxplot_vrr)
export(computeVol)
export(extdata_file)
export(lidconfig_to_swmm)
export(makeRainfallRunoffEvents)
export(monthlyPattern)
export(plot_vrr_median)
export(readObservations)
export(readPredictions)
export(read_scenarios)
export(simulate_performance)
export(simulate_performances)
import(ggplot2)
importFrom(dplyr,any_vars)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_rows)
importFrom(dplyr,desc)
importFrom(dplyr,filter_at)
importFrom(dplyr,group_by)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,select)
importFrom(dplyr,summarise)
importFrom(dplyr,vars)
importFrom(kwb.event,getEvents)
importFrom(kwb.swmm,calculate_rainevent_stats)
importFrom(kwb.swmm,extdata_file)
importFrom(kwb.swmm,get_results)
importFrom(kwb.utils,catAndRun)
importFrom(kwb.utils,resolve)
importFrom(lubridate,year)
importFrom(magrittr,"%>%")
importFrom(plotly,ggplotly)
importFrom(plotly,layout)
importFrom(plotly,plot_ly)
importFrom(readr,cols)
importFrom(readr,read_csv)
importFrom(readxl,excel_sheets)
importFrom(readxl,read_xlsx)
importFrom(rlang,.data)
importFrom(stats,aggregate)
importFrom(stats,approx)
importFrom(stats,median)
importFrom(stats,setNames)
importFrom(stringr,str_replace)
importFrom(stringr,str_to_upper)
importFrom(swmmr,read_inp)
importFrom(swmmr,read_out)
importFrom(swmmr,run_swmm)
importFrom(swmmr,write_inp)
importFrom(tibble,tibble)
importFrom(tidyr,pivot_longer)
importFrom(tidyr,pivot_wider)
importFrom(tidyr,unnest)
importFrom(tidyselect,all_of)
importFrom(tidyselect,starts_with)
importFrom(utils,read.table)
importFrom(xts,tzone)
importFrom(zoo,coredata)
Expand Down
56 changes: 56 additions & 0 deletions R/lidconfig_to_swmm.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' Convert LID config to SWMM LID controls
#' @param df data frame for a single scenario of a LID (as returned by
#' \code{\link{read_scenarios}})
#' @return data frame with SWMM LID controls
#' @export
#' @importFrom readr cols read_csv
#' @importFrom kwb.swmm extdata_file
#' @importFrom dplyr any_vars bind_rows filter_at left_join mutate select vars
#' @importFrom tidyr pivot_wider
#' @importFrom rlang .data
#' @importFrom tidyselect all_of starts_with
#' @importFrom stringr str_to_upper
#' @examples
#' scenarios <- keys.lid::read_scenarios()
#' unique(scenarios$lid_name_tidy)
#' lid <- "permeable_pavement"
#' lid_selected <- scenarios %>% dplyr::filter(.data$lid_name_tidy == lid)
#' scenario_names <- unique(lid_selected$scenario_name)
#' scenario_name <- scenario_names[1]
#' scenario_name
#' lid_selected_scenario <- lid_selected[lid_selected$scenario_name == scenario_name,]
#' lid_controls <- lidconfig_to_swmm(lid_selected_scenario)
#' str(lid_controls)
lidconfig_to_swmm <- function(df) {

lid_para <- readr::read_csv(kwb.swmm::extdata_file("lid/required_parameteristion.csv"),
col_types = readr::cols(.default = "c"))

lid_parametersation <- df %>%
dplyr::filter(!is.na(.data$value)) %>%
dplyr::filter(!is.na(.data$id_type_parameter)) %>%
dplyr::select(tidyselect::all_of(c("lid_name_tidy", "type", "id_type_parameter", "scenario_name", "value"))) %>%
dplyr::left_join(lid_para %>%
dplyr::select(.data$lid_id, .data$lid_name_tidy), by = "lid_name_tidy") %>%
dplyr::mutate("Name" = sprintf("%s.%s", .data$lid_name_tidy, .data$scenario_name),
"Type/Layer" = stringr::str_to_upper(.data$type),
) %>%
dplyr::filter(!is.na(.data$value)) %>%
dplyr::select(tidyselect::all_of(c("Name", "Type/Layer", "id_type_parameter", "value"))) %>%
tidyr::pivot_wider(names_from = "id_type_parameter",
names_prefix = "Par",
values_from = "value") %>%
dplyr::filter_at(dplyr::vars(tidyselect::starts_with("Par")), dplyr::any_vars(!is.na(.data)))

## dont know why 5 is needed by SWMM (but generated in SWMM GUI)
lid_parametersation[lid_parametersation$`Type/Layer` == "SURFACE", "Par5"] <- 5

lid_id <- lid_para$lid_id[lid_para$lid_name_tidy == unique(df$lid_name_tidy)]

lid_header <- lid_parametersation[1,]
lid_header[1,3:ncol(lid_header)] <- NA_real_
lid_header$`Type/Layer` <- lid_id

dplyr::bind_rows(lid_header, lid_parametersation)

}
25 changes: 25 additions & 0 deletions R/performances.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#' Performance results for LIDs
#'
#' A dataset containing the performance of LIDs for different climate conditions
#' created with R script in /data-raw/performances.R
#'
#' @format A nested tibble with 290 rows and 16 variables:
#' \describe{
#' \item{zone_id}{climate zone id}
#' \item{lid_name_tidy}{tidy LID name}
#' \item{scenario_name}{name of LID scenario}
#' \item{catchment_area_m2}{catchment area in squaremeters}
#' \item{lid_area_fraction}{fraction of LID compared to total catchment}
#' \item{lid_area_m2}{total LID area}
#' \item{lid_usage}{tibble with LID usage parameterisation}
#' \item{lid_controls}{tibble with LID controls parameterisation}
#' \item{subcatchment}{tibble with subcatchment parameterisation}
#' \item{annual}{tibble with two columns "year" and "vrr" (volume rainfall retended for each year}
#' \item{events_max}{tibble with maximum values for each rainfall event}
#' \item{events_sum}{tibble with sum values for each rainfall event}
#' \item{col_eventsep}{name of SWMM results used for event separation}
#' \item{model_inp}{path to SWMM model input file}
#' \item{model_rpt}{path to SWMM model report file}
#' \item{model_out}{path to SWMM model output file}
#' }
"performances"
Loading

0 comments on commit 1823e00

Please sign in to comment.