A Python package for reading and writing Gadget, and Gadget-like, data files. (glio ~ Gadget-like I/O.)
glio is released under the MIT licence (see LICENCE.md). glio is compatible with Python 2.7.x and Python 3.x, and requires numpy.
New file types may be added by defining a schema for the header and block data,
and subclassing SnapshotBase
(or one of its subclasses). See for example
GadgetSnapshot
in gadget.py
and SPHRAYSnapshot
in sphray.py
.
To load a Gadget-2 data file,
>>> import glio
>>> s = glio.GadgetSnapshot('filename')
>>> s.load()
and similarly for the SPHRAYSnapshot
class. Header data is accessible as
>>> s.header.header_item
where valid header_item
attributes are defined by the current class' header
schema. They can also be obtained as a list of strings, or looped through,
>>> s.header.fields
['npart', 'mass', ... ]
>>> for (name, value) in s.header.iterfields():
>>> # Do something with header data.
Block data is accessed similarly, and can be iterated over similarly,
>>> s.ID
[array([1370534, 1370614, 1348895, ..., 1370615, 1370533, 1370531], dtype=uint32), ... ]
>>> s.fields
['pos', 'vel', 'ID', ... ]
>>> for (name, field) in s.iterfields():
>>> # Do something with block data.
Each block data (field
above) is a list, with each index i
corresponding to
that block's particle data for Gadget particle type i
. When reading from file,
if a block contains no data for a particle type i
, an empty numpy.ndarray
is
set for that particle type in the list; if a particle type is not valid for the
block, None
is set.
For convenience, particle types may be aliased to readable attribute names.
A dict
mapping the available attributes to their corresponding particle type indices is availabe as s.ptype_aliases
.
For example, for a GadgetSnapshot
,
>>> s.ptype_aliases
{'star': 4, 'bulge': 3, 'gas': 0, 'boundary': 5, 'disk': 2, 'halo': 1}
>>> s.gas.pos is s.pos[0]
True
>>> s.star.vel is s.vel[4]
True
However, the alias attribute (e.g. s.gas
or s.star
) returns a SnapshotView
object, which is read-only.
Modification of the snapshot data in general requires directly modifying the field list(s).
A snapshot can be written to file, optionally with a new filename,
>>> import glio
>>> s = glio.GadgetSnapshot('filename')
>>> # Double all gas-particle smoothing lengths.
>>> s.hsml[0] *= 2
>>> s.save('new_filename')