+def rel_to_abs(T,P,RH):
+ """Returns absolute humidity given relative humidity.
+
+ Inputs:
+ --------
+ T : float
+ Absolute temperature in units Kelvin (K).
+ P : float
+ Total pressure in units Pascals (Pa).
+ RH : float
+ Relative humidity in units percent (%).
+
+ Output:
+ --------
+ absolute_humidity : float
+ Absolute humidity in units [kg water vapor / kg dry air].
+
+ References:
+ --------
+ 1. Sonntag, D. "Advancements in the field of hygrometry". 1994. https://doi.org/10.1127/metz/3/1994/51
+ 2. Green, D. "Perry's Chemical Engineers' Handbook" (8th Edition). Page "12-4". McGraw-Hill Professional Publishing. 2007.
+
+ Version: 0.0.1
+ Author: Steven Baltakatei Sandoval
+ License: GPLv3+
+ """
+
+ import math;
+
+ # Check input types
+ T = float(T);
+ P = float(P);
+ RH = float(RH);
+
+ #debug
+ # print('DEBUG:Input Temperature (K) :' + str(T));
+ # print('DEBUG:Input Pressure (Pa) :' + str(P));
+ # print('DEBUG:Input Rel. Humidity (%) :' + str(RH));
+
+ # Set constants and initial conversions
+ epsilon = 0.62198 # (molar mass of water vapor) / (molar mass of dry air)
+ t = T - 273.15; # Celsius from Kelvin
+ P_hpa = P / 100; # hectoPascals (hPa) from Pascals (Pa)
+
+ # Calculate e_w(T), saturation vapor pressure of water in a pure phase, in Pascals
+ ln_e_w = -6096*T**-1 + 21.2409642 - 2.711193*10**-2*T + 1.673952*10**-5*T**2 + 2.433502*math.log(T); # Sonntag-1994 eq 7; e_w in Pascals
+ e_w = math.exp(ln_e_w);
+ e_w_hpa = e_w / 100; # also save e_w in hectoPascals (hPa)
+ # print('DEBUG:ln_e_w:' + str(ln_e_w)); # debug
+ # print('DEBUG:e_w:' + str(e_w)); # debug
+
+ # Calculate f_w(P,T), enhancement factor for water
+ f_w = 1 + (10**-4*e_w_hpa)/(273 + t)*(((38 + 173*math.exp(-t/43))*(1 - (e_w_hpa / P_hpa))) + ((6.39 + 4.28*math.exp(-t / 107))*((P_hpa / e_w_hpa) - 1))); # Sonntag-1994 eq 22.
+ # print('DEBUG:f_w:' + str(f_w)); # debug
+
+ # Calculate e_prime_w(P,T), saturation vapor pressure of water in air-water mixture, in Pascals
+ e_prime_w = f_w * e_w; # Sonntag-1994 eq 18
+ # print('DEBUG:e_prime_w:' + str(e_prime_w)); # debug
+
+ # Calculate e_prime, vapor pressure of water in air, in Pascals
+ e_prime = (RH / 100) * e_prime_w;
+ # print('DEBUG:e_prime:' + str(e_prime)); # debug
+
+ # Calculate r, the absolute humidity, in [kg water vapor / kg dry air]
+ r = (epsilon * e_prime) / (P - e_prime);
+ # print('DEBUG:r:' + str(r)); # debug
+
+ return float(r);
+