Skip to content

Commit

Permalink
improve document of flood division
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Oct 6, 2024
1 parent 7e6a2f6 commit 7431378
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export(celsius.to.fahrenheit)
export(celsius.to.kelvin)
export(convert_temperature)
export(cv_coef)
export(ddays)
export(deg2rad)
export(detect_flood_events)
export(detect_groups)
Expand Down
21 changes: 13 additions & 8 deletions R/flood_divide.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,31 @@ get_event_info <- function(date, Q, extend = ddays(5)) {
}

#' @param gap_max distance smaller than gap_max is considered as a same group
#'
#'
#' @rdname flood_divide
#' @export
#' @export
detect_groups <- function(df, inds, gap_max = 5, extend = ddays(5)) {
grps <- cumsum(c(0, diff(inds) > gap_max)) # 分组
info <- cbind(group = grps, df[inds, ]) # 分组
info[, get_event_info(date, Q, extend), group]
}

#' detect_flood_events
#'
#' @param Q_min minimum discharge to detect flood events
#' @param Q_peak peak discharge to detect flood events
#'
#'
#' @param gap_max Default `5`. if `index gap > gap_max`, events will be regarded as two.
#' If Q is hourly, gap_max can be set `5*24`.
#' @param extend Default `ddays(5)`. Extend `nday` in the left and right of a event
#'
#' @rdname flood_divide
#' @export
detect_flood_events <- function(date, Q, Q_min = 2, Q_peak = 10, gap_max = 5) {
detect_flood_events <- function(date, Q, Q_min = 2, Q_peak = 10, gap_max = 5, extend = ddays(5)) {
df <- data.table(date, Q)
inds <- df[, which(Q > Q_min)] #

info_group <- detect_groups(df, inds, gap_max) %>%
info_group <- detect_groups(df, inds, gap_max, extend) %>%
.[Q_max > Q_peak, ]

## 数据压缩
Expand All @@ -41,7 +46,7 @@ detect_flood_events <- function(date, Q, Q_min = 2, Q_peak = 10, gap_max = 5) {
lgl[date >= info$date_beg & date <= info$date_end] <- TRUE
}

# 由于扩展了5天,这里对洪水事件重新进行编号
# 由于已经扩展了5天,这里对洪水事件重新进行编号,不需要二次扩展
inds <- which(lgl)
info_group <- detect_groups(df, inds, gap_max, extend = 0)
info_group
Expand All @@ -51,12 +56,12 @@ detect_flood_events <- function(date, Q, Q_min = 2, Q_peak = 10, gap_max = 5) {
#' flood_divide
#' @param df A data.table with date and Q columns
#' @param ... parameters passed to [detect_flood_events()]
#' @export
#' @export
flood_divide <- function(df, ...) {
date <- df$date
if ("time" %in% names(df)) date <- df$time
df <- df[order(date)]
info_group <- detect_flood_events(date, df$Q, ...)
detect_flood_events(date, df$Q, ...) # info_group
}

#' @rdname flood_divide
Expand Down
4 changes: 4 additions & 0 deletions R/hydroTools-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
## usethis namespace: end
NULL

#' @importFrom lubridate ddays
#' @export
lubridate::ddays

.onLoad <- function(libname, pkgname) {
options(rmarkdown.html_vignette.check_title = FALSE)

Expand Down
14 changes: 12 additions & 2 deletions man/flood_divide.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions man/reexports.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7431378

Please sign in to comment.