###########################################################################################
###########################################################################################
## Function to compute kinematic viscosity of water for given temperature
###########################################################################################
###########################################################################################
## created by Marcus Klaus, Swedish University of Agricultural Sciences (SLU), Umeå, Sweden
## contact: marcus.klaus@slu.se
## last update: 2026/04/30
###########################################################################################

## INPUTS:
# temperature...Water temperature (°C)

## OUTPUTS:
# v...kinematic viscosity (m2/s)

## References
# code originally written by Jordan S. Read, from GDopp package, available at https://rdrr.io/github/USGS-R/GDopp/src/R/Temp2kinematicViscosity.R
# Data from Mays, L. W. Water Resources Engineering.  (John Wiley & Sons, Inc., 2005).

###########################################################################################

Temp2kinematicViscosity <- function(temperature=20) {
  tempTable <- seq(0,100,by=5)
  # table in m2/s E-6
  visTable <- c(1.792,1.519,1.308,1.141,1.007,0.897,
                0.804,0.727,0.661,0.605,0.556,0.513,0.477,0.444,
                0.415,0.39,0.367,0.347,0.328,0.311,0.296)
  v <- data.frame(approx(tempTable,visTable,xout = temperature))[2]
  v <- v*1e-6
  return(v$y)
}
