R/thresh_ammonia.R

Defines functions fraction pka thresh_total_ammonia thresh_unionized_ammonia

Documented in thresh_total_ammonia thresh_unionized_ammonia

#' Establish the WQS threshold for unionized ammonia based on of pH and temperature
#'
#' @param .ph a numeric vector of pH values (pH Units).
#' @param .temp a numeric vector of water temperature values (Celsius).
#' @param .trout_water a logical value (TRUE or FALSE) indicating if
#' the trout water criteria should be applied (TRUE) or not (FALSE).
#' @param .type a character vector indicating if the sample should be
#' calculated with the aquatic (chronic) or aquatic (acute) criteria.
#' @return A vector of type double representing the threshold for unionized ammonia.
#' @examples
#' thresh_unionized_ammonia(.ph = 6.5, .temp = 0, .type = "aquatic_chronic", .trout_water = TRUE)
#' @export


thresh_unionized_ammonia <- function(.ph, .temp, .trout_water, .type) {

   if (any(is.na(c(.ph, .temp, .trout_water, .type)))) return(NA_real_)

  if (!is.logical(.trout_water)) {
    stop(paste("trout_water must be a logical value. You supplied:", .trout_water))
  }

  if (!.type %in% c("aquatic_acute", "aquatic_chronic")) {
    stop(paste("type must be 'aquatic_acute' or 'aquatic_chronic'. You supplied:", .type))
  }


  if (.temp < 0) {
    warning(paste("\n NA returned. \n", "\t .temp too low (< 0C):", .temp, "\n"))
    return(NA_real_)
  }
  if (.temp > 30) {
    warning(paste("\n NA returned. \n", "\t .temp too high (> 30C):", .temp, "\n"))
    return(NA_real_)
  }
  if (.ph < 6.5) {
    warning(paste("\n NA returned. \n", "\t .ph too low (< 6.5):", .ph, "\n"))
    return(NA_real_)
  }
  if (.ph > 9.0) {
    warning(paste("\n NA returned. \n", "\t .ph too high (> 9.0):", .ph, "\n"))
    return(NA_real_)
  }

  if (.type == "aquatic_chronic") {
    if (.trout_water == TRUE) {
      tcap <- 15
    } else if (.trout_water == FALSE) {
      tcap <- 20
    }
  } else if (.type == "aquatic_acute") {
    if (.trout_water == TRUE) {
      stop("No ammonia WQS for aquatic acute trout waters")
    } else if (.trout_water == FALSE) {
      tcap <- 205
    }
  }

  if (.temp >= tcap & .temp <= 30) .temp <- tcap
  ft <- 10 ^ (0.03 * (20 - .temp))

  if (.ph > 8 & .ph <= 9) fph <- 1
  if (.ph >= 6.5 & .ph <= 8) fph <- (1 + 10 ^ (7.4 - .ph)) / 1.25

  if (.ph >= 7.7 & .ph <= 9.0) ratio <- 16
  if (.ph >= 6.5 & .ph <= 7.7) ratio <- 24 * ((10 ^ (7.7 - .ph)) / (1 + 10 ^ (7.4 - .ph)))


 if (.type == "aquatic_chronic") final.scalar <- 0.80 / ft / fph / ratio
 if (.type == "aquatic_acute") final.scalar <- 0.52 / ft / fph / 2

  return(final.scalar)
}


#' Establish the WQS threshold for total ammonia based on pH and temperature
#'
#' @param .ph a numeric vector of pH values (pH Units).
#' @param .temp a numeric vector of water temperature values (Celsius).
#' @param .trout_water a logical value (TRUE or FALSE) indicating if
#' the trout water criteria should be applied (TRUE) or not (FALSE).
#' @param .type a character vector indicating if the sample should be
#' calculated with the aquatic (chronic) or aquatic (acute) criteria.
#' @return A vector of type double representing the threshold for total ammonia.
#' @examples
#' thresh_total_ammonia(.ph = 6.5, .temp = 0, .type = "aquatic_chronic", .trout_water = TRUE)
#' @export

thresh_total_ammonia <- function(.ph, .temp, .trout_water, .type) {
  unionized <- thresh_unionized_ammonia(.ph = .ph,
                                        .temp = .temp,
                                        .trout_water = .trout_water,
                                        .type = .type)
  # Multiply by 1,000 to convert from mg/L to ug/L
  unionized / fraction(.temp = .temp,
                       .ph = .ph) * 1000
}




# helpers -----------------------------------------------------------------

pka <- function(.temp) {
  0.0901821 + 2729.92 / (.temp + 273.15)
}

fraction <- function(.temp, .ph) {
  1 / (10 ^ (pka(.temp) - .ph) + 1)
}


# apply_ammonia <- function(x,...) {
#   split.df <- split(x,
#                     list(...),
#                     drop = TRUE)
#
#   keep.vec <- which(grepl("total.ammonia.aquatic", names(split.df)))
#   split.df[keep.vec] <- lapply(split.df[keep.vec],function(x){
#     x$threshold <- thresh_total_ammonia(
#       .ph = x$result_numeric_ph,
#       .temp = x$result_numeric_temperature,
#       .trout_water = grepl("(t)|(ts)", x$class),
#       .type = x$type)
#
#     return(x)
#
#   })
#
#   final.df <- do.call(rbind, split.df)
# }

# test <- apply_ammonia(chem.df, chem.df$fraction, chem.df$parameter, chem.df$type) %>%
#   select(threshold, everything())
BWAM/stayCALM documentation built on May 21, 2020, 3:24 p.m.