Skip to content

Commit

Permalink
[2024-03-05 16:17] Fix for unit conversion issues that occurred due t…
Browse files Browse the repository at this point in the history
…o package updates. (#122)

Signed-off-by: Raoul Linnenbank <[email protected]>
  • Loading branch information
rflinnenbank authored Mar 5, 2024
1 parent f2c28a1 commit fe55ff2
Show file tree
Hide file tree
Showing 6 changed files with 524 additions and 467 deletions.
909 changes: 462 additions & 447 deletions poetry.lock

Large diffs are not rendered by default.

39 changes: 38 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "weather_provider_api"
version = "2.48.0"
version = "2.49.0"
description = "Weather Provider Libraries and API"
authors = ["Verbindingsteam", "Raoul Linnenbank <[email protected]>"]
license = "MPL-2.0"
Expand Down Expand Up @@ -42,6 +42,7 @@ pytest-cov = "^4.0.0"
isort = "^5.10.1"
pylint = "^2.15.10"
black = "^23.10.1"
ruff = {version = "^0.1.8", source = "pypi"}

[tool.poetry.scripts]
wpla_update_era5sl = "weather_provider_api.scripts.update_era5sl_repository:main"
Expand All @@ -54,6 +55,10 @@ wpla_clear_arome = "weather_provider_api.scripts.erase_arome_repository:main"
wpla_clear_waarnemingen = "weather_provider_api.scripts.erase_waarnemingen_register:main"
wpla_run_api = "weather_provider_api.main:main"

[[tool.poetry.source]]
name = "PyPI"
priority = "primary"

[tool.pylint]
max-line-length = 120

Expand All @@ -64,3 +69,35 @@ line-length = 120
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.ruff]
select = [
"ANN", # flake8-annotations
"E", # flake8
"F", # flake8
"I", # isort
"D", # pydocstyle
"S", # flake8-bandit
"NPY", # numpy-specific rules
"RUF", # ruff specific rules
]
ignore = [
"E501",
"E712",

"ANN101", # Missing type annotation for `self` in method
"ANN202", # Missing return type annotation for private function
"ANN204", # Missing return type annotation for special function
"ANN401", # Dynamically typed expressions (typing.Any) are disallowed

# pydocstyle
"D100", # Missing docstring in public module
"D104", # Missing docstring in public package
"D106", # Missing docstring in public nested class
]

[tool.ruff.per-file-ignores]
"__init__.py" = ["F401"]
"tests/**" = ["S", "ANN"]

[tool.ruff.pydocstyle]
convention = "google"
3 changes: 2 additions & 1 deletion weather_provider_api/routers/weather/base_models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ def convert_names_and_units(self, weather_data: xr.Dataset, unit: OutputUnit):

new_data = weather_data[var_name]
if "convert" in conversion_dict[var_name]:
dtype_value = new_data.dtype
converter: callable = conversion_dict[var_name]["convert"]
new_data = converter(weather_data[var_name])
new_data = converter(weather_data[var_name]).astype(dtype_value)

weather_data = weather_data.drop_vars(var_name)
weather_data[new_name] = new_data
Expand Down
3 changes: 1 addition & 2 deletions weather_provider_api/routers/weather/base_models/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@


class WeatherSourceBase(object): # pragma: no cover
"""
Base class that contains the basic functionality for all sources. Any new sources should implement this as their
"""Base class that contains the basic functionality for all sources. Any new sources should implement this as their
base class!
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ def __init__(self):
logger.debug(f"Weather model [{self.id}] initialized successfully")

def get_weather(
self,
coords: List[GeoPosition],
begin: datetime,
end: datetime,
inseason=False,
weather_factors: List[str] = None,
self,
coords: List[GeoPosition],
begin: datetime,
end: datetime,
inseason=False,
weather_factors: List[str] = None,
) -> xr.Dataset:
"""
The function that gathers and processes the requested Daggegevens weather data from the KNMI site
Expand Down Expand Up @@ -222,18 +222,19 @@ def get_weather(

# The KNMI model isn't working properly yet, so we have to cut out any overflow time-wise
ds = ds.sel(time=slice(begin, end))

return ds

def is_async(self): # pragma: no cover
def is_async(self) -> bool: # pragma: no cover
return self.async_model

def _download_weather(
self,
stations: List[int],
start: datetime,
end: datetime,
inseason=False,
weather_factors: List[str] = None,
self,
stations: List[int],
start: datetime,
end: datetime,
inseason=False,
weather_factors: List[str] = None,
):
"""
A function that downloads the weather from the KNMI download location and returns it as a text
Expand Down Expand Up @@ -306,7 +307,8 @@ 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):
def _prepare_weather_data(coordinates: List[GeoPosition], station_id: list[np.int64],
raw_ds: xr.Dataset) -> 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.

Expand All @@ -322,6 +324,7 @@ def _prepare_weather_data(coordinates: List[GeoPosition], station_id, raw_ds):
coords={"time": timeline, "coord": coords_to_pd_index(coordinates)},
)
ds = ds.unstack("coord")

return ds

def _request_weather_factors(self, factors: Optional[List[str]]) -> List[str]:
Expand Down
6 changes: 4 additions & 2 deletions weather_provider_api/routers/weather/sources/knmi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
from weather_provider_api.routers.weather.utils.geo_position import GeoPosition


def find_closest_stn_list(stn_stations: pd.DataFrame, coords: List[GeoPosition]):
def find_closest_stn_list(
stn_stations: pd.DataFrame, coords: List[GeoPosition]
) -> tuple[list[np.int64], list[np.int64], list[int]]:
"""
A function that finds the closest stations to the locations in the given list of GeoPositions
Args:
Expand All @@ -45,7 +47,7 @@ def find_closest_stn_list(stn_stations: pd.DataFrame, coords: List[GeoPosition])
return coords_stn, stns, coords_stn_ind


def _find_closest_stn_single(stn_stations: pd.DataFrame, coord: GeoPosition):
def _find_closest_stn_single(stn_stations: pd.DataFrame, coord: GeoPosition) -> np.int64:
"""
A function that finds the closest station to a single GeoPosition
Args:
Expand Down

0 comments on commit fe55ff2

Please sign in to comment.