R/dh23.R

Defines functions dh23

Documented in dh23

#' Function to return the DH23 hydrologic indicator statistic for a given data frame
#' 
#' This function accepts a data frame that contains a column named "discharge" and a threshold value obtained 
#' using the peakdata and peakthresh functions and calculates 
#' DH23; Flood duration. Compute the flood threshold as the flow equivalent for a flood recurrence of 1.67 years. 
#' Determine the number of days each year that the flow remains above the flood threshold. DH23 is the mean (or 
#' median-Use Preference option) of the number of flood days for years in which floods occur (days-temporal).
#' 
#' @param qfiletempf data frame containing a "discharge" column containing daily flow values
#' @param thresh numeric containing 1.67-year flood threshold calculated by getPeakThresh
#' @return dh23 list containing DH23 for the given data frame
#' @export
#' @examples
#' qfiletempf<-sampleData
#' dh23(qfiletempf, 1158)
dh23 <- function(qfiletempf, thresh) {
  lfcrit <- thresh
  noyears <- aggregate(qfiletempf$discharge, list(qfiletempf$wy_val), 
                       FUN = median, na.rm=TRUE)
  colnames(noyears) <- c("Year", "momax")
  noyrs <- length(noyears$Year)
  dur <- data.frame(Year = rep(0,nrow(qfiletempf)), dur = rep(1,nrow(qfiletempf)))
  nevents <- 0
  for (i in 1:noyrs) {
    subsetyr <- subset(qfiletempf, as.numeric(qfiletempf$wy_val) == noyears$Year[i])
    flag <- 0
    for (j in 1:nrow(subsetyr)) {
      if (subsetyr$discharge[j]>lfcrit) {
        flag <- flag+1
        nevents <- ifelse(flag==1,nevents+1,nevents)
        dur$Year[nevents] <- subsetyr$wy_val[j]
        dur$dur[nevents] <- dur$dur[nevents]+1
      } else {flag <- 0}
    }
  }
  subset_dur <- dur[1:nevents ,]
  meanbyyr <- aggregate(subset_dur$dur, list(subset_dur$Year), mean)
  colnames(meanbyyr) <- c("Year", "num_mean")
  dh23 <- mean(meanbyyr$num_mean)
  return(dh23)
}
jlthomps/EflowStats documentation built on May 19, 2019, 12:48 p.m.