Skip to content

Commit

Permalink
block[weather]: Fix nws apparent temperature (#2069)
Browse files Browse the repository at this point in the history
* block[weather]: Fix nws apparent temperature

australian_apparent_temp requires m/s not km/h.
convert temperature back to fahrenheit if original
temperature is in fahrenheit

* block[weather]: nws convert wind speed from km/s to m/s
  • Loading branch information
bim9262 authored Jul 25, 2024
1 parent 509c81d commit 8ea1a2a
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/blocks/weather/nws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ impl ApiForecast {
.to_string()
}

fn wind_speed(&self) -> f64 {
if self.wind_speed.unit_code.ends_with("km_h-1") {
// m/s
self.wind_speed.value / 3.6
} else {
// mph
self.wind_speed.value
}
}

fn wind_kmh(&self) -> f64 {
if self.wind_speed.unit_code.ends_with("km_h-1") {
self.wind_speed.value
Expand All @@ -177,8 +187,14 @@ impl ApiForecast {
(self.temperature.value - 32.0) * 5.0 / 9.0
};
let humidity = self.relative_humidity.value;
let wind_speed = self.wind_kmh();
australian_apparent_temp(temp, humidity, wind_speed)
// wind_speed in m/s
let wind_speed = self.wind_kmh() / 3.6;
let apparent = australian_apparent_temp(temp, humidity, wind_speed);
if self.temperature.unit_code.ends_with("degC") {
apparent
} else {
(apparent * 9.0 / 5.0) + 32.0
}
}

fn to_moment(&self) -> WeatherMoment {
Expand All @@ -191,7 +207,7 @@ impl ApiForecast {
temp: self.temperature.value,
apparent: self.apparent_temp(),
humidity: self.relative_humidity.value,
wind: self.wind_speed.value,
wind: self.wind_speed(),
wind_kmh: self.wind_kmh(),
wind_direction: self.wind_direction(),
}
Expand All @@ -202,7 +218,7 @@ impl ApiForecast {
temp: self.temperature.value,
apparent: self.apparent_temp(),
humidity: self.relative_humidity.value,
wind: self.wind_speed.value,
wind: self.wind_speed(),
wind_kmh: self.wind_kmh(),
wind_direction: self.wind_direction(),
}
Expand Down

0 comments on commit 8ea1a2a

Please sign in to comment.