R/RHG_graph_coefficients.R

Defines functions RHG_graph_coefficients

Documented in RHG_graph_coefficients

if(getRversion() >= "2.15.1")  utils::globalVariables(c("regional_curve_graphs"))

#' @title Compute power function coefficients
#'
#' @description  Computes the regional curve power function coefficients from
#'     a table of regional hydraulic curve coordinates contained in a built-in
#'     table.
#'
#' @export
#' @param region        character; The region that a dimension will be
#'                      calculated for. See the regional_curves$region field
#'                      for a complete list.
#' @param drainageArea  numeric; The upstream drainage area of the location of
#'                      the dimension (in square miles).
#' @param dimensionType character; The dimension type: "area", "depth", "width"
#'
#' @return A data frame containing the value of slope (\eqn{m}) and intercept
#'     (\eqn{a}) for the requested region and hydraulic dimension.
#'
#' @details Regional curves are typically displayed on log-log plots. This
#'     method is required to extract the formula for calculating the y-axis
#'     value on these graphs (i.e., cross-sectional area, width, mean depth)
#'     from the x-axis value (i.e., drainage area). Monomial relationships of
#'     the form \eqn{y=ax^m}, known as power functions, appear as straight
#'     lines in a log–log graph, with the exponent (\code{m}) and constant
#'     (\eqn{a}) term corresponding to slope and intercept of the line
#'     (\eqn{y = mx + b}).
#'
#' @examples
#' # Calculate the slope and intercept for a watershed with drainage area = 1
#' # square mile in the Eastern United States region.
#' RHG_graph_coefficients(region = "Eastern United States",
#'                        drainageArea = 1,
#'                        dimensionType = "area")
#'
RHG_graph_coefficients <- function(region, drainageArea, dimensionType) {
  # Subset the RHG curve for the selected region and dimension
  rc <- regional_curve_graphs[regional_curve_graphs$region_name == region &
                              regional_curve_graphs$dimension == dimensionType,]
  # Calculate the slope
  m <- log(rc$y_2 / rc$y_1 ) / (log(rc$x_2 / rc$x_1))
  # Calculate the hydrologic geometry for the selected region and dimension
  a <- rc$y_1
  # Create a data frame to hold the coefficients
  coefficients <- data.frame(region, drainageArea, dimensionType, m, a,
                             rc$source)
  return(coefficients)
}
mpdougherty/RegionalCurve documentation built on Nov. 21, 2019, 8:45 p.m.