Skip to content

Commit

Permalink
Basic Support for Dfs2 in GridmodelResult
Browse files Browse the repository at this point in the history
  • Loading branch information
ecomodeller committed Mar 17, 2024
1 parent d55b392 commit 2b9bc48
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions modelskill/model/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def _guess_gtype(data: Any) -> GeometryType:
elif file_ext == ".nc":
# could also be point or track, but we don't know
return GeometryType.GRID
elif file_ext == ".dfs2":
return GeometryType.GRID
else:
raise ValueError(
"Could not guess gtype from file extension, please specify gtype, e.g. gtype='track'"
Expand Down
10 changes: 9 additions & 1 deletion modelskill/model/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
from typing import Optional, Sequence, get_args

import mikeio
import pandas as pd
import xarray as xr

Expand Down Expand Up @@ -51,7 +52,14 @@ def __init__(
ds = xr.open_mfdataset(data)
else:
assert Path(data).exists(), f"{data}: File does not exist."
ds = xr.open_dataset(data)
fp = Path(data)
if fp.suffix == ".dfs2":
# how robust is it to rely on Dataset.to_xarray()?
ds = mikeio.read(data).to_xarray()
for v in ds.data_vars:
ds[v].attrs["long_name"] = ds[v].name
else:
ds = xr.open_dataset(data)

elif isinstance(data, Sequence) and all(
isinstance(file, (str, Path)) for file in data
Expand Down
16 changes: 16 additions & 0 deletions tests/model/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ def trackobs_c2_hm0():
)


def test_grid_from_dfs2():
mr = ms.model_result("tests/testdata/SW/ERA5_DutchCoast.dfs2", item="swh")
assert mr.quantity.name == "swh"
assert mr.quantity.unit == "meter"


def test_grid_from_dfs_explicit():
mr = ms.GridModelResult(
"tests/testdata/SW/ERA5_DutchCoast.dfs2",
item="swh",
quantity=ms.Quantity("Significant height", unit="meter"),
)
assert mr.quantity.name == "Significant height"
assert mr.quantity.unit == "meter"


def test_grid_from_nc(mr_ERA5_pp1d):
mr = mr_ERA5_pp1d
assert mr.name == "ERA5_DutchCoast"
Expand Down
Binary file added tests/testdata/SW/ERA5_DutchCoast.dfs2
Binary file not shown.

0 comments on commit 2b9bc48

Please sign in to comment.