202202 :members:
203203
204204"""
205+
205206import numpy
206207import re
207208import gzip
@@ -229,12 +230,12 @@ def _gzip_open(filename, mode="rt"):
229230class DXclass (object ):
230231 """'class' object as defined by OpenDX"""
231232
232- def __init__ (self , classid ):
233+ def __init__ (self , classid , name = None , component = None ):
233234 """id is the object number"""
234235 self .id = classid # serial number of the object
235- self .name = None # name of the DXclass
236- self .component = None # component type
237- self .D = None # dimensions
236+ self .name = name # name of the DXclass
237+ self .component = component # component type
238+ self .D = 3 # dimensions
238239
239240 def write (self , stream , optstring = "" , quote = False ):
240241 """write the 'object' line; additional args are packed in string"""
@@ -279,12 +280,11 @@ class gridpositions(DXclass):
279280 def __init__ (self , classid , shape = None , origin = None , delta = None , ** kwargs ):
280281 if shape is None or origin is None or delta is None :
281282 raise ValueError ("all keyword arguments are required" )
282- self .id = classid
283- self .name = "gridpositions"
284- self .component = "positions"
283+ super ().__init__ (classid , name = "gridpositions" , component = "positions" )
285284 self .shape = numpy .asarray (shape ) # D dimensional shape
286285 self .origin = numpy .asarray (origin ) # D vector
287286 self .rank = len (self .shape ) # D === rank
287+ assert self .rank == self .D , "DXClass is only used for 3D arrays"
288288
289289 self .delta = numpy .asarray (delta ) # DxD array of grid spacings
290290 # gridDataFormats actually provides a simple 1D array with the deltas because only
@@ -301,9 +301,7 @@ def __init__(self, classid, shape=None, origin=None, delta=None, **kwargs):
301301 )
302302
303303 def write (self , stream ):
304- super (gridpositions , self ).write (
305- stream , ("counts " + self .ndformat (" %d" )) % tuple (self .shape )
306- )
304+ super ().write (stream , ("counts " + self .ndformat (" %d" )) % tuple (self .shape ))
307305 self ._write_line (stream , "origin %f %f %f\n " % tuple (self .origin ))
308306 for delta in self .delta :
309307 self ._write_line (
@@ -329,9 +327,7 @@ class gridconnections(DXclass):
329327 def __init__ (self , classid , shape = None , ** kwargs ):
330328 if shape is None :
331329 raise ValueError ("all keyword arguments are required" )
332- self .id = classid
333- self .name = "gridconnections"
334- self .component = "connections"
330+ super ().__init__ (classid , name = "gridconnections" , component = "connections" )
335331 self .shape = numpy .asarray (shape ) # D dimensional shape
336332
337333 def write (self , stream ):
@@ -410,9 +406,7 @@ def __init__(self, classid, array=None, type=None, typequote='"', **kwargs):
410406 """
411407 if array is None :
412408 raise ValueError ("array keyword argument is required" )
413- self .id = classid
414- self .name = "array"
415- self .component = "data"
409+ super ().__init__ (classid , name = "array" , component = "data" )
416410 # detect type https://github.com/MDAnalysis/GridDataFormats/issues/35
417411 if type is None :
418412 self .array = numpy .asarray (array )
@@ -464,7 +458,7 @@ def write(self, stream):
464458 ).format (self .type , list (self .dx_types .keys ()))
465459 )
466460 typelabel = self .typequote + self .type + self .typequote
467- super (array , self ).write (
461+ super ().write (
468462 stream ,
469463 "type {0} rank 0 items {1} data follows" .format (typelabel , self .array .size ),
470464 )
@@ -544,6 +538,11 @@ def __init__(self, classid="0", components=None, comments=None):
544538 dx = OpenDX.field('density',[gridpoints,gridconnections,array])
545539
546540 """
541+ super ().__init__ (
542+ classid , # can be an arbitrary string
543+ name = "field" ,
544+ component = None , # cannot be a component of a field
545+ )
547546 if components is None :
548547 components = dict (positions = None , connections = None , data = None )
549548 if comments is None :
@@ -553,9 +552,6 @@ def __init__(self, classid="0", components=None, comments=None):
553552 ]
554553 elif type (comments ) is not list :
555554 comments = [str (comments )]
556- self .id = classid # can be an arbitrary string
557- self .name = "field"
558- self .component = None # cannot be a component of a field
559555 self .components = components
560556 self .comments = comments
561557
@@ -584,8 +580,8 @@ def from_grid(cls, grid, type=None, typequote='"', **kwargs):
584580 -------
585581 field
586582 OpenDX field wrapper
587-
588-
583+
584+
589585 .. versionadded:: 1.2.0
590586 """
591587 comments = [
@@ -612,10 +608,10 @@ def from_grid(cls, grid, type=None, typequote='"', **kwargs):
612608 @property
613609 def native (self ):
614610 """Return native object
615-
611+
616612 The "native" object is the :class:`gridData.OpenDX.field` itself.
617-
618-
613+
614+
619615 .. versionadded:: 1.2.0
620616 """
621617 return self
@@ -647,7 +643,7 @@ def write(self, filename):
647643 for component , object in self .sorted_components ():
648644 object .write (outfile )
649645 # the field object itself
650- super (field , self ).write (outfile , quote = True )
646+ super ().write (outfile , quote = True )
651647 for component , object in self .sorted_components ():
652648 self ._write_line (
653649 outfile , 'component "%s" value %s\n ' % (component , str (object .id ))
@@ -683,9 +679,11 @@ def read(self, stream):
683679 except (UnicodeDecodeError , RecursionError ) as err :
684680 # parser got confused, likely not a valid file
685681 # (RecursionError was only observed on Windows)
686- raise ValueError ("DX file could not be read. "
687- "The original error was\n "
688- f" { err .__class__ .__name__ } : { err } " )
682+ raise ValueError (
683+ "DX file could not be read. "
684+ "The original error was\n "
685+ f" { err .__class__ .__name__ } : { err } "
686+ )
689687
690688 def add (self , component , DXobj ):
691689 """add a component to the field"""
0 commit comments