Skip to content

Commit

Permalink
👌 Handle numpy.timedelta64 in deltatime_to_utctime conversion
Browse files Browse the repository at this point in the history
Patches #40. Allow for converting a single numpy timedelta64 value to datetime64, instead of raising a "ValueError: Could not convert object to NumPy timedelta".
  • Loading branch information
weiji14 committed Jun 10, 2020
1 parent efb643e commit 2c74f95
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
5 changes: 4 additions & 1 deletion deepicedrain/spatiotemporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ def deltatime_to_utctime(
Note, does not account for leap seconds! There are none declared since the
last one announced on 31/12/2016, so it should be fine for now as of 2020.
"""
start_epoch = dataarray.__class__(start_epoch).squeeze()
try:
start_epoch = dataarray.__class__(start_epoch).squeeze()
except ValueError: # Could not convert object to NumPy timedelta
pass

utc_time: xr.DataArray = start_epoch + dataarray

Expand Down
21 changes: 16 additions & 5 deletions deepicedrain/tests/test_spatiotemporal_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@
from deepicedrain import catalog, deltatime_to_utctime, lonlat_to_xy


def test_deltatime_to_utctime_numpy_timedelta64():
"""
Test that converting from ICESat-2 delta_time to utc_time works on a
single numpy.timedelta object.
"""
delta_time = np.timedelta64(24731275413287379, "ns")
utc_time: np.datetime64 = deltatime_to_utctime(dataarray=delta_time)

npt.assert_equal(
actual=utc_time, desired=np.datetime64("2018-10-14T05:47:55.413287379")
)


def test_deltatime_to_utctime_pandas_series():
"""
Test that converting from ICESat-2 delta_time to utc_time works on a
Expand All @@ -25,7 +38,7 @@ def test_deltatime_to_utctime_pandas_series():
assert utc_time.shape == (2808,)

npt.assert_equal(
actual=utc_time.min(), desired=pd.Timestamp("2019-05-19T20:53:51.039891534"),
actual=utc_time.min(), desired=pd.Timestamp("2019-05-19T20:53:51.039891534")
)

npt.assert_equal(
Expand All @@ -37,7 +50,7 @@ def test_deltatime_to_utctime_pandas_series():
desired=pd.Timestamp("2019-08-18 16:33:47.791226368"),
)
npt.assert_equal(
actual=utc_time.max(), desired=pd.Timestamp("2019-08-18T16:33:57.834610209"),
actual=utc_time.max(), desired=pd.Timestamp("2019-08-18T16:33:57.834610209")
)


Expand Down Expand Up @@ -83,13 +96,11 @@ def test_lonlat_to_xy_dask_series():
atl11_dataframe: dask.dataframe.core.DataFrame = atl11_dataset.to_dask_dataframe()

x, y = lonlat_to_xy(
longitude=atl11_dataframe.longitude, latitude=atl11_dataframe.latitude,
longitude=atl11_dataframe.longitude, latitude=atl11_dataframe.latitude
)
npt.assert_equal(actual=x.mean(), desired=-56900105.00307033)
npt.assert_equal(actual=y.mean(), desired=48141607.48486084)

atl11_dataset.close()


def test_lonlat_to_xy_xarray_dataarray():
"""
Expand Down

0 comments on commit 2c74f95

Please sign in to comment.