aboutsummaryrefslogtreecommitdiff
path: root/lib/Web/OpenWeatherMap
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2021-05-12 16:34:03 +0200
committerIgor Pashev <pashev.igor@gmail.com>2021-05-12 16:34:03 +0200
commitbf844972aad8839d430be88b14097fd4cdaec059 (patch)
tree67ab5796cbbe2665914bdab3ecbe4bab4236c3ca /lib/Web/OpenWeatherMap
parent9cfef7b14ac5af7109449b54b1cb352b4c76167a (diff)
downloadopenweathermap-bf844972aad8839d430be88b14097fd4cdaec059.tar.gz
Show absolute humidity
Diffstat (limited to 'lib/Web/OpenWeatherMap')
-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