Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
463d370
Add tests for reading dfsu files with a submesh custom block
ryan-kipawa Sep 24, 2026
9d29532
WIP
ryan-kipawa Sep 24, 2026
6edce0b
Keep DfsuFile custom block logic as on master, except for submesh files
ryan-kipawa Sep 29, 2026
4c2c2c4
Restore the file pointer after checking for spectral static items
ryan-kipawa Sep 29, 2026
1a140ec
Read spectral static items from a separate dfs file
ryan-kipawa Sep 29, 2026
ca306e4
Replace __HasSpectralStaticItems with __IsSubMesh
ryan-kipawa Sep 29, 2026
d73b388
Read static items for __IsSubMesh from the dfsu file itself
ryan-kipawa Sep 29, 2026
fd8f77b
Add IsSubMesh and SubMeshType properties to DfsuFile
ryan-kipawa Sep 29, 2026
c568cf4
Annotate __IsSubMesh decisions and inline the build check
ryan-kipawa Sep 29, 2026
30b374e
Identify legacy spectral files by exactly 11 static items
ryan-kipawa Sep 29, 2026
4fda6de
Only test submesh reading on the provided test data files
ryan-kipawa Sep 29, 2026
a6b65ea
Drop the 2D 5 value reference file from the submesh tests
ryan-kipawa Sep 29, 2026
0701ae1
Merge remote-tracking branch 'origin/master' into feature/submesh-bac…
ryan-kipawa Nov 10, 2026
9f721cf
Read 2001 files by the legacy Spectral0D exception, expose SubMeshType
ryan-kipawa Nov 11, 2026
15670b4
Add DfsuBuilder.SetSubMeshType
ryan-kipawa Nov 11, 2026
859c481
Describe submesh type reading and writing in the CHANGELOG
ryan-kipawa Nov 11, 2026
d2a68e6
Test legacy Spectral0D files with generic item types and 7 values
ryan-kipawa Nov 11, 2026
0524c03
Address review of submesh type reading and writing
ryan-kipawa Nov 11, 2026
3175f2a
Remove the pyrefly warnings added on this branch
ryan-kipawa Nov 11, 2026
0ce448a
Read single-axis legacy Spectral0D files as DfsuSpectral0D
ryan-kipawa Nov 11, 2026
8686f5e
Shorten submesh comments and docstrings
ryan-kipawa Nov 11, 2026
6c20079
Merge remote-tracking branch 'origin/master' into feature/submesh-bac…
ryan-kipawa Nov 11, 2026
589ad09
Narrow spectral axes before len in the legacy Spectral0D tests
ryan-kipawa Nov 11, 2026
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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `DfsuFile.SubMeshType` tells whether a dfsu file from MIKE 2027 has a
local refinement submesh: 1 if it has, 0 if not, and `None` for a file
without a submesh type. Compare it with `None`, as a plain truth test is
false for submesh type 0
([#54](https://github.com/DHI/mikecore-python/issues/54)).
- `DfsuBuilder.SetSubMeshType(None | 0 | 1)` writes a dfsu file with a
submesh type, as MIKE 2027 does. Without it, older mikecore releases can
still open the file. With it, they can only open a 2D file with submesh
type 0.
- `MIKECORE_PYTHON_BIN`: set it to a MIKE installation's `bin` folder to have
mikecore use that installation's native libraries instead of its bundled
ones, at your own risk (see README).
Expand All @@ -29,6 +38,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `DfsuFile` opens dfsu files from MIKE 2027 with a submesh type, such as 3D
files and locally refined meshes, which raised "not a valid dfsu file".
Files from later MIKE versions with more custom block values also open.
Legacy Spectral0D (rose-plot) files still open as `DfsuSpectral0D`,
including files with frequencies only or directions only
([#54](https://github.com/DHI/mikecore-python/issues/54)).
- A failed open of a dfs file shorter than 4 bytes, such as an empty file, no
longer leaves the file open. Each such open used up a file handle, so after
about 500 of them no file could be opened (Error code 2003)
Expand Down
52 changes: 45 additions & 7 deletions mikecore/DfsuBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
from mikecore.eum import eumUnit, eumQuantity, eumItem
from mikecore.Projections import MapProjection

spectralDfsuFileTypes = (
DfsuFileType.DfsuSpectral0D,
DfsuFileType.DfsuSpectral1D,
DfsuFileType.DfsuSpectral2D,
)


# / <summary>
# / Builder for creating a dfsu file.
Expand Down Expand Up @@ -48,6 +54,7 @@ def __init__(self, dfsuFileType):

self.__dfsuFileType = dfsuFileType
self.__numberOfSigmaLayers = -1
self.__subMeshType: int | None = None

# Projection variables
self.__dfsProjection: DfsProjection | None = None
Expand Down Expand Up @@ -125,6 +132,31 @@ def SetNumberOfSigmaLayers(self, numberOfSigmaLayers: int):
else:
raise Exception("dfsuFileType")

def SetSubMeshType(self, subMeshType: int | np.integer | None):
"""
Set the submesh type: 1 for a local refinement submesh, 0 otherwise.
The mesh is not checked against it.

None (default) writes the pre-2027 5-value custom block; 0 or 1 writes
the MIKE 2027 6-value block. Older mikecore releases can open such a
file only if it is 2D with type 0. MIKE before 2027 is untested.

Raises ValueError for values other than None, 0 or 1, and an exception
for spectral file types.
"""
# True is an int, but not a submesh type
isInteger = isinstance(subMeshType, (int, np.integer)) and not isinstance(
subMeshType, bool
)
if self.__dfsuFileType in spectralDfsuFileTypes:
raise Exception("Can not set submesh type on a spectral dfsu file")
elif subMeshType is None:
self.__subMeshType = None
elif isInteger and subMeshType in (0, 1):
self.__subMeshType = int(subMeshType)
else:
raise ValueError(f"Submesh type must be None, 0 or 1, not {subMeshType!r}")

def SetFrequencies(self, frequencies):
self.__frequencies = frequencies

Expand Down Expand Up @@ -400,11 +432,7 @@ def Validate(self, dieOnError: bool = False):
errors.append(
"The minimum number of layers is smaller than the number of sigma layers. Element table is invalid"
)
elif self.__dfsuFileType in (
DfsuFileType.DfsuSpectral0D,
DfsuFileType.DfsuSpectral1D,
DfsuFileType.DfsuSpectral2D,
):
elif self.__dfsuFileType in spectralDfsuFileTypes:
# TODO do we need to check frequency or directions?
pass
else:
Expand Down Expand Up @@ -497,11 +525,15 @@ def SetupBuilder(self):
)
dfsBuilder.DeleteValueFloat = np.float32(1e-35)

# Set up custom block
# Set up custom block, with the submesh type as an optional 6th value
subMeshTypeValues = [] if self.__subMeshType is None else [self.__subMeshType]
if self.__dfsuFileType == DfsuFileType.Dfsu2D:
dfsBuilder.AddCreateCustomBlock(
"MIKE_FM",
np.array([nodes.x.size, len(connectivity), 2, 0, 0], np.int32),
np.array(
[nodes.x.size, len(connectivity), 2, 0, 0, *subMeshTypeValues],
np.int32,
),
)
elif self.__dfsuFileType == DfsuFileType.DfsuVerticalColumn:
maxNumberOfLayers = len(connectivity)
Expand All @@ -514,6 +546,7 @@ def SetupBuilder(self):
1,
maxNumberOfLayers,
self.__numberOfSigmaLayers,
*subMeshTypeValues,
],
np.int32,
),
Expand All @@ -529,6 +562,7 @@ def SetupBuilder(self):
2,
maxNumberOfLayers,
self.__numberOfSigmaLayers,
*subMeshTypeValues,
],
np.int32,
),
Expand All @@ -546,6 +580,7 @@ def SetupBuilder(self):
2,
maxNumberOfLayers,
self.__numberOfSigmaLayers,
*subMeshTypeValues,
],
np.int32,
),
Expand All @@ -561,6 +596,7 @@ def SetupBuilder(self):
3,
maxNumberOfLayers,
self.__numberOfSigmaLayers,
*subMeshTypeValues,
],
np.int32,
),
Expand All @@ -578,6 +614,7 @@ def SetupBuilder(self):
3,
maxNumberOfLayers,
self.__numberOfSigmaLayers,
*subMeshTypeValues,
],
np.int32,
),
Expand Down Expand Up @@ -782,6 +819,7 @@ def CreateDfsu(self, dfsBuilder, elementType, nodesPerElmt, connectivityArray):
self.__zUnit,
frequencyItem,
directionItem,
self.__subMeshType,
)

return dfsuFile
Expand Down
111 changes: 96 additions & 15 deletions mikecore/DfsuFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ class DfsuFile:
Use the DfsuBuilder to create a new dfsu file.
You can read, write and append item-timesteps to the file.
The geometry can be altered, by moving the nodes, but the element connectivity can not be changed.

The "MIKE_FM" custom block (dfsu file specification, section 4.3) of a
data type 2001 file is read as:

1. 5 values or fewer (before MIKE 2027): value 5, if present, is the
number of sigma layers, otherwise it equals the number of layers.
2. Exactly 6 values in a legacy Spectral0D file: values 5 and 6 are the
number of frequencies and directions. This undocumented layout has one
static item per axis after the 9 of section 4.4.
3. Otherwise (MIKE 2027 and later): value 5 is the number of sigma layers,
value 6 the submesh type. Values from 7 on are ignored.

Other data types are read by their own fixed layouts.
"""

def __init__(self, dfsFile=None):
Expand All @@ -58,6 +71,7 @@ def __init__(self, dfsFile=None):
self.NumberOfSigmaLayers = -1
self.NumberOfFrequencies = -1
self.NumberOfDirections = -1
self.__subMeshType: int | None = None

# Static item
self.__nodeIdItem = None
Expand Down Expand Up @@ -94,7 +108,7 @@ def __init__(self, dfsFile=None):
if dfsFile is not None:
self.__Init(dfsFile)

def __Init(self, dfsFile, build=False):
def __Init(self, dfsFile, build=False, subMeshType=None):
"""
Wrap a dfs file in the DfsuFile object.
It will throw an exception if file is not a dfsu file.
Expand Down Expand Up @@ -131,15 +145,31 @@ def __Init(self, dfsFile, build=False):
self.NumberOfSigmaLayers = customBlock[4]
else:
self.NumberOfSigmaLayers = self.NumberOfLayers
self.NumberOfFrequencies = 0
self.NumberOfDirections = 0
self.__subMeshType = None

if self.FileInfo.DataType in (2002, 2003) or (
self.FileInfo.DataType == 2001 and (customBlock.Count == 6)
):
if self.FileInfo.DataType in (2002, 2003):
self.NumberOfFrequencies = customBlock[4]
self.NumberOfDirections = customBlock[5]
else:
self.NumberOfFrequencies = 0
self.NumberOfDirections = 0
elif self.FileInfo.DataType == 2001 and customBlock.Count >= 6:
if build:
# Static items are not written yet. DfsuBuilder writes 6
# values only with a submesh type, never for Spectral0D.
if subMeshType is None:
raise Exception(
"DfsuBuilder wrote 6 custom block values without a submesh type"
)
isLegacySpectral0D = False
else:
isLegacySpectral0D = self.__IsLegacySpectral0D(dfsFile, customBlock)
if isLegacySpectral0D:
self.NumberOfFrequencies = customBlock[4]
self.NumberOfDirections = customBlock[5]
else:
# MIKE 2027 layout
self.NumberOfSigmaLayers = customBlock[4]
self.__subMeshType = int(customBlock[5])

# Figuring out dfsu file type from custom block MIKE_FM
if dimensions == 1:
Expand Down Expand Up @@ -193,7 +223,8 @@ def __Init(self, dfsFile, build=False):
# "Frequency" , double
# "Direction" , double

self.__nodeIdItem = self.dfsFile.ReadStaticItemNext()
# Read by number, __IsLegacySpectral0D may have moved the file pointer
self.__nodeIdItem = self.dfsFile.ReadStaticItem(1)
CheckForNull(self.__nodeIdItem)
self.NodeIds = self.__nodeIdItem.Data

Expand Down Expand Up @@ -257,11 +288,11 @@ def __Init(self, dfsFile, build=False):
k1 = k2

# Spectral Dfsu
if self.NumberOfFrequencies:
if self.NumberOfFrequencies > 0:
frequency = self.dfsFile.ReadStaticItemNext()
CheckForNull(frequency)
self.Frequencies = frequency.Data
if self.NumberOfDirections:
if self.NumberOfDirections > 0:
direction = self.dfsFile.ReadStaticItemNext()
CheckForNull(direction)
self.Directions = direction.Data
Expand All @@ -277,6 +308,48 @@ def __Init(self, dfsFile, build=False):
if not build and self.dfsFile.FileMode == DfsFileMode.Append:
self.dfsFile.FindTimeStep(self.NumberOfTimeSteps)

def __IsLegacySpectral0D(self, dfsFile, customBlock):
"""
Check if a 2001 file is a legacy Spectral0D (rose-plot) file: exactly
6 custom block values, [..., NumberOfFrequencies, NumberOfDirections],
at least one of them above 0, and after the 9 static items of section
4.4 exactly one static item per such axis: frequencies in hertz, then
directions in radians or degrees.
"""
if customBlock.Count != 6:
return False

numberOfFrequencies = customBlock[4]
numberOfDirections = customBlock[5]
if numberOfFrequencies <= 0 and numberOfDirections <= 0:
return False

itemNumber = 10
if numberOfFrequencies > 0:
frequency = dfsFile.ReadStaticItem(itemNumber)
itemNumber += 1
if not (
frequency is not None
and frequency.Quantity.Item
in (eumItem.eumIWaveFrequency, eumItem.eumIFrequency)
and frequency.Quantity.Unit == eumUnit.eumUhertz
and frequency.ElementCount == numberOfFrequencies
):
return False
if numberOfDirections > 0:
direction = dfsFile.ReadStaticItem(itemNumber)
itemNumber += 1
if not (
direction is not None
and direction.Quantity.Item
in (eumItem.eumIWaveDirection, eumItem.eumIDirection)
and direction.Quantity.Unit in (eumUnit.eumUradian, eumUnit.eumUdegree)
and direction.ElementCount == numberOfDirections
):
return False

return dfsFile.ReadStaticItem(itemNumber) is None

def Dispose(self):
"""
Close file and release ressources on the unmanaged side.
Expand Down Expand Up @@ -335,10 +408,6 @@ def OpenAppend(fileName):
dfs.Open(fileName, DfsFileMode.Append)
return DfsuFile(dfs)

"""
Internal factory method, used by the DfsuBuilder
"""

def DfsuFileBuild(
self,
dfsFile,
Expand All @@ -359,7 +428,11 @@ def DfsuFileBuild(
zUnit,
freqItem,
dirItem,
subMeshType=None,
):
"""
Internal factory method, used by the DfsuBuilder
"""

self.dfsFile = dfsFile
self.__nodeIdItem = nodeIdItem
Expand All @@ -379,7 +452,7 @@ def DfsuFileBuild(
self.ZUnit = zUnit
self.__freqItem = freqItem
self.__dirItem = dirItem
self.__Init(dfsFile, build=True)
self.__Init(dfsFile, build=True, subMeshType=subMeshType)

def __GetFileName(self):
return self.FileInfo.FileName
Expand Down Expand Up @@ -467,6 +540,14 @@ def __SetDeleteValueFloat(self, value):
def IsSpectral(self):
return (self.NumberOfFrequencies > 0) or (self.NumberOfDirections > 0)

@property
def SubMeshType(self) -> int | None:
"""
Submesh type: 1 for a local refinement submesh, 0 otherwise, None if
the file has none. Compare with None, as type 0 is falsy.
"""
return self.__subMeshType

@staticmethod
def CreateEmptyItemDatas(dfsFile: DfsFile):
"""
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Loading