R/RProjects/HITHATStats/R/mh21.R

Defines functions mh21

Documented in mh21

#' Function to return the MH21 hydrologic indicator statistic for a given data frame
#' 
#' This function accepts a data frame that contains a column named "discharge" and 
#' calculates MH21. High flow volume index. Compute the average volume for flow events above a threshold 
#' equal to the median flow for the entire record. MH21 is the average volume divided by the median flow for the entire record.
#' 
#' @param x data frame containing a "discharge" column containing daily flow values
#' @return mh21 numeric value of the average volume of exceedence events divided by the median flow for the given data frame
#' @export
#' @examples
#' load_data<-paste(system.file(package="HITHATStats"),"/data/obs_data.csv",sep="")
#' x<-read.csv(load_data)
#' mh21(x)
mh21 <- function(x) {
  thresh <- ma2(x)
  nevents <- 0
  flag <- 0
  total <- 0
  for (i in 1:nrow(x)) {
    temp <- x$discharge[i]-thresh
    if (temp>0) {
      total <- total + temp
      flag <- flag+1
      nevents <- ifelse(flag==1,nevents+1,nevents)
    } else {flag <- 0}
  }
  avg_ex <- total/nevents
  mh21 <- avg_ex/thresh
  return(mh21)
}
jlthomps/EflowStats documentation built on May 19, 2019, 12:48 p.m.