diff --git a/NAMESPACE b/NAMESPACE index ca80cd7..6e6c3bd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/flood_divide.R b/R/flood_divide.R index c97d893..feac058 100644 --- a/R/flood_divide.R +++ b/R/flood_divide.R @@ -12,9 +12,9 @@ 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, ]) # 分组 @@ -22,16 +22,21 @@ detect_groups <- function(df, inds, gap_max = 5, extend = ddays(5)) { } #' 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, ] ## 数据压缩 @@ -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 @@ -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 diff --git a/R/hydroTools-package.R b/R/hydroTools-package.R index 28d3592..b6b5bff 100644 --- a/R/hydroTools-package.R +++ b/R/hydroTools-package.R @@ -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) diff --git a/man/flood_divide.Rd b/man/flood_divide.Rd index 7139922..87857e4 100644 --- a/man/flood_divide.Rd +++ b/man/flood_divide.Rd @@ -9,7 +9,14 @@ \usage{ detect_groups(df, inds, gap_max = 5, extend = ddays(5)) -detect_flood_events(date, Q, Q_min = 2, Q_peak = 10, gap_max = 5) +detect_flood_events( + date, + Q, + Q_min = 2, + Q_peak = 10, + gap_max = 5, + extend = ddays(5) +) flood_divide(df, ...) @@ -18,7 +25,10 @@ merge_flood(df, info_flood, format = "\%Y.\%m") \arguments{ \item{df}{A data.table with date and Q columns} -\item{gap_max}{distance smaller than gap_max is considered as a same group} +\item{gap_max}{Default \code{5}. if \verb{index gap > gap_max}, events will be regarded as two. +If Q is hourly, gap_max can be set \code{5*24}.} + +\item{extend}{Default \code{ddays(5)}. Extend \code{nday} in the left and right of a event} \item{Q_min}{minimum discharge to detect flood events} diff --git a/man/reexports.Rd b/man/reexports.Rd new file mode 100644 index 0000000..2432ec1 --- /dev/null +++ b/man/reexports.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/hydroTools-package.R +\docType{import} +\name{reexports} +\alias{reexports} +\alias{ddays} +\title{Objects exported from other packages} +\keyword{internal} +\description{ +These objects are imported from other packages. Follow the links +below to see their documentation. + +\describe{ + \item{lubridate}{\code{\link[lubridate:duration]{ddays}}} +}} +