@@ -66,6 +66,10 @@ def __init__(self, dfsuFileType):
6666 # Dynamic item information
6767 self .__dynamicItemData = []
6868
69+ # spectral
70+ self .__frequencies = None
71+ self .__directions = None
72+
6973 self .__dfsuFileType = dfsuFileType
7074 if dfsuFileType == DfsuFileType .Dfsu2D :
7175 self .FileTitle = "Area Series"
@@ -113,6 +117,13 @@ def SetNumberOfSigmaLayers(self, numberOfSigmaLayers: int):
113117 else :
114118 raise Exception ("dfsuFileType" )
115119
120+ def SetFrequencies (self , frequencies ):
121+ self .__frequencies = frequencies
122+
123+ def SetDirections (self , directions ):
124+ self .__directions = directions
125+
126+
116127 #/ <summary>
117128 #/ Set a non-standard temporal axis for the dfsu file. WARNING: The dfsu file will not be valid in all contexts.
118129 #/ <para>
@@ -278,7 +289,7 @@ def Validate(self, dieOnError: bool = False):
278289 errors .append ("Nodes have not been set" )
279290 if (not self .__isSetConnectivity ):
280291 errors .append ("Elements have not been set" )
281- if (not self .__isSetNumberOfSigmaLayers and self .__dfsuFileType != DfsuFileType .Dfsu2D ):
292+ if (not self .__isSetNumberOfSigmaLayers and self .__dfsuFileType in ( DfsuFileType .Dfsu3DSigma , DfsuFileType . Dfsu3DSigmaZ , DfsuFileType . DfsuVerticalColumn , DfsuFileType . DfsuVerticalProfileSigma , DfsuFileType . DfsuVerticalProfileSigmaZ ) ):
282293 errors .append ("Number of sigma layers has not been set" )
283294
284295 # Check that all nodenumbers are within the range of
@@ -326,7 +337,11 @@ def Validate(self, dieOnError: bool = False):
326337 minNumberOfLayers = DfsuUtil .FindMinNumberOfLayers (topLayerElements )
327338 if (minNumberOfLayers < self .__numberOfSigmaLayers ):
328339 errors .append ("The minimum number of layers is smaller than the number of sigma layers. Element table is invalid" )
340+ elif self .__dfsuFileType in (DfsuFileType .DfsuSpectral0D , DfsuFileType .DfsuSpectral1D , DfsuFileType .DfsuSpectral2D ):
341+ # TODO do we need to check frequency or directions?
342+ pass
329343 else :
344+
330345 raise Exception ("Dfsu file type {} not supported" .format (self .__dfsuFileType ))
331346
332347
@@ -384,7 +399,15 @@ def SetupBuilder(self):
384399 factory = DfsFactory ()
385400 dfsBuilder = DfsBuilder .Create (self .FileTitle , self .ApplicationTitle , self .ApplicationVersion )
386401
387- dfsBuilder .SetDataType (2001 )
402+ if self .__dfsuFileType == DfsuFileType .DfsuSpectral1D :
403+ dfsBuilder .SetDataType (2002 )
404+ elif self .__dfsuFileType == DfsuFileType .DfsuSpectral2D :
405+ dfsBuilder .SetDataType (2003 )
406+ else :
407+ dfsBuilder .SetDataType (2001 )
408+
409+
410+
388411 dfsBuilder .SetGeographicalProjection (self .__dfsProjection )
389412 if (self .__timeAxis != None ):
390413 dfsBuilder .SetTemporalAxis (self .__timeAxis )
@@ -410,6 +433,10 @@ def SetupBuilder(self):
410433 elif self .__dfsuFileType == DfsuFileType .Dfsu3DSigmaZ :
411434 maxNumberOfLayers = DfsuUtil .FindMaxNumberOfLayers (DfsuUtil .FindTopLayerElements (self .__connectivity ))
412435 dfsBuilder .AddCreateCustomBlock ("MIKE_FM" ,np .array ([ self .__x .size , len (self .__connectivity ), 3 , maxNumberOfLayers , self .__numberOfSigmaLayers ], np .int32 ))
436+ elif self .__dfsuFileType == DfsuFileType .DfsuSpectral1D :
437+ dfsBuilder .AddCreateCustomBlock ("MIKE_FM" ,np .array ([ self .__x .size , len (self .__connectivity ), 1 , 0 , len (self .__frequencies ), len (self .__directions )], np .int32 ))
438+ elif self .__dfsuFileType == DfsuFileType .DfsuSpectral2D :
439+ dfsBuilder .AddCreateCustomBlock ("MIKE_FM" ,np .array ([ self .__x .size , len (self .__connectivity ), 2 , 0 , len (self .__frequencies ), len (self .__directions )], np .int32 ))
413440 else :
414441 raise Exception ()
415442
@@ -446,7 +473,21 @@ def SetupBuilder(self):
446473 # dfsItem.SetAxis(factory.CreateAxisDummy(len(self.__connectivity)))
447474 #else
448475 # Set axis to have meter unit (not necessary, just to make file exactly equal)
449- dfsItem .SetAxis (factory .CreateAxisEqD1 (eumUnit .eumUmeter , len (self .__connectivity ), 0 , 1 ))
476+
477+ if self .__dfsuFileType in (DfsuFileType .DfsuSpectral1D , DfsuFileType .DfsuSpectral2D ):
478+ if self .__dfsuFileType == DfsuFileType .DfsuSpectral1D :
479+ size = self .__x .size
480+ if self .__dfsuFileType == DfsuFileType .DfsuSpectral2D :
481+ size = len (self .__connectivity )
482+
483+ if self .__frequencies is not None :
484+ size *= len (self .__frequencies )
485+ if self .__directions is not None :
486+ size *= len (self .__directions )
487+
488+ dfsItem .SetAxis (factory .CreateAxisEqD1 (eumUnit .eumUmeter , size , 0 , 1 ))
489+ else :
490+ dfsItem .SetAxis (factory .CreateAxisEqD1 (eumUnit .eumUmeter , len (self .__connectivity ), 0 , 1 ))
450491 # Set to default ufs delete values (not used anyway, just to make file exactly equal)
451492 dfsItem .SetReferenceCoordinates (- 1e-35 , - 1e-35 , - 1e-35 )
452493 dfsItem .SetOrientation (- 1e-35 , - 1e-35 , - 1e-35 )
@@ -479,6 +520,7 @@ def CreateDfsu(self, dfsBuilder, elementType, nodesPerElmt, connectivityArray):
479520
480521 intCode = eumQuantity (eumItem .eumIIntegerCode , eumUnit .eumUintCode )
481522 xyQuantity = eumQuantity (eumItem .eumIGeographicalCoordinate , eumUnit .eumUmeter )
523+
482524 # TODO: reenable:
483525 #if (MapProjection.IsValid(self.__dfsProjection.WKTString)):
484526 # if (MapProjection.IsGeographical(self.__dfsProjection.WKTString)):
@@ -511,6 +553,12 @@ def CreateDfsu(self, dfsBuilder, elementType, nodesPerElmt, connectivityArray):
511553 # Connectivity
512554 connectivityItem = dfsBuilder .AddCreateStaticItem ("Connectivity" , intCode , connectivityArray )
513555
556+ # Spectral
557+ frequencyItem = dfsBuilder .AddCreateStaticItem ("Frequency" , eumQuantity (eumItem .eumIFrequency , eumUnit .eumUhertz ), self .__frequencies ) if self .__frequencies is not None else None
558+
559+ # TODO unit
560+ directionItem = dfsBuilder .AddCreateStaticItem ("Direction" , eumQuantity (eumItem .eumIDirection , eumUnit .eumUradian ), self .__directions ) if self .__directions is not None else None
561+
514562 dfsFile = dfsBuilder .GetFile ()
515563
516564 dfsuFile = DfsuFile ()
@@ -530,7 +578,9 @@ def CreateDfsu(self, dfsBuilder, elementType, nodesPerElmt, connectivityArray):
530578 self .__elementIds ,
531579 elementType ,
532580 self .__connectivity ,
533- self .__zUnit
581+ self .__zUnit ,
582+ frequencyItem ,
583+ directionItem
534584 )
535585
536586 return (dfsuFile )
0 commit comments