Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rfaR
Title: RMC Reservoir Frequency Analysis in R
Version: 0.5.1
Version: 0.9.0
Authors@R: c(
person("Daniel", "McGraw", , "daniel.e.mcgraw@usace.army.mil",
role = c("aut", "cre"),
Expand All @@ -26,29 +26,29 @@ Authors@R: c(
role = "rev", comment = "P.E., USACE RMC"),
person("Bryan", "Robinson", , "bryan.j.robinson@usace.army.mil",
role = "rev", comment = "P.E., USACE RMC"))
Description: An R implementation of the U.S. Army Corps of Engineers (USACE)
Risk Management Center-Reservoir Frequency Analysis (RMC-RFA) methodology/software.
This package produces reservoir stage-frequency curves with uncertainty bounds
by combining deterministic flood routing (Modified Puls method) with a nested
Monte Carlo framework. The outer loop quantifies knowledge uncertainty in inflow
volume frequency distributions, while the inner loop simulates natural variability
across thousands of flood events. Uncertain variables include inflow volume,
flood hydrograph shape, seasonal occurrence, and antecedent reservoir stage.
The resulting stage-frequency curves provide critical hydrologic hazard
assessment data for dam safety risk analyses, enabling evaluation of loading
parameters (stage, discharge, duration) and their associated annual exceedance
Description: An R implementation of the U.S. Army Corps of Engineers (USACE)
Risk Management Center-Reservoir Frequency Analysis (RMC-RFA) methodology/software.
This package produces reservoir stage-frequency curves with uncertainty bounds
by combining deterministic flood routing (Modified Puls method) with a nested
Monte Carlo framework. The outer loop quantifies knowledge uncertainty in inflow
volume frequency distributions, while the inner loop simulates natural variability
across thousands of flood events. Uncertain variables include inflow volume,
flood hydrograph shape, seasonal occurrence, and antecedent reservoir stage.
The resulting stage-frequency curves provide critical hydrologic hazard
assessment data for dam safety risk analyses, enabling evaluation of loading
parameters (stage, discharge, duration) and their associated annual exceedance
probabilities.
Nickname: Friday
Nickname: Friday After Next
License: file LICENSE
URL: https://github.com/USACE-RMC/rfaR, https://usace-rmc.github.io/rfaR/
BugReports: https://github.com/USACE-RMC/rfaR/issues
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.3
Depends:
Depends:
R (>= 4.1)
LazyData: true
Suggests:
Suggests:
gt,
knitr,
rmarkdown,
Expand All @@ -60,7 +60,7 @@ Suggests:
patchwork,
ggsci
VignetteBuilder: knitr
Imports:
Imports:
cli,
future,
future.apply,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export(qp3)
export(rejection_sampling_stage)
export(rfa_simulate)
export(scale_hydrograph)
export(scale_x_aep)
export(stage2aep)
export(stratified_sampler)
export(theme_rfar_conceptual)
export(unreg_reg_transform)
importFrom(stats,approx)
importFrom(stats,approxfun)
importFrom(stats,pnorm)
Expand Down
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# rfaR 0.9.0

Documentation will statisfy quality assurance for v1.0. No major functional changes expected between 0.9.0 and 1.0.

I want to personally thank any users for their on-going patience for documetation. We're close. -Dan

## New features

* Added `unreg_reg_transform()` to create an unregulated-to-regulated
inflow transformation function using linear interpolation.

* `rfa_simulate()` now accepts an optional `unreg_reg_transform` function
to transform sampled volumes before hydrograph scaling.

# rfaR 0.5.1
## Summary

Expand Down
86 changes: 80 additions & 6 deletions R/plotting_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,85 @@
#' subtitle = "Demonstrating theme_rfar_conceptual()",
#' x = "AEP", y = "Stage (ft)") +
#' theme_rfar_conceptual()
theme_rfar_conceptual <- function(){
theme_rfar_conceptual <- function() {
ggplot2::theme_bw() +
ggplot2::theme(legend.position = "none",
plot.title = ggplot2::element_text(size = 11, face = "bold.italic"),
plot.subtitle = ggplot2::element_text(size = 8, face = "italic"),
axis.title = ggplot2::element_text(size = 9, face = "bold"),
axis.text = ggplot2::element_text(size = 8))
ggplot2::theme(
legend.position = "none",
plot.title = ggplot2::element_text(size = 11, face = "bold.italic"),
plot.subtitle = ggplot2::element_text(size = 8, face = "italic"),
axis.title = ggplot2::element_text(size = 9, face = "bold"),
axis.text = ggplot2::element_text(size = 8)
)
}

#' AEP-Labelled Normal-Variate X Scale
#'
#' Creates a continuous \pkg{ggplot2} x-axis positioned on the standard-normal
#' variate scale while displaying labels as annual exceedance probabilities
#' (AEPs). This is useful for stage-frequency, flow-frequency, and similar
#' conceptual plots where values are modeled or transformed in normal-variate
#' space but should be interpreted as probabilities.
#'
#' Major breaks are specified as AEP values and transformed using
#' \code{qnorm(1 - AEP)}. Minor breaks are generated from the supplied
#' multipliers across AEP decades from \eqn{10^{-1}} through \eqn{10^{-10}}.
#'
#' @param aep_breaks Numeric vector of annual exceedance probabilities to use
#' as major axis breaks. Values must be greater than 0 and less than 1.
#' Defaults range from 0.99 through \eqn{10^{-10}}.
#' @param minor_aep Numeric vector of multipliers used to generate minor AEP
#' breaks within each decade. The default, \code{2:9}, produces minor breaks
#' such as 0.2 through 0.9, 0.02 through 0.09, and so on.
#'
#' @return A \pkg{ggplot2} continuous x-scale object that can be added to a
#' \code{ggplot} via \code{+}.
#'
#' @export
#'
#' @examples
#' library(ggplot2)
#'
#' example_data <- data.frame(
#' AEP = c(0.99, 0.5, 0.1, 0.01, 0.001),
#' Stage = c(10.2, 11.5, 14.0, 17.3, 20.1)
#' )
#'
#' ggplot(example_data, aes(x = qnorm(1 - AEP), y = Stage)) +
#' geom_line() +
#' scale_x_aep() +
#' labs(
#' title = "Example Stage-Frequency Curve",
#' x = "Annual Exceedance Probability",
#' y = "Stage (ft)"
#' )
scale_x_aep <- function(
aep_breaks = c(
9.9e-1,
9e-1,
5e-1,
1e-1,
1e-2,
1e-3,
1e-4,
1e-5,
1e-6,
1e-7,
1e-8,
1e-9,
1e-10
),
minor_aep = 2:9
) {
minor_aep_breaks <- unlist(
lapply(minor_aep, function(i) i * 10^-(1:10))
)

z_breaks <- stats::qnorm(1 - aep_breaks)
z_breaks_minor <- stats::qnorm(1 - minor_aep_breaks)

ggplot2::scale_x_continuous(
breaks = z_breaks,
minor_breaks = z_breaks_minor,
labels = aep_breaks
)
}
2 changes: 1 addition & 1 deletion R/rejection_sampling.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pmf_stage_lognormal <- function(
#'
#' @examples
#'
#' pmf_ln <- pmf_stage_lognormal(pmf_shift = 239, pmf_mean = 241.9, pmf_sigma = 0.5)
#' pmf_ln <- pmf_stage_lognormal(pmf_shift = 1651, pmf_mean = 1663, pmf_sigma = 0.5)
#' result <- rejection_sampling_stage(
#' pmf_stage_LN = pmf_ln,
#' stage_freq_df = ex_rs_stage_loading,
Expand Down
41 changes: 36 additions & 5 deletions R/rfa_simulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
#' sampling probabilities.
#' @param resmodel Data frame with three columns: elevation (ft), storage (acre-ft),
#' and discharge (cfs). Defines the reservoir model for Modified Puls routing.
#' @param unreg_reg_transform Optional function that transforms sampled
#' unregulated inflow volume to corresponding regulated inflow volumes before
#' hydrograph scaling. Typically created with [unreg_reg_transform()].
#' If `NULL` (default), sampled volumes are used without transformation.
#' @param Nbins Integer. Number of stratified sampling bins in EV1 space.
#' Default is 50.
#' @param events_per_bin Integer. Number of events sampled per bin. Default is 200.
Expand Down Expand Up @@ -194,6 +198,7 @@ rfa_simulate <- function(
seasonality,
hydrographs,
resmodel,
unreg_reg_transform = NULL,
Nbins = 50,
events_per_bin = 200,
routing_dt = 1,
Expand Down Expand Up @@ -236,6 +241,14 @@ rfa_simulate <- function(
# Remove rows with missing stages in stage timeseries
stage_ts <- stage_ts[!is.na(stage_ts$stage), ]

# If Unregulated-Regulated Transform Function is supplied, then transform volume samples
regulated_transform <- !is.null(unreg_reg_transform)
if (regulated_transform) {
cli::cli_alert_info(
"Applying unregulated-to-regulated transformation to sampled inflow volumes."
)
}

# ============================================================================
# MEDIAN ONLY ====
# ============================================================================
Expand Down Expand Up @@ -348,11 +361,17 @@ rfa_simulate <- function(
hydrograph_shape <- hydrographs[[hydroSamps[realiz]]][, 2:3]
# Hydrograph observed volume
obs_hydrograph_vol <- attr(hydrographs[[hydroSamps[realiz]]], "obs_vol")
# Select the target sample volume for hydrograph scaling
sample_volume <- if (regulated_transform) {
unreg_reg_transform(Q_matrix[i, j])
} else {
Q_matrix[i, j]
}
# Scale hydrograph to sample volume
scaled_hydrograph <- scale_hydrograph(
hydrograph_shape,
obs_hydrograph_vol,
Q_matrix[i, j],
sample_volume,
routing_dt = routing_dt
)
# Route scaled hydrograph
Expand All @@ -371,7 +390,7 @@ rfa_simulate <- function(
realiz_numeric[realiz, ] <- c(
InitMonths[realiz],
InitStages[realiz],
Q_matrix[i, j],
sample_volume,
tmpResults[1],
tmpResults[2]
)
Expand Down Expand Up @@ -512,11 +531,17 @@ rfa_simulate <- function(
hydrograph_shape <- hydrographs[[hydroSamps[realiz]]][, 2:3]
# Hydrograph observed volume
obs_hydrograph_vol <- attr(hydrographs[[hydroSamps[realiz]]], "obs_vol")
# Select the target sample volume for hydrograph scaling
sample_volume <- if (regulated_transform) {
unreg_reg_transform(Q_matrix[i, j])
} else {
Q_matrix[i, j]
}
# Scale hydrograph to sample volume
scaled_hydrograph <- scale_hydrograph(
hydrograph_shape,
obs_hydrograph_vol,
Q_matrix[i, j],
sample_volume,
routing_dt = routing_dt
)
# Route scaled hydrograph
Expand All @@ -535,7 +560,7 @@ rfa_simulate <- function(
realiz_numeric[realiz, ] <- c(
InitMonths[realiz],
InitStages[realiz],
Q_matrix[i, j],
sample_volume,
tmpResults[1],
tmpResults[2]
)
Expand Down Expand Up @@ -721,11 +746,17 @@ rfa_simulate <- function(
hydrographs[[hydroSamps[realiz]]],
"obs_vol"
)
# Set Target for unreg or reg
sample_volume <- if (regulated_transform) {
unreg_reg_transform(Q_matrix[i, j])
} else {
Q_matrix[i, j]
}

scaled_hydrograph <- scale_hydrograph(
hydrograph_shape,
obs_hydrograph_vol,
Q_matrix[i, j],
sample_volume,
routing_dt = routing_dt
)

Expand Down
69 changes: 69 additions & 0 deletions R/unreg_reg_transform.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#' Create an Unregulated-to-Regulated Inflow Transformation Function
#'
#' Constructs a function that transforms unregulated inflow values to corresponding
#' regulated inflow values using linear interpolation. By default, input values
#' outside the range of the supplied unregulated inflows are returned unchanged.
#'
#' @param unreg_inflow Numeric vector of unregulated inflow values. Values should
#' represent the independent variable for the interpolation.
#' @param reg_inflow Numeric vector of regulated inflow values corresponding
#' element-wise to `unreg_inflow`.
#' @param extend Logical; if `TRUE` (default), values passed to the returned
#' function that fall outside the observed range of `unreg_inflow` are returned
#' unchanged. If `FALSE`, out-of-range values return `NA`.
#'
#' @return A function accepting a numeric vector `x` and returning a numeric vector
#' of interpolated regulated inflow values.
#'
#' @details
#' The transformation uses [stats::approxfun()] with `rule = 1`, which produces
#' `NA` for values outside the interpolation range. When `extend = TRUE`, those
#' out-of-range values are replaced with their original input values rather than
#' extrapolated.
#'
#' Missing values in `x` are preserved as `NA`. The minimum and maximum
#' interpolation bounds are calculated with missing values removed from
#' `unreg_inflow`.
#'
#' @examples
#' unreg_inflow <- c(0, 100, 200, 300)
#' reg_inflow <- c(0, 90, 170, 240)
#'
#' transform_inflow <- unreg_reg_transform(unreg_inflow, reg_inflow)
#'
#' # Interpolated value
#' transform_inflow(150)
#'
#' # Out-of-range values are unchanged when extend = TRUE
#' transform_inflow(c(-10, 50, 350))
#'
#' # Out-of-range values return NA when extend = FALSE
#' strict_transform <- unreg_reg_transform(
#' unreg_inflow,
#' reg_inflow,
#' extend = FALSE
#' )
#' strict_transform(c(-10, 50, 350))
#'
#' @export
unreg_reg_transform <- function(
unreg_inflow,
reg_inflow,
extend = TRUE
) {
f <- approxfun(unreg_inflow, reg_inflow, rule = 1)

xmin <- min(unreg_inflow, na.rm = TRUE)
xmax <- max(unreg_inflow, na.rm = TRUE)

function(x) {
result <- f(x)

if (extend) {
outside_range <- !is.na(x) & (x < xmin | x > xmax)
result[outside_range] <- x[outside_range]
}

result
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ An R implementation of the U.S. Army Corps of Engineers (USACE) Risk Management

See [`NEWS.md`](NEWS.md) for a history of user-facing changes across versions.

## Acknowledgments

The authors thank Allen Avance, Samantha Hartke, Reuben Sasaki, and Bryan Robinson of the U.S. Army Corps of Engineers Risk Management Center (USACE - RMC) for their technical review, feedback, and contributions to package development.

## Installation

### For users
Expand Down
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ reference:
- interpolate_stage_matrix
- power_function
- theme_rfar_conceptual
- scale_x_aep
- pmf_stage_lognormal
- rejection_sampling_stage
- unreg_reg_transform
- title: "Datasets"
desc: "Example datasets included with rfaR"
contents:
Expand Down
Loading
Loading