R/get.Sc.R

#'@title Returns Schmidt number for a specific gas at a given temperature
#'@description
#'Returns the temperture specific Schmidt number (Sc) for a given gas.  Sc is the ratio of the kinematic viscosity of water
#'to a diffusion coefficient. Coefficients are included for He, O2, CO2, CH4, SF6, N2O, Ar, and N2.
#'Code taken almost verbatim from the Lake Metabolizer package (thanks guys!)\cr
#'
#'@usage
#'get.Sc(temperature, gas)
#'
#'@param temperature Numeric vector of water temperatures in deg. Celsius
#'@param gas String for gas code. Use "all" to get the full list of supported gasses.
#'@return Schmidt number (Sc; unitless)
#'@note Temperature range is only valid from 4-35 deg Celsius
#'@references
#'Raymond, Peter A., Christopher J. Zappa, David Butman, Thomas L. Bott, Jody Potter, Patrick Mulholland,
#'Andrew E. Laursen, William H. McDowell, and Denis Newbold. \emph{Scaling the gas transfer velocity and hydraulic
#'geometry in streams and small rivers}. Limnology & Oceanography: Fluids & Environments 2 (2012): 41-53.
#'@author
#'Jordan S. Read (edited by Gordon W. Holtgrieve)
#'@examples
#'get.Sc(temperature=12, gas="O2")
#'get.Sc(gas="all") #returns full list of supported gasses and their temperature coefficients
#'@export
get.Sc	<-	function(temperature=NA, gas){

  range.t	<-	c(4,35) # supported temperature range

  Schmidt	<-	data.frame(
    "He"=c(368,-16.75,0.374,-0.0036),
    "O2"=c(1568,-86.04,2.142,-0.0216),
    "CO2"=c(1742,-91.24,2.208,-0.0219),
    "CH4"=c(1824,-98.12,2.413,-0.0241),
    "SF6"=c(3255,-217.13,6.837,-0.0861),
    "N2O"=c(2105,-130.08,3.486,-0.0365),
    "Ar"=c(1799,-106.96,2.797,-0.0289),
    "N2"=c(1615,-92.15,2.349,-0.0240)
  )
  row.names(Schmidt) <- LETTERS[1:4]

  obsT <- is.finite(temperature) # logical for observed (not NA or NaN [or Inf or -Inf]) -RDB

  #Error handling
  if(is.null(gas)) stop("Error: Missing argument 'gas'.")
  if (!is.character(gas)){stop("Error: Argument 'gas' must be of type character.")}
  if (length(gas)>1){stop("Error: Argument 'gas' should be length == 1 (i.e., a single molecule.)")}
  if (!any(names(Schmidt)==gas) & gas != "all"){
    stop(cat("Error: Supplied argument 'gas' not included in list of supported molecules.\n Call get.Sc(gas='all') to see current list.\n"))
  }

  if(gas=="all"){
    return(Schmidt)
  } else{

  if (any(temperature[obsT] < range.t[1] | temperature[obsT] > range.t[2])) warning("'temperature' out of range (4-35 degrees C)")

  A	<-	unlist(Schmidt[gas])[1]
  B	<-	unlist(Schmidt[gas])[2]
  C	<-	unlist(Schmidt[gas])[3]
  D	<-	unlist(Schmidt[gas])[4]

  Sc = as.numeric(A+B*temperature+C*temperature^2+D*temperature^3)

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