6.4 KiB
humidity
Functions to compute relative humidity, absolute humidity, wet bulb temperature, and other values related to humidity of air, given partial data.
Purpose
I'm using this code to explore human survivability given global warming. The following essays of mine are relevant:
- The Everyone Dies Event Class
- The 'everyone dies' event comes home
- Extreme Heat
- Analysis of 2026 heatwaves UK
- Exploration of what happened on 26th June this year
Disclaimer
This library is supplied in the hope that it may be useful, but results computed with it should not be taken as authoritative but should be checked against other sources. I do not have enough expertise to guarantee its results are correct.
General architecture
The packet
The 'packet', in the terminology of this library, is a map which has bindings for some of the following keywords:
:absolute-humiditygrammes per metre3.:actual-vapour-pressurePascals:dew-point-celsius:pressure-pascals:pressure-millibars:relative-humiditypercentage, 0...100.:saturation-vapour-pressurePascals:temperatureassumed to be ° Celsius:temperature-celsius:temperature-kelvin:volumein metres3, relative to 1 at standard temperature and pressure.:wet-bulb-temperature-celsiusin ° Celsius.
These keywords represent variables in the pressure equations, and they are all to a degree inter-related; so if you have (or can assume) some of them, you can compute others.
Rather than having a tangle of functions all of which call one another, which seemed likely to result in horrible messes and potential infinite recursion, there is one central function, humidity.core/resolution, which takes a partial packet and repeatedly tries to compute the values for more variables until it can compute no more, and then returns the enhanced packet.
Thus:
humidity.core=> (use 'humidity.core :reload)
nil
humidity.core=> (resolution :relative-humidity 100 :temperature-celsius 35)
{:relative-humidity 100, :temperature-celsius 35, :pressure-pascals 101325, :pressure-millibars 4053/4, :saturation-vapour-pressure 5622.488734516426, :temperature-kelvin 308.15, :absolute-humidity 3953.618101887827}
Naming
Associated with each variable there is a namespace of the same name which contains a function, whose name is the name of the variable with -fn appended, that takes a packet, and, provided that packet contains enough information to compute that variable, returns a numeric value, else nil.
Validation
A packet may be validated by calling the function validate. This recalculates all values in the packet, and, by default, tolerates discrepancies of up to 1%. This tolerance can be varied by binding the dynamic variable *validation-limit*. Validation merely checks that the values within the packet are consistent with one another, and does not properly account for pressure, so will not be valid at altitude.
humidity.core=> (validate {:absolute-humidity 3953.618101887827,
#_=> :actual-vapour-pressure 4765.10094206357,
#_=> :dew-point-celsius 35.00000000000001,
#_=> :pressure-millibars 1013.25,
#_=> :pressure-pascals 101325,
#_=> :relative-humidity 100,
#_=> :saturation-vapour-pressure 5622.488734516426,
#_=> :temperature-celsius 35,
#_=> :temperature-kelvin 308.15,
#_=> :volume 1.1281347245103424,
#_=> :wet-bulb-temperature-celsius 35.11625928298915}
#_=> )
true
humidity.core=> (validate {:absolute-humidity 3953.618101887827,
#_=> :actual-vapour-pressure 4765.10094206357,
#_=> :dew-point-celsius 35.00000000000001,
#_=> :pressure-millibars 1013.25,
#_=> :pressure-pascals 101325,
#_=> :relative-humidity 95, ;; <-- intentionally wrong
#_=> :saturation-vapour-pressure 5622.488734516426,
#_=> :temperature-celsius 35,
#_=> :temperature-kelvin 308.15,
#_=> :volume 1.1281347245103424,
#_=> :wet-bulb-temperature-celsius 35.11625928298915}
#_=> )
ERROR: key: :dew-point-celsius; a: 35.00000000000001; b 34.07737599732603; errror: 2.857142925262451%.
false
humidity.core=> (binding [*validation-limit* 5]
#_=> (validate {:absolute-humidity 3953.618101887827,
#_=> :actual-vapour-pressure 4765.10094206357,
#_=> :dew-point-celsius 35.00000000000001,
#_=> :pressure-millibars 1013.25,
#_=> :pressure-pascals 101325,
#_=> :relative-humidity 95, ;; <-- intentionally wrong
#_=> :saturation-vapour-pressure 5622.488734516426,
#_=> :temperature-celsius 35,
#_=> :temperature-kelvin 308.15,
#_=> :volume 1.1281347245103424,
#_=> :wet-bulb-temperature-celsius 35.11625928298915}))
true
Developing
This is a fairly standard leiningen project. I anticipate that most Clojure developers will have leininen installed, even if they no longer use it much. Start a repl with
lein repl
and hook in your preferred editor as appropriate for that editor.
Full documentation is available here.
Testing
Tests are reasonably complets but all could do with more examples, especially around the crucial 35° C wet bulb area and towards the extremities of normal weather range. If you are contributing additional code, I would appreciate it being accompanied by tests.
I suggest running tests using
lein cloverage
As this gives not only pass/fail output, but also test coverage output.
Contributing
Pull requests are welcomed, provided they do not contain AI generated code. I'm not going to waste time debugging Claude's mess, my own is bad enough.
License
Copyright © 2026 Simon Brooke. Licensed under the GNU General Public License, version 2.0 or (at your option) any later version.