Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concat neq dfs0 #652

Merged
merged 1 commit into from
Feb 20, 2024
Merged
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
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
Loading