Created
July 26, 2019 07:55
-
-
Save KasperSkytte/0b94ae24645d16e97099a201f48bcd8b to your computer and use it in GitHub Desktop.
Homebrew: R function to adjust hydrometer gravity reading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
adjustHydrometer <- function(SG = 1010, | |
temp = 25, | |
calibTemp = 20, | |
unit = "C") { | |
#initial argument checks | |
if(!is.numeric(SG)) | |
stop("Argument \"SG\" must be numeric", call. = FALSE) | |
if(!is.numeric(temp)) | |
stop("Argument \"temp\" must be numeric", call. = FALSE) | |
if(!is.numeric(calibTemp)) | |
stop("Argument \"calibTemp\" must be numeric", call. = FALSE) | |
if(length(calibTemp) != 1) | |
stop("Calibration temperature must be a single value", call. = FALSE) | |
if(!is.character(unit)) | |
stop("Argument \"unit\" must be character", call. = FALSE) | |
if(!tolower(unit) %in% c("f", "c")) | |
stop("Unit must be either \"F\" (for fahrenheit) or \"C\" (for celcius)", call. = FALSE) | |
#formula from http://www.musther.net/vinocalc.html | |
#formula is for temperatures in fahrenheit only, range: 0-60C | |
if(tolower(unit) == "c") { | |
temp <- temp*1.8+32 | |
calibTemp <- calibTemp*1.8+32 | |
} | |
CSG <- SG * ((1.00130346 - (0.000134722124 * temp) + (0.00000204052596 * temp^2) - (0.00000000232820948 * temp^3)) / | |
(1.00130346 - (0.000134722124 * calibTemp) + (0.00000204052596 * calibTemp^2) - (0.00000000232820948 * calibTemp^3))) | |
return(CSG) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment