R/flood.length.max.R

Defines functions flood.length.max

Documented in flood.length.max

flood.length.max <- function(flow.ts, threshold, ind.days = 5) {
    
    high.flows <- ifelse(flow.ts[["Q"]] > threshold, 1, 0)
    
    high.flow.runs <- rle(high.flows)
    too.short <- which(high.flow.runs$lengths < ind.days & high.flow.runs$values == 0)
    spell.factor <- rep(seq_along(high.flow.runs$lengths), times = high.flow.runs$lengths)
    add.to.spell <- which(spell.factor %in% too.short)
    high.flows[add.to.spell] <- 1
    
    
    high.flow.runs <- rle(high.flows)
    if (length(which(high.flow.runs$values == 1)) == 0) {
        
        max.duration <- 0
    } else {
        max.duration <- max(high.flow.runs$lengths[which(high.flow.runs$values == 1)], na.rm = T)
    }
    
    max.duration <- data.frame(max.duration = max.duration)
    
    return(max.duration)
} 

Try the hydrostats package in your browser

Any scripts or data that you put into this service are public.

hydrostats documentation built on June 1, 2022, 9:06 a.m.