.WaveStart <- 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) {initial <- 0
  } else {
    duration <- sum(durations[durations >= min.conseq])
    initial <- unlist(split(a, cumsum(c(TRUE, diff(a)!=1)))[which(durations >= min.conseq)[1]])[1]
    if (case == 'heat'){
      summer <- c(paste(rep(6,30), 1:30, sep = "-"), paste(rep(7,31), 1:31, sep = "-"), 
                  paste(rep(8,31), 1:31, sep = "-"))
      initial <- summer[initial]} 
    else { 
      winter <- c(paste(rep(12,31), 1:31, sep = "-"), paste(rep(1,31), 1:31, sep = "-"), 
                  paste(rep(2,29), 1:28, sep = "-"))
      initial <- winter[initial]
    } 
  }
  return(initial)
}
WaveStart <- function(x, case, threshold, min.conseq = 5, indices){
  tapply(x, INDEX = indices, FUN = .WaveStart, case = case, threshold = threshold, min.conseq = min.conseq)
}
