Skip to content

Commit

Permalink
Merge branch 'main' into docs_navs
Browse files Browse the repository at this point in the history
  • Loading branch information
ecomodeller authored Jan 17, 2025
2 parents d4cff3b + 2d8fa70 commit 5a48499
Show file tree
Hide file tree
Showing 26 changed files with 106 additions and 537 deletions.
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ build: typecheck test
python -m build

lint:
ruff check $(LIB)/
ruff check .

format:
ruff format $(LIB)/

pylint:
pylint --disable=all --enable=attribute-defined-outside-init mikeio/

test:
pytest --disable-warnings

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

Read, write and manipulate dfs0, dfs1, dfs2, dfs3, dfsu and mesh files.

MIKE IO facilitates common data processing workflows for [MIKE files in Python](https://www.mikepoweredbydhi.com/products/mike-for-developers#io).
MIKE IO facilitates common data processing workflows for [MIKE files](https://www.dhigroup.com/technologies/mikepoweredbydhi) using Python.

[![MIKEIO. Read, write and analyze MIKE dfs files with Python on Vimeo](https://raw.githubusercontent.com/DHI/mikeio/main/images/youtube1.png)](https://player.vimeo.com/video/708275619)

Expand Down
2 changes: 2 additions & 0 deletions mikeio/dataset/_data_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,8 @@ def __init__(self, da: DataArray) -> None:


class _DatasetPlotter:
"""Class for plotting scatter plots from datasets."""

def __init__(self, ds: Dataset) -> None:
self.ds = ds

Expand Down
2 changes: 2 additions & 0 deletions mikeio/dfs/_dfs0.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def _write_dfs0(


class Dfs0:
"""Class for reading/writing dfs0 files."""

def __init__(self, filename: str | Path):
"""Create a Dfs0 object for reading, writing.
Expand Down
2 changes: 2 additions & 0 deletions mikeio/dfs/_dfs1.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def _write_dfs1_header(filename: str | Path, ds: Dataset, title: str) -> DfsFile


class Dfs1(_Dfs123):
"""Class for reading/writing dfs1 files."""

_ndim = 1

def __init__(self, filename: str | Path) -> None:
Expand Down
2 changes: 2 additions & 0 deletions mikeio/dfs/_dfs2.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ def _write_dfs2_spatial_axis(


class Dfs2(_Dfs123):
"""Class for reading/writing dfs2 files."""

_ndim = 2

def __init__(
Expand Down
2 changes: 2 additions & 0 deletions mikeio/dfs/_dfs3.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def _write_dfs3_header(filename: str | Path, ds: Dataset, title: str) -> DfsFile


class Dfs3(_Dfs123):
"""Class for reading/writing dfs3 files."""

_ndim = 3

def __init__(self, filename: str | Path):
Expand Down
2 changes: 2 additions & 0 deletions mikeio/dfsu/_dfsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ def _get_dfsu_info(filename: str | Path) -> _DfsuInfo:


class Dfsu2DH:
"""Class for reading/writing dfsu 2d horizontal files."""

show_progress = False

def __init__(self, filename: str | Path) -> None:
Expand Down
2 changes: 2 additions & 0 deletions mikeio/dfsu/_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ def dfsu(filename: str | Path) -> Any:


class Dfsu:
"""Factory class for dfsu files."""

def __new__(self, filename: str | Path) -> Any:
return dfsu(filename)
4 changes: 4 additions & 0 deletions mikeio/dfsu/_layered.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ def append(self, ds: Dataset, validate: bool = True) -> None:


class Dfsu2DV(DfsuLayered):
"""Class for reading/writing dfsu 2d vertical files."""

def plot_vertical_profile(
self,
values: np.ndarray | DataArray,
Expand Down Expand Up @@ -465,6 +467,8 @@ def plot_vertical_profile(


class Dfsu3D(DfsuLayered):
"""Class for reading/writing dfsu 3d files."""

@property
def geometry2d(self) -> GeometryFM2D:
"""The 2d geometry for a 3d object."""
Expand Down
2 changes: 2 additions & 0 deletions mikeio/dfsu/_spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@


class DfsuSpectral:
"""Dfsu for Spectral data."""

show_progress = False

def __init__(self, filename: str | Path) -> None:
Expand Down
2 changes: 1 addition & 1 deletion mikeio/eum/_eum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ def __repr__(self) -> str:


class ItemInfo:
"""ItemInfo.
"""Info for dynamicc items (variables).
Parameters
----------
Expand Down
2 changes: 2 additions & 0 deletions mikeio/pfs/_pfssection.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class PfsNonUniqueList(list):


class PfsSection(SimpleNamespace, MutableMapping[str, Any]):
"""Class for reading/writing sections in a pfs file."""

@staticmethod
def from_dataframe(df: pd.DataFrame, prefix: str) -> "PfsSection":
"""Create a PfsSection from a DataFrame.
Expand Down
2 changes: 2 additions & 0 deletions mikeio/spatial/_FM_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ def __eq__(self, value: Any) -> bool:


class GeometryFM2D(_GeometryFM):
"""Flexible 2d mesh geometry."""

def __init__(
self,
node_coordinates: np.ndarray,
Expand Down
25 changes: 18 additions & 7 deletions mikeio/spatial/_FM_geometry_layered.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,27 @@ def to_2d_geometry(self) -> GeometryFM2D:
Returns
-------
UnstructuredGeometry
2d geometry (bottom nodes)
GeometryFM2D
2d geometry
"""
# extract information for selected elements
elem_ids = self.bottom_elements
if self._type == DfsuFileType.Dfsu3DSigmaZ:
# for z-layers nodes will not match on neighboring elements!
elem_ids = self.top_elements
match self._type:
case DfsuFileType.Dfsu3DSigmaZ:
elem_ids = self.top_elements
case DfsuFileType.Dfsu3DSigma:
elem_ids = self.bottom_elements
case _:
raise NotImplementedError(
f"Conversion to 2D not implemented for {self._type}"
)

node_ids, elem_tbl = self._get_nodes_and_table_for_elements(
elem_ids, node_layers="bottom"
)
node_coords = self.node_coordinates[node_ids]
codes = self._codes[node_ids]

# create new geometry
geom = GeometryFM2D(
node_coordinates=node_coords,
codes=codes,
Expand All @@ -255,6 +259,9 @@ def to_2d_geometry(self) -> GeometryFM2D:
reindex=True,
)

# TODO do this before creating the geometry

# TODO extract method
# Fix z-coordinate for sigma-z:
if self._type == DfsuFileType.Dfsu3DSigmaZ:
zn = geom.node_coordinates[:, 2].copy()
Expand Down Expand Up @@ -613,6 +620,8 @@ def _calc_dz(self) -> np.ndarray:


class GeometryFM3D(_GeometryFMLayered):
"""Flexible 3d mesh geometry."""

def __init__(
self,
*,
Expand Down Expand Up @@ -701,6 +710,8 @@ def find_index(


class GeometryFMVerticalProfile(_GeometryFMLayered):
"""Flexible mesh 2d vertical profile geometry."""

def __init__(
self,
node_coordinates: np.ndarray,
Expand Down
6 changes: 6 additions & 0 deletions mikeio/spatial/_FM_geometry_spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@


class GeometryFMPointSpectrum(_Geometry):
"""Flexible mesh point spectrum."""

def __init__(
self,
frequencies: np.ndarray | None = None,
Expand Down Expand Up @@ -126,6 +128,8 @@ def directions(self) -> np.ndarray | None:

# TODO reconsider inheritance to avoid overriding method signature
class GeometryFMAreaSpectrum(_GeometryFMSpectrum):
"""Flexible mesh area spectrum geometry."""

def isel( # type: ignore
self, idx: Sequence[int], **kwargs: Any
) -> "GeometryFMPointSpectrum" | "GeometryFMAreaSpectrum":
Expand Down Expand Up @@ -178,6 +182,8 @@ def elements_to_geometry( # type: ignore

# TODO this inherits indirectly from GeometryFM2D, which is not ideal
class GeometryFMLineSpectrum(_GeometryFMSpectrum):
"""Flexible mesh line spectrum geometry."""

def isel( # type: ignore
self, idx: Sequence[int], axis: str = "node"
) -> GeometryFMPointSpectrum | GeometryFMLineSpectrum:
Expand Down
6 changes: 4 additions & 2 deletions mikeio/spatial/_grid_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def _print_axis_txt(name: str, x: np.ndarray, dx: float) -> str:

@dataclass
class Grid1D(_Geometry):
"""1D grid (node-based)."""

_dx: float
_nx: int
_x0: float
Expand All @@ -89,9 +91,9 @@ def __init__(
node_coordinates: np.ndarray | None = None,
axis_name: str = "x",
):
"""1D grid (node-based).
"""Create a Grid1D.
axis is increasing and equidistant
The axis is increasing and equidistant
Parameters
----------
Expand Down
6 changes: 3 additions & 3 deletions notebooks/DataArray - Dfs123 plotting.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@
}
],
"source": [
"da.plot.contourf(figsize=(10,6), levels=4, label=\"Højde (m)\");\n",
"da.plot.contourf(figsize=(10,6), levels=4, label=\"Højde (m)\")\n",
"plt.xlabel(\"Længdekreds (°)\")\n",
"plt.ylabel(\"Breddekreds (°)\")"
]
Expand All @@ -331,7 +331,7 @@
],
"source": [
"ax = da.plot.contour(figsize=(8,8), cmap=\"plasma\")\n",
"ax.set_xlim([12.5, 12.9]);\n",
"ax.set_xlim([12.5, 12.9])\n",
"ax.set_ylim([55.8, 56]);"
]
},
Expand Down Expand Up @@ -477,7 +477,7 @@
}
],
"source": [
"ax = ds.Temperature[:,0,:,:].plot.contourf();\n",
"ax = ds.Temperature[:,0,:,:].plot.contourf()\n",
"ax.grid()"
]
},
Expand Down
2 changes: 1 addition & 1 deletion notebooks/DataArray - Dfsu plotting.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
],
"source": [
"_, ax = plt.subplots(1,2)\n",
"da.plot.mesh(ax=ax[0]);\n",
"da.plot.mesh(ax=ax[0])\n",
"da.plot.outline(ax=ax[1]);"
]
},
Expand Down
1 change: 0 additions & 1 deletion notebooks/Dfsu - Export to netcdf.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.remove(\"oresund_sigma_z.nc\")"
]
}
Expand Down
529 changes: 24 additions & 505 deletions notebooks/Dfsu - Export to shapefile.ipynb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions notebooks/Dfsu - Spectral data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
}
],
"source": [
"ax = da.plot.patch(rmax=8);\n",
"ax = da.plot.patch(rmax=8)\n",
"dird = np.round(da.directions, 2)\n",
"ax.set_thetagrids(dird, labels=dird);"
]
Expand Down Expand Up @@ -356,8 +356,8 @@
"def plot_element(id=(0,da.geometry.n_elements-1), step=(0,da.n_timesteps-1)):\n",
" spec = da[step,id]\n",
" time = da.start_time + timedelta(seconds=(step*da.timestep))\n",
" spec.plot(vmax=0.04, vmin=0, rmax=8, title=f\"Wave spectrum, {time}, element: {id}\");\n",
" plt.show();"
" spec.plot(vmax=0.04, vmin=0, rmax=8, title=f\"Wave spectrum, {time}, element: {id}\")\n",
" plt.show()"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion notebooks/Dfsu - Vertical Profile.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
"ax[0].plot(*ptA, color=\"b\", marker=\"*\", markersize=10)\n",
"ax[0].plot(*ptB, color=\"b\", marker=\"*\", markersize=10)\n",
"\n",
"g.plot.mesh(ax=ax[1], title=\"Transect mesh\");\n",
"g.plot.mesh(ax=ax[1], title=\"Transect mesh\")\n",
"ax[1].axvline(distA, color=\"0.5\")\n",
"ax[1].text(distA + 500, -20, 'position A')\n",
"ax[1].axvline(distB, color=\"0.5\")\n",
Expand Down
14 changes: 7 additions & 7 deletions notebooks/Dfsu and Mesh - Plotting.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,12 @@
"source": [
"fig, ax = plt.subplots(3,2)\n",
"\n",
"geom.plot(title='patch', ax=ax[0,0]);\n",
"geom.plot.contourf(title='contourf', levels=5, ax=ax[0,1]);\n",
"geom.plot(plot_type='shaded', title='shaded', ax=ax[1,0]);\n",
"geom.plot.contour(title='contour', show_mesh=True, levels=6, vmin=-30, ax=ax[1,1]);\n",
"geom.plot.mesh(title='mesh_only', ax=ax[2,0]);\n",
"geom.plot.outline(title='outline_only', ax=ax[2,1]);\n",
"geom.plot(title='patch', ax=ax[0,0])\n",
"geom.plot.contourf(title='contourf', levels=5, ax=ax[0,1])\n",
"geom.plot(plot_type='shaded', title='shaded', ax=ax[1,0])\n",
"geom.plot.contour(title='contour', show_mesh=True, levels=6, vmin=-30, ax=ax[1,1])\n",
"geom.plot.mesh(title='mesh_only', ax=ax[2,0])\n",
"geom.plot.outline(title='outline_only', ax=ax[2,1])\n",
"\n",
"plt.tight_layout()"
]
Expand Down Expand Up @@ -457,7 +457,7 @@
}
],
"source": [
"ax = da.plot(cmap='plasma');\n",
"ax = da.plot(cmap='plasma')\n",
"dfs.geometry.plot.outline(ax=ax, title=None);"
]
},
Expand Down
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ notebooks = [
ignore = ["E501", "E741"]

select = [
# ruff defaults
"E4",
"E7",
"E9",
"F",
# ruff defaults
"D100",
"D101",
"D102",
Expand All @@ -113,3 +119,8 @@ select = [
"D415",
"D417",
]

[tool.ruff.lint.per-file-ignores]
"tests/*" = ["D"]
"notebooks/*" = ["D", "E402"]
"conftest.py" = ["D"]
2 changes: 1 addition & 1 deletion tests/test_dfsu_layered.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def test_top_elements_subset() -> None:

assert len(subg.top_elements) == 6

ds = mikeio.Dfsu3D(filename).read(elements=idx3d)
_ = mikeio.Dfsu3D(filename).read(elements=idx3d)


def test_bottom_elements():
Expand Down

0 comments on commit 5a48499

Please sign in to comment.