Skip to content

Commit 2963035

Browse files
authored
Merge pull request #25 from dawidlinek/feat/calendar-source-config
feat(calendar): allow overriding lat/lon for daylight calculation
2 parents a8ed488 + fdc910d commit 2963035

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

epftoolbox2/data/sources/calendar.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def __init__(
4444
hour: Union[str, bool] = False,
4545
month: Union[str, bool] = False,
4646
daylight: bool = False,
47+
lat: Optional[float] = None,
48+
lon: Optional[float] = None,
4749
prefix: str = "",
4850
freq: str = "1h",
4951
):
@@ -52,8 +54,8 @@ def __init__(
5254

5355
country_info = COUNTRY_DATA[self.country]
5456
self.timezone = timezone or country_info["timezone"]
55-
self.lat = country_info["lat"]
56-
self.lon = country_info["lon"]
57+
self.lat = lat if lat is not None else country_info["lat"]
58+
self.lon = lon if lon is not None else country_info["lon"]
5759

5860
self.holidays = holidays
5961
self.weekday = weekday

tests/data/sources/test_calendar.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ def test_init_custom_timezone(self):
2121
source = CalendarSource(country="PL", timezone="UTC")
2222
assert source.timezone == "UTC"
2323

24+
def test_init_default_coordinates(self):
25+
source = CalendarSource(country="PL")
26+
assert source.lat == COUNTRY_DATA["PL"]["lat"]
27+
assert source.lon == COUNTRY_DATA["PL"]["lon"]
28+
29+
def test_init_override_coordinates(self):
30+
source = CalendarSource(country="PL", lat=54.3520, lon=18.6466)
31+
assert source.lat == 54.3520
32+
assert source.lon == 18.6466
33+
34+
def test_init_override_lat_only(self):
35+
source = CalendarSource(country="PL", lat=54.3520)
36+
assert source.lat == 54.3520
37+
assert source.lon == COUNTRY_DATA["PL"]["lon"]
38+
2439
def test_init_invalid_holidays_value(self):
2540
with pytest.raises(ValueError, match="Invalid holidays value"):
2641
CalendarSource(country="PL", holidays="invalid")
@@ -168,6 +183,18 @@ def test_daylight_summer_longer(self):
168183
winter = source.fetch(pd.Timestamp("2024-12-21", tz="UTC"), pd.Timestamp("2024-12-22", tz="UTC"))
169184
assert summer["daylight_hours"].iloc[0] > winter["daylight_hours"].iloc[0]
170185

186+
def test_daylight_respects_coordinate_override(self):
187+
# A more northern latitude has longer daylight at the summer solstice.
188+
warsaw = CalendarSource(country="PL", holidays=False, weekday=False, daylight=True)
189+
northern = CalendarSource(
190+
country="PL", holidays=False, weekday=False, daylight=True, lat=59.3293, lon=18.0686
191+
)
192+
start = pd.Timestamp("2024-06-21", tz="UTC")
193+
end = pd.Timestamp("2024-06-22", tz="UTC")
194+
warsaw_df = warsaw.fetch(start, end)
195+
northern_df = northern.fetch(start, end)
196+
assert northern_df["daylight_hours"].iloc[0] > warsaw_df["daylight_hours"].iloc[0]
197+
171198

172199
class TestPrefix:
173200
def test_custom_prefix(self):

0 commit comments

Comments
 (0)