Esempio n. 1
0
def load(filename, ds=None, obj=None, LoadHalo=1):
    """Function to load a CAESAR object from disk.

    Parameters
    ----------
    filename : str
        Input file.
    ds : yt dataset, optional
        yt dataset to link to.
    obj : :class:`main.CAESAR`, optional
        For loading into an already created CAESAR object.
    LoadHalo: int
        Option to load halo information: 0=No; 1=Yes; 2=Yes, but not mass/radius info


    Examples
    --------
    >>> import caesar
    >>> import yt
    >>> ds  = yt.load('snapshot')
    >>> obj = caesar.load(ds)

    """
    from yt.funcs import mylog

    mylog.info('Reading %s' % filename)
    try:
        infile = h5py.File(filename, 'r')
    except IOError:
        raise

    if obj is None:
        import os
        from caesar.main import CAESAR
        obj = CAESAR()
        obj.data_file = os.path.abspath(filename)

    unit_registry_json = infile.attrs['unit_registry_json']
    obj.unit_registry = UnitRegistry.from_json(
        unit_registry_json.decode('utf8'))

    restore_global_attributes(obj, infile)
    obj.simulation._unpack(obj, infile)

    # restore halo data
    if 'halo_data' in infile and LoadHalo:
        mylog.info('Restoring halo attributes')
        hd = infile['halo_data']
        obj.halos = []
        for i in range(0, obj.nhalos):
            obj.halos.append(Halo(obj))
        restore_object_attributes(obj.halos, hd, obj.unit_registry)
        restore_object_dicts(obj.halos, hd, obj.unit_registry)
        restore_object_list(obj.halos, 'galaxy_index_list', hd)

        if LoadHalo == 1:
            #Compute the virial/200/500/2500 masses
            if hasattr(obj.simulation, 'Densities'):
                PiFac = 4. / 3. * np.pi
                h = obj.halos[0]
                for h in obj.halos:
                    h.masses['virial'] = obj.simulation.Densities[
                        0] * PiFac * (h.radii['virial'] * h.radii['virial'] *
                                      h.radii['virial'])
                    h.masses['m200c'] = (obj.simulation.Densities[1] * PiFac *
                                         (h.radii['r200c'] * h.radii['r200c'] *
                                          h.radii['r200c'])).to('Msun')
                    h.masses['m500c'] = (obj.simulation.Densities[2] * PiFac *
                                         (h.radii['r500c'] * h.radii['r500c'] *
                                          h.radii['r500c'])).to('Msun')
                    h.masses['m2500c'] = (
                        obj.simulation.Densities[3] * PiFac *
                        (h.radii['r2500c'] * h.radii['r2500c'] *
                         h.radii['r2500c'])).to('Msun')

        # optional
        for vals in ['dmlist', 'glist', 'slist', 'bhlist', 'dlist']:
            restore_object_list(obj.halos, vals, hd)

    # restore galaxy data
    if 'galaxy_data' in infile:
        mylog.info('Restoring galaxy attributes')
        hd = infile['galaxy_data']
        obj.galaxies = []
        for i in range(0, obj.ngalaxies):
            obj.galaxies.append(Galaxy(obj))

        restore_object_attributes(obj.galaxies, hd, obj.unit_registry)
        restore_object_dicts(obj.galaxies, hd, obj.unit_registry)
        restore_object_list(obj.galaxies, 'cloud_index_list', hd)

        # optional
        for vals in ['glist', 'slist', 'bhlist', 'dlist']:
            restore_object_list(obj.galaxies, vals, hd)

    if 'cloud_data' in infile:
        mylog.info('Restoring cloud attributes')
        hd = infile['cloud_data']
        obj.clouds = []
        for i in range(0, obj.nclouds):
            obj.clouds.append(Cloud(obj))

        restore_object_attributes(obj.clouds, hd, obj.unit_registry)
        restore_object_dicts(obj.clouds, hd, obj.unit_registry)

        # optional
        for vals in ['glist', 'slist', 'bhlist', 'dlist']:
            restore_object_list(obj.clouds, vals, hd)

    infile.close()

    if LoadHalo:
        obj._link_objects()

    if ds is not None:
        obj.yt_dataset = ds

    return obj
Esempio n. 2
0
    def __init__(self, filename, skip_hash_check=False):
        self._ds = None
        self.data_file = os.path.abspath(filename)
        self.skip_hash_check = skip_hash_check

        self._halo_dmlist = LazyDataset(self, 'halo_data/lists/dmlist')
        self._halo_slist = LazyDataset(self, 'halo_data/lists/slist')
        self._halo_glist = LazyDataset(self, 'halo_data/lists/glist')
        self._halo_bhlist = LazyDataset(self, 'halo_data/lists/bhlist')
        self._halo_dlist = LazyDataset(self, 'halo_data/lists/dlist')

        self._galaxy_slist = LazyDataset(self, 'galaxy_data/lists/slist')
        self._galaxy_glist = LazyDataset(self, 'galaxy_data/lists/glist')
        self._galaxy_bhlist = LazyDataset(self, 'galaxy_data/lists/bhlist')
        self._galaxy_dlist = LazyDataset(self, 'galaxy_data/lists/dlist')

        self._cloud_glist = LazyDataset(self, 'cloud_data/lists/glist')
        self._cloud_dlist = LazyDataset(self, 'cloud_data/lists/dlist')

        with h5py.File(filename, 'r') as hd:
            mylog.info('Opening {}'.format(filename))

            if 'hash' in hd.attrs:
                self.hash = hd.attrs['hash']
            else:
                self.hash = None
            if isinstance(self.hash, np.bytes_):
                self.hash = self.hash.decode('utf8')

            # This should probably be caesar_version or something
            self.caesar = hd.attrs['caesar']

            self.unit_registry = UnitRegistry.from_json(
                hd.attrs['unit_registry_json'])

            # Load the information about the simulation itself
            self.simulation = SimulationAttributes()
            self.simulation._unpack(self, hd)

            # Halo data is loaded unconditionally, AFAICT it's always present
            self._galaxy_index_list = None
            if 'halo_data/lists/galaxy_index_list' in hd:
                self._galaxy_index_list = LazyDataset(
                    self, 'halo_data/lists/galaxy_index_list')

            self._halo_data = {}
            for k, v in hd['halo_data'].items():
                if type(v) is h5py.Dataset:
                    self._halo_data[k] = LazyDataset(self, 'halo_data/' + k)

            self._halo_dicts = defaultdict(dict)
            for k in hd['halo_data/dicts']:
                dictname, arrname = k.split('.')
                self._halo_dicts[dictname][arrname] = LazyDataset(
                    self, 'halo_data/dicts/' + k)

            if 'tree_data/progen_halo_dm' in hd:
                self._halo_data[
                    'progen_halo_dm'] = self._progen_halo_dm = LazyDataset(
                        self, 'tree_data/progen_halo_dm')

            if 'tree_data/descend_halo_dm' in hd:
                self._halo_data[
                    'descend_halo_dm'] = self._descend_halo_dm = LazyDataset(
                        self, 'tree_data/descend_halo_dm')

            self.nhalos = hd.attrs['nhalos']
            self.halos = LazyList(self.nhalos, lambda i: Halo(self, i))
            mylog.info('Found {} halos'.format(len(self.halos)))

            # Provide default values for everything, so that if a simulation
            # without galaxies is loaded we get zero galaxies, not AttributeErrors
            self._galaxy_data = {}
            self._galaxy_dicts = defaultdict(dict)
            self.ngalaxies = 0
            self.galaxies = LazyList(self.ngalaxies, lambda i: Galaxy(self, i))
            if 'galaxy_data' in hd:
                self._cloud_index_list = None
                if 'galaxy_data/lists/cloud_index_list' in hd:
                    self._cloud_index_list = LazyDataset(
                        self, 'galaxy_data/lists/cloud_index_list')

                if 'tree_data/progen_galaxy_star' in hd:
                    self._galaxy_data[
                        'progen_galaxy_star'] = self._progen_galaxy_star = LazyDataset(
                            self, 'tree_data/progen_galaxy_star')

                if 'tree_data/descend_galaxy_star' in hd:
                    self._galaxy_data[
                        'descend_galaxy_star'] = self._descend_galaxy_star = LazyDataset(
                            self, 'tree_data/descend_galaxy_star')

                for k, v in hd['galaxy_data'].items():
                    if type(v) is h5py.Dataset:
                        self._galaxy_data[k] = LazyDataset(
                            self, 'galaxy_data/' + k)

                for k in hd['galaxy_data/dicts']:
                    dictname, arrname = k.split('.')
                    self._galaxy_dicts[dictname][arrname] = LazyDataset(
                        self, 'galaxy_data/dicts/' + k)

                self.ngalaxies = hd.attrs['ngalaxies']
                self.galaxies = LazyList(self.ngalaxies,
                                         lambda i: Galaxy(self, i))
                mylog.info('Found {} galaxies'.format(len(self.galaxies)))

            self._cloud_data = {}
            self._cloud_dicts = defaultdict(dict)
            self.nclouds = 0
            self.clouds = LazyList(self.nclouds, lambda i: Cloud(self, i))
            if 'cloud_data' in hd:
                for k, v in hd['cloud_data'].items():
                    if type(v) is h5py.Dataset:
                        self._cloud_data[k] = LazyDataset(
                            self, 'cloud_data/' + k)

                for k in hd['cloud_data/dicts']:
                    dictname, arrname = k.split('.')
                    self._cloud_dicts[dictname][arrname] = LazyDataset(
                        self, 'cloud_data/dicts/' + k)

                if 'tree_data/progen_cloud_gas' in hd:
                    self._cloud_data[
                        'progen_cloud_gas'] = self._progen_cloud_gas = LazyDataset(
                            self, 'tree_data/progen_cloud_gas')

                if 'tree_data/descend_cloud_gas' in hd:
                    self._cloud_data[
                        'descend_cloud_gas'] = self._descend_cloud_gas = LazyDataset(
                            self, 'tree_data/descend_cloud_gas')

                self.nclouds = hd.attrs['nclouds']
                self.clouds = LazyList(self.nclouds, lambda i: Cloud(self, i))
                mylog.info('Found {} clouds'.format(len(self.clouds)))
Esempio n. 3
0
    def __init__(self, filename):
        self._ds = None
        self.data_file = os.path.abspath(filename)

        self._halo_dmlist = LazyArray(self, 'halo_data/lists/dmlist')
        self._halo_slist = LazyArray(self, 'halo_data/lists/slist')
        self._halo_glist = LazyArray(self, 'halo_data/lists/glist')
        self._halo_bhlist = LazyArray(self, 'halo_data/lists/bhlist')
        self._halo_dlist = LazyArray(self, 'halo_data/lists/dlist')

        self._galaxy_slist = LazyArray(self, 'galaxy_data/lists/slist')
        self._galaxy_glist = LazyArray(self, 'galaxy_data/lists/glist')
        self._galaxy_bhlist = LazyArray(self, 'galaxy_data/lists/bhlist')
        self._galaxy_dlist = LazyArray(self, 'galaxy_data/lists/dlist')

        self._cloud_glist = LazyArray(self, 'cloud_data/lists/glist')
        self._cloud_dlist = LazyArray(self, 'cloud_data/lists/dlist')

        with h5py.File(filename, 'r') as hd:
            mylog.info('Reading {}'.format(filename))

            self.hash = hd.attrs['hash']
            self.caesar = hd.attrs['caesar']

            self.unit_registry = UnitRegistry.from_json(
                hd.attrs['unit_registry_json'].decode('utf8'))

            # Load the information about the simulation itself
            self.simulation = SimulationAttributes()
            self.simulation._unpack(self, hd)

            mylog.info('Loading halos')
            self._galaxy_index_list = None
            if 'halo_data/lists/galaxy_index_list' in hd:
                self._galaxy_index_list = LazyArray(
                    self, 'halo_data/lists/galaxy_index_list')

            self._halo_data = {}
            for k, v in hd['halo_data'].items():
                if type(v) is h5py.Dataset:
                    self._halo_data[k] = LazyArray(self, 'halo_data/' + k)

            self._halo_dicts = defaultdict(dict)
            for k in hd['halo_data/dicts']:
                dictname, arrname = k.split('.')
                self._halo_dicts[dictname][arrname] = LazyArray(
                    self, 'halo_data/dicts/' + k)

            self.nhalos = hd.attrs['nhalos']
            self.halos = LazyList(self.nhalos, lambda i: Halo(self, i))
            mylog.info('Loaded {} halos'.format(len(self.halos)))

            self._galaxy_data = {}
            self._galaxy_dicts = defaultdict(dict)
            self.ngalaxies = 0
            self.galaxies = LazyList(self.ngalaxies, lambda i: Galaxy(self, i))
            if 'galaxy_data' in hd:
                mylog.info('Loading galaxies')
                self._cloud_index_list = None
                if 'galaxy_data/lists/cloud_index_list' in hd:
                    self._cloud_index_list = LazyArray(
                        self, 'galaxy_data/lists/cloud_index_list')

                for k, v in hd['galaxy_data'].items():
                    if type(v) is h5py.Dataset:
                        self._galaxy_data[k] = LazyArray(
                            self, 'galaxy_data/' + k)

                for k in hd['galaxy_data/dicts']:
                    dictname, arrname = k.split('.')
                    self._galaxy_dicts[dictname][arrname] = LazyArray(
                        self, 'galaxy_data/dicts/' + k)

                self.ngalaxies = hd.attrs['ngalaxies']
                self.galaxies = LazyList(self.ngalaxies,
                                         lambda i: Galaxy(self, i))
                mylog.info('Loaded {} galaxies'.format(len(self.galaxies)))

            self._cloud_data = {}
            self._cloud_dicts = defaultdict(dict)
            self.nclouds = 0
            self.clouds = LazyList(self.nclouds, lambda i: Cloud(self, i))
            if 'cloud_data' in hd:
                mylog.info('Loading clouds')
                for k, v in hd['cloud_data'].items():
                    if type(v) is h5py.Dataset:
                        self._cloud_data[k] = LazyArray(
                            self, 'cloud_data/' + k)

                for k in hd['cloud_data/dicts']:
                    dictname, arrname = k.split('.')
                    self._cloud_dicts[dictname][arrname] = LazyArray(
                        self, 'cloud_data/dicts/' + k)

                self.nclouds = hd.attrs['nclouds']
                self.clouds = LazyList(self.nclouds, lambda i: Cloud(self, i))
                mylog.info('Loaded {} clouds'.format(len(self.clouds)))