Skip to content

Commit 906b15e

Browse files
authored
Merge pull request #32 from KWB-R/rescale_and_distribute
Rescale and distribute
2 parents 433b64d + 9154254 commit 906b15e

18 files changed

Lines changed: 791 additions & 103 deletions

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ RoxygenNote: 7.3.1
1919
Suggests:
2020
covr,
2121
kwb.rect,
22+
plumber,
2223
testthat (>= 3.0.0)
2324
Imports:
2425
dplyr,

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export(prepare_berlin_inputs)
2121
export(read_column_info)
2222
export(real_evapo_transpiration)
2323
export(run_rabimo)
24+
export(run_rabimo_with_measures)
25+
export(test_api)
2426
export(yearly_height_to_volume_flow)
2527
importFrom(dplyr,left_join)
2628
importFrom(kwb.utils,catAndRun)

R/distribute_measures.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ get_swale_connection_table <- function(unpaved_area_table, target, total_area)
162162
sealed_areas <- select_columns(unpaved_area_table, "sealed_area")
163163
corrected_scas <- select_columns(unpaved_area_table, "corr_sca")
164164
shares <- select_columns(unpaved_area_table, "corr_sca_frac")
165-
to_swale_areas <- select_columns(unpaved_area_table, "corr_sca")
165+
to_swale_areas <- select_columns(unpaved_area_table, "corr_sca") #???
166+
zeros <- rep(0, nrow(unpaved_area_table))
166167

167168
sealed_mean <- sum(sealed_areas) / total_area
168169

@@ -177,14 +178,19 @@ get_swale_connection_table <- function(unpaved_area_table, target, total_area)
177178
shares > target
178179
}
179180

180-
ref_areas <- rep(0, length(sealed_areas))
181+
ref_areas <- zeros
182+
deltas <- zeros
183+
181184
ref_areas[consider] <- if (to_increase) {
182185
sealed_areas[consider] - corrected_scas[consider]
183186
} else {
184187
to_swale_areas[consider]
185188
}
186189

187-
deltas <- get_weight(ref_areas) * total_diff_area
190+
#deltas <- get_weight(ref_areas) * total_diff_area
191+
if ((tot_area <- sum(ref_areas)) > 0) {
192+
deltas <- ref_areas / tot_area * total_diff_area
193+
}
188194

189195
to_swale_areas <- corrected_scas + deltas
190196

R/distribute_shares.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ distribute_shares <- function(
1818
length(target_value) == 1L
1919
)
2020

21-
total_diff_area <- sum(target_value * base_areas) - sum(partial_areas)
21+
total_diff_area <- round(sum(target_value * base_areas) - sum(partial_areas),2)
2222

2323
to_increase <- total_diff_area > 0
2424
shares <- ifelse(base_areas > 0, partial_areas / base_areas, 0)
2525

2626
consider <- if (to_increase) {
27-
shares < target_value
27+
round(shares,2) < round(target_value,2)
2828
} else {
29-
shares > target_value
29+
round(shares,2) > round(target_value,2)
3030
}
3131

3232
ref_areas <- rep(0, n_areas)

R/get_measure_stats.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ get_measure_stats <- function(blocks, reference_system = 2)
6060
}
6161

6262
list(
63-
green_roof = c(mean = mean_green_roof, max = max_green_roof),
64-
unpaved = c(mean = mean_unpvd, max = max_unpvd),
65-
to_swale = c(mean = mean_sca, max = max_sca)
63+
green_roof = list(mean = mean_green_roof, max = max_green_roof),
64+
unpaved = list(mean = mean_unpvd, max = max_unpvd),
65+
to_swale = list(mean = mean_sca, max = max_sca)
6666
)
6767
}
6868

R/rescale_target_values.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# rescale_target_values --------------------------------------------------------
2+
rescale_target_values <- function(new_targets, blocks)
3+
{
4+
total_area <- sum(get_main_area(blocks))
5+
total_roof_area <- sum(get_roof_area(blocks))
6+
total_sealed_area <- sum(get_sealed_area(blocks))
7+
8+
green_roof_new <- select_elements(new_targets, "green_roof")
9+
unpaved_new <- select_elements(new_targets, "unpaved")
10+
to_swale_new <- select_elements(new_targets, "to_swale")
11+
12+
green_roof <- green_roof_new * total_area / total_roof_area
13+
unpaved <- unpaved_new
14+
to_swale <- to_swale_new * total_area / total_sealed_area
15+
16+
max_allowed_unpaved <- (total_area - total_roof_area)/total_area
17+
18+
stopifnot(green_roof <= 1)
19+
stopifnot(unpaved <= max_allowed_unpaved)
20+
stopifnot(to_swale <= 1)
21+
22+
list(green_roof = green_roof, unpaved = unpaved, to_swale = to_swale)
23+
}

R/run_rabimo.R

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,22 @@ run_rabimo <- function(data, config, controls = define_controls())
256256
output_format <- control("output_format")
257257

258258
result_data <- if (output_format == "abimo") {
259+
259260
# Provide the same columns as Abimo does
260261
rename_columns(result_data_raw, name_mapping)
262+
261263
} else if (output_format == "rabimo") {
262-
remove_columns(result_data_raw, pattern = "_flow") %>%
263-
remove_columns("total_runoff") %>%
264-
move_columns_to_front(c("code", "total_area")) %>%
265-
dplyr::rename_with(~gsub("total_","",.))
264+
265+
data.frame(
266+
code = result_data_raw$code,
267+
area = result_data_raw$total_area,
268+
runoff = result_data_raw$total_surface_runoff,
269+
infiltr = result_data_raw$total_infiltration,
270+
evapor = result_data_raw$total_evaporation
271+
)
272+
266273
} else {
274+
267275
clean_stop("controls$output_format must be either 'abimo' or 'rabimo'.")
268276
}
269277

R/run_rabimo_with_measures.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#' Distribute Rainwater Management Measures and run R-Abimo
2+
#'
3+
#' @param blocks data frame of selected blocks (same columns as in
4+
#' \code{\link{rabimo_inputs_2020}$data})
5+
#' @param measures list with elements \code{green_roof}, \code{unpaved},
6+
#' \code{to_swale} representing the target percentages of the total areas
7+
#' corresponding to each measure
8+
#' @param config configuration object, default:
9+
#' \code{\link{rabimo_inputs_2020}$config}
10+
#' @export
11+
run_rabimo_with_measures <- function(
12+
blocks,
13+
measures,
14+
config = kwb.rabimo::rabimo_inputs_2020$config
15+
)
16+
{
17+
rescaled_targets <- rescale_target_values(
18+
new_targets = measures,
19+
blocks = blocks
20+
)
21+
22+
run_rabimo(
23+
distribute_measures(blocks = blocks, targets = rescaled_targets),
24+
config = config)
25+
}
26+
27+
# config_file = kwb.abimo::default_config()
28+
# config <- kwb.abimo:::read_config(file = safe_path(config_file))
29+
# new_config <- abimo_config_to_config(config, add_class_5 = TRUE)
30+
# config = prepare_config(new_config)

R/test_api.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#' Test the API to main functions using the plumber package and swagger
2+
#'
3+
#' @export
4+
test_api <- function()
5+
{
6+
plumber_file <- system.file("scripts/plumber.R", package = "kwb.rabimo")
7+
plumber::pr_run(plumber::pr(plumber_file))
8+
}

R/wabila.R

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
# calculate_wabila_roof --------------------------------------------------------
2-
calculate_wabila_roof <- function(area, retention_height)
3-
{
2+
calculate_wabila_roof <- function(area, retention_height, return_code = TRUE) {
43
# get climatic parameters from area
5-
P <- area$prec_yr
6-
ET_p <- area$epot_yr
4+
P <- area["prec_yr"]
5+
ET_p <- area["epot_yr"]
6+
code <- area["code"]
77

88
# runoff component
9-
a <- 0.9115 + 0.00007063 * P - 0.000007498 * ET_p - 0.2063 *
10-
log(retention_height + 1)
9+
a <- 0.9115 + 0.00007063 * P - 0.000007498 * ET_p - 0.2063 * log(retention_height + 1)
1110

1211
# infiltration component
13-
g <- 0
12+
g <- rep(0, length(P)) # if P is a vector, g will be a vector of zeros with the same length
1413

15-
#evaporation component
14+
# evaporation component
1615
v <- 1 - a
1716

18-
c(a, g, v)
17+
result <- setNames(c(a, g, v),
18+
c("surface_runoff", "infiltration", "evaporation"))
19+
20+
if (return_code){
21+
return(cbind(code, result))
22+
}
23+
24+
result
25+
1926
}
2027

2128
# calculate_wabila_green_roof --------------------------------------------------
22-
calculate_wabila_green_roof <- function(area, height, kf, w_diff)
29+
calculate_wabila_green_roof <- function(area, height, kf, w_diff, return_code = TRUE)
2330
{
2431
# h: depth, in cm
2532
# kf: permeability of soil, in mm/h
2633
# w_diff: difference between water holding capacity and wilting point, unitless
2734

2835
# get climatic parameters from area
29-
P <- area$prec_yr
30-
ET_p <- area$epot_yr
36+
P <- area["prec_yr"]
37+
ET_p <- area["epot_yr"]
38+
code <- area["code"]
3139

3240
# runoff component
3341
a = -2.182 + 0.4293 * log(P) - 0.0001092 * P + 236.1/ET_p +
@@ -40,7 +48,14 @@ calculate_wabila_green_roof <- function(area, height, kf, w_diff)
4048
#evaporation component
4149
v <- 1 - a
4250

43-
c(a, g, v)
51+
result <- setNames(c(a, g, v),
52+
c("surface_runoff", "infiltration", "evaporation"))
53+
54+
if (return_code){
55+
return(cbind(code, result))
56+
}
57+
58+
result
4459
}
4560

4661
# calculate_wabila_paved -------------------------------------------------------
@@ -131,7 +146,7 @@ estimate_swale_area <- function(kf)
131146

132147

133148
# calculate_delta_mod ----------------------------------------------------------
134-
calculate_delta_mod <- function(results_mod_1, results_mod_2,
149+
calculate_delta_mod_v0 <- function(results_mod_1, results_mod_2,
135150
has_codes = TRUE,
136151
var_names = c("surface_runoff",
137152
"infiltration",
@@ -171,3 +186,45 @@ calculate_delta_mod <- function(results_mod_1, results_mod_2,
171186
return(delta_mod)
172187

173188
}
189+
190+
calculate_delta_mod <- function(results_mod_1, results_mod_2,
191+
has_codes = TRUE,
192+
col_patterns = c("runoff|infiltration|evaporation"),
193+
codes_name = "code"){
194+
195+
# force result objects to be data.frames
196+
if(is.vector(results_mod_1)){
197+
results_mod_1 <- as.data.frame(t(results_mod_1))
198+
result_mod_2 <- as.data.frame(t(results_mod_2))
199+
} else {
200+
results_mod_1 <- as.data.frame(results_mod_1)
201+
results_mod_2 <- as.data.frame(results_mod_2)
202+
}
203+
204+
stopifnot(nrow(results_mod_1) == nrow(results_mod_2))
205+
206+
var_names_1 <- grep(pattern = col_patterns, x = names(results_mod_1), value = TRUE)
207+
var_names_2 <- grep(pattern = col_patterns, x = names(results_mod_2), value = TRUE)
208+
209+
precipitation_1 <- rowSums(results_mod_1[var_names_1])
210+
precipitation_2 <- rowSums(results_mod_2[var_names_2])
211+
tolerance <- 1e-6
212+
stopifnot(all(abs(precipitation_1 - precipitation_2) < tolerance))
213+
214+
delta_mod <- data.frame(
215+
delta_mod = rowSums(
216+
abs(results_mod_1[var_names_1] - results_mod_2[var_names_2])
217+
) * 0.5 / precipitation_1
218+
)
219+
220+
if(has_codes){
221+
stopifnot(
222+
identical(results_mod_1[[codes_name]], results_mod_2[[codes_name]])
223+
)
224+
codes <- results_mod_1[[codes_name]]
225+
return(cbind(code = codes, delta_mod = delta_mod))
226+
}
227+
228+
delta_mod
229+
230+
}

0 commit comments

Comments
 (0)