-
Notifications
You must be signed in to change notification settings - Fork 0
/
libwx.go
279 lines (256 loc) · 8.72 KB
/
libwx.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package libwx
// libwx: a variety of weather-related calculations, collected from around the internet
// and implemented in Go by Chris Dzombak <github.com/cdzombak>.
//
// Also included are some simple type definitions to help avoid erroneously mixing units.
import (
"errors"
"math"
)
var ErrInputRange = errors.New("one or more input values are outside the calculation's supported range")
// DewPointF calculates the dew point given the current temperature (in Fahrenheit)
// and relative humidity percentage (an integer 0-100, *not* a float 0.0-1.0).
func DewPointF(t TempF, rh RelHumidity) TempF {
return DewPointC(t.C(), rh).F()
}
// DewPointC calculates the dew point given the current temperature (in Celsius)
// and relative humidity percentage (an integer 0-100, *not* a float 0.0-1.0).
func DewPointC(t TempC, rh RelHumidity) TempC {
rh = rh.Clamped()
const (
a = 17.625
b = 243.04
)
alpha := math.Log(float64(rh)/100.0) + a*float64(t)/(b+float64(t))
return TempC((b * alpha) / (a - alpha))
}
// WindChillF calculates the wind chill for the given temperature (in Fahrenheit)
// and wind speed (in miles/hour).
// If wind speed is less than 3 mph, or temperature is over 50 degrees F, the
// given temperature is returned - the formula works below 50 degrees F and above 3 mph.
func WindChillF(t TempF, windSpeed SpeedMph) TempF {
if t > 50.0 || windSpeed < 3.0 {
return t
}
return TempF(35.74 + (0.6215 * float64(t)) - (35.75 * math.Pow(windSpeed.Unwrap(), 0.16)) + (0.4275 * float64(t) * math.Pow(windSpeed.Unwrap(), 0.16)))
}
// WindChillFWithValidation calculates the wind chill for the given temperature (in Fahrenheit)
// and wind speed (in miles/hour).
// If wind speed or temperature are outside the supported range, ErrInputRange is returned.
func WindChillFWithValidation(t TempF, windSpeed SpeedMph) (TempF, error) {
if t > 50.0 || windSpeed < 3.0 {
return t, ErrInputRange
}
return WindChillF(t, windSpeed), nil
}
// WindChillC calculates the wind chill for the given temperature (in Celsius)
// and wind speed (in miles/hour).
// If wind speed is less than 3 mph, or temperature is over 10 degrees C, the
// given temperature is returned - the formula works below 10 degrees C and above 3 mph.
func WindChillC(temp TempC, windSpeed SpeedMph) TempC {
return WindChillF(temp.F(), windSpeed).C()
}
// WindChillCWithValidation calculates the wind chill for the given temperature (in Celsius)
// and wind speed (in miles/hour).
// If wind speed or temperature are outside the supported range, ErrInputRange is returned.
func WindChillCWithValidation(temp TempC, windSpeed SpeedMph) (TempC, error) {
if temp.F() > 50.0 || windSpeed < 3.0 {
return temp, ErrInputRange
}
return WindChillF(temp.F(), windSpeed).C(), nil
}
// IndoorHumidityRecommendationF returns the maximum recommended indoor relative
// humidity percentage for the given outdoor temperature (in degrees F).
func IndoorHumidityRecommendationF(outdoorT TempF) RelHumidity {
if outdoorT >= 50 {
return 50
}
if outdoorT >= 40 {
return 45
}
if outdoorT >= 30 {
return 40
}
if outdoorT >= 20 {
return 35
}
if outdoorT >= 10 {
return 30
}
if outdoorT >= 0 {
return 25
}
if outdoorT >= -10 {
return 20
}
return 15
}
// IndoorHumidityRecommendationC returns the maximum recommended indoor relative
// humidity percentage for the given outdoor temperature (in degrees C).
func IndoorHumidityRecommendationC(outdoorT TempC) RelHumidity {
return IndoorHumidityRecommendationF(outdoorT.F())
}
// WetBulbF calculates the wet bulb temperature (in Fahrenheit) given a dry bulb
// temperature (in Fahrenheit) and relative humidity percentage.
// If the given temperature or relative humidity are outside the supported range,
// ErrInputRange is returned.
// See: https://journals.ametsoc.org/view/journals/apme/50/11/jamc-d-11-0143.1.xml
func WetBulbF(temp TempF, rh RelHumidity) (TempF, error) {
result, err := WetBulbC(temp.C(), rh)
return result.F(), err
}
// WetBulbC calculates the wet bulb temperature (in Celsius) given a dry bulb
// temperature (in Celsius) and relative humidity percentage.
// If the given temperature or relative humidity are outside the supported range,
// ErrInputRange is returned.
// See: https://journals.ametsoc.org/view/journals/apme/50/11/jamc-d-11-0143.1.xml
func WetBulbC(temp TempC, rh RelHumidity) (TempC, error) {
rh = rh.Clamped()
if rh.Unwrap() < 5 || rh.Unwrap() > 99 {
return temp, ErrInputRange
}
if temp.Unwrap() < -20 || temp.Unwrap() > 50 {
return temp, ErrInputRange
}
// formula for the left validity border line:
// y = -1*(75-5)/(20+9.25) * x + 25
// where y == RH% and x == dry bulb degC
// see full-jamc-d-11-0143.1-f3-annotated.jpg in this repo, taken from
// https://journals.ametsoc.org/view/journals/apme/50/11/jamc-d-11-0143.1.xml
// and annotated
y := -1*(75-5)/(20+9.25)*temp.Unwrap() + 25
if y > rh.UnwrapFloat64() {
return temp, ErrInputRange
}
// Tw = T*atan[0.151977(RH% + 8.313659)**1/2] + atan(T + RH%) - atan(RH% - 1.676331)
// + 0.00391838(RH%)**3/2 * atan(0.023101*RH%) - 4.686035
// taken from figure 1 of https://journals.ametsoc.org/view/journals/apme/50/11/jamc-d-11-0143.1.xml
return TempC(
temp.Unwrap()*math.Atan(0.151977*math.Pow(rh.UnwrapFloat64()+8.313659, 0.5)) +
math.Atan(temp.Unwrap()+rh.UnwrapFloat64()) -
math.Atan(rh.UnwrapFloat64()-1.676331) +
0.00391838*math.Pow(rh.UnwrapFloat64(), 1.5)*math.Atan(0.023101*rh.UnwrapFloat64()) -
4.686035,
),
nil
}
func heatIndexConstantsF() [9]float64 {
// from https://en.wikipedia.org/wiki/Heat_index#Formula
// captured on 2024-07-17
return [9]float64{
-42.379,
2.04901523,
10.14333127,
-0.22475541,
-6.83783e-3,
-5.481717e-2,
1.22874e-3,
8.5282e-4,
-1.99e-6,
}
}
func heatIndexConstantsC() [9]float64 {
// from https://en.wikipedia.org/wiki/Heat_index#Formula
// captured on 2024-07-17
return [9]float64{
-8.78469475556,
1.61139411,
2.33854883889,
-0.14611605,
-0.012308094,
-0.0164248277778,
2.211732e-3,
7.2546e-4,
-3.582e-6,
}
}
func rawHeatIndex(c [9]float64, rawTemp, rawRelH float64) float64 {
// from https://en.wikipedia.org/wiki/Heat_index#Formula
// captured on 2024-07-17
// note that constants on that page are 1-indexed, while the constants
// array here is 0-indexed
// see also: https://www.weather.gov/media/ffc/ta_htindx.PDF
return c[0] +
c[1]*rawTemp +
c[2]*rawRelH +
c[3]*rawTemp*rawRelH +
c[4]*math.Pow(rawTemp, 2) +
c[5]*math.Pow(rawRelH, 2) +
c[6]*math.Pow(rawTemp, 2)*rawRelH +
c[7]*rawTemp*math.Pow(rawRelH, 2) +
c[8]*math.Pow(rawTemp, 2)*math.Pow(rawRelH, 2)
}
// HeatIndexF is deprecated; use HeatIndexFWithValidation
func HeatIndexF(temp TempF, rh RelHumidity) TempF {
retv, _ := HeatIndexFWithValidation(temp, rh)
return retv
}
// HeatIndexC is deprecated; use HeatIndexCWithValidation
func HeatIndexC(temp TempC, rh RelHumidity) TempC {
retv, _ := HeatIndexCWithValidation(temp, rh)
return retv
}
// HeatIndexFWithValidation calculates the heat index for the given temperature (in Fahrenheit)
// and relative humidity percentage.
func HeatIndexFWithValidation(temp TempF, rh RelHumidity) (TempF, error) {
var err error
if temp < TempC(25).F() {
err = ErrInputRange
}
return TempF(rawHeatIndex(
heatIndexConstantsF(),
temp.Unwrap(),
rh.Clamped().UnwrapFloat64(),
)), err
}
// HeatIndexCWithValidation calculates the heat index for the given temperature (in Celsius)
// and relative humidity percentage.
func HeatIndexCWithValidation(temp TempC, rh RelHumidity) (TempC, error) {
var err error
if temp < TempC(25) {
err = ErrInputRange
}
return TempC(rawHeatIndex(
heatIndexConstantsC(),
temp.Unwrap(),
rh.Clamped().UnwrapFloat64(),
)), err
}
// HeatIndexWarningF returns a heat index warning level for the
// given heat index temperature (in Fahrenheit) per
// https://en.wikipedia.org/wiki/Heat_index#Table_of_values
// captured on 2024-07-17.
func HeatIndexWarningF(heatIndex TempF) HeatIndexWarning {
if heatIndex.Unwrap() < 80 {
return HeatIndexWarningNone
}
if heatIndex.Unwrap() < 91 {
return HeatIndexWarningCaution
}
if heatIndex.Unwrap() < 104 {
return HeatIndexWarningExtremeCaution
}
if heatIndex.Unwrap() < 125 {
return HeatIndexWarningDanger
}
return HeatIndexWarningExtremeDanger
}
// HeatIndexWarningC returns a heat index warning level for the
// given heat index temperature (in Celsius) per
// https://en.wikipedia.org/wiki/Heat_index#Table_of_values
// captured on 2024-07-17.
func HeatIndexWarningC(heatIndex TempC) HeatIndexWarning {
if heatIndex.Unwrap() < 27 {
return HeatIndexWarningNone
}
if heatIndex.Unwrap() < 33 {
return HeatIndexWarningCaution
}
if heatIndex.Unwrap() < 40 {
return HeatIndexWarningExtremeCaution
}
if heatIndex.Unwrap() < 52 {
return HeatIndexWarningDanger
}
return HeatIndexWarningExtremeDanger
}