diff --git a/aemet_opendata/const.py b/aemet_opendata/const.py index 6b9ac4a..3b9921d 100644 --- a/aemet_opendata/const.py +++ b/aemet_opendata/const.py @@ -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 diff --git a/aemet_opendata/forecast.py b/aemet_opendata/forecast.py index c8e7ebf..e5c32d3 100644 --- a/aemet_opendata/forecast.py +++ b/aemet_opendata/forecast.py @@ -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, @@ -595,7 +596,8 @@ 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: @@ -603,11 +605,17 @@ def parse_interval_value( 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: @@ -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