Skip to content

Instantly share code, notes, and snippets.

@mc3k
Last active December 31, 2024 23:41
Show Gist options
  • Save mc3k/de289c52e95c2a82f6ef726fd6ee9a13 to your computer and use it in GitHub Desktop.
Save mc3k/de289c52e95c2a82f6ef726fd6ee9a13 to your computer and use it in GitHub Desktop.
Calculate Heat Index from Temperature and Relative Humidity
import math, sys, os
def heat_index_wpc(temp,humid):
# https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
T = float(temp)*9/5 +32
RH = float(humid)
HI = -42.379 + 2.04901523*T + 10.14333127*RH - .22475541*T*RH - .00683783*T*T - .05481717*RH*RH + .00122874*T*T*RH + .00085282*T*RH*RH - .00000199*T*T*RH*RH
if RH < 13 and T > 80 and T < 112 :
HI = HI - ((13-RH)/4)*math.sqrt((17-abs(T-95))/17)
if RH > 85 and T > 80 and T < 87 :
HI = HI + ((RH-82)/10)*((87-T)/5)
if HI < 80 :
HI = 0.5 * (T + 61.0 + ((T-68.0)*1.2) + (RH*0.094))
HI = (HI-32)*5/9
T = (T-32)*5/9
return HI
def heat_index_calc(temp,humid,calc=None) :
# https://en.wikipedia.org/wiki/Heat_index
T = float(temp)*9/5 +32 # to degF
R = float(humid)
# Coefficients
C1 = [ 0, -42.379, 2.04901523, 10.14333127, -0.22475541, -6.83783e-03, -5.481717e-02, 1.22874e-03, 8.5282e-04, -1.99e-06]
C2 = [ 0, 0.363445176, 0.988622465, 4.777114035, -0.114037667, -8.50208e-04, -2.0716198e-02, 6.87678e-04, 2.74954e-04, 0]
C3 = [ 0, 16.923, 0.185212, 5.37941, -0.100254, 9.41695e-03, 7.28898e-03, 3.45372e-04, -8.14971e-04, 1.02102e-05, -3.8646e-05, 2.91583e-05, 1.42721e-06, 1.97483e-07, -2.18429e-08, 8.43296e-10, -4.81975e-11]
if T >= 80 and R >=40 :
HI = C1[1] + C1[2]*T + C1[3]*R + C1[4]*T*R + C1[5]*T*T + C1[6]*R*R + C1[7]*T*T*R + C1[8]*T*R*R + C1[9]*T*T*R*R
elif T >= 70 and T <= 115 and R <=80 and calc != 'alt':
HI = C2[1] + C2[2]*T + C2[3]*R + C2[4]*T*R + C2[5]*T*T + C2[6]*R*R + C2[7]*T*T*R + C2[8]*T*R*R + C2[9]*T*T*R*R
elif T >= 70 and T <= 115 and R <=80 and calc == 'alt':
HI = C3[1] + C3[2]*T + C3[3]*R + C3[4]*T*R + C3[5]*T*T + C3[6]*R*R + C3[7]*T*T*R + C3[8]*T*R*R + C1[9]*T*T*R*R + C3[10]*T*T*T + C3[11]*R*R*R + C3[12]*T*T*T*R + C3[13]*T*R*R*R + C3[14]*T*T*T*R*R + C3[15]*T*T*R*R*R + C3[16]*T*T*T*R*R*R
else :
HI = T
HI = (HI-32)*5/9
return HI
if __name__ == "__main__":
temp, humid, calc = None, None, ''
if len(sys.argv) > 2 :
temp = float(sys.argv[1])
humid = float(sys.argv[2])
if len(sys.argv) > 3 :
calc = sys.argv[3].lower()
if temp is not None and humid is not None :
if calc == 'wpc' :
HI = heat_index_wpc(temp,humid)
else :
HI = heat_index_calc(temp,humid,calc)
print('Temperature = '+str(round(temp,1))+'°C')
print('Humidity = '+str(round(humid,1))+'%')
print('Heat Index = '+str(round(HI,1))+'°C')
else :
print('Usage: '+os.path.basename(os.path.realpath(__file__))+' [temp] [humidity]')
@mc3k
Copy link
Author

mc3k commented Dec 31, 2024

Command line usage:
heatindex.py temperature humidity [wpc | alt]

or call functions directly:
HI = heat_index_calc(temp,humid)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment