R/spec_head_from_slope.R

Defines functions spec_head_from_slope

Documented in spec_head_from_slope

#' Calculate specified head elevations given geometry & slope
#' Assumes a single line.
#'
#' @param swdf DataFrame/Geometry of node barycentric coordinates as returned by
#'   \code{\link{calc_stream_voronoi_weights}}
#' @param sp integer, stress period of heads, used for column naming
#' @param slope numeric, slope
#' @param initial_elev streambed elevation at start of segment 1
#'
#' @return swdf DataFrame/Geometry with added seg1.head[sp] and seg1.head[sp] columns
#' @author Leland Scantlebury
#' @export spec_head_from_slope
#'
#' @seealso \code{\link{stream_elev_from_slope}}
#'
#' @examples
#' #-- Read in shapefiles
#' str <- read_sf(system.file("extdata", "MehlandHill2010_stream.shp", package = "pbjr"))
#' tri <- read_sf(system.file("extdata", "720_triangles.shp", package = "pbjr"))
#' vor <- read_sf(system.file("extdata", "720_voronoi.shp", package = "pbjr"))
#' str <- line_explode(str)
#'
#' #-- Calculate barycentric weight DF
#' swdf <- calc_stream_voronoi_weights(stream = str, voronoi = vor, triangles = tri)
#'
#' #-- Calculate distances
#' swdf <- spec_head_from_slope(swdf = swdf, sp = 1, slope = 0.0015, initial_elev = 50)
spec_head_from_slope <- function(swdf, sp, slope, initial_elev, order_by_index=F) {

  #-- Order to index (if generated by calc_stream_voronoi_weights, order will be correct)
  if (order_by_index) {
    swdf <- swdf[order(swdf$index), ]
  }

  #-- Calculate a cumulative length (distance from start)
  swdf$Distance <- cumsum(swdf$Length)

  #-- Calculate upstream coordinate elevation
  swdf[,paste0('seg1.head',as.integer(sp))] <- initial_elev - (swdf$Distance - swdf$Length) * slope

  #-- Calculate downstream coordination elevation
  swdf[,paste0('seg2.head',as.integer(sp))] <- initial_elev - swdf$Distance * slope

  return(swdf)
}
scantle/pbjr documentation built on Dec. 22, 2021, 10:19 p.m.