Exemplo n.º 1
0
    def yt_dataset(self, value):
        if value == 0: return

        if not hasattr(value, 'dataset_type'):
            raise IOError('not a yt dataset?')

        infile = '%s/%s' % (value.fullpath, value.basename)

        self.skip_hash_check = False
        if hasattr(self, 'hash'):
            if isinstance(self.hash, np.bytes_):
                self.hash = self.hash.decode('utf8')

            hash = get_hash(infile)
            if hash != self.hash:
                raise IOError('hash mismatch!')
            else:
                self._ds = value
        else:
            self._ds = value
            self.hash = get_hash(infile)

        self._ds = value
        self._ds_type = DatasetType(self._ds)
        self._assign_simulation_attributes()
Exemplo n.º 2
0
    def yt_dataset(self, value):
        if value is None:
            return

        if not hasattr(value, 'dataset_type'):
            raise ValueError('not a yt dataset?')

        hash = get_hash(os.path.join(value.fullpath, value.basename))
        if hash != self.hash:
            raise RuntimeError('hash mismatch!')
        else:
            self._ds = value

        self._ds = value
        self._ds_type = DatasetType(self._ds)
Exemplo n.º 3
0
    def yt_dataset(self, value):
        if value is None:
            return

        if not hasattr(value, 'dataset_type'):
            raise ValueError('not a yt dataset?')

        #if 'skip_hash_check' in self._kwargs and self._kwargs['skip_hash_check']:
        if (self.skip_hash_check) or (self.hash is None):
            hash = self.hash
        else:
            hash = get_hash(os.path.join(value.fullpath, value.basename))
        if hash != self.hash:
            raise RuntimeError('hash mismatch!')

        self._ds = value
        self._ds_type = DatasetType(self._ds)
Exemplo n.º 4
0
    def yt_dataset(self, value):
        if value is None:
            return

        if not hasattr(value, 'dataset_type'):
            raise IOError('not a yt dataset?')

        infile = '%s/%s' % (value.fullpath, value.basename)

        hash = get_hash(infile)
        if hash != self.hash:
            raise IOError('hash mismatch!')
        else:
            self._ds = value

        self._ds = value
        self._ds_type = DatasetType(self._ds)
Exemplo n.º 5
0
def get_IC_pos(group,
               ic_ds,
               radius_type,
               search_factor=2.5,
               return_mask=False):
    """Get the initial dark matter positions of a ``CAESAR`` halo.

    If called on a galaxy, it will return the IC DM positions of the
    parent halo.
    
    Parameters
    ----------
    group : :class:`group.Group`
        Group we are querying.
    ic_ds : yt dataset
        The initial condition dataset via ``yt.load()``
    search_factor : float, optional
        How far from the center to select DM particles (defaults to 2.5).
    return_mask : bool, optional
        Return initial condition positions from 0-->1 rather than raw 
        data.  Useful for writing a MUSIC mask file.
    
    Returns
    -------
    ic_dmpos : np.ndarray
        DM positions of this object in the initial condition file.
    
    """
    from caesar.property_manager import ptype_aliases, get_property, DatasetType
    from caesar.periodic_kdtree import PeriodicCKDTree

    ic_ds_type = ic_ds.__class__.__name__
    if ic_ds_type not in ptype_aliases:
        raise NotImplementedError('%s not yet supported' % ic_ds_type)
    if group.obj.yt_dataset.domain_width[0].d != ic_ds.domain_width[0].d:
        raise Exception(
            'IC and SNAP boxes do not match! (%f vs %f)' %
            (ic_ds.domain_width[0].d, group.obj.yt_dataset.domain_width[0].d))
    if str(ic_ds.length_unit) != str(group.obj.yt_dataset.length_unit):
        raise Exception('LENGTH UNIT MISMATCH! '\
                        'This may arise from loading the snap/IC '\
                        'incorrectly and WILL cause problems with '\
                        'the matching process. (%s vs %s)' %
                        (str(ic_ds.length_unit), str(group.obj.yt_dataset.length_unit)))

    if group.obj_type == 'halo':
        obj = group
    elif group.obj_type == 'galaxy':
        if group.halo is None:
            mylog.warning('Galaxy %d has no halo!' % group.GroupID)
            return
        obj = group.halo

    search_params = dict(
        pos=obj.pos.in_units('code_length').d,
        r=obj.radii[radius_type].in_units('code_length').d * search_factor,
    )

    box = ic_ds.domain_width[0].d
    bounds = np.array([box, box, box])

    dmpids = get_property(obj.obj, 'pid', 'dm').d
    dmpos = get_property(obj.obj, 'pos', 'dm').d

    dm_TREE = PeriodicCKDTree(bounds, dmpos)

    valid = dm_TREE.query_ball_point(search_params['pos'], search_params['r'])
    search_params['ids'] = dmpids[valid]

    ic_ds_type = DatasetType(ic_ds)
    ic_dmpos = ic_ds_type.get_property('dm', 'pos').d
    ic_dmpids = ic_ds_type.get_property('dm', 'pid').d

    matches = np.in1d(ic_dmpids, search_params['ids'], assume_unique=True)
    nmatches = len(np.where(matches)[0])
    nvalid = len(valid)
    if nmatches != nvalid:
        raise Exception('Could not match all particles! '\
                        'Only %0.2f%% particles matched.' %
                        (float(nmatches)/float(nvalid) * 100.0))

    mylog.info('MATCHED %d particles from %s %d in %s' %
               (nmatches, obj.obj_type, obj.GroupID, ic_ds.basename))
    mylog.info('Returning %0.2f%% of the total DM from the sim' %
               (float(nmatches) / float(len(ic_dmpids)) * 100.0))

    matched_pos = ic_dmpos[matches]

    if return_mask:
        matched_pos /= box

    return matched_pos
Exemplo n.º 6
0
    def __init__(self,
                 obj,
                 group_list,
                 ds=None,
                 band_names='v',
                 ssp_model='FSPS',
                 ssp_table_file='FSPS_Chab_EL.hdf5',
                 view_dir='x',
                 use_dust=True,
                 ext_law='mw',
                 use_cosmic_ext=True,
                 kernel_type='cubic',
                 nproc=-1):

        from caesar.property_manager import ptype_ints
        self.obj = obj  # caesar object
        self.groups = group_list  # list of objects to process

        # optional arguments
        self.band_names = band_names
        if hasattr(self.obj, '_kwargs') and 'fsps_bands' in self.obj._kwargs:
            self.band_names = self.obj._kwargs['fsps_bands']
        self.ssp_model = ssp_model
        if hasattr(self.obj, '_kwargs') and 'ssp_model' in self.obj._kwargs:
            self.ssp_model = self.obj._kwargs['ssp_model']
        if 'caesar/' not in ssp_table_file:
            self.ssp_table_file = os.path.expanduser('~/caesar/%s' %
                                                     ssp_table_file)
        else:
            self.ssp_table_file = ssp_table_file
        if hasattr(self.obj,
                   '_kwargs') and 'ssp_table_file' in self.obj._kwargs:
            self.ssp_table_file = self.obj._kwargs['ssp_table_file']
        self.ext_law = ext_law
        if hasattr(self.obj, '_kwargs') and 'ext_law' in self.obj._kwargs:
            self.ext_law = self.obj._kwargs['ext_law'].lower()
        if hasattr(self.obj, '_kwargs') and 'view_dir' in self.obj._kwargs:
            view_dir = self.obj._kwargs['view_dir'].lower()
        if view_dir == 'x' or view_dir == '0': self.viewdir = 0
        if view_dir == 'y' or view_dir == '1': self.viewdir = 1
        if view_dir == 'z' or view_dir == '2': self.viewdir = 2
        self.use_dust = use_dust  # if False, will use metals plus an assumed dust-to-metal ratio
        if hasattr(self.obj, '_kwargs') and 'use_dust' in self.obj._kwargs:
            use_dust = self.obj._kwargs['use_dust'].lower()
        self.use_cosmic_ext = use_cosmic_ext
        if hasattr(self.obj,
                   '_kwargs') and 'use_cosmic_ext' in self.obj._kwargs:
            use_cosmic_ext = self.obj._kwargs['use_cosmic_ext'].lower()
        self.kernel_type = kernel_type
        if hasattr(self.obj, '_kwargs') and 'kernel_type' in self.obj._kwargs:
            use_cosmic_ext = self.obj._kwargs['kernel_type'].lower()
        self.nkerntab = 2000
        if nproc == -1:
            try:
                self.nproc = obj.nproc
            except:
                self.nproc = 1
        else:
            self.nproc = nproc
        self.obj.skip_hash_check = True  # WARNING: this will skip the check that the supplied snapshot file (via ds.load) is the same as the original one used to generate photometry!

        # useful quantities
        self.boxsize = self.obj.simulation.boxsize
        self.solar_abund = Solar
        self.lumtoflux_abs = const.L_sun.to('erg/s').value / (
            4 * np.pi * 10.**2 * const.pc.to('cm').value**2)
        cosmo = FlatLambdaCDM(H0=100. * self.obj.simulation.hubble_constant,
                              Om0=self.obj.simulation.omega_matter,
                              Tcmb0=2.73)
        lumdist = cosmo.luminosity_distance(
            self.obj.simulation.redshift).to('pc').value
        self.lumtoflux = const.L_sun.to('erg/s').value / (
            4 * np.pi * lumdist**2 * const.pc.to('cm').value**2)
        self.lumtoflux *= 1. + self.obj.simulation.redshift  # we compute apparent mags by blueshifting the band, which reduces the flux by (1+z); correct for this here

        # if there is no data_manager, assume we're running interactively
        # this means we have to load in the particle info, and set some other info we need
        if not hasattr(self.obj, 'data_manager'):
            from caesar.data_manager import DataManager
            from caesar.property_manager import DatasetType
            self.obj._ds_type = DatasetType(ds)
            self.obj.data_manager = DataManager(self.obj)
            self.obj.yt_dataset = ds
            self.obj.units = dict(mass='Msun',
                                  length='kpccm',
                                  velocity='km/s',
                                  time='yr',
                                  temperature='K')
            self.obj.data_manager._photometry_init()