Skip to content

Commit

Permalink
Fix and simplify smhi
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcth committed May 5, 2024
1 parent b455417 commit e8651d4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 33 deletions.
12 changes: 6 additions & 6 deletions src/smhi/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def get_now() -> datetime:
return arrow.utcnow().datetime # .isoformat("T", "seconds") + "Z"


METOBS_AVAILABLE_PERIODS = [
"latest-hour",
"latest-day",
"latest-months",
"corrected-archive",
]
METOBS_AVAILABLE_PERIODS = {
"latest-hour": 0,
"latest-day": 1,
"latest-months": 2,
"corrected-archive": 3,
}

METFCTS_URL = (
"https://opendata-download-metfcst.smhi.se/"
Expand Down
22 changes: 7 additions & 15 deletions src/smhi/metobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def __init__(
class Data(BaseMetobs):
"""Get data from period for version 1 of Metobs API."""

_metobs_available_periods: Dict[str, str] = METOBS_AVAILABLE_PERIODS
_metobs_available_periods: Dict[str, int] = METOBS_AVAILABLE_PERIODS
_metobs_parameter_tim: List[str] = ["Datum", "Tid (UTC)"]
_metobs_parameter_dygn: List[str] = ["Representativt dygn"]
_metobs_parameter_manad: List[str] = ["Representativ månad"]
Expand All @@ -323,21 +323,13 @@ def __init__(
"""
super().__init__()

if period is None:
ordering = {
"latest-hour": 0,
"latest-day": 1,
"latest-months": 2,
"corrected-archive": 3,
}
available_periods = sorted(
periods_in_station.data, key=lambda x: ordering.__getitem__(x)
)
period = available_periods[0]

if data_type != "json":
raise TypeError("Only json supported.")

if period is None:
period = periods_in_station.data[0]
logger.info("No period selected, selecting: {period}.")

if self._check_available_periods(periods_in_station.data, period):
logger.info(
"Found only one period to download. "
Expand All @@ -347,8 +339,8 @@ def __init__(

if period not in self._metobs_available_periods:
raise NotImplementedError(
"Select a supported periods: }"
+ ", ".join([p for p in self._metobs_available_periods])
"Select a supported period: "
+ ", ".join([p for p in self._metobs_available_periods.keys()])
)

self.periods_in_station = periods_in_station
Expand Down
3 changes: 2 additions & 1 deletion src/smhi/models/metobs_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pandas as pd
from pydantic import BaseModel, ConfigDict, Field, field_validator
from smhi.constants import METOBS_AVAILABLE_PERIODS


class MetobsLink(BaseModel):
Expand Down Expand Up @@ -183,7 +184,7 @@ class MetobsStationModel(MetobsBaseModel):
@field_validator("period")
@classmethod
def serialise_period_in_order(cls, period: List[MetobsLinks]):
return sorted(period, key=lambda x: x.key)
return sorted(period, key=lambda x: METOBS_AVAILABLE_PERIODS[x.key])

@property
def data(self) -> Tuple[Optional[str], ...]:
Expand Down
19 changes: 8 additions & 11 deletions tests/unit/test_unit_smhi.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def __init__(self, data="str"):


@pytest.fixture
def setup_test_x():
"""Pytest fixture"""
def setup_iterate_over_time():
"""Iterate over time fixture."""
df = pd.DataFrame(
{
"date": [
Expand Down Expand Up @@ -108,7 +108,7 @@ def test_unit_smhi_get_stations_from_title(
@patch("smhi.metobs.Stations.__new__")
@patch("smhi.metobs.Periods.__new__")
@patch("smhi.metobs.Data.__new__")
@patch("smhi.smhi.SMHI._interpolate")
@patch("smhi.smhi.SMHI._interpolate", return_value="test")
def test_unit_get_data(
self,
mock_interpolate,
Expand All @@ -130,7 +130,6 @@ def test_unit_get_data(
station,
distance,
"""
mock_interpolate.return_value = "test"
client = SMHI()
data = client.get_data(parameter, station, distance)
assert data == "test"
Expand All @@ -142,7 +141,7 @@ def test_unit_get_data(
@patch("smhi.metobs.Periods.__new__")
@patch("smhi.metobs.Data.__new__")
@patch("smhi.smhi.SMHI._find_stations_by_city")
@patch("smhi.smhi.SMHI._interpolate")
@patch("smhi.smhi.SMHI._interpolate", return_value="test")
def test_unit_get_data_by_city(
self,
mock_interpolate,
Expand All @@ -166,7 +165,6 @@ def test_unit_get_data_by_city(
city,
distance
"""
mock_interpolate.return_value = "test"
client = SMHI()
data = client.get_data_by_city(parameter, city, distance)
assert data == "test"
Expand Down Expand Up @@ -232,6 +230,7 @@ def test_find_stations_by_city(
client = SMHI()
_ = client._find_stations_by_city(parameter, city, distance)
mock_nominatim.assert_called_once()
mock_find_from_gps.assert_called_once()

@pytest.mark.parametrize("distance", [(0), (50)])
@patch("smhi.metobs.Stations.__new__")
Expand Down Expand Up @@ -270,17 +269,15 @@ def test_interpolate(
else:
mock_find_from_gps.assert_called_once()

def test_iterate_over_time(self, setup_test_x):
def test_iterate_over_time(self, setup_iterate_over_time):
"""Unit test for SMHI _iterate_over_time method."""
df, nearby_df, missing_df = setup_test_x
df, nearby_df, missing_df = setup_iterate_over_time
client = SMHI()

data = client._iterate_over_time(df, nearby_df, missing_df)
assert data.tail(2).iloc[0]["Temperatur"] == nearby_df.iloc[0]["Temperatur"]

def test_find_missing_data(
self,
):
def test_find_missing_data(self):
"""Unit test for SMHI _find_missing_data method."""
df = pd.DataFrame(
{
Expand Down

0 comments on commit e8651d4

Please sign in to comment.