Exemplo n.º 1
0
 def setCosmology(self, cosmodef):
     if type(cosmodef) is dict:
         self.cosmo = cosmology.FlatLambdaCDM(**cosmodef)
     elif isinstance(cosmodef, basestring):
         self.cosmo = cosmology.FlatLambdaCDM(**eval(cosmodef))
     elif isinstance(cosmodef, cosmology.FLRW):
         self.cosmo = cosmodef
     elif cosmodef is None:
         self.cosmo = cosmology.get_current()
     else:
         raise ValueError
Exemplo n.º 2
0
def abs_mag(app_mag, z):
    cosmo = cosmology.FlatLambdaCDM(
        H0=100, Om0=0.3)  #h=1, Omega_m=0.3, Omega_Lambda=0.7
    ld = cosmology.funcs.luminosity_distance(z, cosmo=cosmo)
    Mag = app_mag - 5.0 * (np.log10(ld * 1000.0 * 1000.0) - 1.0)

    return Mag
Exemplo n.º 3
0
def set_cosmology(Hubble_h, Omega_m):

    cosmo = cosmology.FlatLambdaCDM(H0=Hubble_h * 100, Om0=Omega_m)
    t_BigBang = cosmo.lookback_time(
        100000).value  # Lookback time to the Big Bang in Gyr.

    return cosmo, t_BigBang
Exemplo n.º 4
0
 def __getCosmology(self, config):
     try:
         if uf.checkkeys(config, 'H0') and uf.checkkeys(config, 'Om0'):
             H0 = config['H0']
             Om0 = config['Om0']
         else:
             print('\nWe assume a Flat Lambda CDM universe')
             H0 = input(
                 'Please enter the Hubble constant (H0) in units of km/s/Mpc: '
             )
             Om0 = input('Please enter the Omega matter (Om0): ')
         self.cosmology = astcos.FlatLambdaCDM(H0, Om0)
         logger.info(
             'Cosmology: H0 = %.1f %s, Om0 = %.3f' %
             (self.cosmology.H0.value, self.cosmology.H0.unit.to_string(),
              self.cosmology.Om0))
     except KeyboardInterrupt:
         raise uf.exit(self)
     except SystemExit:
         raise uf.exit(self)
     except:
         logger.error('There was a problem with entering the cosmology.',
                      exc_info=True)
         uf.earlyexit(self)
     return
Exemplo n.º 5
0
def get_sncosmo(mdl, redshift, waves, fine_waves, phase, absmag = - 19.08):
    sncosmo_model = sncosmo.Model(source=mdl)
    sntype = {"s11-2005hl": "Ib", "s11-2005hm": "Ib", "s11-2006fo": "Ic", "s11-2006jo":
              "Ib", "hsiao": "Ia", "nugent-sn1a": "Ia", "salt2-extended": "Ia"}[mdl]
    cosmo = cosmology.FlatLambdaCDM(Om0 = 0.3, H0 = 70.)
    ten_pc_z = 2.33494867e-9
    assert abs(cosmo.distmod(z=ten_pc_z).value) < 1.e-3, "Distance modulus zeropoint wrong!"

    ampl = 1.
    for i in range(2):
        if not mdl.count("salt2"):
            sncosmo_model.set(z=ten_pc_z, t0=0., amplitude=ampl)
        else:
            sncosmo_model.set(z=ten_pc_z, t0=0., x0=ampl)

        mag = sncosmo_model.bandmag('bessellv', 'ab', 0.)
        print "mag, ampl ", mag, ampl
        ampl *= 10.**(0.4*(mag - absmag))
        

    if not mdl.count("salt2"):
        sncosmo_model.set(z=redshift, t0=0., amplitude=ampl)
    else:
        sncosmo_model.set(z=redshift, t0=0., x0=ampl)


    mu = cosmo.distmod(redshift).value
    print "mu ", mu

    f_lamb_SN = sncosmo_model.flux(phase*(1. + redshift), waves)*10.**(-0.4*mu)
    f_lamb_SN_fine = sncosmo_model.flux(phase*(1. + redshift), fine_waves)*10.**(-0.4*mu)
    return f_lamb_SN, f_lamb_SN_fine, sntype, sncosmo_model
Exemplo n.º 6
0
 def __init__(self,
              mvir,
              r_s,
              a=1,
              cosm=cosmo.FlatLambdaCDM(H0=67.9,
                                       Om0=0.306,
                                       Neff=0.,
                                       Tcmb0=0.)):
     self.mvir = mvir
     self.a = a
     z = (1. - a) / a
     x = 1. / (1. + cosm.Ode0 / (cosm.Om0 / a**3.)) - 1.
     Del_vir = 18. * np.pi**2. + 82. * x - 39. * x**2
     self.Del_vir = Del_vir
     rhocrit = cosm.critical_density(z)
     rhocrit = rhocrit.to(u.Msun / u.kpc**3)
     rvir3 = 3. / 4. / np.pi * mvir / Del_vir / rhocrit
     rvir3 = rvir3.to(u.kpc**3)
     self.rvir = rvir3**(1. / 3.)
     tmp = np.sqrt(G_newton * self.mvir / self.rvir)
     self.vvir = tmp.to(u.km / u.s)
     self.cvir = (self.rvir / r_s).decompose()
     tmp = self.mvir / (4.0 * np.pi * r_s**3.)
     tmp = tmp / (np.log(1. + self.cvir) - self.cvir / (1. + self.cvir))
     rho_s = tmp.to(u.Msun / u.kpc**3)
     self.Evir = self.vvir**2.
     self.Lvir = self.rvir * self.vvir
     super().__init__(rho_s, r_s)
Exemplo n.º 7
0
def set_cosmology(Hubble_h, Omega_m, Omega_b):
    """    
    Sets a flat, LCDM cosmology.

    Parameters
    ----------

    Hubble_h : float
        Value of Hubble little h (i.e., between 0 and 1).


    Omega_m : float
        Value of critical matter density.

    Returns
    ---------

    cosmo : Class ``astropy.cosmology`` 
        ``Astropy`` class containing the cosmology for this model.

    t_bigbang : float
        The lookback time to the Big Bang in megayears. 
    """

    cosmo = cosmology.FlatLambdaCDM(H0 = Hubble_h*100, Om0 = Omega_m,
                                    Ob0 = Omega_b) 
    t_bigbang = (cosmo.lookback_time(100000).value)*1.0e3 # Lookback time to the Big Bang in Myr.

    return cosmo, t_bigbang
Exemplo n.º 8
0
    def __init__(self, cosmo=None, kwargs=None, zmin=1.e-8, zmax=1.e2, ztol=1.e-8, maxfun=500):

        # import useful objects
        from astropy import cosmology
        self._z_at_value    = cosmology.z_at_value

        # initialize cosmology metric
        if cosmo == None and kwargs == None:
            logger.error("Unable to initialize cosmology class. Both cosmology name and keyword arguments are None.")
            raise RuntimeError("Unable to initialize cosmology class. Both cosmology name and keyword arguments are None.")
        elif cosmo != None and kwargs == None:
            self.cosmo = cosmology.__dict__[cosmo]
        elif cosmo == None and kwargs != None:
            H0          = kwargs.get('H0',      70.)
            Om0         = kwargs.get('Om0',     0.3)
            Tcmb0       = kwargs.get('Tcmb0',   2.725)
            Neff        = kwargs.get('Neff',    3.04)
            m_nu        = kwargs.get('m_nu',    [0., 0., 0.])
            Ob0         = kwargs.get('Ob0',     None)
            self.cosmo  = cosmology.FlatLambdaCDM(H0, Om0, Tcmb0, Neff, m_nu, Ob0)
        else:
            logger.warning("Both cosmology name and keyword arguments are passed to Cosmology class. Ignoring keyword arguments.")
            self.cosmo = cosmology.__dict__[cosmo]

        # initialize arguments for z_at_value method
        self._zmax   = zmax
        self._zmin   = zmin
        self._ztol   = ztol
        self._maxfun = maxfun
Exemplo n.º 9
0
def ra_dec_z(x, v, cosmo=None):
    """
    Calculate ra, dec, and redshift for a mock assuming an observer placed at (0,0,0).
    
    Parameters
    ----------
    x: array_like
        Npts x 3 numpy array containing 3-d positions in Mpc/h units
    
    v: array_like
        Npts x 3 numpy array containing 3-d velocities of shape (N,3) in km/s
    
    cosmo: astropy.cosmology object, optional
        default is FlatLambdaCDM(H0=0.7, Om0=0.3)
    
    Returns
    -------
    ra: np.array
        right accession in radians
    dec: np.array
        declination in radians
    z: np.array
        redshift
    """

    #calculate the observed redshift
    if cosmo == None:
        cosmo = cosmology.FlatLambdaCDM(H0=0.7, Om0=0.3)
    c_km_s = c.to('km/s').value

    #remove h scaling from position so we can use the cosmo object
    x = x / cosmo.h

    #compute comoving distance from observer
    r = np.sqrt(x[:, 0]**2 + x[:, 1]**2 + x[:, 2]**2)

    #compute radial velocity
    ct = x[:, 2] / r
    st = np.sqrt(1.0 - ct**2)
    cp = x[:, 0] / np.sqrt(x[:, 0]**2 + x[:, 1]**2)
    sp = x[:, 1] / np.sqrt(x[:, 0]**2 + x[:, 1]**2)
    vr = v[:, 0] * st * cp + v[:, 1] * st * sp + v[:, 2] * ct

    #compute cosmological redshift and add contribution from perculiar velocity
    yy = np.arange(0, 1.0, 0.001)
    xx = cosmo.comoving_distance(yy).value
    f = interp1d(xx, yy, kind='cubic')
    z_cos = f(r)
    redshift = z_cos + (vr / c_km_s) * (1.0 + z_cos)

    #calculate spherical coordinates
    theta = np.arccos(x[:, 2] / r)
    phi = np.arccos(cp)  #atan(y/x)

    #convert spherical coordinates into ra,dec
    ra = phi
    dec = (np.pi / 2.0) - theta

    return ra, dec, redshift
def cosmo_create(cosmo_choice='WMAP7',
                 H0=100.,
                 Om0=0.25,
                 Ob0=0.04,
                 Tcmb0=2.7255):
    """
    Creates instance of the cosmology used throughout the project.

    Parameters
    ----------
    cosmo_choice : {'WMAP7', 'WMAP9', 'Planck', 'custom'} `str`
        The choice of cosmology for the project. This variable is set to
        'WMPA7' by default.

        Options:
            - WMAP5: Cosmology from WMAP-5 cosmology
            - WMAP7: Cosmology from WMAP-7 cosmology
            - WMAP9: Cosmology from WMAP-9 cosmology
            - Planck15: Cosmology from Plack-2015
            - custom: Custom cosmology. The user sets the default parameters.
    
    h: float, optional (default = 1.0)
        value for small cosmological 'h'.

    Returns
    ----------                  
    cosmo_model : `astropy.cosmology.core.FlatLambdaCDM`
        cosmology used throughout the project

    Notes
    ----------
    For more information regarding the choice of cosmology, the author is
    advised to read the docs: `http://docs.astropy.org/en/stable/cosmology/`
    """
    ## Choosing cosmology
    if (cosmo_choice == 'WMAP5'):
        cosmo_model = astrocosmo.WMAP5.clone(H0=H0)
    elif (cosmo_choice == 'WMAP7'):
        cosmo_model = astrocosmo.WMAP7.clone(H0=H0)
    elif (cosmo_choice == 'WMPA9'):
        cosmo_model = astrocosmo.WMAP9.clone(H0=H0)
    elif (cosmo_choice == 'Planck15'):
        cosmo_model = astrocosmo.Planck15.clone(H0=H0)
    elif (cosmo_choice == 'custom'):
        cosmo_model = astrocosmo.FlatLambdaCDM(H0=H0,
                                               Om0=Om0,
                                               Ob0=Ob0,
                                               Tcmb0=Tcmb0)
    ##
    ## Cosmological parameters
    cosmo_params = {}
    cosmo_params['H0'] = cosmo_model.H0.value
    cosmo_params['Om0'] = cosmo_model.Om0
    cosmo_params['Ob0'] = cosmo_model.Ob0
    cosmo_params['Ode0'] = cosmo_model.Ode0
    cosmo_params['Ok0'] = cosmo_model.Ok0

    return cosmo_model, cosmo_params
Exemplo n.º 11
0
    def default_model(cls, use_mcmc=False, cosmo=None):
        """ Pass back a default fN_model from Prochaska+14

        Tested against XIDL code by JXP on 09 Nov 2014

        Parameters
        ----------
        recalc : boolean, optional (False)
          Recalculate the default model
        use_mcmc : boolean, optional (False)
          Use the MCMC chain to generate the model
        write : boolean, optional (False)
          Write out the model
        cosmo : astropy.cosmology, optional
          Cosmology for some calculations
        """
        # Cosmology
        if cosmo is None:
            cosmo = cosmology.FlatLambdaCDM(H0=70, Om0=0.3)  # Adopted in P14
        else:
            warnings.warn(
                "Be careful here.  This fN model was derived with a specific cosmology."
            )

        if use_mcmc:
            from pyigm.fN import mcmc
            #raise ValueError("DEPRECATED")
            # MCMC Analysis (might put these on a website)
            chain_file = (os.environ.get('DROPBOX_DIR') +
                          'IGM/fN/MCMC/mcmc_spline_k13r13o13n12_8.fits.gz')
            outp = mcmc.chain_stats(chain_file)

            # Build a model
            NHI_pivots = [12., 15., 17.0, 18.0, 20.0, 21., 21.5, 22.]
            fN_model = cls(
                'Hspline',
                zmnx=(0.5, 3.0),
                pivots=NHI_pivots,
                cosmo=cosmo,
                param=dict(sply=np.array(
                    outp['best_p'].flatten())))  # fN_data['FN']).flatten()))
            #param=outp['best_p'])
        else:
            # Input the f(N) at z=2.4 from Prochaska+14
            fN_file = (pyigm_path + '/data/fN/fN_spline_z24.fits.gz')
            print('Using P14 spline values to generate a default model')
            print('Loading: {:s}'.format(fN_file))
            hdu = fits.open(fN_file)
            fN_data = hdu[1].data
            # Instantiate
            fN_model = cls('Hspline',
                           zmnx=(0.5, 3.0),
                           cosmo=cosmo,
                           pivots=np.array(fN_data['LGN']).flatten(),
                           param=dict(sply=np.array(fN_data['FN']).flatten()))
        # Return
        return fN_model
Exemplo n.º 12
0
    def __init__(self, Om=0.286, H0=69.3):

        astro_cosmo = cosmology.FlatLambdaCDM(H0=H0, Om0=Om)
        self._Or = cosmo.Onu0 + cosmo.Ogamma0
        self._sol = sol.value

        self._Om = Om
        self._H0 = H0

        self._Ode = 1 - self._Om - self._Or

        self._Om_Ode_sum = self._Om + self._Ode

        self._dh = self._sol * 1E-3 / self._H0
def test_ra_dec_z():
    """
    test ra_dec_z function
    """
    from astropy import cosmology
    cosmo = cosmology.FlatLambdaCDM(H0=0.7, Om0=0.3)

    ra, dec, z = ra_dec_z(x, v, cosmo=cosmo)

    assert len(ra) == N
    assert len(dec) == N
    assert len(z) == N
    assert np.all(ra < 2.0 * np.pi) & np.all(ra > 0.0), "ra range is incorrect"
    assert np.all(dec > -1.0 * np.pi / 2.0) & np.all(
        dec < np.pi / 2.0), "ra range is incorrect"
Exemplo n.º 14
0
def radecz_to_xyz(ra, dec, red, H0=70., Om0=0.3):
    ''' convert RA, Dec, z to x, y, z
     default cosmology consistent with Kauffmann et al. (2013) choices
    '''
    cosmos = astrocosmo.FlatLambdaCDM(H0=H0, Om0=Om0)
    r = cosmos.comoving_distance(red).value
    phi = ra
    theta = 90. - dec

    DRADEG = 180.0 / np.pi
    x = r * np.cos(phi / DRADEG) * np.sin(theta / DRADEG)
    y = r * np.sin(phi / DRADEG) * np.sin(theta / DRADEG)
    z = r * np.cos(theta / DRADEG)

    return np.array([x, y, z]).T
Exemplo n.º 15
0
    def calc_angular_diameter(self):
        "Create the data arrays which will be needed by plot_angular_diameter()"

        # In this case we just need to define the matter density
        # and hubble parameter at z=0.

        # Note the default units for the hubble parameter H0 are km/s/Mpc.
        # We will pass in a `Quantity` object with the units specified.

        cosmo = cosm.FlatLambdaCDM(H0=70 * u.km / u.s / u.Mpc, Om0=0.3)
        dist = cosmo.angular_diameter_distance(self.zvals)

        ages = np.array([13, 10, 8, 6, 5, 4, 3, 2, 1.5, 1.2, 1]) * u.Gyr
        ageticks = [z_at_value(cosmo.age, age) for age in ages]

        return dist, ages, ageticks
Exemplo n.º 16
0
    def run_prep(self):
        self.lcdm_bestfit = cosmo.FlatLambdaCDM(H0=hubble_const,
                                                Om0=omega_m_lcdm_bestfit)
        self.distmod_bestfit_lcdm = np.array(
            self.lcdm_bestfit.distmod(self.redshifts))
        if self.do_marg:
            self.m_bestfit_lcdm_marg = opt.fmin(
                chi2,
                x0=-19.,
                args=(self.redshifts, self.distmod_bestfit_lcdm,
                      self.redshifts_marg, self.distmod_marg,
                      self.sigmas_marg),
                xtol=0.005,
                disp=0)

        self.deltamu = self.get_deltamu()
def computeSDProfiles(halobase, mcmodel='diemer15'):

    #read 2D profile
    simprofile = readMXXLProfile.MXXLProfile('{}.radial_profile.txt'.format(halobase))
    simarea = np.pi*(simprofile.outer_radius**2 - simprofile.inner_radius**2)
    simdensity = simprofile.diff_mass / simarea  #M_sol / Mpc**2
    r_mpc = simprofile.median_radius

    #read halo mass
    sim_and_haloid = os.path.basename(halobase)
    tokens = sim_and_haloid.split('_')
    simid = '_'.join(tokens[:2])
    haloid = '_'.join(tokens[2:])

    #make sure cosmology always matches
    curcosmo = readMXXLProfile.cosmo
    nfwutils.global_cosmology.set_cosmology(curcosmo)
    cmc.matchCosmo()

    #compute Diemer SD prediction
    r_kpch = (r_mpc*1000*curcosmo.h)
    
    m200 = answers[simid][haloid]['m200'] #M_sol/h
    zcluster = answers[simid][haloid]['redshift']
    c200 = chc.concentration(m200, '200c', zcluster, model=mcmodel)

    diemer_profile = dk14prof.getDK14ProfileWithOuterTerms(M = m200, c = c200, z = zcluster, mdef = '200c')
    surfacedensity_func, deltaSigma_func = readAnalytic.calcLensingTerms(diemer_profile, np.max(r_kpch))
    convert_units = 1./(curcosmo.h*1e6) #M_sol / Mpc^2 -> diemer units
    diemer_surfacedensity = surfacedensity_func(r_kpch)/convert_units


    #add mean background density to Diemer prediction
    acosmo = astrocosmo.FlatLambdaCDM(curcosmo.H0, curcosmo.omega_m)
    density_units_3d = units.solMass / units.Mpc**3
    density_units_2d = units.solMass / units.Mpc**2
    back_density = (acosmo.Om(zcluster)*acosmo.critical_density(zcluster)).to(density_units_3d)
    print back_density
    back_sd_contrib = back_density*(200.*units.Mpc/curcosmo.h)
    print back_sd_contrib
#    simdensity = simdensity*density_units_2d - back_sd_contrib
    diemer_surfacedensity += back_sd_contrib

    return dict(radius=r_mpc,
                simprofile=simdensity,
                diemerprofile=diemer_surfacedensity)
Exemplo n.º 18
0
    def _get_cosmology():
        if cfg.get('misc'):
            if cfg["misc"].get('cosmology'):
                if cfg["misc"]["cosmology"] in cosmology.parameters.available:
                    cosmo = cosmology.default_cosmology.get_cosmology_from_string(
                        cfg["misc"]["cosmology"])
                elif isinstance(cfg["misc"]["cosmology"], dict):
                    par = cfg["misc"]["cosmology"]
                    try:
                        if par.get('flat'):
                            cosmo = cosmology.FlatLambdaCDM(
                                par['H0'],
                                par['Om0'],
                                Tcmb0=par.get('Tcmb0', 2.725),
                                Neff=par.get('Neff', 3.04),
                                m_nu=u.Quantity(
                                    par.get('m_nu', [0.0, 0.0, 0.0]), u.eV),
                                name=par.get("name", "user_cosmology"),
                                Ob0=par.get('Ob0', 0.0455),
                            )
                        else:
                            cosmo = cosmology.LambdaCDM(
                                par['H0'],
                                par['Om0'],
                                par['Ode0'],
                                Tcmb0=par.get('Tcmb0', 2.725),
                                Neff=par.get('Neff', 3.04),
                                m_nu=u.Quantity(
                                    par.get('m_nu', [0.0, 0.0, 0.0]), u.eV),
                                name=par.get("name", "user_cosmology"),
                                Ob0=par.get('Ob0', 0.0455),
                            )
                    except (KeyError, NameError) as e:
                        log(f'{e}')
                        log('exception in determining user defined cosmology')
                        cosmo = fallback_cosmology
                else:
                    log('cosmology: dont know how to deal with the user supplied cosmology'
                        )
                    cosmo = fallback_cosmology
        else:
            cosmo = fallback_cosmology

        log(f'using {cosmo.name} for cosmological calculations')
        log(f'{cosmo}')
        return cosmo
Exemplo n.º 19
0
def test_ra_dec_z():

    N = 100
    x = np.random.random((N, 3))
    v = np.random.random((N, 3)) * 0.1
    period = np.array([1, 1, 1])
    from astropy import cosmology
    cosmo = cosmology.FlatLambdaCDM(H0=0.7, Om0=0.3)

    ra, dec, z = ra_dec_z(x, v, cosmo=cosmo)

    assert len(ra) == N
    assert len(dec) == N
    assert len(z) == N
    assert np.all(ra < 2.0 * np.pi) & np.all(ra > 0.0), "ra range is incorrect"
    assert np.all(dec > -1.0 * np.pi / 2.0) & np.all(
        dec < np.pi / 2.0), "ra range is incorrect"
Exemplo n.º 20
0
def establish_cosmology(cfg=cfg):
    user_cosmo = cfg['misc']['cosmology']

    if user_cosmo in cosmology.realizations.available:
        cosmo = cosmology.default_cosmology.get_cosmology_from_string(
            user_cosmo)

    elif isinstance(user_cosmo, dict):
        try:
            if user_cosmo.get('flat'):
                cosmo = cosmology.FlatLambdaCDM(
                    user_cosmo['H0'],
                    user_cosmo['Om0'],
                    Tcmb0=user_cosmo.get('Tcmb0', 2.725),
                    Neff=user_cosmo.get('Neff', 3.04),
                    m_nu=u.Quantity(user_cosmo.get('m_nu', [0.0, 0.0, 0.0]),
                                    u.eV),
                    name=user_cosmo.get("name", "user_cosmology"),
                    Ob0=user_cosmo.get('Ob0', 0.0455),
                )
            else:
                cosmo = cosmology.LambdaCDM(
                    user_cosmo['H0'],
                    user_cosmo['Om0'],
                    user_cosmo['Ode0'],
                    Tcmb0=user_cosmo.get('Tcmb0', 2.725),
                    Neff=user_cosmo.get('Neff', 3.04),
                    m_nu=u.Quantity(user_cosmo.get('m_nu', [0.0, 0.0, 0.0]),
                                    u.eV),
                    name=user_cosmo.get("name", "user_cosmology"),
                    Ob0=user_cosmo.get('Ob0', 0.0455),
                )
        except (KeyError, NameError) as e:
            log(f'{e}')
            log('Exception while processing user-defined cosmology')
            raise RuntimeError('Error parsing user-defined cosmology')

    else:
        log(f"Invalid user-defined cosmology [{user_cosmo}]")
        log(f"Try setting it to one of [{cosmology.realizations.available}]")
        raise RuntimeError("No valid cosmology specified")

    log(f'Using {cosmo.name} for cosmological calculations')
    log(f'{cosmo}')

    return cosmo
Exemplo n.º 21
0
    def __init__(self, h=1., Om0=0.3, rlimit=4.0, vlimit=3500, kernal_stretch=10.0,
                 rgridmax=6.0, vgridmax=5000.0, cut_sample=True, edge_int_remove=False,
                 gapper=True, mirror=True, inflection=False, edge_perc=0.1, fbr=0.65):

        self.cosmo = cosmology.FlatLambdaCDM(H0=100. * h, Om0=Om0)
        self.rlimit = rlimit
        self.vlimit = vlimit
        self.kernal_stretch = kernal_stretch
        self.rgridmax = rgridmax
        self.vgridmax = vgridmax
        self.cut_sample = cut_sample
        self.edge_int_remove = edge_int_remove
        self.gapper = gapper
        self.mirror = mirror
        self.inflection = inflection
        self.edge_perc = edge_perc
        self.fbr = fbr
        self.r_range = np.arange(0, rgridmax, 0.05)
Exemplo n.º 22
0
def realize_SN_model(redshift, x1, c, MV, beta = 3.1, alpha = 0.13):
    sncosmo_model = sncosmo.Model(source="salt2-extended")
    cosmo = cosmology.FlatLambdaCDM(Om0 = 0.3, H0 = 70.)
    ten_pc_z = 2.33494867e-9
    assert abs(cosmo.distmod(z=ten_pc_z).value) < 1.e-3, "Distance modulus zeropoint wrong!"

    ampl = 1.
    for i in range(2):
        sncosmo_model.set(z=ten_pc_z, t0=0., x0=ampl, x1 = x1, c = c)

        mag = sncosmo_model.bandmag('bessellv', 'ab', 0.)
        print "mag, ampl ", mag, ampl
        ampl *= 10.**(0.4*(mag - (MV - alpha*x1 + beta*c)))
        
    mu = cosmo.distmod(redshift).value
    print "mu ", mu

    sncosmo_model.set(z=redshift, t0=0., x0 = ampl*10**(-0.4*mu))
    return sncosmo_model
Exemplo n.º 23
0
def set_cosmology(cosmology=None):
    """
    Get an instance of a astropy.cosmology.FLRW subclass.

    To avoid repeatedly instantiating the same class, test if it is the same
    as the last used cosmology.

    Parameters
    ==========
    cosmology: astropy.cosmology.FLRW, str, dict
        Description of cosmology, one of:
            None - Use DEFAULT_COSMOLOGY
            Instance of astropy.cosmology.FLRW subclass
            String with name of known Astropy cosmology, e.g., "Planck13"
            Dictionary with arguments required to instantiate the cosmology
            class.

    Returns
    =======
    cosmo: astropy.cosmology.FLRW
        Cosmology instance
    """
    from astropy import cosmology as cosmo
    _set_default_cosmology()
    if cosmology is None:
        cosmology = DEFAULT_COSMOLOGY
    elif isinstance(cosmology, cosmo.FLRW):
        cosmology = cosmology
    elif isinstance(cosmology, str):
        cosmology = cosmo.__dict__[cosmology]
    elif isinstance(cosmology, dict):
        if 'Ode0' in cosmology.keys():
            if 'w0' in cosmology.keys():
                cosmology = cosmo.wCDM(**cosmology)
            else:
                cosmology = cosmo.LambdaCDM(**cosmology)
        else:
            cosmology = cosmo.FlatLambdaCDM(**cosmology)
    COSMOLOGY[0] = cosmology
    if cosmology.name is not None:
        COSMOLOGY[1] = cosmology.name
    else:
        COSMOLOGY[1] = repr(cosmology)
def xi_2h_func(r, bias, z0):

    ## r : in unit of kpc / h
    R = r / 1e3
    cosmos = apcy.FlatLambdaCDM(
        H0=H0,
        Om0=Omega_m,
        Tcmb0=Test_model.Tcmb0,
        Neff=3.05,
        Ob0=Omega_b,
    )
    A_mode = DMHaloModel(rmin=R.min(),
                         rmax=R.max(),
                         rnum=len(R),
                         z=z0,
                         cosmo_model=cosmos)
    # xi_2h = A_mode.corr_2h_auto_matter
    xi_2h = A_mode.corr_linear_mm

    return xi_2h * bias
Exemplo n.º 25
0
    def _setup_astropy_cosmology(self, astropy_instance, cosmo_kwargs):

        if not hasattr(self, 'astropy'):

            if astropy_instance is None:

                astropy_kwargs = {}

                keys = ['H0', 'Om0', 'Ob0']
                for key in keys:
                    if key not in cosmo_kwargs.keys():
                        astropy_kwargs.update({key: cosmo_defaults(key)})
                    else:
                        astropy_kwargs.update({key: cosmo_kwargs[key]})

                astropy_instance = astropy_cosmo.FlatLambdaCDM(
                    **astropy_kwargs)

            self.astropy = astropy_instance

        return self.astropy
Exemplo n.º 26
0
def get_imaging_SN(PSFs, exp_time, effective_meters2_fl, wavemin = 4000, wavemax = 25000, waves = None, redshift=0, phase=0, gal_flamb = lambda x:0., pixel_scale = 0.11, IPC = 0.02, offset_i = 5, offset_j = 5, source_dir = "input", zodi_fl = "aldering.txt", mdl = "hsiao", dark_current = 0.015, TTel = 282., verbose = False, approximate_PSF = True):
    scale = int(round(pixel_scale/0.005))
    if waves == None:
        waves = arange(wavemin, wavemax, 50.) # These should span (and perhaps slightly overfill) the filter
        dwaves = ones(len(waves), dtype=float64)*50.
    else:
        dwaves = waves[1:] - waves[:-1]
        dwaves = concatenate(([dwaves[0]], dwaves))

    if hasattr(effective_meters2_fl, '__call__'):
        effective_meters2 = effective_meters2_fl
    else:
        effective_meters2 = interpfile(source_dir + "/" + effective_meters2_fl, bounds_error = False)

    if hasattr(zodi_fl, '__call__'):
        zodi_flamb = zodi_fl
    else:
        zodi_flamb = interpfile(source_dir + "/" + zodi_fl)


    
    read_noise = sqrt(5.**2 + 
                      3.* 20.**2. / (exp_time/11.3)
                 )
    if verbose:
        print "read_noise", read_noise

    try:
        model_len = len(mdl)
    except:
        model_len = None

    if mdl == 'hsiao':
        cosmo = cosmology.FlatLambdaCDM(Om0 = 0.3, H0 = 70.)
        ten_pc_z = 2.33494867e-9
        assert abs(cosmo.distmod(z=ten_pc_z).value) < 1.e-3, "Distance modulus zeropoint wrong!"


        model = sncosmo.Model(source=mdl)
        sntype = {"s11-2005hl": "Ib", "s11-2005hm": "Ib", "s11-2006fo": "Ic", "s11-2006jo": "Ib", "hsiao": "Ia", "nugent-sn1a": "Ia", "salt2-extended": "Ia"}[mdl]
        

        ampl = 1.
        for i in range(3):
            model.set(z=ten_pc_z, t0=0., amplitude=ampl)
            
            mag = model.bandmag('bessellv', 'ab', 0.)
            if verbose:
                print "mag, ampl ", mag, ampl
            ampl *= 10.**(0.4*(mag -- 19.08))

        model.set(z=redshift, t0=0., amplitude=ampl)

        mu = cosmo.distmod(redshift).value
        if verbose:
            print "mu ", mu
        
        f_lamb_SN = model.flux(phase*(1. + redshift), waves)*10.**(-0.4*mu)
        f_lamb_SN_at_max = model.flux(0., waves)*10.**(-0.4*mu)
    elif type(mdl) == types.FunctionType:
        f_lamb_SN = mdl(waves)
        f_lamb_SN_at_max = mdl(waves)
        sntype = "None"
    elif model_len == len(waves):
        f_lamb_SN = mdl
        f_lamb_SN_at_max = mdl
        sntype = "None"
    elif len(glob.glob(source_dir + "/" + mdl)) == 1:
        mdlfn = interpfl(source_dir + "/" + mdl)
        f_lamb_SN = mdlfn(waves)
        f_lamb_SN_at_max = mdlfn(waves)
        sntype = "None"

    if 0:
        plt_root = "z=%.2f_exp=%.1f_ph=%.1f_gal=%.1g_PSF=%s_pxsl=%.3f_mdl=%s_type=%s_%s" % (redshift, exp_time, phase, gal_flamb(15000.), PSFs["PSF_source"], pixel_scale, mdl, sntype, effective_meters2_fl)

    #if show_plots:
    #    writecol(output_dir + "/SN_template_" + plt_root + ".txt", transpose(array([arange(3000., 25001., 10.), model.flux(0., arange(3000., 25001., 10.))*10.**(-0.4*mu)])))
    photons_SN_per_secwave = flamb_to_photons_per_wave(f_lamb_SN, effective_meters2(waves), waves, dwaves)

    zodi_photons_per_pixsec = sum(flamb_to_photons_per_wave(10.**(zodi_flamb(waves)), effective_meters2(waves), waves, dwaves))*pixel_scale**2.
    if verbose:
        print "zodi_photons_per_pixsec ", effective_meters2_fl, zodi_photons_per_pixsec
    galaxy_photons_per_pixsec = sum(flamb_to_photons_per_wave(gal_flamb(waves), effective_meters2(waves), waves, dwaves))*pixel_scale**2.
    thermal_photons_per_pixsec = sum(get_thermal_background_per_pix_per_sec(waves, dwaves, pixel_scale = pixel_scale,
                                                                            TTel = TTel, throughput = effective_meters2(waves)/(pi*1.2**2.))
                                     )

    if verbose:
        print "thermal_photons_per_pixsec ", effective_meters2_fl, thermal_photons_per_pixsec

    if PSFs == None:
        PSFs = initialize_PSFs(scales = [int(round(pixel_scale/0.005))])
    if approximate_PSF:
        the_PSF = get_approximate_pixelized_broadband_PSF(PSFs, scale = int(round(pixel_scale/0.005)), waves = waves, weights = photons_SN_per_secwave/sum(photons_SN_per_secwave), IPC = IPC, offset_i = offset_i, offset_j = offset_j)
    else:
        the_PSF = get_pixelized_broadband_PSF(PSFs, scale = int(round(pixel_scale/0.005)), waves = waves, weights = photons_SN_per_secwave/sum(photons_SN_per_secwave), IPC = IPC, offset_i = offset_i, offset_j = offset_j)
        

    SN_photon_image = the_PSF*sum(photons_SN_per_secwave)*exp_time
    if verbose:
        print "SN photons/sec ", redshift, sum(photons_SN_per_secwave)
    zodi_image = ones(the_PSF.shape, dtype=float64)*zodi_photons_per_pixsec*exp_time
    gal_image = ones(the_PSF.shape, dtype=float64)*galaxy_photons_per_pixsec*exp_time
    thermal_image = ones(the_PSF.shape, dtype=float64)*thermal_photons_per_pixsec*exp_time
    dark_current_image = ones(the_PSF.shape, dtype=float64)*dark_current*exp_time

    total_image = SN_photon_image + zodi_image + gal_image + thermal_image + dark_current_image
    total_noise = sqrt(total_image + read_noise**2.)
    sim_weight = 1./total_noise**2.

    #print SN_photon_image
    #print the_PSF
    #print sim_weight
    #print "exp_time ", exp_time



    signal_to_noise = sum(SN_photon_image*the_PSF*sim_weight)/sqrt(sum(the_PSF**2. * sim_weight))
    ETC_results = {}
    ETC_results["PSF_phot_S/N"] = signal_to_noise
    ETC_results["SN_photons/s"] = sum(photons_SN_per_secwave)
    AB_0 = sum(flamb_to_photons_per_wave(0.10884806248/waves**2., effective_meters2(waves), waves, dwaves))
    ETC_results["AB_mag"] = -2.5*log10(ETC_results["SN_photons/s"]/AB_0)
    ETC_results["zodi/s"] = zodi_photons_per_pixsec
    ETC_results["thermal/s"] = thermal_photons_per_pixsec
    

    return ETC_results
import os

import pandas as pd
import numpy as np
import scipy.optimize as opt

import autolens as al
from astropy import cosmology

import matplotlib.pyplot as plt
from pathlib import Path
import matplotlib.cm as cm
from matplotlib import markers

M_o = 1.989e30
cosmo = cosmology.FlatLambdaCDM(H0=70, Om0=0.3)

lens_name = np.array([
    'slacs0008-0004',
    'slacs0330-0020',
    'slacs0903+4116',
    'slacs0959+0410',
    'slacs1029+0420',
    'slacs1153+4612',
    'slacs1402+6321',
    'slacs1451-0239',
    'slacs2300+0022',
    'slacs0029-0055',
    'slacs0728+3835',
    'slacs0912+0029',
    'slacs0959+4416',
Exemplo n.º 28
0
def IdentifyNeighbors(catalog,
                      primary_info=None,
                      del_v_cut=500.,
                      r_perp_cut=5.):
    ''' Identify the neighboring galaxies of primary galaxies in input 
    catalog dictionary using the same steps as for primaries but for different 
    sets of delta v and r_perp cut offs

    1. Run a KDTree and identify the neighbors within r_eff of the 'target' galaxy. 
        r_eff = sqrt( r_perp^2 + d_delv,max^2 )
    '''
    c_kms = 299792.458  # speed of light
    # same cosmology as Kauffmann et al. (2013)
    cosmo_kauff = astrocosmo.FlatLambdaCDM(H0=70., Om0=0.3)

    if 'primary' not in primary_info.keys():
        raise ValueError
    if len(primary_info['primary']) != len(catalog['z']):
        raise ValueError

    d_delv_max = cosmo_kauff.comoving_distance(del_v_cut / c_kms).value
    r_eff = np.sqrt(d_delv_max**2 + r_perp_cut**2)
    print 'd_delv_max', d_delv_max
    print 'KDTree radius', r_eff, 'Mpc'

    # loop through primaries and identify their neighbors
    is_primary = np.where(primary_info['primary'] == 1)[0]
    primary_xyz = catalog['xyz'][is_primary]

    # construct KDTree
    kd_time = time.time()
    cat_tree = scispace.KDTree(catalog['xyz'])
    primary_tree = scispace.KDTree(primary_xyz)

    kdt_neighbor_indices = primary_tree.query_ball_tree(cat_tree, r_eff)
    print 'KDTree takes ', time.time() - kd_time, ' seconds'

    neigh_indices = [[] for i in range(len(catalog['z']))
                     ]  # satellites of primary isolation criteria
    neigh_rperp = [[]
                   for i in range(len(catalog['z']))]  # r_perp of satellites
    n_neigh = np.zeros(len(catalog['z']))  # number of satellites
    for i_targ in range(len(is_primary)):
        # calculate Delta v between target and satellites
        del_v_pair = c_kms * (catalog['z'][is_primary[i_targ]] -
                              catalog['z'][kdt_neighbor_indices[i_targ]])

        # calculate r_perp
        targ_xyz = primary_xyz[i_targ]
        sat_xyz = catalog['xyz'][kdt_neighbor_indices[i_targ]]
        targ_mag = np.sum(targ_xyz**2)
        targ_dot_env = np.sum(targ_xyz * sat_xyz, axis=1)
        proj = targ_dot_env / targ_mag
        proj_xyz = np.array([proj[i] * targ_xyz for i in xrange(len(proj))])

        rll = np.sqrt(np.sum((proj_xyz - targ_xyz)**2, axis=1))
        rperp = np.sqrt(np.sum((proj_xyz - sat_xyz)**2, axis=1))

        # So that we don't count the target
        dr = np.sqrt(np.sum((sat_xyz - targ_xyz)**2, axis=1))

        keep_neigh = np.where((del_v_pair < del_v_cut) & (rperp < r_perp_cut)
                              & (dr > 0.))
        if len(keep_neigh[0]) < primary_info['n_secondary'][
                is_primary[i_targ]]:
            raise ValueError
        n_neigh[is_primary[i_targ]] = len(keep_neigh[0])
        neigh_rperp[is_primary[i_targ]] = list(rperp[keep_neigh])
        neigh_indices[is_primary[i_targ]] = list(
            np.array(kdt_neighbor_indices[i_targ])[keep_neigh])

    neigh_primary = np.repeat(-999, len(catalog['z']))
    neigh_primary[is_primary] = is_primary

    out_catalog = {}
    out_catalog['neighbor_primary'] = neigh_primary
    out_catalog['n_neighbor'] = n_neigh
    out_catalog['neighbor_rperp'] = neigh_rperp
    out_catalog['neighbor_indices'] = neigh_indices
    return out_catalog
Exemplo n.º 29
0
def IdentifyPrimaries(catalog, masses=None, del_v_cut=500., r_perp_cut=0.5):
    ''' Identify the primary galaxies in the input catalog dictionary using
    the following steps: 

    1. Run a KDTree and identify the neighbors within r_eff of the 'target' galaxy. 
        r_eff = sqrt( r_perp^2 + d_delv,max^2 )
        
    2. Go through each 'target' galaxy and cut by del v and r_perp and impose 
        stellar mass criteria to identify the primaries. 

    Notes
    -----
    '''
    c_kms = 299792.458  # speed of light
    # same cosmology as Kauffmann et al. (2013)
    cosmo_kauff = astrocosmo.FlatLambdaCDM(H0=70., Om0=0.3)

    # x, y, z coordinates of
    if 'xyz' not in catalog.keys():
        catalog['xyz'] = UT.radecz_to_xyz(catalog['ra'],
                                          catalog['dec'],
                                          catalog['z'],
                                          H0=cosmo_kauff.H0.value,
                                          Om0=cosmo_kauff.Om0)

    if masses is None:
        raise ValueError
    else:
        if len(masses) != len(catalog['z']):
            raise ValueError

    d_delv_max = cosmo_kauff.comoving_distance(del_v_cut / c_kms).value
    r_eff = np.sqrt(d_delv_max**2 + r_perp_cut**2)
    print 'd_delv_max', d_delv_max
    print 'KDTree radius', r_eff, 'Mpc'

    # construct KDTree
    kd_time = time.time()
    cat_tree = scispace.KDTree(catalog['xyz'])
    kdt_satellite_indices = cat_tree.query_ball_tree(cat_tree, r_eff)
    print 'KDTree takes ', time.time() - kd_time, ' seconds'

    sat_indices = [[] for i in range(len(catalog['z']))
                   ]  # satellites of primary isolation criteria
    sat_rperp = [[] for i in range(len(catalog['z']))]  # r_perp of satellites
    n_sat = np.zeros(len(catalog['z']))  # number of satellites
    isprimary = np.zeros(len(catalog['z']))
    for i_targ in range(len(catalog['z'])):
        # calculate Delta v between target and satellites
        del_v_pair = c_kms * (catalog['z'][i_targ] -
                              catalog['z'][kdt_satellite_indices[i_targ]])

        # calculate r_perp
        targ_xyz = catalog['xyz'][i_targ]
        sat_xyz = catalog['xyz'][kdt_satellite_indices[i_targ]]
        targ_mag = np.sum(targ_xyz**2)
        targ_dot_env = np.sum(targ_xyz * sat_xyz, axis=1)
        proj = targ_dot_env / targ_mag
        proj_xyz = np.array([proj[i] * targ_xyz for i in xrange(len(proj))])

        rll = np.sqrt(np.sum((proj_xyz - targ_xyz)**2, axis=1))
        rperp = np.sqrt(np.sum((proj_xyz - sat_xyz)**2, axis=1))

        # So that we don't count the target
        dr = np.sqrt(np.sum((sat_xyz - targ_xyz)**2, axis=1))

        # mass criteria
        M_targ = masses[i_targ]
        M_sat = masses[kdt_satellite_indices[i_targ]]
        #if not mpajhu:
        #    M_targ = catalog['mass'][i_targ]
        #    M_sat = catalog['mass'][kdt_satellite_indices[i_targ]]
        #else:
        #    M_targ = catalog['mass_tot_mpajhu'][i_targ]
        #    M_sat = catalog['mass_tot_mpajhu'][kdt_satellite_indices[i_targ]]

        keep_sat = np.where((del_v_pair < del_v_cut) & (rperp < r_perp_cut)
                            & (dr > 0.))

        if len(keep_sat[0]) > 0:
            n_sat[i_targ] = len(keep_sat[0])
            sat_rperp[i_targ] = list(rperp[keep_sat])
            sat_indices[i_targ] = list(
                np.array(kdt_satellite_indices[i_targ])[keep_sat])

            if M_sat[keep_sat].max() < (M_targ + np.log10(0.5)):
                # More massive than twice the most massive neighbor
                isprimary[i_targ] = 1
        else:
            # is by default a primary
            isprimary[i_targ] = 1

    out_catalog = {}
    out_catalog['n_secondary'] = n_sat
    out_catalog['secondary_rperp'] = sat_rperp
    out_catalog['secondary_indices'] = sat_indices
    out_catalog['primary'] = np.array(isprimary)
    print 'Identified ', np.sum(isprimary), ' primaries'
    return out_catalog
Exemplo n.º 30
0
def main():
    ###make sure to change these when running in a new enviorment!###############################
    #location of data directory
    filepath = '/scratch/dac29/data/Tinker_groups/mock_runs/4th_run/'
    #save data to directory...
    savepath = '/scratch/dac29/output/processed_data/tinker_groupcat/mock_runs/4th_run/custom_catalogues/'
    #############################################################################################
    i = int(sys.argv[1])
    catalogues = ['clf_groups_M19_1','clf_groups_M19_2','clf_groups_M19_3','clf_groups_M19_4',\
                  'clf_groups_M19_5','clf_groups_M19_6','clf_groups_M19_7']
    mocks      = ['Mr19_age_distribution_matching_mock_sys_empty_shuffle_satsys_shuffle',\
                  'Mr19_age_distribution_matching_mock_sys_empty_shuffle_satrel_shuffle',\
                  'Mr19_age_distribution_matching_mock_sys_empty_shuffle_cen_shuffle',\
                  'Mr19_age_distribution_matching_mock_sys_empty_shuffle',\
                  'Mr19_age_distribution_matching_mock',\
                  'Mr19_age_distribution_matching_mock_satsys_shuffle',\
                  'Mr19_age_distribution_matching_mock_cen_shuffle']
    catalogue = catalogues[i]
    mock = mocks[i]
    #############################################################################################

    #open group files
    names=['foo1','group_id','cen_id','group_mass','group_mass_prev','n_sat','l_tot',\
          'l_central','foo2','cen_ra','cen_dec','cen_cz','foo3','foo4']
    filename = catalogue + '.groups'
    groups = ascii.read(filepath+filename, delimiter='\s', names=names, \
                        data_start=0)
    groups = np.array(groups)
    print 'number of groups:', len(groups)

    #open the satellite probability files
    names = ['foo1','gal_id','group_id','cen_id','M_r','p_sat','lum','foo2','foo3','foo4',\
             'R_proj','d_gal','da_halo']
    filename = catalogue + '.prob'
    prob = ascii.read(filepath+filename, delimiter='\s', names=names, \
                      data_start=0)
    prob = np.array(prob)
    print 'nuber of galaxies:', len(prob)
    print 'number of satellites:', len(np.where(prob['p_sat'] > 0.5)[0])

    #open the index files
    names = ['foo1', 'ind', 'M_r']
    filename = catalogue + '.indx'
    indx = ascii.read(filepath+filename, delimiter='\s', names=names, \
                      data_start=0)
    indx = np.array(indx)
    print 'number of galaxies:', len(indx)
    #############################################################################################
    #open the radec mock
    filename = mock + '_radec_mock.dat'
    filepath = '/scratch/dac29/output/processed_data/hearin_mocks/custom_catalogues/'
    radec_mock = ascii.read(filepath + filename,
                            delimiter='\s',
                            Reader=ascii.Basic)
    #open the full mock
    filename = mock + '.hdf5'
    filepath = '/scratch/dac29/output/processed_data/hearin_mocks/custom_catalogues/'
    f = h5py.File(filepath + filename, 'r')
    full_mock = f.get(mock)
    print full_mock.dtype.names
    #############################################################################################

    #define some constants
    c = 299792.458  #km/s
    cosmo = cosmology.FlatLambdaCDM(H0=100, Om0=0.27)  #h=1
    Omega_m = 0.27
    z_upper_lim = 0.068
    z_lower_lim = 0.020

    #make group catalogue
    dtype=[('ID','>i8'),('k_1','>i8'),('k_2','>i8'),('RA','>f8'),('DEC','>f8'),('Z','>f8'),('red','>i8'),\
           ('M_u,0.1','>f8'),('M_g,0.1','>f8'),('M_r,0.1','>f8'),('M_i,0.1','>f8'),('M_z,0.1','>f8'),('MSTAR','>f8'),\
           ('GROUP_ID','>i8'),('MGROUP','>f8'),('ZGROUP','>f8'),('R200','>f8'),\
           ('CEN_IND','>i8'),('RANK','>i8'),('RPROJ','>f8'),('N_sat','>i8'),('N_sat_red','>i8'),('N_sat_blue','>i8'),\
           ('HALO_M','>f8'),('HALO_RANK','>i8')]
    dtype = np.dtype(dtype)
    data = np.recarray((len(indx), ), dtype=dtype)
    data.fill(-99.9)  #if no value is available, set = -99.9

    #calculate index into xyz mock
    ind_full_mock = radec_mock['k'][indx['ind'] - 1]
    #caclulate index into ra-dec mock file
    ind_radec_mock = indx['ind'] - 1

    #grab values from ra-dec mock
    data['k_1'] = ind_radec_mock
    data['ID'] = indx['ind']
    data['RA'] = radec_mock['ra'][ind_radec_mock]
    data['DEC'] = radec_mock['dec'][ind_radec_mock]
    data['Z'] = radec_mock['z'][ind_radec_mock]

    #grab values from xyz mock
    data['ID'] = full_mock['ID_halo'][ind_full_mock]
    data['M_g,0.1'] = full_mock['g-r'][ind_full_mock] + indx['M_r']
    data['M_r,0.1'] = indx['M_r']
    data['HALO_M'] = full_mock['M_host'][ind_full_mock]
    ind = np.where(full_mock['ID_host'][ind_full_mock] == -1)[0]
    data['HALO_RANK'][ind] = 1
    ind = np.where(full_mock['ID_host'][ind_full_mock] != -1)[0]
    data['HALO_RANK'][ind] = 0

    color = data['M_g,0.1'] - data['M_r,0.1']
    LHS = 0.7 - 0.032 * (data['M_r,0.1'] + 16.5)  #Weinmann 2006
    blue = np.where(color < LHS)[0]  #indices of blue galaxies
    red = np.where(color > LHS)[0]  #indicies of red galaxies

    #record color designation
    data['red'][red] = 1
    data['red'][blue] = 0

    #calculate the project seperation in units of kpc/h
    data['RPROJ'] = prob['d_gal'] * (180.0 / math.pi) * 60.0 * (
        cosmology.funcs.kpc_proper_per_arcmin(data['Z'], cosmo=cosmo))
    data['GROUP_ID'] = prob['group_id']

    group_ind = np.empty_like(data['GROUP_ID'])
    for i in range(0, len(data)):
        group_ind[i] = np.where(groups['group_id'] == data['GROUP_ID'][i])[0]
        group_id = data['GROUP_ID'][i]
        members = np.where(data['GROUP_ID'] == group_id)[0]
        central = np.where(
            data['M_r,0.1'][members] == min(data['M_r,0.1'][members]))[0][0]
        central = members[central]
        satellites = np.where(members != central)[0]
        satellites = members[satellites]
        #record rank
        data['RANK'][central] = 1
        data['RANK'][satellites] = 0
        #record number of satellites in the group
        data['N_sat'][members] = len(satellites)
        sat_red = np.where(np.in1d(satellites, red) == True)[0]
        data['N_sat_red'][members] = len(sat_red)
        sat_blue = np.where(np.in1d(satellites, blue) == True)[0]
        data['N_sat_blue'][members] = len(sat_blue)
        #record other group information
        data['CEN_IND'][members] = central
    #calculate group information
    data['MGROUP'] = np.log10(groups['group_mass'][group_ind])
    data['ZGROUP'] = groups['cen_cz'][group_ind] / (299792.458)
    data['R200'] = 258.1 * (10.0**data['MGROUP'] /
                            (10.0**12.0))**(1.0 / 3.0) * (Omega_m / 0.25)**(
                                1.0 / 3.0) * (1.0 + data['ZGROUP'])**(-1.0)

    print 'check:', min(data['Z']) >= z_lower_lim
    print 'check:', max(data['Z']) <= z_upper_lim

    #read in mass halo function
    filepath = '/scratch/dac29/fortran_code/mass_functions/'
    filename = 'Bolshoi_Massfunc.dat'
    names = ['dM', 'dn', 'nsum']
    dndM = ascii.read(filepath + filename,
                      delimiter='\s',
                      names=names,
                      data_start=0)
    dndM = np.array(dndM)

    #idenditify centrals and satellites
    centrals = np.where(data['RPROJ'] == 0)[0]
    satellites = np.where(data['RPROJ'] > 0)[0]

    #calculate group total r-band luminosities
    S_r = 4.64
    group_L = np.zeros((len(data), ), dtype=np.float)

    for i in range(0, len(centrals)):
        gal = np.where(data['GROUP_ID'] == data['GROUP_ID'][centrals[i]])[0]
        group_L[gal] = np.log10(
            np.sum(10.0**(solar_lum(data['M_r,0.1'][gal], S_r))))
    tot_lum = group_L[centrals]

    #calculate abundance matched masses for groups
    geo_f = 1.0 / 8.0  #gemoetric factor: spherical octant
    r_max = cosmology.funcs.comoving_distance(z_upper_lim,
                                              cosmo=cosmo).value  #in Mpc
    r_min = cosmology.funcs.comoving_distance(z_lower_lim,
                                              cosmo=cosmo).value  #in Mpc
    mock_volume = (4.0 / 3.0) * math.pi * (r_max**3.0 - r_min**3) * geo_f

    #caclulate the group luminosity function
    N_gal = np.cumsum(np.zeros(len(centrals)) +
                      1)  #cumulative number of groups
    n_gal = N_gal / mock_volume  #number density
    L_gal = np.sort(tot_lum)[::-1]  #group luminosity
    ind = np.argsort(tot_lum)[::-1]

    #integrate halo mass function
    n_halo = dndM['nsum'][::-1]  #cumulative number desnity
    M_halo = dndM['dM'][::-1]  #halo mass

    #interpolate the halo mass function
    x = np.log10(n_halo)
    y = M_halo
    f = interpolate.interp1d(x,
                             y,
                             kind='cubic',
                             bounds_error='False',
                             fill_value=0.0)

    data['MGROUP'][centrals[ind]] = f(np.log10(n_gal))

    for i in range(0, len(centrals)):
        gal = np.where(data['GROUP_ID'] == data['GROUP_ID'][centrals[i]])[0]
        data['MGROUP'][gal] = data['MGROUP'][centrals[i]]

    print 'saving hdf5 version of the catalogue...'
    filename = mock + '_clf_groups_M19'
    f = h5py.File(savepath + filename + '.hdf5', 'w')
    dset = f.create_dataset(filename, data=data)
    f.close()

    print 'saving ascii version of the catalogue...'
    filename = mock + '_clf_groups_M19'
    data_table = table.table.Table(data=data)
    ascii.write(data_table, savepath + filename + '.dat')
    print data_table