.WaveIntensity <- function(x, case, threshold, min.conseq){
  if(case == 'heat') {a <- which(x > threshold)} else { a <- which(x < threshold)}
  durations <- lengths(split(a, cumsum(c(TRUE, diff(a)!=1))))
  if (max(durations) < min.conseq) {intensity <- 0
  } else {
    intensity <- ifelse(case == 'heat', max(x-threshold), max(threshold - x))
  }
  return(intensity)
}
WaveIntensity <- function(x, case, threshold, min.conseq = 5, indices){
  tapply(x, INDEX = indices, FUN = .WaveIntensity, case = case, threshold = threshold, min.conseq = min.conseq)
}
