001  (ns ^{:authority "https://journals.ametsoc.org/view/journals/apme/35/4/1520-0450_1996_035_0601_imfaos_2_0_co_2.xml/"
002        :author "simon"
003        :doc "Function to return relative humidity."} humidity.relative-humidity
004    (:require [humidity.constants :refer [e Rw]]
005              [humidity.utils :refer [expt]]))
006  
007  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
008  ;;;
009  ;;; Copyright (C) 2026 Simon Brooke
010  ;;;
011  ;;; This program is free software; you can redistribute it and/or
012  ;;; modify it under the terms of the GNU General Public License
013  ;;; as published by the Free Software Foundation; either version 2
014  ;;; of the License, or (at your option) any later version.
015  ;;;
016  ;;; This program is distributed in the hope that it will be useful,
017  ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
018  ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
019  ;;; GNU General Public License for more details.
020  ;;;
021  ;;; You should have received a copy of the GNU General Public License
022  ;;; along with this program; if not, write to the Free Software
023  ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
024  ;;; USA.
025  ;;;
026  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
027  
028  (def ^:const magic-numbers
029    "The magic numbers (empirical constants) from the derived expression. It
030     bothers me that all these formulae depend on 'empirical constants', *and*
031     that there is no commonality between the empirical constants of different
032     formulae."
033    [0 17.625 243.04])
034  
035  (def C magic-numbers)
036  
037  ;;        / / relative-humidity x saturation-pressure \ \
038  ;;        | | --------------------------------------- | |
039  ;; 1000 * | \                  Rw                     / | = absolute-humidity
040  ;;        | ------------------------------------------- |
041  ;;        \             temperature-kelvin              /
042  ;;
043  ;; divide both sides by 1000 ==>
044  ;; / / relative-humidity x saturation-pressure \ \
045  ;; | | --------------------------------------- | |   / absolute-humidity \
046  ;; | \                  Rw                     / | = | ----------------- |
047  ;; | ------------------------------------------- |   \       1000        /
048  ;; \             temperature-kelvin              /
049  ;;
050  ;; multiply both sides by temperature-kelvin ==>
051  ;; / relative-humidity x saturation-pressure \   / absolute-humidity x temperature-kelvin \
052  ;; | --------------------------------------- | = | -------------------------------------- |
053  ;; \                  Rw                     /   \                  1000                  /
054  ;;
055  ;; Multiply both sides by Rw ==>
056  ;;                                           absolute-humidity x temperature-kelvin x Rw
057  ;; relative-humidity x saturation-pressure = -------------------------------------------
058  ;;                                                                 1000
059  ;;
060  ;; Divide both sides by saturation-pressure ==>
061  ;;
062  ;;                      absolute-humidity x temperature-kelvin x Rw
063  ;; relative-humidity  = -------------------------------------------
064  ;;                               1000 x saturation-pressure
065  
066  (defn relative-humidity-fn
067    "Returns relative humidity (as a percentage, 0...100) given values for dew
068     point and temperature in Celsius or for absolute humidity, saturation
069     vapour pressure, temperature in Kelvin."
070    [& {:keys [absolute-humidity dew-point saturation-vapour-pressure
071      temperature-celsius temperature-kelvin]}]
072    (cond (and dew-point temperature-celsius)
073          (* 100 (/ (expt e (/ (* (C 1) dew-point)
074                               (+ (C 2) dew-point)))
075                    (expt e (/ (* (C 1) temperature-celsius)
076                               (+ (C 2) temperature-celsius)))))
077          (and absolute-humidity saturation-vapour-pressure temperature-kelvin)
078          (/ (* absolute-humidity temperature-kelvin Rw)
079            (* 1000 saturation-vapour-pressure))))