Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions tests/test_missing_times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import numpy as np
import pytest
from numpy.testing import assert_allclose

from utide import reconstruct, solve


@pytest.mark.parametrize("kind", ["python", "matlab", "custom", "masked_date", "nat"])
@pytest.mark.parametrize("vector", [False, True])
def test_missing_times(kind, vector):
hours = np.arange(24 * 35)
dates = np.datetime64("2020-01-01", "h") + hours.astype("timedelta64[h]")
missing = (hours % 19) == 7
data = np.cos(2 * np.pi * hours / 12.42) + 0.1 * np.sin(hours)
data[missing] = 100
if kind in ("masked_date", "nat"):
times = dates.copy()
epoch = None
if kind == "nat":
times[missing] = np.datetime64("NaT", "h")
else:
times = np.ma.array(times, mask=missing)
else:
epoch = {"python": "python", "matlab": "matlab", "custom": "2020-01-01"}[kind]
times = hours / 24 + {"python": 737425, "matlab": 737791, "custom": 0}[kind]
times = np.ma.array(times, mask=missing)

opts = {
"epoch": epoch,
"lat": 21,
"constit": ["M2"],
"conf_int": "none",
"verbose": False,
}
v = 0.5 * data if vector else None
reference = solve(
times[~missing],
data[~missing],
None if v is None else v[~missing],
**opts,
)
actual = solve(times, data, v, **opts)
keys = (
["Lsmaj", "Lsmin", "theta", "g", "umean", "vmean"]
if vector
else ["A", "g", "mean"]
)
for key in keys:
assert_allclose(actual[key], reference[key], rtol=1e-12, atol=1e-10)

result = reconstruct(times, actual, epoch=epoch, verbose=False)
expected = reconstruct(times[~missing], reference, epoch=epoch, verbose=False)
for key in (["u", "v"] if vector else ["h"]):
assert np.isnan(result[key][missing]).all()
assert_allclose(result[key][~missing], expected[key], rtol=1e-12, atol=1e-10)
6 changes: 3 additions & 3 deletions utide/_time_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def _date2num(date, epoch="1970-01-01 00:00:00.000"):

Default `epoch` is the unix epoch 1970-01-01 00:00:00.
"""
date = np.asarray(date)
date = np.asanyarray(date)

try:
date = date.astype("datetime64[ms]")
Expand All @@ -37,7 +37,7 @@ def _date2num(date, epoch="1970-01-01 00:00:00.000"):
) from err

# datenum calculation
datenum = (date - epoch).astype(float) / _MS_PER_DAY
datenum = (date - epoch) / np.timedelta64(_MS_PER_DAY, "ms")
return datenum


Expand All @@ -61,7 +61,7 @@ def _normalize_time(t, epoch=None):
`t` input time or datenum array
`epoch` either 'python', 'matlab', or np.datetime64 compatible value
"""
t = np.asarray(t)
t = np.asanyarray(t)

if epoch is None:
# default datetime, datetime64, or datetime array
Expand Down
Loading