aboutsummaryrefslogtreecommitdiff
path: root/lib/Web
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Web')
-rw-r--r--lib/Web/OpenWeatherMap/Formulas.hs37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/Web/OpenWeatherMap/Formulas.hs b/lib/Web/OpenWeatherMap/Formulas.hs
new file mode 100644
index 0000000..f63bc9f
--- /dev/null
+++ b/lib/Web/OpenWeatherMap/Formulas.hs
@@ -0,0 +1,37 @@
+{-# LANGUAGE NamedFieldPuns #-}
+
+module Web.OpenWeatherMap.Formulas
+ ( absoluteHumidity
+ ) where
+
+import Web.OpenWeatherMap.Types.Main
+
+{-- | Calculate absolute humidity (g/m³)
+
+Returns 'Nothing' if the temperature is out of range (−30°C, +35°C).
+
+-}
+absoluteHumidity :: Main -> Maybe Double
+absoluteHumidity Main {temp, humidity}
+ | tC > -30 && tC < 35 = Just $ ahBolton1980 temp humidity
+ | otherwise = Nothing
+ where
+ tC = k2c temp
+
+{-- | Calculate absolute humidity (g/m³)
+
+Ref.: Bolton D. "The Computation of Equivalent Potential Temperature",
+Monthly Weather Review, 1980, 108(7):1046–1053.
+
+-}
+ahBolton1980 ::
+ Double -- ^ Temperature in Kelvins
+ -> Double -- ^ Relative humidity in %
+ -> Double
+ahBolton1980 temp humidity =
+ 13.25 * humidity * exp (17.67 * tC / (tC + 243.5)) / temp
+ where
+ tC = k2c temp
+
+k2c :: Double -> Double
+k2c t = t - 273.15