Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
They were `eumIGeographicalCoordinate` in metres whatever the projection.
Files with a projected coordinate system are unchanged
([#47](https://github.com/DHI/mikecore-python/issues/47)).
- Reading an item's value type raises the DFS error when the native library
reports one, instead of returning `DataValueType.Instantaneous`.

[Unreleased]: https://github.com/DHI/mikecore-python/compare/v0.3.a1...HEAD
8 changes: 4 additions & 4 deletions buildUtil/eumXMLProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
eumLines = eumFile.readlines()
eumFile.close()

recItem = re.compile('"(eumI\w+)" MzId="(\w*)"')
recUnit = re.compile('"(eumU\w+)" MzId="(\w*)"')
recItem = re.compile(r'"(eumI\w+)" MzId="(\w*)"')
recUnit = re.compile(r'"(eumU\w+)" MzId="(\w*)"')

itemDict = {}
unitDict = {}
Expand All @@ -38,12 +38,12 @@
print("# Predefined enums of EUM item types.")
print("#")
print("# Must be updated with every new release, or if the EUM.xml is updated")
print("# Run buildUtil\eumXMLProcess.py to create the lists")
print(r"# Run buildUtil\eumXMLProcess.py to create the lists")
print("class eumItem(IntEnum):")
for key in itemKeys:
print(f" {itemDict[key]} = {key}")

print("")
print()
print("# Predefined enums of EUM units.")
print("#")
print("# Must be updated with every new release, or if the EUM.xml is updated")
Expand Down
8 changes: 4 additions & 4 deletions mikecore/DfsBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,11 @@ def CreateFile(self, filename):
fnp.value, headerPointer, ctypes.byref(filePointer)
)

except Exception as e:
except Exception:
# In case of any exception, destroy the header.
if headerPointer.value is not None:
DfsDLL.Wrapper.dfsHeaderDestroy(ctypes.byref(headerPointer))
raise e
raise

# The create function will free ressources, if something fails.
dfsFile = DfsFile()
Expand Down Expand Up @@ -468,12 +468,12 @@ def AddStaticItem(self, staticItem):
DfsBuilder.__SetValuesToItem(dfsFile.headPointer, itemPointer, staticItem)

# From now on the responsibility of the staticVectorPointer is taken over bye the DfsStaticItem (ending the try-catch)
except Exception as e:
except Exception:
# As long as the static vector pointer is not null, the
# responsibility for destroying the header structure is here.
if staticVectorPointer.value is not None:
DfsDLL.Wrapper.dfsStaticDestroy(ctypes.byref(staticVectorPointer))
raise e
raise

# Create a DfsStaticItem that belongs to the current dfs file
myStaticItem = DfsStaticItem(dfsFile, staticVectorPointer, itemPointer)
Expand Down
46 changes: 24 additions & 22 deletions mikecore/DfsFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1167,16 +1167,15 @@ def WriteItemTimeStep(self, itemNumber, timestepIndex, time, data):

# More elaborate action is required, when appending to file. If appending then
# (itemNumber == _fpItemNumber && timestepIndex == _fileInfo.TimeAxis.NumberOfTimeSteps == _fpTimeStepIndex)
if timestepIndex == self.FileInfo.TimeAxis.NumberOfTimeSteps:
if (
timestepIndex == self.fpTimeStepIndex
and itemNumber != self.fpItemNumber
or timestepIndex != self.fpTimeStepIndex
and itemNumber != 1
):
raise IndexError(
"Wrong item number while trying to append item data to file. Item data must be appended in order, and for all items in the time step"
)
if timestepIndex == self.FileInfo.TimeAxis.NumberOfTimeSteps and (
timestepIndex == self.fpTimeStepIndex
and itemNumber != self.fpItemNumber
or timestepIndex != self.fpTimeStepIndex
and itemNumber != 1
):
raise IndexError(
"Wrong item number while trying to append item data to file. Item data must be appended in order, and for all items in the time step"
)

# Position file pointer
self.__FpFindItemTimeStep(itemNumber, timestepIndex)
Expand Down Expand Up @@ -1249,11 +1248,13 @@ def WriteItemTimeStepNext(self, time, data):
self.headPointer, self.filePointer, ctypes.c_double(time), data.ctypes.data
)

if self.__FpDynamicIncrement():
if self.fpTimeStepIndex > self.FileInfo.TimeAxis.NumberOfTimeSteps:
# One entire time step (all items) has just been written to file,
# increment the number of time steps
self.FileInfo.TimeAxis.IncrementNumberOfTimeSteps(time)
if (
self.__FpDynamicIncrement()
and self.fpTimeStepIndex > self.FileInfo.TimeAxis.NumberOfTimeSteps
):
# One entire time step (all items) has just been written to file,
# increment the number of time steps
self.FileInfo.TimeAxis.IncrementNumberOfTimeSteps(time)

def Reset(self):
"""
Expand Down Expand Up @@ -1365,12 +1366,12 @@ def ReadDfs0DataDouble(self, itemsToLoad=None):

# Need to handle StartTimeOffset similar to in __GetTime
timeaxis = self.FileInfo.TimeAxis
if timeaxis.TimeAxisType == TimeAxisType.TimeEquidistant:
if timeaxis.StartTimeOffset != 0.0:
data[:, 0] = data[:, 0] + timeaxis.StartTimeOffset
elif timeaxis.TimeAxisType == TimeAxisType.CalendarEquidistant:
if timeaxis.StartTimeOffset != 0.0:
data[:, 0] = data[:, 0] + timeaxis.StartTimeOffset
if (
timeaxis.TimeAxisType
in (TimeAxisType.TimeEquidistant, TimeAxisType.CalendarEquidistant)
and timeaxis.StartTimeOffset != 0.0
):
data[:, 0] = data[:, 0] + timeaxis.StartTimeOffset

return data

Expand Down Expand Up @@ -2112,7 +2113,8 @@ def ToDfsDateStrings(datetime):
@staticmethod
def dfsGetItemValueType(itemPointer):
valueTypeInt = ctypes.c_int()
err = DfsDLL.Wrapper.dfsGetItemValueType(
rok = DfsDLL.Wrapper.dfsGetItemValueType(
itemPointer, ctypes.byref(valueTypeInt)
)
DfsDLL.CheckReturnCode(rok)
return DataValueType(valueTypeInt.value)
12 changes: 3 additions & 9 deletions mikecore/DfsuBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,19 +730,13 @@ def CreateDfsu(self, dfsBuilder, elementType, nodesPerElmt, connectivityArray):
)

# Element type
elmtTypeItem = dfsBuilder.AddCreateStaticItem(
"Element type", intCode, elementType
)
dfsBuilder.AddCreateStaticItem("Element type", intCode, elementType)

# No of nodes (per element)
nodesPerElmtItem = dfsBuilder.AddCreateStaticItem(
"No of nodes", intCode, nodesPerElmt
)
dfsBuilder.AddCreateStaticItem("No of nodes", intCode, nodesPerElmt)

# Connectivity
connectivityItem = dfsBuilder.AddCreateStaticItem(
"Connectivity", intCode, connectivityArray
)
dfsBuilder.AddCreateStaticItem("Connectivity", intCode, connectivityArray)

# Spectral
frequencyItem = (
Expand Down
18 changes: 7 additions & 11 deletions mikecore/DfsuFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,10 @@ def __Init(self, dfsFile, build=False):
self.NumberOfElements = self.ElementIds.size
# self.NumberOfNodes = self.X.size

if not build:
# In append mode, move the file pointer to end of file
# It was moved elsewhere when reading static data...
if self.dfsFile.FileMode == DfsFileMode.Append:
self.dfsFile.FindTimeStep(self.NumberOfTimeSteps)
# In append mode, move the file pointer to end of file
# It was moved elsewhere when reading static data...
if not build and self.dfsFile.FileMode == DfsFileMode.Append:
self.dfsFile.FindTimeStep(self.NumberOfTimeSteps)

def Dispose(self):
"""
Expand Down Expand Up @@ -655,8 +654,7 @@ def FindTopLayerElementsXY(elementTable, x, y):
dx = x2 - x1
dy = y2 - y1
nodeDist2 = dx * dx + dy * dy
if nodeDist2 > maxNodeDist2:
maxNodeDist2 = nodeDist2
maxNodeDist2 = max(maxNodeDist2, nodeDist2)

# Check if element center coordinates differ more than a tolerance
# times the reference lenght - the maximum node distance.
Expand All @@ -681,8 +679,7 @@ def FindMaxNumberOfLayers(topLayerElements):
maxLayers = topLayerElements[0] + 1
for i in range(1, topLayerElements.size):
layers = topLayerElements[i] - topLayerElements[i - 1]
if layers > maxLayers:
maxLayers = layers
maxLayers = max(maxLayers, layers)
return maxLayers

@staticmethod
Expand All @@ -697,6 +694,5 @@ def FindMinNumberOfLayers(topLayerElements):
minLayers = topLayerElements[0] + 1
for i in range(1, topLayerElements.size):
layers = topLayerElements[i] - topLayerElements[i - 1]
if layers < minLayers:
minLayers = layers
minLayers = min(minLayers, layers)
return minLayers
25 changes: 10 additions & 15 deletions mikecore/Projections.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,9 +1109,8 @@ def Create(projectionString: str, validateProjectionString: bool = True):
raise Exception(
"Projection string cannot be null or empty", "projectionString"
)
if validateProjectionString:
if not MapProjection.IsValid(projectionString):
raise Exception("Not a valid projection string", "projectionString")
if validateProjectionString and not MapProjection.IsValid(projectionString):
raise Exception("Not a valid projection string", "projectionString")
return MapProjection(projectionString, mustFree=True)

# / <summary>
Expand All @@ -1137,9 +1136,8 @@ def CreatePointer(
# / </summary>
def __del__(self):
# Release ressources on the unmanaged side when garbage collected
if self._mustFree:
if self._mzMapProjPointer.value is not None:
MzCartDLL.MzMapProjDestroy(self._mzMapProjPointer)
if self._mustFree and self._mzMapProjPointer.value is not None:
MzCartDLL.MzMapProjDestroy(self._mzMapProjPointer)
_mzMapProjPointer = ctypes.c_void_p()

##/ <summary>
Expand Down Expand Up @@ -1523,9 +1521,8 @@ def __init__(

self._mzCartPointer = ctypes.c_void_p(0)

if validateProjectionString:
if not MapProjection.IsValid(projectionString):
raise Exception("Not a valid projection string", "projectionString")
if validateProjectionString and not MapProjection.IsValid(projectionString):
raise Exception("Not a valid projection string", "projectionString")

if lonOrigin is None:
lonOrigin, latOrigin = MapProjection.ProjectionOrigin(
Expand Down Expand Up @@ -1697,9 +1694,8 @@ def CreateGeoOrigin(
):
if projectionString is None or projectionString == "":
raise ValueError("Projection string can not be null or empty")
if validateProjectionString:
if not MapProjection.IsValid(projectionString):
raise ValueError("Not a valid projection string")
if validateProjectionString and not MapProjection.IsValid(projectionString):
raise ValueError("Not a valid projection string")
cart = Cartography(
projectionString,
lonOrigin,
Expand Down Expand Up @@ -1733,9 +1729,8 @@ def CreateProjOrigin(
):
if projectionString is None or projectionString == "":
raise ValueError("Projection string cannot be null or empty")
if validateProjectionString:
if not MapProjection.IsValid(projectionString):
raise ValueError("Not a valid projection string")
if validateProjectionString and not MapProjection.IsValid(projectionString):
raise ValueError("Not a valid projection string")
proj = MapProjection.Create(projectionString, validateProjectionString)
lonOrigin, latOrigin = proj.Proj2Geo(east, north)
orientationGeo = proj.Proj2GeoRotation(east, north, orientationProj)
Expand Down
6 changes: 3 additions & 3 deletions mikecore/eum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,7 @@ def Init(libfilepath=None, libfilename=None):
# TODO: Should this not be simpler?
eumFilePath = path + "/EUM.xml"
eumFilePathP = ctypes.c_char_p(eumFilePath.encode("ascii"))
res = lib.eumSetupLoadLinux(eumFilePathP)
lib.eumSetupLoadLinux(eumFilePathP)

lib.eumUnitGetParameters.argtypes = [
ctypes.c_int32,
Expand Down Expand Up @@ -1536,7 +1536,7 @@ def DecodeString(
# / <returns>A hashtable with item type text description, numeric key pairs.</returns>
@staticmethod
def CreateItemHashtable() -> dict:
itemHash = dict()
itemHash = {}
n = eumWrapper.eumGetItemTypeCount()
for i in range(1, n + 1):
ok, key, desc = eumWrapper.eumGetItemTypeSeq(i)
Expand All @@ -1554,7 +1554,7 @@ def CreateItemHashtable() -> dict:
# / <returns></returns>
@staticmethod
def CreateUnitHashTable(abbreviations: bool = False):
unitHash = dict() # current number of unique desc/abbrs.
unitHash = {} # current number of unique desc/abbrs.
prevKey = eumUnit.eumUUnitUndefined
ok, unitKey, desc = eumWrapper.eumGetNextUnit(prevKey)
while ok:
Expand Down
25 changes: 12 additions & 13 deletions miketools/Dfs0ToAscii.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,24 @@ def Dfs0ToAscii(dfs0FileName, txtFileName):
txt.write("\n")

txt.write(" Time")
for item in dfs.ItemInfo:
txt.write(f" {item.Name}")
txt.writelines(f" {item.Name}" for item in dfs.ItemInfo)
txt.write("\n")

txt.write(" Item")
for item in dfs.ItemInfo:
txt.write(
f" {item.Quantity.Item.value:11} {item.Quantity.Unit.value:11} {item.ValueType.name:11}"
)
txt.writelines(
f" {item.Quantity.Item.value:11} {item.Quantity.Unit.value:11} {item.ValueType.name:11}"
for item in dfs.ItemInfo
)
txt.write("\n")
txt.write(" Item")
for item in dfs.ItemInfo:
txt.write(
" {:11} {:11} {:11}".format(
'"' + item.Quantity.ItemDescription + '"',
'"' + item.Quantity.UnitDescription + '"',
item.ValueType.name,
)
txt.writelines(
" {:11} {:11} {:11}".format(
'"' + item.Quantity.ItemDescription + '"',
'"' + item.Quantity.UnitDescription + '"',
item.ValueType.name,
)
for item in dfs.ItemInfo
)
txt.write("\n")

# Only set for calendar time axes; otherwise times are written as offsets.
Expand Down
11 changes: 4 additions & 7 deletions miketools/DfsShowInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def DfsShowInfo(dfsFileName, showAxis=False):
dfs.FileInfo.TimeAxis.TimeAxisType == TimeAxisType.CalendarEquidistant
or dfs.FileInfo.TimeAxis.TimeAxisType == TimeAxisType.CalendarNonEquidistant
):
isCalendarTime = True
startDateTime = dfs.FileInfo.TimeAxis.StartDateTime
txt.write(
f"Time : {dfs.FileInfo.TimeAxis.TimeAxisType}, {dfs.FileInfo.TimeAxis.NumberOfTimeSteps}, {startDateTime}\n"
Expand All @@ -47,11 +46,10 @@ def DfsShowInfo(dfsFileName, showAxis=False):
txt.write(
f"item {item.ItemNumber!s:>2}: {item.Name!s:<40}: {item.ElementCount!s:>5}: {item.DataType.name!s:>6} {item.SpatialAxis.AxisType.name!s:>6} ({item.Quantity.ItemDescription!s:>11}: {item.Quantity.UnitDescription!s:>11}) \n"
)
if showAxis:
if item.SpatialAxis.AxisType is SpaceAxisType.EqD1:
txt.write(
f" axis : dx = {item.SpatialAxis.Dx}, xCount = {item.SpatialAxis.XCount}, x0 = {item.SpatialAxis.X0} ({item.SpatialAxis.AxisUnit.name})\n"
)
if showAxis and item.SpatialAxis.AxisType is SpaceAxisType.EqD1:
txt.write(
f" axis : dx = {item.SpatialAxis.Dx}, xCount = {item.SpatialAxis.XCount}, x0 = {item.SpatialAxis.X0} ({item.SpatialAxis.AxisUnit.name})\n"
)

dfs.Close()

Expand All @@ -74,7 +72,6 @@ def DfsuShowInfo(dfsFileName, showMesh=False):
dfs.FileInfo.TimeAxis.TimeAxisType == TimeAxisType.CalendarEquidistant
or dfs.FileInfo.TimeAxis.TimeAxisType == TimeAxisType.CalendarNonEquidistant
):
isCalendarTime = True
startDateTime = dfs.FileInfo.TimeAxis.StartDateTime
txt.write(
f"Time : {dfs.FileInfo.TimeAxis.TimeAxisType}, {dfs.FileInfo.TimeAxis.NumberOfTimeSteps}, {startDateTime}\n"
Expand Down
Loading
Loading