Skip to content

Commit

Permalink
forecast: parse_interval_value: fix for only first interval provided
Browse files Browse the repository at this point in the history
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
  • Loading branch information
Noltari committed Feb 28, 2024
1 parent 88ead54 commit d142150
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions aemet_opendata/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
AOD_WIND_SPEED_MAX: Final[str] = "wind-speed-max"

API_ID_PFX: Final[str] = "id"
API_MAX_PERIOD_OFFSET: Final[int] = 2
API_MIN_STATION_DISTANCE_KM: Final[int] = 40
API_MIN_TOWN_DISTANCE_KM: Final[int] = 40
API_PERIOD_24H: Final[int] = 24
Expand Down
18 changes: 13 additions & 5 deletions aemet_opendata/forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
AOD_WIND_DIRECTION,
AOD_WIND_SPEED,
AOD_WIND_SPEED_MAX,
API_MAX_PERIOD_OFFSET,
API_PERIOD_24H,
API_PERIOD_FULL_DAY,
API_PERIOD_HALF_2_DAY,
Expand Down Expand Up @@ -595,19 +596,26 @@ def parse_interval_value(
self, values: Any, hour: int, key: str = AEMET_ATTR_VALUE
) -> Any:
"""Parse Town hourly forecast interval value from data."""
period_offset = None
min_period_start = None
min_period_end = None

for value in values:
if key not in value:
continue
period = value[AEMET_ATTR_PERIOD]
period_start = int(period[0:API_PERIOD_SPLIT])
period_end = int(period[API_PERIOD_SPLIT : API_PERIOD_SPLIT * 2])
if min_period_start is None or period_start < min_period_start:
min_period_start = period_start
if period_end < period_start:
if period_offset is None or period_end < period_offset:
period_offset = period_end
if min_period_end is None or period_end < min_period_end:
min_period_end = period_end

if period_offset is None:
if min_period_end is not None and min_period_end <= API_MAX_PERIOD_OFFSET:
period_offset = min_period_end
elif min_period_start is not None and min_period_start <= API_MAX_PERIOD_OFFSET:
period_offset = min_period_start
else:
period_offset = 0

for value in values:
Expand All @@ -623,7 +631,7 @@ def parse_interval_value(
if hour == 0:
hour = hour + API_PERIOD_24H
if period_start <= hour < period_end:
return None if not value[key] else value[key]
return None if not value[key] else value[key]

return None

Expand Down

0 comments on commit d142150

Please sign in to comment.