Skip to content

Commit

Permalink
[2024-04-17 11:17] Package updates, including vulnerability updates
Browse files Browse the repository at this point in the history
Signed-off-by: Raoul Linnenbank <[email protected]>
  • Loading branch information
rflinnenbank committed Apr 17, 2024
1 parent 6a93c48 commit ea7cb25
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
10 changes: 7 additions & 3 deletions tests/test_knmi_actuele_waarnemingen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
import requests
import xarray as xr

from weather_provider_api.routers.weather.sources.knmi.models.actuele_waarnemingen import ActueleWaarnemingenModel
from weather_provider_api.routers.weather.sources.knmi.utils import download_actuele_waarnemingen_weather, \
_retrieve_observation_moment
from weather_provider_api.routers.weather.sources.knmi.models.actuele_waarnemingen import (
ActueleWaarnemingenModel,
)
from weather_provider_api.routers.weather.sources.knmi.utils import (
_retrieve_observation_moment,
download_actuele_waarnemingen_weather,
)
from weather_provider_api.routers.weather.utils.geo_position import GeoPosition


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

"""KNMI hour models data fetcher.
"""
"""KNMI hour models data fetcher."""
import copy
import json
from datetime import datetime
Expand All @@ -27,11 +26,7 @@


class UurgegevensModel(WeatherModelBase):
"""
A Weather Model that incorporates the:
KNMI Uurgegevens
dataset into the Weather Provider API
"""
"""A Weather Model that incorporates the: KNMI Uurgegevens dataset into the Weather Provider API."""

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -140,18 +135,20 @@ def get_weather(
coords: List[GeoPosition],
begin: datetime,
end: datetime,
weather_factors: List[str] = None,
weather_factors: List[str] | None = None,
) -> xr.Dataset:
"""
The function that gathers and processes the requested Daggegevens weather data from the KNMI site
and returns it as a Xarray Dataset.
(Though this model downloads from a specific download url, the question remains whether this source is also
listed on the new KNMI Data Platform)
"""The function that gathers and processes the requested Daggegevens weather data from the KNMI site
and returns it as a Xarray Dataset.
(Though this model downloads from a specific download url, the question remains whether this source is also
listed on the new KNMI Data Platform).
Args:
coords: A list of GeoPositions containing the locations the data is requested for.
begin: A datetime containing the start of the period to request data for.
end: A datetime containing the end of the period to request data for.
weather_factors: A list of weather factors to request data for (in string format)
Returns:
An Xarray Dataset containing the weather data for the requested period, locations and factors.
"""
Expand Down Expand Up @@ -186,7 +183,7 @@ def _download_weather(
stations: List[int],
start: datetime,
end: datetime,
weather_factors: List[str] = None,
weather_factors: List[str] | None = None,
):
"""
A function that downloads the weather from the KNMI download location and returns it as a text
Expand All @@ -200,7 +197,7 @@ def _download_weather(
"""
# fetch data
params = self._create_request_params(start, end, stations, weather_factors)
r = requests.post(url=self.download_url, data=params)
r = requests.post(url=self.download_url, data=params, timeout=60)

if r.status_code != 200:
raise requests.HTTPError(
Expand All @@ -216,14 +213,15 @@ def _download_weather(
)
return r.text

def _create_request_params(self, start, end, stations, weather_factors):
"""
A Function that transforms the request settings into parameters usable for the KMNI download request.
def _create_request_params(self, start: datetime, end: datetime, stations: list[int], weather_factors: list[str]):
"""A Function that transforms the request settings into parameters usable for the KMNI download request.
Args:
start: A datetime containing the start of the period to request data for.
end: A datetime containing the end of the period to request data for.
stations: A list containing the requested stations
weather_factors: A list of weather factors to request data for (in string format)
Returns:
A params field (string) containing the matching settings for a KNMI Daggegevens download request.
"""
Expand Down Expand Up @@ -271,10 +269,11 @@ def _parse_raw_weather_data(self, raw_data: str) -> xr.Dataset:
return dataframe_data.to_xarray()

@staticmethod
def _prepare_weather_data(coordinates: List[GeoPosition], station_id, raw_ds):
# A function that prepares the weather data for return by the API, by replacing the matching station with the
# lat/lon location that was requested, and properly formatting the dimensions.
def _prepare_weather_data(coordinates: List[GeoPosition], station_id: list[np.int64], raw_ds: xr.Dataset):
"""A function that prepares the weather data for return by the API, by replacing the matching station with the
lat/lon location that was requested, and properly formatting the dimensions.
"""
# re-arrange stns
ds = raw_ds.sel(station_code=station_id)

Expand All @@ -290,7 +289,9 @@ def _prepare_weather_data(coordinates: List[GeoPosition], station_id, raw_ds):
return ds

def _request_weather_factors(self, factors: Optional[List[str]]) -> List[str]:
# Implementation of the Base Weather Model function that returns a list of known weather factors for the model.
"""Implementation of the Base Weather Model function that returns a list of known weather factors for the
model.
"""
if factors is None:
return list(self.to_si.keys())

Expand Down
2 changes: 1 addition & 1 deletion weather_provider_api/routers/weather/utils/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def get_weather_slice_for_coords(coord, unserialized_data) -> pd.DataFrame:
if isinstance(coord, list):
coord = coord[0]
weather = unserialized_data.sel(lat=coord[0], lon=coord[1], method="nearest")
if weather.dims["time"] == 1:
if weather.sizes["time"] == 1:
# Because a single moment in time can't be squeezed...
df = weather.to_dataframe()
else:
Expand Down

0 comments on commit ea7cb25

Please sign in to comment.