Metric implementation

This commit is contained in:
2025-01-04 20:54:36 -05:00
parent 23da46fe07
commit 59b598c6b3
13 changed files with 290 additions and 96 deletions

View File

@ -4,18 +4,13 @@ import "math"
// Attempts to complete missing fields that may not
// be set by a specific provider, such as DewPoint and WindChill
//
// If enrich is called repeated with the same station ID, measurements
// will be recorded to produce averages. This will be more stable
// and support scaling if Redis is available
// TODO: Implement average tracker
func (u *WeatherUpdate) Enrich() {
if u.WindChillF == 0 {
u.WindChillF = CalculateWindChill(u.TempF, u.WindSpeedMPH)
u.WindChillF = CalculateWindChill(u.TempOutdoorF, u.WindSpeedMPH)
}
if u.DewPointF == 0 {
u.DewPointF = CalculateDewPoint(u.TempF, float32(u.Humidity))
u.DewPointF = CalculateDewPoint(u.TempOutdoorF, float64(u.HumidityOudoor))
}
if u.BaromAbsoluteIn == 0 {
@ -23,15 +18,15 @@ func (u *WeatherUpdate) Enrich() {
}
}
func CalculateDewPoint(tempF, humidity float32) float32 {
func CalculateDewPoint(tempF, humidity float64) float64 {
// Convert temperature from Fahrenheit to Celsius
tempC := (tempF - 32) * 5 / 9
// Calculate the dew point using the Magnus-Tetens approximation
a := float32(17.27)
b := float32(237.7)
a := float64(17.27)
b := float64(237.7)
alpha := (a*tempC)/(b+tempC) + float32(math.Log(float64(humidity)/100))
alpha := (a*tempC)/(b+tempC) + math.Log(humidity/100)
dewPointC := (b * alpha) / (a - alpha)
// Convert dew point back to Fahrenheit
@ -39,16 +34,14 @@ func CalculateDewPoint(tempF, humidity float32) float32 {
return dewPointF
}
func CalculateWindChill(tempF float32, windSpeedMPH float32) float32 {
func CalculateWindChill(tempF float64, windSpeedMPH float64) float64 {
if windSpeedMPH <= 3 {
// Wind chill calculation doesn't apply for very low wind speeds
return tempF
}
// Formula for calculating wind chill
return float32(
35.74 + 0.6215*float64(tempF) -
35.75*math.Pow(float64(windSpeedMPH), 0.16) +
0.4275*float64(tempF)*
math.Pow(float64(windSpeedMPH), 0.16))
return 35.74 + 0.6215*tempF -
35.75*math.Pow(windSpeedMPH, 0.16) +
0.4275*tempF*math.Pow(windSpeedMPH, 0.16)
}