Skip to content

Commit 2c5494e

Browse files
committed
Fix the time unit bug in drift calculation
- numpy changed how timestamps are parsed into datetime64 - old conversion relied on the units being `ns` - this caused the conversion from `ms` to result in many 0 time deltas - dividing by 0 produced `inf` and attempting `int(inf)` raised an Error - now use numpy API to convert to `s`
1 parent f0e9b12 commit 2c5494e

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

bufrtools/encoding/wildlife_computers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ def drift(df: pd.DataFrame) -> np.ndarray:
6161
travel no distance over no time will have a drift of zero. The last element of the returned
6262
array will be 0, as it can not be effectively calculated.
6363
"""
64-
# Convert to epoch seconds
65-
t = df.groupby('profile')['time'].first().view('int64') // 1e9
66-
dt = np.diff(t)
64+
t = df.groupby('profile')['time'].first()
65+
# Calculate time differences and convert to seconds
66+
dt = np.diff(t).astype('timedelta64[s]').view('int64')
6767

6868
x = df.groupby('profile')['lon'].first() * np.pi / 180
6969
y = df.groupby('profile')['lat'].first() * np.pi / 180
7070
ds = haversine_distance(x.values, y.values)
71-
ds_dt = np.zeros_like(t)
71+
ds_dt = np.zeros_like(t.view('float64'))
7272
for i in range(ds.shape[0]):
7373
if np.abs(ds[i]) < 0.0001 and np.abs(dt[i]) < 0.0001:
7474
ds_dt[i] = 0

0 commit comments

Comments
 (0)