Skip to content

Commit 6056e6b

Browse files
add test and fix pylits
1 parent 5a5c128 commit 6056e6b

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

climada/hazard/tc_tracks.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,9 @@ def subset_by_basin(self):
432432
Returns
433433
-------
434434
dict_tc_basins : dict
435-
A dictionary where the keys are basin names (e.g., "NA", "EP", "WP", etc.) and the values are instances
436-
of the `TCTracks` class containing the tropical cyclones that belong to each basin.
435+
A dictionary where the keys are basin names (e.g., "NA", "EP", "WP", etc.) and the
436+
values are instances of the `TCTracks` class containing the tropical cyclones that
437+
belong to each basin.
437438
438439
Example:
439440
--------
@@ -447,24 +448,24 @@ def subset_by_basin(self):
447448
basins_dict = defaultdict(list)
448449
tracks_outside_basin: list = []
449450
# Iterate over each tropical cyclone
450-
for tc in self.data:
451-
lat, lon = tc.lat.values[0], tc.lon.values[0]
451+
for track in self.data:
452+
lat, lon = track.lat.values[0], track.lon.values[0]
452453
origin_point = Point(lon, lat)
453454
point_in_basin = False
454455

455456
# Find the basin that contains the point
456457
for basin in Basin:
457458
if basin.value.contains(origin_point):
458-
basins_dict[basin.name].append(tc)
459+
basins_dict[basin.name].append(track)
459460
point_in_basin = True
460461
break
461462

462463
if not point_in_basin:
463-
tracks_outside_basin.append(tc.id_no)
464+
tracks_outside_basin.append(track.id_no)
464465

465466
if tracks_outside_basin:
466467
warnings.warn(
467-
f"A total of {len(tracks_outside_basin)} tracks did not originate in any of the "
468+
f"A total of {len(tracks_outside_basin)} tracks did not originate in any of the \n"
468469
f"defined basins. IDs of the tracks outside the basins: {tracks_outside_basin}",
469470
UserWarning,
470471
)

climada/hazard/test/test_tc_tracks.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,28 @@ def test_subset_years(self):
874874
):
875875
tc_test.subset_year((2100, False, False), (2150, False, False))
876876

877+
def test_subset_basin(self):
878+
"""test the correct splitting of a single tc object into different tc objets by basin"""
879+
880+
tc_test = tc.TCTracks.from_simulations_emanuel(TEST_TRACK_EMANUEL)
881+
tc_test.data[-1].lat[0] = 0 # modify lat of track to exclude it from a basin
882+
883+
with self.assertWarnsRegex(
884+
UserWarning,
885+
"A total of 1 tracks did not originate in any of the \n"
886+
"defined basins. IDs of the tracks outside the basins: \[4\]",
887+
):
888+
dict_basins = tc_test.subset_by_basin()
889+
890+
self.assertEqual(dict_basins["EP"].data[0].lat[0].item(), 12.553)
891+
self.assertEqual(dict_basins["EP"].data[0].lon[0].item(), -109.445)
892+
self.assertEqual(dict_basins["SI"].data[0].lat[0].item(), -8.699)
893+
self.assertEqual(dict_basins["SI"].data[0].lon[0].item(), 52.761)
894+
self.assertEqual(dict_basins["WP"].data[0].lat[0].item(), 8.502)
895+
self.assertEqual(dict_basins["WP"].data[0].lon[0].item(), 164.909)
896+
self.assertEqual(dict_basins["WP"].data[1].lat[0].item(), 16.234)
897+
self.assertEqual(dict_basins["WP"].data[1].lon[0].item(), 116.424)
898+
877899
def test_get_extent(self):
878900
"""Test extent/bounds attributes."""
879901
storms = ["1988169N14259", "2002073S16161", "2002143S07157"]

0 commit comments

Comments
 (0)