Skip to content

Commit 2e61624

Browse files
update date functionality
1 parent 8b580a6 commit 2e61624

File tree

2 files changed

+34
-59
lines changed

2 files changed

+34
-59
lines changed

climada/hazard/tc_tracks.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ def subset_year(
334334
start_date: tuple
335335
First date to include in the selection (YYYY, MM, DD). Each element can either
336336
be an integer or `False`. If an element is `False`, it is ignored during the filter.
337-
end_date: tuple of int
338-
Last date to include in the selection, same as start_date for the corresponding field.
337+
end_date: tuple
338+
Last date to include in the selection, same as start_date if selecting only one day.
339339
340340
Returns:
341341
--------
@@ -347,7 +347,6 @@ def subset_year(
347347
ValueError
348348
- If there's a mismatch between `start_*` and `end_*` values (e.g., one is set to `True` while the other is `False`).
349349
- If no tracks are found within the specified date range.
350-
TypeError
351350
- If `start_date` or `end_date` are incorrectly ordered (start > end).
352351
353352
Example 1 (Filter by Year Only):
@@ -382,11 +381,6 @@ def subset_year(
382381
start_month, end_month = start_date[1], end_date[1]
383382
start_day, end_day = start_date[2], end_date[2]
384383

385-
# Check if only one of start_* or end_* is set (True and False)
386-
# if not start_day and not (1 <= start_day <= 31) or not end_day and not (1 <= end_day <= 31):
387-
# raise TypeError("Day values should be between 1 and 31.")
388-
# if not start_month and not (1 <= start_month <= 12) or not end_month and not(1 <= end_month <= 12):
389-
# raise TypeError("Day values should be between 1 and 31.")
390384
if (start_day and not end_day) or (not start_day and end_day):
391385
raise ValueError(
392386
"Mismatch between start_day and end_day: Both must be either True or False."
@@ -400,20 +394,16 @@ def subset_year(
400394
"Mismatch between start_year and end_year: Both must be either True or False."
401395
)
402396
elif start_year and end_year and start_year > end_year:
403-
raise TypeError("Start year is after end year, control your entry.")
397+
raise ValueError("Start year is after end year.")
404398

405399
# Find indices corresponding to the date range
406400
index: list = []
407401
for i, track in enumerate(self.data):
408-
try:
409-
date_array = track.time[0].to_numpy()
410-
year = date_array.astype("datetime64[Y]").item().year
411-
month = date_array.astype("datetime64[M]").item().month
412-
day = date_array.astype("datetime64[D]").item().day
413-
except AttributeError:
414-
raise ValueError(
415-
f"Invalid date format in track {i}, could not extract date."
416-
)
402+
403+
date_array = track.time[0].to_numpy()
404+
year = date_array.astype("datetime64[Y]").item().year
405+
month = date_array.astype("datetime64[M]").item().month
406+
day = date_array.astype("datetime64[D]").item().day
417407

418408
condition_year = start_year <= year <= end_year
419409
condition_month = start_month <= month <= end_month
@@ -431,11 +421,7 @@ def subset_year(
431421

432422
# Raise error if no tracks found
433423
if not index:
434-
raise ValueError(
435-
f"No tracks found for the specified date range: {start_date} to \n"
436-
"{end_date}."
437-
)
438-
424+
raise ValueError("No tracks found for the specified date range")
439425
# Create subset with filtered tracks
440426
subset.data = [self.data[i] for i in index]
441427

climada/hazard/test/test_tc_tracks.py

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -848,42 +848,31 @@ def test_subset_years(self):
848848
self.assertEqual(tc_subset.data[3].time[0].item().year, 2003)
849849
self.assertEqual(tc_subset.data[3].time[0].item().month, 4)
850850

851-
# improper calling
852-
853-
# self.assertEqual(tc_subset.data[0].time[0].item().year, 2001)
854-
# self.assertEqual(tc_subset.data[1].time[0].item().year, 2002)
855-
# self.assertEqual(tc_subset.data[2].time[0].item().year, 2003)
856-
857-
# # Invalid input: non-integer start_year
858-
# with self.assertRaisesRegex(
859-
# TypeError, "Both start_year and end_year must be integers."
860-
# ):
861-
# tc_test.subset_year(start_year="2000", end_year=2003)
862-
863-
# # Invalid input: non-integer end_year
864-
# with self.assertRaisesRegex(
865-
# TypeError, "Both start_year and end_year must be integers."
866-
# ):
867-
# tc_test.subset_year(start_year=2000, end_year=None)
868-
869-
# # Invalid range: start_year greater than end_year
870-
# with self.assertRaisesRegex(
871-
# ValueError, r"start_year \(2005\) cannot be greater than end_year \(2000\)."
872-
# ):
873-
# tc_test.subset_year(start_year=2005, end_year=2000)
874-
875-
# # No tracks match the year range
876-
# with self.assertRaisesRegex(
877-
# ValueError, "No tracks found for the years between 2050 and 2060."
878-
# ):
879-
# tc_test.subset_year(start_year=2050, end_year=2060)
880-
881-
# # Empty data case
882-
# empty_tc = tc.TCTracks()
883-
# with self.assertRaisesRegex(
884-
# TypeError, "self.data should be a non-empty list of tracks."
885-
# ):
886-
# empty_tc.subset_year(start_year=2000, end_year=2010)
851+
# Invalid input: Mismatch between start_day and end_day
852+
with self.assertRaisesRegex(
853+
ValueError,
854+
"Mismatch between start_year and end_year: "
855+
"Both must be either True or False.",
856+
):
857+
tc_test.subset_year((2000, False, False), (False, False, False))
858+
with self.assertRaisesRegex(
859+
ValueError,
860+
"Mismatch between start_month and end_month: "
861+
"Both must be either True or False.",
862+
):
863+
tc_test.subset_year((2000, False, False), (2000, 5, False))
864+
with self.assertRaisesRegex(
865+
ValueError,
866+
"Mismatch between start_day and end_day: "
867+
"Both must be either True or False.",
868+
):
869+
tc_test.subset_year((False, False, False), (False, False, 3))
870+
with self.assertRaisesRegex(ValueError, "Start year is after end year."):
871+
tc_test.subset_year((2007, False, False), (2000, False, False))
872+
with self.assertRaisesRegex(
873+
ValueError, "No tracks found for the specified date range"
874+
):
875+
tc_test.subset_year((2100, False, False), (2150, False, False))
887876

888877
def test_get_extent(self):
889878
"""Test extent/bounds attributes."""

0 commit comments

Comments
 (0)