<gh_stars>0
#!/bin/env python
#
# Python module to read spatially indexed Eagle snapshots
#
# Needs the _read_eagle C extension module.
# See the README for how to compile it.
#
import _read_eagle

class EagleSnapshotClosedException(Exception):
    pass

class EagleSnapshot:
    """Class to represent an open Eagle snapshot"""
    
    def __init__(self, fname):
        """Open a new snapshot"""
        try:
            (self.snap, n0, n1, n2, n3, n4, n5, 
             self.boxsize, self.numfiles, self.hashbits) = _read_eagle.open_snapshot(fname)
        except _read_eagle.error:
            self.open=False
            raise
        else:
            self.open = True
        # Store particle number
        self.numpart_total = (n0, n1, n2, n3, n4, n5)
        # Get names of datasets
        self._dataset = []
        for itype in range(6):
            self._dataset.append([])
            for iset in range(_read_eagle.get_dataset_count(self.snap, itype)):
                self._dataset[-1].append(_read_eagle.get_dataset_name(self.snap, itype, iset))

    def __del__(self):
        """Close the snapshot and deallocate, if we haven't already"""
        if self.open:
            self.open = False
            _read_eagle.close_snapshot(self.snap)

    def select_region(self, xmin, xmax, ymin, ymax, zmin, zmax):
        """Select a region to read in"""
        if not(self.open):
            raise EagleSnapshotClosedException("Cannot select region in closed snapshot!")
        _read_eagle.select_region(self.snap, xmin, xmax, ymin, ymax, zmin, zmax)

    def set_sampling_rate(self, rate):
        """Set the sampling rate for subsequent reads"""
        if not(self.open):
            raise EagleSnapshotClosedException("Cannot set sample rate in closed snapshot!")
        _read_eagle.set_sampling_rate(self.snap, rate)

    def clear_selection(self):
        """Clear the current selection"""
        if not(self.open):
            raise EagleSnapshotClosedException("Cannot clear selection in closed snapshot!")
        _read_eagle.clear_selection(self.snap)

    def count_particles(self, itype):
        """Return the number of particles in the selected region"""
        if not(self.open):
            raise EagleSnapshotClosedException("Cannot count particles in closed snapshot!")
        return _read_eagle.count_particles(self.snap, itype)

    def get_particle_locations(self, itype):
        """Return the locations of particles in the selected region"""
        if not(self.open):
            raise EagleSnapshotClosedException("Cannot count particles in closed snapshot!")
        return _read_eagle.get_particle_locations(self.snap, itype)

    def read_dataset(self, itype, name):
        """Read a dataset and return it as a Numpy array"""
        if not(self.open):
            raise EagleSnapshotClosedException("Cannot read dataset from closed snapshot!")
        return _read_eagle.read_dataset(self.snap, itype, name)
    
    def datasets(self, itype):
        """Return a list of datasets available for the specified particle type"""
        if itype < 0 or itype > 5:
            raise ValueError("Particle type index is out of range")
        return self._dataset[itype]

    def close(self):
        """Close the snapshot and deallocate associated memory"""
        if not(self.open):
            raise EagleSnapshotClosedException("Snapshot is already closed!")
        _read_eagle.close_snapshot(self.snap)
        self.open = False
        

#!/bin/env python
#
# Python module to read spatially indexed Eagle snapshots
#
# Needs the _read_eagle C extension module.
# See the README for how to compile it.
#
import _read_eagle

class EagleSnapshotClosedException(Exception):
    pass

class EagleSnapshot:
    """Class to represent an open Eagle snapshot"""
    
    def __init__(self, fname):
        """Open a new snapshot"""
        try:
            (self.snap, n0, n1, n2, n3, n4, n5, 
             self.boxsize, self.numfiles, self.hashbits) = _read_eagle.open_snapshot(fname)
        except _read_eagle.error:
            self.open=False
            raise
        else:
            self.open = True
        # Store particle number
        self.numpart_total = (n0, n1, n2, n3, n4, n5)
        # Get names of datasets
        self._dataset = []
        for itype in range(6):
            self._dataset.append([])
            for iset in range(_read_eagle.get_dataset_count(self.snap, itype)):
                self._dataset[-1].append(_read_eagle.get_dataset_name(self.snap, itype, iset))

    def __del__(self):
        """Close the snapshot and deallocate, if we haven't already"""
        if self.open:
            self.open = False
            _read_eagle.close_snapshot(self.snap)

    def select_region(self, xmin, xmax, ymin, ymax, zmin, zmax):
        """Select a region to read in"""
        if not(self.open):
            raise EagleSnapshotClosedException("Cannot select region in closed snapshot!")
        _read_eagle.select_region(self.snap, xmin, xmax, ymin, ymax, zmin, zmax)

    def set_sampling_rate(self, rate):
        """Set the sampling rate for subsequent reads"""
        if not(self.open):
            raise EagleSnapshotClosedException("Cannot set sample rate in closed snapshot!")
        _read_eagle.set_sampling_rate(self.snap, rate)

    def clear_selection(self):
        """Clear the current selection"""
        if not(self.open):
            raise EagleSnapshotClosedException("Cannot clear selection in closed snapshot!")
        _read_eagle.clear_selection(self.snap)

    def count_particles(self, itype):
        """Return the number of particles in the selected region"""
        if not(self.open):
            raise EagleSnapshotClosedException("Cannot count particles in closed snapshot!")
        return _read_eagle.count_particles(self.snap, itype)

    def get_particle_locations(self, itype):
        """Return the locations of particles in the selected region"""
        if not(self.open):
            raise EagleSnapshotClosedException("Cannot count particles in closed snapshot!")
        return _read_eagle.get_particle_locations(self.snap, itype)

    def read_dataset(self, itype, name):
        """Read a dataset and return it as a Numpy array"""
        if not(self.open):
            raise EagleSnapshotClosedException("Cannot read dataset from closed snapshot!")
        return _read_eagle.read_dataset(self.snap, itype, name)
    
    def datasets(self, itype):
        """Return a list of datasets available for the specified particle type"""
        if itype < 0 or itype > 5:
            raise ValueError("Particle type index is out of range")
        return self._dataset[itype]

    def close(self):
        """Close the snapshot and deallocate associated memory"""
        if not(self.open):
            raise EagleSnapshotClosedException("Snapshot is already closed!")
        _read_eagle.close_snapshot(self.snap)
        self.open = False
#!/bin/env python # # Python module to read spatially indexed Eagle snapshots # # Needs the _read_eagle C extension module. # See the README for how to compile it. # Class to represent an open Eagle snapshot Open a new snapshot # Store particle number # Get names of datasets Close the snapshot and deallocate, if we haven't already Select a region to read in Set the sampling rate for subsequent reads Clear the current selection Return the number of particles in the selected region Return the locations of particles in the selected region Read a dataset and return it as a Numpy array Return a list of datasets available for the specified particle type Close the snapshot and deallocate associated memory