---
title: "iemisc: Comparing Saturated Vapor Pressure Formulas to the Reference"
author: "Irucka Embry, E.I.T. (EcoC²S)"
date: "`r Sys.Date()`"
lang: en-us
urlcolor: blue
output:
  rmarkdown::pdf_document:
    highlight: kate
    toc: true
    latex_engine: xelatex
vignette: >
  %\VignetteIndexEntry{iemisc Comparing Saturated Vapor Pressure Formulas to the Reference}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

\bigskip

# Replicate the R code

Note: If you wish to replicate the R code below, then you will need to copy and paste the following commands in R first (to make sure you have all the packages and their dependencies):  

\medskip

```{r eval = FALSE, tidy = TRUE}
install.packages(c("install.load", "iemisc", "units"))
# install the packages and their dependencies
```

\bigskip
\bigskip

```{r, warning = FALSE, message = FALSE, tidy = TRUE}
# load the required packages
install.load::load_package("iemisc", "units", "pander")
# load needed packages using the load_package function from the install.load package (it is assumed that you have already installed these packages)

import::from(fpCompare, "%==%")
```

\bigskip
\bigskip

# Compare Water Saturation Vapor Pressure

```{r, warning = FALSE, message = FALSE, tidy = TRUE}

install.load::load_package("iemisc", "data.table", "units", "pander")


# reference vapor pressures from the Huang reference
reference <- sort(c(611.655, 2339.32, 7384.94, 19946.4, 47414.5, 101418))

Temp <- sort(c(0.01, seq(from = 20, to = 100, by = 20)))

# hydraulics
hydraulics_svp <- hydraulics::svp(T = Temp, units = "SI")


# iemisc
iemisc_sat_vapor_pressure_huang <- sat_vapor_pressure(Temp = Temp, units = "SI", formula = "Huang")

iemisc_sat_vapor_pressure_buck <- sat_vapor_pressure(Temp = Temp, units = "SI", formula = "Buck")

iemisc_sat_vapor_pressure_iapws <- sat_vapor_pressure(Temp = Temp, units = "SI", formula = "IAPWS")



# aiRthermo

# create a numeric vector with the units of degrees Celsius
T_C <- set_units(Temp, "degree_C")
T_C

# create a numeric vector to convert from degrees Celsius to Kelvin
T_K <- T_C
T_K

# create a numeric vector with the units of Kelvin
units(T_K) <- make_units(K)

aiRthermo_saturation_pressure_H2O <- aiRthermo::saturation_pressure_H2O(drop_units(T_K))



comparePress <- data.table(Reference_Pressure = reference, Hydraulics_Pressure = hydraulics_svp, Huang_Pressure = iemisc_sat_vapor_pressure_huang, Buck_Pressure = iemisc_sat_vapor_pressure_buck, IAPWS_Pressure = iemisc_sat_vapor_pressure_iapws, aiRthermo_Pressure = aiRthermo_saturation_pressure_H2O)

comparePress[, `:=` (mreHydraulics = mapply(mre, Hydraulics_Pressure, Reference_Pressure) *
100, mreHuang = mapply(mre, Huang_Pressure, Reference_Pressure) * 100,
mreBuck = mapply(mre, Buck_Pressure, Reference_Pressure) * 100, 
mreIAPWS = mapply(mre, IAPWS_Pressure, Reference_Pressure) *
100, mreaiRthermo = mapply(mre, aiRthermo_Pressure, Reference_Pressure) *
100)] # Source 1


# which row(s) has the maximum value
max_row <- pmax(comparePress$mreHydraulics, comparePress$mreHuang, comparePress$mreBuck, comparePress$mreIAPWS, comparePress$mreaiRthermo)

# which row(s) has the minimum value
min_row <- pmin(comparePress$mreHydraulics, comparePress$mreHuang, comparePress$mreBuck, comparePress$mreIAPWS, comparePress$mreaiRthermo)

# which rows are TRUE
max_row2 <- comparePress == max_row


# which rows are TRUE
min_row2 <- comparePress == min_row

comparePress[, max_mre := c(rep("mreaiRthermo", 3), rep( "mreBuck", 3))]

comparePress[, min_mre := c("mreBuck", rep("mreHydraulics / mreHuang", 4), "mreIAPWS")]

setnames(comparePress, c("Reference Pressure (Pa)", "Hydraulics Package Pressure (Pa)", "Huang Pressure (Pa)", "Buck Pressure (Pa)", "IAPWS Pressure (Pa)", "aiRthermo Pressure (Pa)", "MRE % (Hydraulics Package vs. Reference)", "MRE % (Huang vs. Reference)", "MRE % (Buck vs. Reference)", "MRE % (IAPWS vs. Reference)", "MRE % (aiRthermo vs. Reference)", "Maximum MRE % Formula", "Minumum MRE % Formula"))

pander(comparePress)
```

\bigskip
\bigskip

# Works Cited

Ed Maurer and Irucka Embry, `hydraulics` version 0.6.1, 2022-12-06, https://CRAN.R-project.org/package=hydraulics  

Holger Vömel, National Center for Atmospheric Research Earth Observing Laboratory, "Saturation vapor pressure formulations", https://web.archive.org/web/20170623040102/http://cires1.colorado.edu/~voemel/vp.html. Retrieved thanks to the Internet Archive: Wayback Machine  

Huang, J. (2018). "A Simple Accurate Formula for Calculating Saturation Vapor Pressure of Water and Ice", *Journal of Applied Meteorology and Climatology*, 57(6), 1265-1272. Retrieved Nov 4, 2021, https://journals.ametsoc.org/view/journals/apme/57/6/jamc-d-17-0334.1.xml. Please note that the **Reference** values are obtained from this reference.  

Jon Sáenz, Santos J. González-Rojí, Sheila Carreno-Madinabeitia and Gabriel Ibarra-Berastegi, `aiRthermo` version 1.2.1, 2018-09-16, https://CRAN.R-project.org/package=aiRthermo  

The International Association for the Properties of Water and Steam. IAPWS SR1-86(1992). "Revised Supplementary Release on Saturation Properties of Ordinary Water Substance", September 1992, http://www.iapws.org/relguide/Supp-sat.pdf  

\bigskip
\bigskip

# R Source

Source 1  
r - How do I reset all options() arguments to their default values? - Stack Overflow answered by stevec on Jul 27 2020 and edited by stevec on Feb 27 2022. See https://stackoverflow.com/questions/36848785/how-do-i-reset-all-options-arguments-to-their-default-values  

\bigskip
\bigskip

# EcoC²S Links

EcoC²S Home -- https://www.ecoccs.com/  
About EcoC²S -- https://www.ecoccs.com/about_ecoc2s.html  
Services -- https://www.ecoccs.com/services.html  
1 Stop Shop -- https://www.ecoccs.com/other_biz.html  
Products -- https://www.questionuniverse.com/products.html  
Media -- https://www.ecoccs.com/media.html  
Resources -- https://www.ecoccs.com/resources.html  
R Trainings and Resources provided by EcoC²S (Irucka Embry, E.I.T.) -- https://www.ecoccs.com/rtraining.html  

\bigskip
\bigskip

# Copyright and License

All R code written by Irucka Embry is distributed under the GPL-3 (or later) license, see the [GNU General Public License {GPL} page](https://www.gnu.org/licenses/gpl-3.0.html).  

All written content originally created by Irucka Embry is copyrighted under the Creative Commons Attribution-ShareAlike 4.0 International License. All other written content retains the copyright of the original author(s).  

This work is licensed under a [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/).  
