sdfr 1.1.0__py3-none-win32.whl → 1.2.1__py3-none-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sdfr/SDF.py CHANGED
@@ -20,11 +20,11 @@ import numpy as np
20
20
  from enum import IntEnum
21
21
  from .loadlib import sdf_lib
22
22
 
23
- #try:
23
+ # try:
24
24
  # import xarray as xr
25
25
  #
26
26
  # got_xarray = True
27
- #except ImportError:
27
+ # except ImportError:
28
28
  # print("WARNING: xarray not installed. Generating plain numpy arrays.")
29
29
  # got_xarray = False
30
30
 
@@ -63,12 +63,14 @@ class SdfBlockType(IntEnum):
63
63
  SDF_BLOCKTYPE_DATABLOCK = 28
64
64
  SDF_BLOCKTYPE_NAMEVALUE = 29
65
65
 
66
+
66
67
  class SdfGeometry(IntEnum):
67
68
  SDF_GEOMETRY_NULL = 0
68
69
  SDF_GEOMETRY_CARTESIAN = 1
69
70
  SDF_GEOMETRY_CYLINDRICAL = 2
70
71
  SDF_GEOMETRY_SPHERICAL = 3
71
72
 
73
+
72
74
  class SdfStagger(IntEnum):
73
75
  SDF_STAGGER_CELL_CENTRE = 0
74
76
  SDF_STAGGER_FACE_X = 1
@@ -79,6 +81,7 @@ class SdfStagger(IntEnum):
79
81
  SDF_STAGGER_EDGE_Z = 6
80
82
  SDF_STAGGER_VERTEX = 7
81
83
 
84
+
82
85
  class SdfDataType(IntEnum):
83
86
  SDF_DATATYPE_NULL = 0
84
87
  SDF_DATATYPE_INTEGER4 = 1
@@ -90,22 +93,45 @@ class SdfDataType(IntEnum):
90
93
  SDF_DATATYPE_LOGICAL = 7
91
94
  SDF_DATATYPE_OTHER = 8
92
95
 
93
- _np_datatypes = [0, np.int32, np.int64, np.float32, np.float64, \
94
- np.longdouble, np.byte, np.int32, bool, 0]
95
- _ct_datatypes = [0, ct.c_int32, ct.c_int64, ct.c_float, ct.c_double, \
96
- ct.c_longdouble, ct.c_char, ct.c_bool, 0]
96
+
97
+ _np_datatypes = [
98
+ 0,
99
+ np.int32,
100
+ np.int64,
101
+ np.float32,
102
+ np.float64,
103
+ np.longdouble,
104
+ np.byte,
105
+ np.int32,
106
+ bool,
107
+ 0,
108
+ ]
109
+ _ct_datatypes = [
110
+ 0,
111
+ ct.c_int32,
112
+ ct.c_int64,
113
+ ct.c_float,
114
+ ct.c_double,
115
+ ct.c_longdouble,
116
+ ct.c_char,
117
+ ct.c_bool,
118
+ 0,
119
+ ]
97
120
 
98
121
  # Constants
99
122
  SDF_READ = 1
100
123
  SDF_WRITE = 2
101
124
  SDF_MAXDIMS = 4
102
125
 
126
+
103
127
  class SdfBlock(ct.Structure):
104
128
  pass # Forward declaration for self-referencing structure
105
129
 
130
+
106
131
  class SdfFile(ct.Structure):
107
132
  pass # Forward declaration for function pointer compatibility
108
133
 
134
+
109
135
  SdfBlock._fields_ = [
110
136
  ("extents", ct.POINTER(ct.c_double)),
111
137
  ("dim_mults", ct.POINTER(ct.c_double)),
@@ -198,7 +224,12 @@ SdfBlock._fields_ = [
198
224
  ("prev", ct.POINTER(SdfBlock)),
199
225
  ("subblock", ct.POINTER(SdfBlock)),
200
226
  ("subblock2", ct.POINTER(SdfBlock)),
201
- ("populate_data", ct.CFUNCTYPE(ct.POINTER(SdfBlock), ct.POINTER(SdfFile), ct.POINTER(SdfBlock))),
227
+ (
228
+ "populate_data",
229
+ ct.CFUNCTYPE(
230
+ ct.POINTER(SdfBlock), ct.POINTER(SdfFile), ct.POINTER(SdfBlock)
231
+ ),
232
+ ),
202
233
  ("cpu_split", ct.c_int * SDF_MAXDIMS),
203
234
  ("starts", ct.c_int * SDF_MAXDIMS),
204
235
  ("proc_min", ct.c_int * 3),
@@ -285,9 +316,10 @@ SdfFile._fields_ = [
285
316
  ("fd", ct.c_int),
286
317
  ("purge_duplicated_ids", ct.c_int),
287
318
  ("internal_ghost_cells", ct.c_int),
288
- ("ignore_nblocks", ct.c_int)
319
+ ("ignore_nblocks", ct.c_int),
289
320
  ]
290
321
 
322
+
291
323
  class RunInfo(ct.Structure):
292
324
  _fields_ = [
293
325
  ("defines", ct.c_int64),
@@ -306,6 +338,7 @@ class RunInfo(ct.Structure):
306
338
 
307
339
  class BlockList:
308
340
  """Contains all the blocks"""
341
+
309
342
  def __init__(self, filename, convert=False, derived=True):
310
343
  clib = sdf_lib
311
344
  self._clib = clib
@@ -332,7 +365,6 @@ class BlockList:
332
365
  else:
333
366
  clib.sdf_read_blocklist(h)
334
367
 
335
-
336
368
  block = h.contents.blocklist
337
369
  self.Header = get_header(h.contents)
338
370
  meshes = []
@@ -350,23 +382,33 @@ class BlockList:
350
382
  newblock = BlockArray(block)
351
383
  elif blocktype == SdfBlockType.SDF_BLOCKTYPE_CONSTANT:
352
384
  newblock = BlockConstant(block)
353
- elif blocktype == SdfBlockType.SDF_BLOCKTYPE_CONTIGUOUS \
354
- or blocktype == SdfBlockType.SDF_BLOCKTYPE_STITCHED:
385
+ elif (
386
+ blocktype == SdfBlockType.SDF_BLOCKTYPE_CONTIGUOUS
387
+ or blocktype == SdfBlockType.SDF_BLOCKTYPE_STITCHED
388
+ ):
355
389
  if block.stagger == 10 or block.stagger == 12:
356
390
  newblock = BlockStitchedPath(block)
357
391
  else:
358
392
  newblock = BlockStitched(block)
359
- elif blocktype == SdfBlockType.SDF_BLOCKTYPE_CONTIGUOUS_MATERIAL \
360
- or blocktype == SdfBlockType.SDF_BLOCKTYPE_STITCHED_MATERIAL:
393
+ elif (
394
+ blocktype == SdfBlockType.SDF_BLOCKTYPE_CONTIGUOUS_MATERIAL
395
+ or blocktype == SdfBlockType.SDF_BLOCKTYPE_STITCHED_MATERIAL
396
+ ):
361
397
  newblock = BlockStitchedMaterial(block)
362
- elif blocktype == SdfBlockType.SDF_BLOCKTYPE_CONTIGUOUS_MATVAR \
363
- or blocktype == SdfBlockType.SDF_BLOCKTYPE_STITCHED_MATVAR:
398
+ elif (
399
+ blocktype == SdfBlockType.SDF_BLOCKTYPE_CONTIGUOUS_MATVAR
400
+ or blocktype == SdfBlockType.SDF_BLOCKTYPE_STITCHED_MATVAR
401
+ ):
364
402
  newblock = BlockStitchedMatvar(block)
365
- elif blocktype == SdfBlockType.SDF_BLOCKTYPE_CONTIGUOUS_SPECIES \
366
- or blocktype == SdfBlockType.SDF_BLOCKTYPE_STITCHED_SPECIES:
403
+ elif (
404
+ blocktype == SdfBlockType.SDF_BLOCKTYPE_CONTIGUOUS_SPECIES
405
+ or blocktype == SdfBlockType.SDF_BLOCKTYPE_STITCHED_SPECIES
406
+ ):
367
407
  newblock = BlockStitchedSpecies(block)
368
- elif blocktype == SdfBlockType.SDF_BLOCKTYPE_CONTIGUOUS_TENSOR \
369
- or blocktype == SdfBlockType.SDF_BLOCKTYPE_STITCHED_TENSOR:
408
+ elif (
409
+ blocktype == SdfBlockType.SDF_BLOCKTYPE_CONTIGUOUS_TENSOR
410
+ or blocktype == SdfBlockType.SDF_BLOCKTYPE_STITCHED_TENSOR
411
+ ):
370
412
  newblock = BlockStitchedTensor(block)
371
413
  elif blocktype == SdfBlockType.SDF_BLOCKTYPE_DATABLOCK:
372
414
  newblock = BlockData(block)
@@ -375,15 +417,19 @@ class BlockList:
375
417
  meshes.append(newblock)
376
418
  elif blocktype == SdfBlockType.SDF_BLOCKTYPE_NAMEVALUE:
377
419
  newblock = BlockNameValue(block)
378
- elif blocktype == SdfBlockType.SDF_BLOCKTYPE_PLAIN_DERIVED \
379
- or blocktype == SdfBlockType.SDF_BLOCKTYPE_PLAIN_VARIABLE:
420
+ elif (
421
+ blocktype == SdfBlockType.SDF_BLOCKTYPE_PLAIN_DERIVED
422
+ or blocktype == SdfBlockType.SDF_BLOCKTYPE_PLAIN_VARIABLE
423
+ ):
380
424
  newblock = BlockPlainVariable(block)
381
425
  mesh_vars.append(newblock)
382
426
  elif blocktype == SdfBlockType.SDF_BLOCKTYPE_PLAIN_MESH:
383
427
  newblock = BlockPlainMesh(block)
384
428
  meshes.append(newblock)
385
- elif blocktype == SdfBlockType.SDF_BLOCKTYPE_POINT_DERIVED \
386
- or blocktype == SdfBlockType.SDF_BLOCKTYPE_POINT_VARIABLE:
429
+ elif (
430
+ blocktype == SdfBlockType.SDF_BLOCKTYPE_POINT_DERIVED
431
+ or blocktype == SdfBlockType.SDF_BLOCKTYPE_POINT_VARIABLE
432
+ ):
387
433
  newblock = BlockPointVariable(block)
388
434
  mesh_vars.append(newblock)
389
435
  elif blocktype == SdfBlockType.SDF_BLOCKTYPE_POINT_MESH:
@@ -397,8 +443,8 @@ class BlockList:
397
443
  pass
398
444
  if newblock is not None:
399
445
  self.__dict__[name] = newblock
400
- self._block_ids.update({block.id.decode() : newblock})
401
- self._block_names.update({block.name.decode() : newblock})
446
+ self._block_ids.update({block.id.decode(): newblock})
447
+ self._block_names.update({block.name.decode(): newblock})
402
448
  block = block.next
403
449
 
404
450
  for var in mesh_vars:
@@ -430,13 +476,14 @@ class Block:
430
476
  Contains the data and metadata for a single
431
477
  block from an SDF file.
432
478
  """
479
+
433
480
  def __init__(self, block):
434
481
  self._handle = block._handle
435
482
  self._id = block.id.decode()
436
483
  self._name = block.name.decode()
437
484
  self._datatype = _np_datatypes[block.datatype_out]
438
485
  self._data_length = block.data_length
439
- self._dims = tuple(block.dims[:block.ndims])
486
+ self._dims = tuple(block.dims[: block.ndims])
440
487
  self._contents = block
441
488
  self._owndata = True
442
489
  self._blocklist = block._blocklist
@@ -452,7 +499,7 @@ class Block:
452
499
  buffer_from_memory.restype = ct.py_object
453
500
  dtype = self._datatype
454
501
  if dtype == np.byte:
455
- dtype = np.dtype('|S1')
502
+ dtype = np.dtype("|S1")
456
503
  totype = _ct_datatypes[self._contents.datatype]
457
504
  cast = ct.cast(data, ct.POINTER(totype))
458
505
  buf = buffer_from_memory(cast, blen)
@@ -495,12 +542,12 @@ class Block:
495
542
  return self._name
496
543
 
497
544
 
498
-
499
545
  class BlockConstant(Block):
500
546
  """Constant block"""
547
+
501
548
  def __init__(self, block):
502
549
  super().__init__(block)
503
- offset = getattr(SdfBlock, 'const_value').offset
550
+ offset = getattr(SdfBlock, "const_value").offset
504
551
  self._datatype = _np_datatypes[block.datatype]
505
552
  totype = _ct_datatypes[block.datatype]
506
553
  self._data = totype.from_buffer(block, offset).value
@@ -508,6 +555,7 @@ class BlockConstant(Block):
508
555
 
509
556
  class BlockPlainVariable(Block):
510
557
  """Plain variable block"""
558
+
511
559
  @property
512
560
  def data(self):
513
561
  """Block data contents"""
@@ -518,7 +566,7 @@ class BlockPlainVariable(Block):
518
566
  for d in self.dims:
519
567
  blen *= d
520
568
  array = self._numpy_from_buffer(self._contents.data, blen)
521
- self._data = array.reshape(self.dims, order='F')
569
+ self._data = array.reshape(self.dims, order="F")
522
570
  return self._data
523
571
 
524
572
  @property
@@ -549,16 +597,21 @@ class BlockPlainVariable(Block):
549
597
 
550
598
  class BlockPlainMesh(Block):
551
599
  """Plain mesh block"""
600
+
552
601
  def __init__(self, block):
553
602
  super().__init__(block)
554
603
  self._data = None
555
- self._units = tuple([block.dim_units[i].decode() for i in range(block.ndims)])
556
- self._labels = tuple([block.dim_labels[i].decode() for i in range(block.ndims)])
604
+ self._units = tuple(
605
+ [block.dim_units[i].decode() for i in range(block.ndims)]
606
+ )
607
+ self._labels = tuple(
608
+ [block.dim_labels[i].decode() for i in range(block.ndims)]
609
+ )
557
610
  self._mult = None
558
611
  if bool(block.dim_mults):
559
- self._mult = tuple(block.dim_mults[:block.ndims])
612
+ self._mult = tuple(block.dim_mults[: block.ndims])
560
613
  if bool(block.extents):
561
- self._extents = tuple(block.extents[:2*block.ndims])
614
+ self._extents = tuple(block.extents[: 2 * block.ndims])
562
615
 
563
616
  @property
564
617
  def data(self):
@@ -602,6 +655,7 @@ class BlockPlainMesh(Block):
602
655
 
603
656
  class BlockLagrangianMesh(BlockPlainMesh):
604
657
  """Lagrangian mesh block"""
658
+
605
659
  @property
606
660
  def data(self):
607
661
  """Block data contents"""
@@ -614,7 +668,7 @@ class BlockLagrangianMesh(BlockPlainMesh):
614
668
  grids = []
615
669
  for i, d in enumerate(self.dims):
616
670
  array = self._numpy_from_buffer(self._contents.grids[i], blen)
617
- array = array.reshape(self.dims, order='F')
671
+ array = array.reshape(self.dims, order="F")
618
672
  grids.append(array)
619
673
  self._data = tuple(grids)
620
674
  return self._data
@@ -622,6 +676,7 @@ class BlockLagrangianMesh(BlockPlainMesh):
622
676
 
623
677
  class BlockPointMesh(BlockPlainMesh):
624
678
  """Point mesh block"""
679
+
625
680
  @property
626
681
  def species_id(self):
627
682
  """Species ID"""
@@ -630,6 +685,7 @@ class BlockPointMesh(BlockPlainMesh):
630
685
 
631
686
  class BlockPointVariable(BlockPlainVariable):
632
687
  """Point variable block"""
688
+
633
689
  @property
634
690
  def species_id(self):
635
691
  """Species ID"""
@@ -638,6 +694,7 @@ class BlockPointVariable(BlockPlainVariable):
638
694
 
639
695
  class BlockNameValue(Block):
640
696
  """Name/value block"""
697
+
641
698
  def __init__(self, block):
642
699
  super().__init__(block)
643
700
  self._dims = (block.ndims,)
@@ -658,6 +715,7 @@ class BlockNameValue(Block):
658
715
 
659
716
  class BlockArray(Block):
660
717
  """Array block"""
718
+
661
719
  @property
662
720
  def data(self):
663
721
  """Block data contents"""
@@ -668,12 +726,13 @@ class BlockArray(Block):
668
726
  for d in self.dims:
669
727
  blen *= d
670
728
  array = self._numpy_from_buffer(self._contents.data, blen)
671
- self._data = array.reshape(self.dims, order='F')
729
+ self._data = array.reshape(self.dims, order="F")
672
730
  return self._data
673
731
 
674
732
 
675
733
  class BlockData(Block):
676
734
  """Data block"""
735
+
677
736
  def __init__(self, block):
678
737
  super().__init__(block)
679
738
  self._checksum = block.checksum.decode()
@@ -709,6 +768,7 @@ class BlockData(Block):
709
768
 
710
769
  class BlockStitched(Block):
711
770
  """Stitched block"""
771
+
712
772
  @property
713
773
  def data(self):
714
774
  """Block data contents"""
@@ -724,73 +784,91 @@ class BlockStitched(Block):
724
784
 
725
785
  class BlockStitchedPath(BlockStitched):
726
786
  """Stitched path block"""
787
+
727
788
  pass
728
789
 
729
790
 
730
791
  class BlockStitchedMaterial(BlockStitched):
731
792
  """Stitched material block"""
793
+
732
794
  pass
733
795
 
734
796
 
735
797
  class BlockStitchedMatvar(BlockStitched):
736
798
  """Stitched material variable block"""
799
+
737
800
  pass
738
801
 
739
802
 
740
803
  class BlockStitchedSpecies(BlockStitched):
741
804
  """Stitched species block"""
805
+
742
806
  pass
743
807
 
744
808
 
745
809
  class BlockStitchedTensor(BlockStitched):
746
810
  """Stitched tensor block"""
811
+
747
812
  pass
748
813
 
749
814
 
750
815
  def get_header(h):
751
816
  d = {}
752
- d['filename'] = h.filename.decode()
753
- d['file_version'] = h.file_version
754
- d['file_revision'] = h.file_revision
755
- d['code_name'] = h.code_name.decode()
756
- d['step'] = h.step
757
- d['time'] = h.time
758
- d['jobid1'] = h.jobid1
759
- d['jobid2'] = h.jobid2
760
- d['code_io_version'] = h.code_io_version
761
- d['restart_flag'] = h.restart_flag
762
- d['other_domains'] = h.other_domains
763
- d['station_file'] = h.station_file
817
+ d["filename"] = h.filename.decode()
818
+ d["file_version"] = h.file_version
819
+ d["file_revision"] = h.file_revision
820
+ d["code_name"] = h.code_name.decode()
821
+ d["step"] = h.step
822
+ d["time"] = h.time
823
+ d["jobid1"] = h.jobid1
824
+ d["jobid2"] = h.jobid2
825
+ d["code_io_version"] = h.code_io_version
826
+ d["restart_flag"] = h.restart_flag
827
+ d["other_domains"] = h.other_domains
828
+ d["station_file"] = h.station_file
764
829
  return d
765
830
 
766
831
 
767
832
  def get_run_info(block):
768
833
  from datetime import datetime
834
+
769
835
  h = ct.cast(block.data, ct.POINTER(RunInfo)).contents
770
836
  d = {}
771
- d['version'] = f"{h.version}.{h.revision}.{h.minor_rev}"
772
- d['commit_id'] = h.commit_id.decode()
773
- d['sha1sum'] = h.sha1sum.decode()
774
- d['compile_machine'] = h.compile_machine.decode()
775
- d['compile_flags'] = h.compile_flags.decode()
776
- d['compile_date'] = datetime.utcfromtimestamp(h.compile_date).strftime('%c')
777
- d['run_date'] = datetime.utcfromtimestamp(h.run_date).strftime('%c')
778
- d['io_data'] = datetime.utcfromtimestamp(h.io_date).strftime('%c')
837
+ d["version"] = f"{h.version}.{h.revision}.{h.minor_rev}"
838
+ d["commit_id"] = h.commit_id.decode()
839
+ d["sha1sum"] = h.sha1sum.decode()
840
+ d["compile_machine"] = h.compile_machine.decode()
841
+ d["compile_flags"] = h.compile_flags.decode()
842
+ d["compile_date"] = datetime.utcfromtimestamp(h.compile_date).strftime("%c")
843
+ d["run_date"] = datetime.utcfromtimestamp(h.run_date).strftime("%c")
844
+ d["io_data"] = datetime.utcfromtimestamp(h.io_date).strftime("%c")
779
845
  return d
780
846
 
781
847
 
782
848
  def get_member_name(name):
783
849
  sname = name.decode()
784
- return ''.join([i if ((i >= "a" and i <= "z") or (i >= "A" and i <= "Z") \
785
- or (i >= "0" and i <= "9")) else "_" \
786
- for i in sname])
787
-
788
- def read(filename, convert=False, mmap=0, dict=False, derived=True):
850
+ return "".join(
851
+ [
852
+ (
853
+ i
854
+ if (
855
+ (i >= "a" and i <= "z")
856
+ or (i >= "A" and i <= "Z")
857
+ or (i >= "0" and i <= "9")
858
+ )
859
+ else "_"
860
+ )
861
+ for i in sname
862
+ ]
863
+ )
864
+
865
+
866
+ def read(file=None, convert=False, mmap=0, dict=False, derived=True):
789
867
  """Reads the SDF data and returns a dictionary of NumPy arrays.
790
868
 
791
869
  Parameters
792
870
  ----------
793
- filename : string
871
+ file : string
794
872
  The name of the SDF file to open.
795
873
  convert : bool, optional
796
874
  Convert double precision data to single when reading file.
@@ -802,10 +880,13 @@ def read(filename, convert=False, mmap=0, dict=False, derived=True):
802
880
 
803
881
  import warnings
804
882
 
883
+ if file == None:
884
+ raise TypeError("Missing file parameter")
885
+
805
886
  if mmap != 0:
806
887
  warnings.warn("mmap flag ignored")
807
888
 
808
- blocklist = BlockList(filename, convert, derived)
889
+ blocklist = BlockList(file, convert, derived)
809
890
 
810
891
  if isinstance(dict, str):
811
892
  if dict == "id" or dict == "ids":
sdfr/_commit_info.py CHANGED
@@ -1,2 +1,2 @@
1
- __commit_date__ = "Sun Feb 23 22:03:55 2025 +0000"
2
- __commit_id__ = "ce8f3ff465dd3584add21962673c73732205e637"
1
+ __commit_date__ = "Thu May 1 13:59:33 2025 +0100"
2
+ __commit_id__ = "6700eab92d2526d52b012861672b0b9e6ff57975"