diff --git a/mikeio/dfs/_dfs.py b/mikeio/dfs/_dfs.py index 0de9ec4e1..7cfab977c 100644 --- a/mikeio/dfs/_dfs.py +++ b/mikeio/dfs/_dfs.py @@ -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: diff --git a/mikeio/dfsu/_dfsu.py b/mikeio/dfsu/_dfsu.py index 82c4cf4c8..7c89fd84c 100644 --- a/mikeio/dfsu/_dfsu.py +++ b/mikeio/dfsu/_dfsu.py @@ -252,7 +252,7 @@ def _read_dfsu_header(self, dfs): element_ids=el_ids, node_ids=node_ids, n_layers=dfs.NumberOfLayers, - n_sigma=dfs.NumberOfSigmaLayers, + n_sigma=min(dfs.NumberOfSigmaLayers, dfs.NumberOfLayers), validate=False, ) elif self._type == DfsuFileType.DfsuSpectral1D: diff --git a/tests/test_dfs2.py b/tests/test_dfs2.py index b186a10c5..610e14913 100644 --- a/tests/test_dfs2.py +++ b/tests/test_dfs2.py @@ -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 \ No newline at end of file diff --git a/tests/testdata/single_time_dt_zero.dfs2 b/tests/testdata/single_time_dt_zero.dfs2 new file mode 100755 index 000000000..f39b6a72b Binary files /dev/null and b/tests/testdata/single_time_dt_zero.dfs2 differ