Skip to content

Commit

Permalink
Handle non-positive dt (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecomodeller authored Nov 30, 2023
1 parent cf089da commit b8d1ad1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions mikeio/dfs/_dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ def _valid_timesteps(dfsFileInfo: DfsFileInfo, time_steps) -> Tuple[bool, List[i
TimeAxisType.TimeEquidistant,
):
time_step_file = time_axis.TimeStep

if time_step_file <= 0:

if nt > 1:
raise ValueError(f"Time step must be a positive number. Time step in the file is {time_step_file} seconds.")

warnings.warn(
f"Time step is {time_step_file} seconds. This must be a positive number. Setting to 1 second."
)
time_step_file = 1

freq = pd.Timedelta(seconds=time_step_file)
time = pd.date_range(start_time_file, periods=nt, freq=freq)
elif time_axis.TimeAxisType == TimeAxisType.CalendarNonEquidistant:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_dfs2.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,3 +870,17 @@ def test_MIKE_SHE_output():
assert g2.x[0] == g.x[0] + 30 * g.dx
assert g2.y[0] == g.y[0] + 35 * g.dy
assert g2.origin == pytest.approx((g2.x[0], g2.y[0]))


def test_read_dfs2_static_dt_zero():

with pytest.warns(UserWarning, match="positive"):
ds = mikeio.read("tests/testdata/single_time_dt_zero.dfs2")
assert ds.n_timesteps == 1
assert ds.shape == (1, 2, 2)

with pytest.warns(UserWarning, match="positive"):
ds2 = mikeio.read("tests/testdata/single_time_dt_zero.dfs2", time=0)

assert ds2.shape == (2,2)
assert "time" not in ds2.dims
Binary file added tests/testdata/single_time_dt_zero.dfs2
Binary file not shown.

0 comments on commit b8d1ad1

Please sign in to comment.