aboutsummaryrefslogtreecommitdiff
path: root/lib/Web/OpenWeatherMap/Formulas.hs
blob: f63bc9f5f9f7916f4696a2530e3e303d564da6dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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