Skip to content

Commit ee152ec

Browse files
add datatype to from_FAST and fix test
1 parent 1046328 commit ee152ec

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

climada/hazard/tc_tracks.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,7 @@ def from_FAST(cls, folder_name: str):
16441644
"""
16451645

16461646
LOGGER.info("Reading %s files.", len(get_file_names(folder_name)))
1647-
data = []
1647+
data: list = []
16481648
for file in get_file_names(folder_name):
16491649
if Path(file).suffix != ".nc":
16501650
continue
@@ -1653,28 +1653,32 @@ def from_FAST(cls, folder_name: str):
16531653
for i in dataset.n_trk:
16541654

16551655
# Select track
1656-
track = dataset.sel(n_trk=i, year=year)
1656+
track: xr.Dataset = dataset.sel(n_trk=i, year=year)
16571657
# chunk dataset at first NaN value
1658-
lon = track.lon_trks.data
1659-
last_valid_index = np.where(np.isfinite(lon))[0][-1]
1660-
track = track.isel(time=slice(0, last_valid_index + 1))
1658+
lon: np.ndarray = track.lon_trks.data
1659+
last_valid_index: int = np.where(np.isfinite(lon))[0][-1]
1660+
track: xr.Dataset = track.isel(
1661+
time=slice(0, last_valid_index + 1)
1662+
)
16611663
# Select lat, lon
1662-
lat = track.lat_trks.data
1663-
lon = track.lon_trks.data
1664+
lat: np.ndarray = track.lat_trks.data
1665+
lon: np.ndarray = track.lon_trks.data
16641666
# Convert lon from 0-360 to -180 - 180
1665-
lon = ((lon + 180) % 360) - 180
1667+
lon: np.ndarray = ((lon + 180) % 360) - 180
16661668
# Convert time to pandas Datetime "yyyy.mm.dd"
16671669
reference_time = (
16681670
f"{track.tc_years.item()}-{int(track.tc_month.item())}-01"
16691671
)
1670-
time = pd.to_datetime(
1672+
time: np.datetime64 = pd.to_datetime(
16711673
track.time.data, unit="s", origin=reference_time
16721674
).astype("datetime64[s]")
16731675
# Define variables
1674-
ms_to_kn = 1.943844
1675-
max_wind_kn = track.vmax_trks.data * ms_to_kn
1676-
env_pressure = BASIN_ENV_PRESSURE[track.tc_basins.data.item()]
1677-
cen_pres = _estimate_pressure(
1676+
ms_to_kn: float = 1.943844
1677+
max_wind_kn: np.ndarray = track.vmax_trks.data * ms_to_kn
1678+
env_pressure: float = BASIN_ENV_PRESSURE[
1679+
track.tc_basins.data.item()
1680+
]
1681+
cen_pres: np.ndarray = _estimate_pressure(
16781682
np.full(lat.shape, np.nan),
16791683
lat,
16801684
lon,
@@ -3033,7 +3037,7 @@ def compute_track_density(
30333037
30343038
"""
30353039

3036-
limit_ratio = 1.12 * 1.1 # record tc speed 112km/h -> 1.12°/h + 10% margin
3040+
limit_ratio: float = 1.12 * 1.1 # record tc speed 112km/h -> 1.12°/h + 10% margin
30373041
time_value: float = tc_track.data[0].time_step[0].values # Type hint for jenkins
30383042

30393043
if time_value > (res / limit_ratio):
@@ -3048,8 +3052,8 @@ def compute_track_density(
30483052
)
30493053

30503054
# define grid resolution and bounds for density computation
3051-
lat_bins = np.linspace(-90, 90, int(180 / res))
3052-
lon_bins = np.linspace(-180, 180, int(360 / res))
3055+
lat_bins: np.ndarray = np.linspace(-90, 90, int(180 / res))
3056+
lon_bins: np.ndarray = np.linspace(-180, 180, int(360 / res))
30533057
# compute 2D density
30543058
if genesis:
30553059
hist_count = compute_genesis_density(
@@ -3148,8 +3152,8 @@ def normalize_hist(
31483152

31493153
if norm == "area":
31503154
grid_area, _ = u_coord.compute_grid_cell_area(res=res)
3151-
norm_hist = hist_count / grid_area
3155+
norm_hist: np.ndarray = hist_count / grid_area
31523156
elif norm == "sum":
3153-
norm_hist = hist_count / hist_count.sum()
3157+
norm_hist: np.ndarray = hist_count / hist_count.sum()
31543158

31553159
return norm_hist

climada/hazard/test/test_tc_tracks.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,20 +1282,20 @@ def test_compute_density_tracks(self):
12821282
hist_abs, *_ = tc.compute_track_density(
12831283
tc_tracks,
12841284
res=10,
1285-
density=False,
1285+
norm=None,
12861286
)
12871287
# hist_norm, *_ = tc.compute_track_density(tc_tracks, res=10, density=True)
12881288
hist_wind_min, *_ = tc.compute_track_density(
1289-
tc_tracks, res=10, norm=False, wind_min=11, wind_max=None
1289+
tc_tracks, res=10, norm=None, wind_min=11, wind_max=None
12901290
)
12911291
hist_wind_max, *_ = tc.compute_track_density(
1292-
tc_tracks, res=10, norm=False, wind_min=None, wind_max=30
1292+
tc_tracks, res=10, norm=None, wind_min=None, wind_max=30
12931293
)
12941294
hist_wind_max, *_ = tc.compute_track_density(
1295-
tc_tracks, res=10, norm=False, wind_min=None, wind_max=30
1295+
tc_tracks, res=10, norm=None, wind_min=None, wind_max=30
12961296
)
12971297
hist_wind_both, *_ = tc.compute_track_density(
1298-
tc_tracks, res=10, norm=False, wind_min=11, wind_max=29
1298+
tc_tracks, res=10, norm=None, wind_min=11, wind_max=29
12991299
)
13001300
self.assertEqual(hist_abs.shape, (17, 35))
13011301
self.assertEqual(hist_abs.sum(), 4)

0 commit comments

Comments
 (0)