diff --git a/mikecore/DfsuFile.py b/mikecore/DfsuFile.py
index 76fd3ab..cc41289 100644
--- a/mikecore/DfsuFile.py
+++ b/mikecore/DfsuFile.py
@@ -116,23 +116,35 @@ def __Init(self, dfsFile, build = False):
dimensions = customBlock[2];
self.NumberOfLayers = customBlock[3];
- if (customBlock.Count == 5):
- self.NumberOfSigmaLayers = customBlock[4];
- else:
+ if (self.FileInfo.DataType in (2002, 2003)):
self.NumberOfSigmaLayers = self.NumberOfLayers;
-
- if (self.FileInfo.DataType in (2002, 2003) or (self.FileInfo.DataType == 2001 and (customBlock.Count == 6))):
self.NumberOfFrequencies = customBlock[4];
self.NumberOfDirections = customBlock[5];
else:
+ if (customBlock.Count >= 5):
+ self.NumberOfSigmaLayers = customBlock[4];
+ else:
+ self.NumberOfSigmaLayers = self.NumberOfLayers;
self.NumberOfFrequencies = 0;
self.NumberOfDirections = 0;
+ # A 6 value custom block in a 2001 file is either a legacy spectral
+ # file, [..., NumberOfFrequencies, NumberOfDirections], or, from
+ # version 2027, [..., NumberOfSigmaLayers, SubMesh]. Legacy spectral
+ # files are identified by their frequency and direction static items.
+ if (self.FileInfo.DataType == 2001 and customBlock.Count == 6):
+ legacyFrequencies = customBlock[4];
+ legacyDirections = customBlock[5];
+ else:
+ legacyFrequencies = 0;
+ legacyDirections = 0;
+
# Figuring out dfsu file type from custom block MIKE_FM
if (dimensions == 1):
if (self.NumberOfLayers > 0):
self.DfsuFileType = DfsuFileType.DfsuVerticalColumn;
- elif (self.FileInfo.DataType == 2001 and (self.NumberOfFrequencies == numberOfElmts or self.NumberOfDirections == numberOfElmts)):
+ elif ((legacyFrequencies == numberOfElmts or legacyDirections == numberOfElmts)
+ and self.__HasSpectralStaticItems(legacyFrequencies, legacyDirections, build)):
# Spectral Frequency-Direction (Rose-plot) geometry
self.DfsuFileType = DfsuFileType.DfsuSpectral0D;
elif (self.FileInfo.DataType == 2002 and self.IsSpectral):
@@ -142,7 +154,8 @@ def __Init(self, dfsFile, build = False):
self.DfsuFileType = DfsuFileType.Dfsu1D;
elif (dimensions == 2):
- if (self.FileInfo.DataType == 2001 and (self.NumberOfFrequencies*self.NumberOfDirections == numberOfElmts)):
+ if (legacyFrequencies*legacyDirections == numberOfElmts
+ and self.__HasSpectralStaticItems(legacyFrequencies, legacyDirections, build)):
# Spectral Frequency-Direction (Rose-plot) geometry
self.DfsuFileType = DfsuFileType.DfsuSpectral0D;
elif self.FileInfo.DataType == 2003:
@@ -160,6 +173,11 @@ def __Init(self, dfsFile, build = False):
else:
self.DfsuFileType = DfsuFileType.Dfsu3DSigmaZ;
+ if (self.DfsuFileType == DfsuFileType.DfsuSpectral0D):
+ self.NumberOfSigmaLayers = self.NumberOfLayers;
+ self.NumberOfFrequencies = legacyFrequencies;
+ self.NumberOfDirections = legacyDirections;
+
# Do not read static items when building, they are already set
if (not build):
@@ -176,7 +194,9 @@ def __Init(self, dfsFile, build = False):
# "Frequency" , double
# "Direction" , double
- self.__nodeIdItem = self.dfsFile.ReadStaticItemNext(); CheckForNull(self.__nodeIdItem);
+ # Read by number, the file pointer may have been moved by
+ # __HasSpectralStaticItems
+ self.__nodeIdItem = self.dfsFile.ReadStaticItem(1); CheckForNull(self.__nodeIdItem);
self.NodeIds = self.__nodeIdItem.Data
# X can be in doubles or in floats. Floats are converted to doubles
@@ -250,6 +270,40 @@ def __Init(self, dfsFile, build = False):
self.dfsFile.FindTimeStep(self.NumberOfTimeSteps);
+ # Frequency and direction static items, as written by MIKE and by DfsuBuilder
+ __frequencyItemTypes = (eumItem.eumIWaveFrequency, eumItem.eumIFrequency)
+ __directionItemTypes = (eumItem.eumIWaveDirection, eumItem.eumIDirection)
+
+ def __HasSpectralStaticItems(self, numberOfFrequencies, numberOfDirections, build):
+ """
+ Check if a 2001 file with a 6 value custom block is a spectral file:
+ static item 10 (and 11) must be the frequency and/or direction items,
+ matching the number of frequencies and directions in the custom block.
+ Units are not checked, directions may be in degrees or radians.
+ """
+ if (numberOfFrequencies <= 0 and numberOfDirections <= 0):
+ return False
+
+ if (build):
+ return ((numberOfFrequencies > 0) == (self.__freqItem is not None)
+ and (numberOfDirections > 0) == (self.__dirItem is not None))
+
+ itemNumber = 10
+ if (numberOfFrequencies > 0):
+ item = self.dfsFile.ReadStaticItem(itemNumber)
+ if (item is None
+ or item.Quantity.Item not in self.__frequencyItemTypes
+ or item.ElementCount != numberOfFrequencies):
+ return False
+ itemNumber += 1
+ if (numberOfDirections > 0):
+ item = self.dfsFile.ReadStaticItem(itemNumber)
+ if (item is None
+ or item.Quantity.Item not in self.__directionItemTypes
+ or item.ElementCount != numberOfDirections):
+ return False
+ return True
+
def Dispose(self):
"""
Close file and release ressources on the unmanaged side.
diff --git a/testdata/submesh/Dfsu2D_CustomBlock5.dfsu b/testdata/submesh/Dfsu2D_CustomBlock5.dfsu
new file mode 100644
index 0000000..ad10f2c
Binary files /dev/null and b/testdata/submesh/Dfsu2D_CustomBlock5.dfsu differ
diff --git a/testdata/submesh/Dfsu2D_CustomBlock6_Submesh0.dfsu b/testdata/submesh/Dfsu2D_CustomBlock6_Submesh0.dfsu
new file mode 100644
index 0000000..5906c04
Binary files /dev/null and b/testdata/submesh/Dfsu2D_CustomBlock6_Submesh0.dfsu differ
diff --git a/testdata/submesh/Dfsu2D_CustomBlock6_Submesh1_LocallyRefined.dfsu b/testdata/submesh/Dfsu2D_CustomBlock6_Submesh1_LocallyRefined.dfsu
new file mode 100644
index 0000000..3883559
Binary files /dev/null and b/testdata/submesh/Dfsu2D_CustomBlock6_Submesh1_LocallyRefined.dfsu differ
diff --git a/testdata/submesh/Dfsu3DSigmaZ_CustomBlock5.dfsu b/testdata/submesh/Dfsu3DSigmaZ_CustomBlock5.dfsu
new file mode 100644
index 0000000..c4cee0f
Binary files /dev/null and b/testdata/submesh/Dfsu3DSigmaZ_CustomBlock5.dfsu differ
diff --git a/testdata/submesh/Dfsu3DSigmaZ_CustomBlock6_Submesh0.dfsu b/testdata/submesh/Dfsu3DSigmaZ_CustomBlock6_Submesh0.dfsu
new file mode 100644
index 0000000..5a817a7
Binary files /dev/null and b/testdata/submesh/Dfsu3DSigmaZ_CustomBlock6_Submesh0.dfsu differ
diff --git a/testdata/submesh/DfsuSpectral0D_CustomBlock6_Legacy.dfsu b/testdata/submesh/DfsuSpectral0D_CustomBlock6_Legacy.dfsu
new file mode 100644
index 0000000..d35a479
Binary files /dev/null and b/testdata/submesh/DfsuSpectral0D_CustomBlock6_Legacy.dfsu differ
diff --git a/tests/test_dfsu_file.py b/tests/test_dfsu_file.py
index 3dbd9f8..f0316b4 100644
--- a/tests/test_dfsu_file.py
+++ b/tests/test_dfsu_file.py
@@ -186,6 +186,147 @@ def Read3DSigmaOdenseTest(self, filename, original):
#endregion
+ #region Submesh files
+
+ # From version 2027, the "MIKE_FM" custom block of a dfsu 2001 file may
+ # have 6 values (previously 4 or 5), the 6th being the submesh type
+ # (1 = local refinement submesh, 0 otherwise):
+ #
+ # [NumberOfNodes, NumberOfElements, Dimension, NumberOfLayers, NumberOfSigmaLayers, SubMesh]
+ #
+ # Data types 2002, 2003 and 2004 have always had 6 values, with their own
+ # layouts, and are unaffected. The problem is that a 6 value custom block
+ # in a 2001 file was taken to mean a legacy spectral (rose-plot) file:
+ #
+ # [NumberOfNodes, NumberOfElements, Dimension, NumberOfLayers, NumberOfFrequencies, NumberOfDirections]
+ #
+ # See the DFSU file specification, section 4.3 "Custom Block", and
+ # https://github.com/DHI/mikecore-python/issues/54
+ #
+ # Test files in testdata/submesh, all data type 2001, named by dfsu file
+ # type, number of custom block values and submesh type:
+ #
+ # File MIKE_FM custom block
+ # Dfsu2D_CustomBlock5.dfsu [ 9, 4, 2, 0, 0]
+ # Dfsu2D_CustomBlock6_Submesh0.dfsu [ 9, 4, 2, 0, 0, 0]
+ # Dfsu2D_CustomBlock6_Submesh1_LocallyRefined.dfsu [ 31, 40, 2, 0, 0, 1]
+ # Dfsu3DSigmaZ_CustomBlock5.dfsu [ 30, 8, 3, 3, 1]
+ # Dfsu3DSigmaZ_CustomBlock6_Submesh0.dfsu [ 30, 8, 3, 3, 1, 0]
+ # DfsuSpectral0D_CustomBlock6_Legacy.dfsu [416, 400, 2, 0, 25, 16]
+ #
+ # The CustomBlock5 and CustomBlock6_Submesh0 files of each pair hold the
+ # same mesh and data, and differ only in the custom block. The Legacy
+ # spectral file is a rose-plot file with 25 frequencies and 16 directions
+ # (25*16 = 400 elements), which must still read as spectral.
+
+ #/
+ #/ Reading a 2D dfsu file with submesh type 0. It must read the
+ #/ same as the old format file with a 5 value custom block.
+ #/
+ def test_Read2DSubmesh0Test(self):
+ self.SubmeshCustomBlockTester("testdata/submesh/Dfsu2D_CustomBlock5.dfsu", [9, 4, 2, 0, 0]);
+ self.SubmeshCustomBlockTester("testdata/submesh/Dfsu2D_CustomBlock6_Submesh0.dfsu", [9, 4, 2, 0, 0, 0]);
+
+ oldFile = DfsuFile.Open("testdata/submesh/Dfsu2D_CustomBlock5.dfsu");
+ newFile = DfsuFile.Open("testdata/submesh/Dfsu2D_CustomBlock6_Submesh0.dfsu");
+
+ Assert.AreEqual(DfsuFileType.Dfsu2D, newFile.DfsuFileType);
+ Assert.AreEqual(oldFile.NumberOfNodes, newFile.NumberOfNodes);
+ Assert.AreEqual(oldFile.NumberOfElements, newFile.NumberOfElements);
+ Assert.AreEqual(oldFile.NumberOfFrequencies, newFile.NumberOfFrequencies);
+ Assert.AreEqual(oldFile.NumberOfDirections, newFile.NumberOfDirections);
+ assert_array_equal(oldFile.ReadItemTimeStep(1, 0).Data, newFile.ReadItemTimeStep(1, 0).Data);
+
+ oldFile.Close();
+ newFile.Close();
+
+ #/
+ #/ Reading a 3D sigma-z dfsu file with submesh type 0. It must read the
+ #/ same as the old format file with a 5 value custom block.
+ #/
+ def test_Read3DSubmesh0Test(self):
+ self.SubmeshCustomBlockTester("testdata/submesh/Dfsu3DSigmaZ_CustomBlock5.dfsu", [30, 8, 3, 3, 1]);
+ self.SubmeshCustomBlockTester("testdata/submesh/Dfsu3DSigmaZ_CustomBlock6_Submesh0.dfsu", [30, 8, 3, 3, 1, 0]);
+
+ oldFile = DfsuFile.Open("testdata/submesh/Dfsu3DSigmaZ_CustomBlock5.dfsu");
+ newFile = DfsuFile.Open("testdata/submesh/Dfsu3DSigmaZ_CustomBlock6_Submesh0.dfsu");
+
+ Assert.AreEqual(DfsuFileType.Dfsu3DSigmaZ, newFile.DfsuFileType);
+ Assert.AreEqual(oldFile.NumberOfNodes, newFile.NumberOfNodes);
+ Assert.AreEqual(oldFile.NumberOfElements, newFile.NumberOfElements);
+ Assert.AreEqual(oldFile.NumberOfLayers, newFile.NumberOfLayers);
+ Assert.AreEqual(oldFile.NumberOfSigmaLayers, newFile.NumberOfSigmaLayers);
+ Assert.AreEqual(oldFile.NumberOfFrequencies, newFile.NumberOfFrequencies);
+ Assert.AreEqual(oldFile.NumberOfDirections, newFile.NumberOfDirections);
+ assert_array_equal(oldFile.ReadItemTimeStep(1, 0).Data, newFile.ReadItemTimeStep(1, 0).Data);
+
+ oldFile.Close();
+ newFile.Close();
+
+ #/
+ #/ Reading a locally refined 2D dfsu file, submesh type 1.
+ #/
+ # TODO: This test is limited in scope to backwards compatibility: a file
+ # with submesh type 1 must open and read as an ordinary 2D mesh. It does
+ # not test full support of submesh files. The expected values come from
+ # the file's own custom block, not from an independent reference.
+ def test_Read2DSubmesh1LocallyRefinedTest(self):
+ self.SubmeshCustomBlockTester("testdata/submesh/Dfsu2D_CustomBlock6_Submesh1_LocallyRefined.dfsu", [31, 40, 2, 0, 0, 1]);
+
+ dfsFile = DfsuFile.Open("testdata/submesh/Dfsu2D_CustomBlock6_Submesh1_LocallyRefined.dfsu");
+
+ Assert.AreEqual(DfsuFileType.Dfsu2D, dfsFile.DfsuFileType);
+ Assert.AreEqual(31, dfsFile.NumberOfNodes);
+ Assert.AreEqual(40, dfsFile.NumberOfElements);
+ Assert.AreEqual(0, dfsFile.NumberOfFrequencies);
+ Assert.AreEqual(0, dfsFile.NumberOfDirections);
+ Assert.AreEqual(40, dfsFile.ReadItemTimeStep(1, 0).Data.size);
+
+ dfsFile.Close();
+
+ #/
+ #/ Reading a legacy spectral (rose-plot) 2001 dfsu file, whose 6 value
+ #/ custom block holds the number of frequencies and directions, not a
+ #/ submesh type. It must not be mistaken for a submesh file.
+ #/
+ def test_ReadLegacySpectral0DTest(self):
+ self.SubmeshCustomBlockTester("testdata/submesh/DfsuSpectral0D_CustomBlock6_Legacy.dfsu", [416, 400, 2, 0, 25, 16]);
+
+ dfsFile = DfsuFile.Open("testdata/submesh/DfsuSpectral0D_CustomBlock6_Legacy.dfsu");
+
+ Assert.AreEqual(DfsuFileType.DfsuSpectral0D, dfsFile.DfsuFileType);
+ Assert.AreEqual(416, dfsFile.NumberOfNodes);
+ Assert.AreEqual(400, dfsFile.NumberOfElements);
+ Assert.AreEqual(25, dfsFile.NumberOfFrequencies);
+ Assert.AreEqual(16, dfsFile.NumberOfDirections);
+ Assert.AreEqual(25, len(dfsFile.Frequencies));
+ Assert.AreEqual(16, len(dfsFile.Directions));
+ Assert.AreEqual(400, dfsFile.ReadItemTimeStep(1, 0).Data.size);
+
+ dfsFile.Close();
+
+ #/
+ #/ Checks the "MIKE_FM" custom block of a data type 2001 file. Opened as a
+ #/ generic dfs file, so it does not depend on how DfsuFile interprets it.
+ #/
+ def SubmeshCustomBlockTester(self, filename, expectedValues):
+ dfsFile = DfsFileFactory.DfsGenericOpen(filename);
+ fileInfo = dfsFile.FileInfo;
+
+ Assert.AreEqual(2001, fileInfo.DataType);
+ Assert.AreEqual(1, len(fileInfo.CustomBlocks));
+
+ customBlock = fileInfo.CustomBlocks[0];
+ Assert.AreEqual(DfsSimpleType.Int, customBlock.SimpleType);
+ Assert.AreEqual("MIKE_FM", customBlock.Name);
+ Assert.AreEqual(len(expectedValues), customBlock.Count);
+ for i in range(customBlock.Count):
+ Assert.AreEqual(expectedValues[i], customBlock[i]);
+
+ dfsFile.Close();
+
+ #endregion
+
def CreateDfsu3DFromSource(self, sourcefilename, filename):
source = DfsuFile.Open(sourcefilename);