Skip to content

Commit 5df0776

Browse files
feat(weatherapi): improve daily forecast wind direction accuracy
Use average of all hourly wind degree values instead of noon hour for more representative daily wind direction in forecast data.
1 parent e3dd77e commit 5df0776

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

defaultmodules/weather/providers/weatherapi.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,6 @@ class WeatherAPIProvider {
302302
const dayDate = forecastDay.date_epoch
303303
? new Date(forecastDay.date_epoch * 1000)
304304
: new Date(`${forecastDay.date}T00:00:00`);
305-
const noonHour = forecastDay.hour?.find((entry) => {
306-
if (!entry.time_epoch) return false;
307-
return new Date(entry.time_epoch * 1000).getHours() === 12;
308-
}) ?? forecastDay.hour?.[0];
309305

310306
const precipitationProbability = forecastDay.hour?.length > 0
311307
? (forecastDay.hour.reduce((sum, hourData) => {
@@ -315,6 +311,12 @@ class WeatherAPIProvider {
315311
}, 0) / forecastDay.hour.length) * 100
316312
: null;
317313

314+
const avgWindDegree = forecastDay.hour?.length > 0
315+
? forecastDay.hour.reduce((sum, hourData) => {
316+
return sum + (this.#toNumber(hourData.wind_degree) ?? 0);
317+
}, 0) / forecastDay.hour.length
318+
: null;
319+
318320
weather.date = dayDate;
319321
weather.minTemperature = this.#toNumber(forecastDay.day?.mintemp_c);
320322
weather.maxTemperature = this.#toNumber(forecastDay.day?.maxtemp_c);
@@ -323,11 +325,8 @@ class WeatherAPIProvider {
323325
const maxWind = this.#toNumber(forecastDay.day?.maxwind_kph);
324326
if (maxWind !== null) weather.windSpeed = convertKmhToMs(maxWind);
325327

326-
if (noonHour?.wind_degree !== undefined) {
327-
const windDegree = this.#toNumber(noonHour.wind_degree);
328-
weather.windFromDirection = windDegree !== null
329-
? windDegree
330-
: cardinalToDegrees(noonHour?.wind_dir);
328+
if (avgWindDegree !== null) {
329+
weather.windFromDirection = avgWindDegree;
331330
}
332331

333332
const sunrise = this.#parseSunDatetime(forecastDay, "sunrise");

0 commit comments

Comments
 (0)