R/get.OH.R

#' @title Calculates the hydroxide concentration in water (mol/L)
#' @description Calculates the hydroxide concentration for a
#' given temperature, salinity, and [H+]. Calls the Kw() function in the seacarb
#' package, which returns the ion product of water with units of mol2/kg2. Dividing
#' by [H+] gives mol/kg.  Units are converted between mass (kg) and volume (L) using
#' water density given by the rho() function in seacarb.
#' @importFrom seacarb Kw
#' @importFrom seacarb rho
#' @usage get.K2(temperature)
#' @param temperature Water temperature in degrees Celsius.
#' @param salinity Salinity in practical salinity units (PSU). Defaults to 0.
#' @param H Hydrogen ion concentration ([H+]) in mol/L
#' @return Numeric vector of [OH] in units of mol/L.
#' @author Gordon W. Holtgrieve (gholt@uw.edu)
#' @export

get.OH <- function(temperature, salinity=0, H){
  # H should be in units of mol/L, but now uses mol/kg.  Need to fix.

  # Error handling
  if(is.null(temperature)) stop("'temperature' missing with no defualt.")
  if(is.null(H)) stop("'H' missing with no defualt.")

  oldw <- getOption("warn") # Keeps seacarb from getting grumpy.
  options(warn = -1)

  #Convert H units to mol/kg from mol/L
  rho <- seacarb::rho(S = salinity, T = temperature, P = 0) # Density in kg/m3
  H_kg <- H * 1000 / rho

  Kw <- seacarb::Kw(S=salinity, T=temperature, P=0, warn = "n") # Units mol2/kg2
  OH_kg <- Kw / H_kg  # Units of mol/kg
  OH <- OH_kg * rho / 1000 # Convert from kg water to L

  attributes(OH) <- NULL  # Strip attributes

  options(warn = oldw) # Returns to original settings

  return(OH)
}
gholtgrieve/gassyPants documentation built on May 9, 2019, 5:02 a.m.