R/get.K2.R

#' @title Calculates the second dissociation constant of carbonic acid (mol/kg)
#' @description Calculates the second dissociation constant of carbonic acid for a
#' given temperature, salinity and pressure. Calls the K2() function in the seacarb
#' package, which returns units of mol/kg. Units are converted from kg to volume (L)
#' using water density given by the rho() function in seacarb.
#' @importFrom seacarb K2
#' @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 pressure Surface pressure in atmospheres. Defaults to 1.
#' @return Numeric vector of K2 in units of mol/L.
#' @author Gordon W. Holtgrieve (gholt@uw.edu)
#' @export

get.K2 <- function(temperature, salinity=0){

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

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

  K2_kg <- seacarb::K2(S=salinity, T=temperature, P=0, k1k2="m06", warn = "n") # Units of mol/kg
  rho <- seacarb::rho(S = salinity, T = temperature, P = 0) # Density in kg/m3
  K2 <- K2_kg * rho  / 1000 # Convert from kg water to L

  attributes(K2) <- NULL  # Strip the attributes

  options(warn = oldw) # Returns to original settings

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