R/mldtempcutoff.R

mldtempcutoff = function(df, depthname, tempname, tempdiff, rd){
  #Take depths and temperatures from the dataframe
  depths <- df[[depthname]]
  temps <- df[[tempname]]

  #Find the temperature at the user-defined reference depth
  targettemp <- df[(df[depthname] == rd),][[tempname]]

  #Allow for the thermocline to occur at both a hotter and cooler temperature
  tempmin <- targettemp - tempdiff
  tempmax <- targettemp + tempdiff

  #Only interested in depths below the reference depth
  submat <- df[(df[depthname] > rd),]

  #Find all rows which are below the reference depth and outside
  #of the allowed temperature range
  goalrow <- submat[(submat[tempname] < tempmin | submat[tempname] > tempmax),]

  #Find the most shallow depth that is deeper than the
  #reference depth and outside of the allowed temperature range
  nolonger <- min(goalrow[depthname])

  #Now find the deepest depth before that cutoff is reached
  goalrow <- df[(df[depthname] < nolonger),]
  md <- max(goalrow[depthname])

  #Check for inverted profile (cooler surface, warmer depths)
  if (temps[1] < temps[length(temps)]){
    cat('Check your profile. Looks inverted.', '\n')
  }
  return(md)
}
kevans27/aqeco documentation built on May 16, 2019, 4:08 a.m.