API reference¶
The Weather Controller class¶
WeatherController ¶
Controller for the Weather Provider API.
This class is responsible for handling all requests for weather data, including fetching data from the appropriate sources and models, converting units, and formatting the output.
get_weather ¶
get_weather(source_id: str, model_id: str, fetch_async: bool, coords: list[list[tuple[float, float]]], begin: datetime | None = None, end: datetime | None = None, factors: list[str] | None = None) -> xr.Dataset | None
Get specific weather factors for a specific time and specific location(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_id
|
str
|
The weather source that need to be queried (e.g.: knmi, cds) |
required |
model_id
|
str
|
The model identifier of the model that needs to be queried (has to be a model that exists within the specific source requested through source_id |
required |
fetch_async
|
bool
|
A boolean indicated if the request was made to asynchronously fetch the data or not |
required |
coords
|
list[list[tuple[float, float]]]
|
A nested 3-layer list representing a list of polygons in the case of points, they are treated as a one-point polygon D0: different polygons D1: sequence of points in each polygon D2: coordinates of each point (lat, lon in coordinates) |
required |
begin
|
datetime | None
|
The starting time of the requested output data |
None
|
end
|
datetime | None
|
The ending time of the requested output data |
None
|
factors
|
list[str] | None
|
A list of the requested weather factors for the output (default is all available) |
None
|
Returns:
| Type | Description |
|---|---|
Dataset | None
|
A Xarray Dataset containing the weather data for the selected model, period(s), location(s) and factor(s) |
convert_names_and_units ¶
convert_names_and_units(source_id: str, model_id: str, fetch_async: bool, weather_data: Dataset, unit: OutputUnit) -> xr.Dataset
Convert the names and units of the weather data to match the requested output unit format.
lat_lon_to_coords
staticmethod
¶
Convert a single pair of coordinates into a nested list format representing a single-point polygon.
str_to_coords
staticmethod
¶
Convert a string containing coordinates into a list of tuples containing those coordinates.
get_source ¶
Get a specific source by its ID.
get_models ¶
Get a list of all available models for a specific source.
get_model ¶
Get a specific model by its ID for a specific source.
Weather Base classes¶
WeatherSourceBase ¶
Base class that contains the basic functionality for all sources.
Any new sources should implement this as their base class!
async_models
property
¶
Get all asynchronous models from the source.
__init__ ¶
__init__(source_id: str, name: str, url: str, model_instances: list[WeatherModelBase], *args: Any, **kwargs: Any) -> None
Initialize the WeatherSourceBase with an ID and set up the models.
get_model ¶
Get a specific model from the source based on the provided model ID and whether it is an asynchronous request or not.
get_models ¶
Get all models from the source based on whether it is an asynchronous request or not.
WeatherModelBase ¶
Base class for all Weather Models. All new models should use this base class!
get_weather
abstractmethod
¶
get_weather(coords: list[GeoPosition], begin: datetime | None = None, end: datetime | None = None, weather_factors: list[str] | None = None) -> xr.Dataset
Abstract method to get weather data for the specified coordinates and time range.
is_async
abstractmethod
¶
Abstract method to determine if the model is asynchronous.
convert_names_and_units ¶
Convert the names and units of the weather data to match the requested output unit format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weather_data
|
Dataset
|
A Xarray Dataset containing |
required |
unit
|
OutputUnit
|
The requested output unit format |
required |
Returns:
| Type | Description |
|---|---|
Dataset
|
The same dataset, but with values altered to match the requested output unit format |
celsius_to_kelvin
staticmethod
¶
Convert a temperature from Celsius to Kelvin.
kelvin_to_celsius
staticmethod
¶
Convert a temperature from Kelvin to Celsius.
tenth_celsius_to_kelvin ¶
Convert a temperature from tenths of Celsius to Kelvin.
normalize_tenths
staticmethod
¶
Normalize a value in tenths to its actual value.
percentage_to_frac
staticmethod
¶
Convert a percentage value to a fraction.
kmh_to_ms
staticmethod
¶
Convert a speed from kilometers per hour to meters per second.
dutch_wind_direction_to_degrees
staticmethod
¶
Convert a Dutch wind direction string to degrees.
knmi_visibility_class_to_meter_estimate
staticmethod
¶
Function to transform KNMI visibility class values to an estimate of meters visibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xs
|
int
|
The visibility class value to be interpreted |
required |
Returns:
| Type | Description |
|---|---|
float
|
A numeric value containing an estimate of the meters of visibility matching the given visibility class value |
WeatherRepositoryBase ¶
Bases: ABC
Base class for weather repositories.
absolute_storage_path
property
¶
Get the absolute path where the weather data will be stored.
source_and_model
property
¶
Get the source and model name affiliated with this repository.
oldest_date_available
property
¶
Get the oldest date for which weather data is available in the repository.
newest_date_available
property
¶
Get the newest date for which weather data is available in the repository.
update
abstractmethod
¶
Update the repository with new weather data up to the specified date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_in_testmode
|
bool
|
Whether to run the update in test mode. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
RepoUpdateResult |
RepoUpdateResult
|
|
str |
str
|
|
cleanup_storage
abstractmethod
¶
Clean up the storage by removing outdated or unnecessary data.
Returns:
| Name | Type | Description |
|---|---|---|
RepoUpdateResult |
RepoUpdateResult
|
|
retrieve_data
abstractmethod
¶
retrieve_data(from_date: date, to_date: date, locations: list[tuple[float, float]], factors: list[str]) -> tuple[xr.Dataset | None, RepoDataFetchResult]
Retrieve weather data for the specified date range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_date
|
date
|
|
required |
to_date
|
date
|
|
required |
locations
|
list[tuple[float, float]]
|
|
required |
factors
|
list[str]
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dataset | None
|
xr.Dataset | None: The retrieved weather data as an xarray Dataset, or None if no data is available. |
|
RepoDataFetchResult |
RepoDataFetchResult
|
|
purge_repository ¶
Permanently delete all data from the repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
RepoUpdateResult |
RepoUpdateResult
|
|
safely_delete_file
classmethod
¶
¶
return_file_or_text_response ¶
return_file_or_text_response(unserialized_data: Dataset, response_format: ResponseFormat, source_id: str, model_id: str, request: WeatherContentRequestQuery | WeatherContentRequestMultiLocationQuery, coords: list[tuple[float, float]]) -> tuple[ScientificJSONResponse | FileResponse, str | None]
Return a file or text response based on the provided response format and data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unserialized_data
|
Dataset
|
The data to be returned in the response. |
required |
response_format
|
ResponseFormat
|
The format in which the response should be returned. |
required |
source_id
|
str
|
The ID of the source for which the data is being returned. |
required |
model_id
|
str
|
The ID of the model for which the data is being returned. |
required |
request
|
WeatherContentRequestQuery | WeatherContentRequestMultiLocationQuery
|
The original request object containing query parameters. |
required |
coords
|
list[tuple[float, float]]
|
A list of coordinates for which the data is being returned. |
required |