-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxs_calcs.R
56 lines (51 loc) · 1.92 KB
/
xs_calcs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
library(tidyverse)
prep_xs <- function(data, sta, elev, delta_x=0.1) {
data %>%
arrange({{sta}}) %>%
mutate(sta = {{sta}}) %>%
complete(sta = seq(from = min({{sta}}), to = max({{sta}}), by = delta_x)) %>%
mutate(gse = zoo::na.approx({{elev}}, x = sta)) %>%
select(c(sta, gse))
}
calc_xs <- function(data, water_elev) {
data %>%
arrange(sta) %>%
mutate(wse = case_when(water_elev > gse ~ water_elev),
delta_x = abs(lag(sta, 1) - sta),
delta_z = abs(lag(gse, 1) - gse),
depth = wse - gse,
hyp_length = sqrt(delta_x^2 + delta_z^2)
) %>%
filter(!is.na(wse)) %>%
summarize(thalweg_elevation = min(gse),
water_surface_elevation = water_elev,
max_depth = water_surface_elevation - thalweg_elevation,
cross_sectional_area = sum(delta_x * depth),
wetted_perimeter = sum(hyp_length),
) %>%
as.list() %>%
list_flatten()
}
plot_xs <- function(data, water_elev) {
data %>%
arrange(sta) %>%
mutate(wse = case_when(water_elev > gse ~ water_elev)) %>%
ggplot(aes(x = sta)) +
geom_line(aes(y = gse)) +
geom_line(aes(y = wse))
}
solve_xs <- function(data, discharge, slope, mannings_n, delta_y=0.1) {
depth_vs_wse <- seq(from=min(data$gse)+delta_y, to=max(data$gse), by=delta_y) %>%
as_tibble() %>%
mutate(result = map(value, function(x){calc_xs(data = data, water_elev = x)})) %>%
unnest_wider(col = result) %>%
drop_na()
wse_vs_discharge <- depth_vs_wse %>%
mutate(discharge_cfs = 1.486 * cross_sectional_area * (cross_sectional_area / wetted_perimeter)^(2/3) * slope^(1/2) * mannings_n^(-1)) %>%
bind_rows(tribble(~selected_water_level, ~discharge_cfs, TRUE, discharge)) %>%
arrange(discharge_cfs)
wse_vs_discharge %>%
mutate(output_wse = zoo::na.approx(water_surface_elevation)) %>%
filter(selected_water_level) %>%
pull(output_wse)
}