Skip to content

Commit

Permalink
Merge pull request #652 from DHI/dfs0-concat-neq
Browse files Browse the repository at this point in the history
Concat neq dfs0
  • Loading branch information
ecomodeller authored Feb 20, 2024
2 parents e4f8c2d + 3a175dd commit ea727b2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mikeio/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from . import __dfs_version__
from .dfs._dfs import _get_item_info, _valid_item_numbers
from .eum import ItemInfo
import mikeio


TimeAxis = Union[
Expand Down Expand Up @@ -449,6 +450,13 @@ def concat(
The list of input files have to be sorted, i.e. in chronological order
"""
# fast path for Dfs0
suffix = pathlib.Path(infilenames[0]).suffix
if suffix == ".dfs0":
dss = [mikeio.read(f) for f in infilenames]
ds = mikeio.Dataset.concat(dss, keep=keep) # type: ignore
ds.to_dfs(outfilename)
return

dfs_i_a = DfsFileFactory.DfsGenericOpen(str(infilenames[0]))

Expand Down
33 changes: 33 additions & 0 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,39 @@ def test_concat_keep(tmp_path):
assert last_out, "overlap should be with last dataset"


def test_concat_non_equidistant_dfs0(tmp_path):

# create two non-equidistant dfs0 files
da1 = mikeio.DataArray(
data=np.array([0.0, 0.1, 0.2]),
time=pd.DatetimeIndex(
["2017-10-27 01:58", "2017-10-27 04:32", "2017-10-27 05:32"]
),
)
assert not da1.is_equidistant
da2 = mikeio.DataArray(
data=np.array([0.9, 1.1, 0.2]),
time=pd.DatetimeIndex(
["2017-10-28 01:59", "2017-10-29 04:32", "2017-11-01 05:32"]
),
)
assert not da2.is_equidistant
files = [tmp_path / "da1.dfs0", tmp_path / "da2.dfs0"]
da1.to_dfs(tmp_path / "da1.dfs0")
da2.to_dfs(tmp_path / "da2.dfs0")

# concatenate
fp = tmp_path / "concat.dfs0"
mikeio.generic.concat(files, fp)

ds = mikeio.read(fp)
assert len(ds.time) == 6

assert ds.sel(time="2017-10-29 04:32").to_dataframe().iloc[0, 0] == pytest.approx(
1.1
)


def test_extract_equidistant(tmp_path):

infile = "tests/testdata/waves.dfs2"
Expand Down

0 comments on commit ea727b2

Please sign in to comment.