ambient-local-exporter/pkg/weather/enrich.go

48 lines
1.2 KiB
Go
Raw Normal View History

2025-01-04 22:51:55 +00:00
package weather
import "math"
// Attempts to complete missing fields that may not
// be set by a specific provider, such as DewPoint and WindChill
func (u *WeatherUpdate) Enrich() {
if u.WindChillF == 0 {
2025-01-05 01:54:36 +00:00
u.WindChillF = CalculateWindChill(u.TempOutdoorF, u.WindSpeedMPH)
2025-01-04 22:51:55 +00:00
}
if u.DewPointF == 0 {
2025-01-05 01:54:36 +00:00
u.DewPointF = CalculateDewPoint(u.TempOutdoorF, float64(u.HumidityOudoor))
2025-01-04 22:51:55 +00:00
}
if u.BaromAbsoluteIn == 0 {
u.BaromAbsoluteIn = u.BaromRelativeIn
}
}
2025-01-05 01:54:36 +00:00
func CalculateDewPoint(tempF, humidity float64) float64 {
2025-01-04 22:51:55 +00:00
// Convert temperature from Fahrenheit to Celsius
tempC := (tempF - 32) * 5 / 9
// Calculate the dew point using the Magnus-Tetens approximation
2025-01-05 01:54:36 +00:00
a := float64(17.27)
b := float64(237.7)
2025-01-04 22:51:55 +00:00
2025-01-05 01:54:36 +00:00
alpha := (a*tempC)/(b+tempC) + math.Log(humidity/100)
2025-01-04 22:51:55 +00:00
dewPointC := (b * alpha) / (a - alpha)
// Convert dew point back to Fahrenheit
dewPointF := (dewPointC * 9 / 5) + 32
return dewPointF
}
2025-01-05 01:54:36 +00:00
func CalculateWindChill(tempF float64, windSpeedMPH float64) float64 {
2025-01-04 22:51:55 +00:00
if windSpeedMPH <= 3 {
// Wind chill calculation doesn't apply for very low wind speeds
return tempF
}
// Formula for calculating wind chill
2025-01-05 01:54:36 +00:00
return 35.74 + 0.6215*tempF -
35.75*math.Pow(windSpeedMPH, 0.16) +
0.4275*tempF*math.Pow(windSpeedMPH, 0.16)
2025-01-04 22:51:55 +00:00
}