def __init__(self, snapnum, subhalos, mi, ci, RA_deg, DEC_deg, snapz,
                 galaxy_camera_posx, galaxy_camera_posy, galaxy_camera_posz,
                 centerz, galaxy_camera_velx, galaxy_camera_vely,
                 galaxy_camera_velz, cyl, gmag):

        #fields=['SubhaloMass','SubhaloMassInMaxRad','SubhaloMassInRadType','SubhaloMassInMaxRadType','SubhaloPos','SubhaloSFR','SubhaloSFRinRad','SubhaloVel','SubhaloBHMass','SubhaloBHMdot','SubhaloStellarPhotometrics','SubhaloWindMass']

        self.snapshot_number = snapnum + np.zeros_like(ci)

        self.subhalo_index = subhalos['SubFindID'][mi[ci]]

        self.RA_deg = RA_deg
        self.DEC_deg = DEC_deg

        self.snapz = snapz + np.zeros_like(RA_deg)
        self.center_z = centerz + np.zeros_like(RA_deg)

        self.cylinder_number = cyl + np.zeros_like(self.subhalo_index)

        self.galaxy_comoving_x_mpc = galaxy_camera_posx[ci] / 1000.0
        self.galaxy_comoving_y_mpc = galaxy_camera_posy[ci] / 1000.0
        self.galaxy_comoving_z_mpc = galaxy_camera_posz[ci] / 1000.0

        #self.galaxy_camera_posx = galaxy_camera_posx[ci]/1000.0
        #self.galaxy_camera_posy = galaxy_camera_posy[ci]/1000.0
        #self.galaxy_camera_posz = galaxy_camera_posz[ci]/1000.0

        self.galaxy_camera_velx = galaxy_camera_velx[ci]
        self.galaxy_camera_vely = galaxy_camera_vely[ci]
        self.galaxy_camera_velz = galaxy_camera_velz[ci]

        self.galaxy_peculiar_vr = 1.0 * self.galaxy_camera_velz
        self.galaxy_peculiar_z = 1.0 * self.galaxy_peculiar_vr / (
            astropy.constants.c.value / 1.0e3)

        self.cosmological_redshift = np.zeros_like(self.RA_deg)

        for i, index in enumerate(ci):
            self.cosmological_redshift[i] = np.float64(
                z_at_value(WMAP7.comoving_distance,
                           self.galaxy_comoving_z_mpc[i] * u.megaparsec,
                           ztol=1e-12,
                           maxfun=2000))

        self.hubble_velocity = self.cosmological_redshift * astropy.constants.c.value / 1.0e3  #in km/s

        self.galaxy_observed_z = 1.0 * self.cosmological_redshift + self.galaxy_peculiar_z

        #self.galaxy_comoving_x_mpc = self.galaxy_camera_posx*(1.0 + self.cosmological_redshift)
        #self.galaxy_comoving_y_mpc = self.galaxy_camera_posy*(1.0 + self.cosmological_redshift)
        #self.galaxy_comoving_z_mpc = self.galaxy_camera_posz*(1.0 + self.cosmological_redshift)

        self.galaxy_camera_posx = self.galaxy_comoving_x_mpc / (
            1.0 + self.cosmological_redshift)
        self.galaxy_camera_posy = self.galaxy_comoving_y_mpc / (
            1.0 + self.cosmological_redshift)
        self.galaxy_camera_posz = self.galaxy_comoving_z_mpc / (
            1.0 + self.cosmological_redshift)

        self.angdiam_mpc = np.asarray(
            WMAP7.angular_diameter_distance(self.cosmological_redshift))
        self.kpc_per_arcsec = np.asarray(
            WMAP7.kpc_proper_per_arcmin(self.cosmological_redshift) / 60.0)

        self.observed_angdiam_mpc = np.asarray(
            WMAP7.angular_diameter_distance(self.galaxy_observed_z))
        self.observed_comoving_mpc = np.asarray(
            WMAP7.comoving_distance(self.galaxy_observed_z))
        self.observed_kpc_per_arcsec = np.asarray(
            WMAP7.kpc_proper_per_arcmin(self.galaxy_observed_z) / 60.0)

        self.RA_kpc = self.RA_deg * 3600.0 * self.kpc_per_arcsec
        self.DEC_kpc = self.DEC_deg * 3600.0 * self.kpc_per_arcsec
        self.observed_RA_kpc = self.RA_deg * 3600.0 * self.observed_kpc_per_arcsec
        self.observed_DEC_kpc = self.DEC_deg * 3600.0 * self.observed_kpc_per_arcsec

        self.mstar_msun = subhalos['SubhaloMassInRadType'][self.subhalo_index,
                                                           4] * (1.0e10) / ilh
        self.mgas_msun = subhalos['SubhaloMassInRadType'][
            self.subhalo_index, 0] * (1.0e10) / ilh  #includes wind mass
        self.mbh_msun = subhalos['SubhaloMassInRadType'][self.subhalo_index,
                                                         5] * (1.0e10) / ilh
        self.mhalo_msun = subhalos['SubhaloMass'][self.subhalo_index] * (
            1.0e10) / ilh

        self.baryonmass_msun = self.mstar_msun + self.mgas_msun + self.mbh_msun  #within 2x stellar half mass radius... best?

        self.xpos_ckh = subhalos['SubhaloPos'][self.subhalo_index,
                                               0]  #in cKpc/h of max bound part
        self.ypos_ckh = subhalos['SubhaloPos'][self.subhalo_index, 1]
        self.zpos_ckh = subhalos['SubhaloPos'][self.subhalo_index, 2]

        self.xpos_pmpc = (self.xpos_ckh * 1.0 / (1.0 + snapz) / ilh) / 1.0e3
        self.ypos_pmpc = (self.ypos_ckh * 1.0 / (1.0 + snapz) / ilh) / 1.0e3
        self.zpos_pmpc = (self.zpos_ckh * 1.0 / (1.0 + snapz) / ilh) / 1.0e3

        self.xvel_kms = subhalos['SubhaloVel'][self.subhalo_index, 0]
        self.yvel_kms = subhalos['SubhaloVel'][self.subhalo_index, 1]
        self.zvel_kms = subhalos['SubhaloVel'][self.subhalo_index, 2]

        self.sfr = subhalos['SubhaloSFRinRad'][self.subhalo_index]
        self.bhmdot = subhalos['SubhaloBHMdot'][self.subhalo_index]

        self.gmag = subhalos['SubhaloStellarPhotometrics'][self.subhalo_index,
                                                           4]
        self.rmag = subhalos['SubhaloStellarPhotometrics'][self.subhalo_index,
                                                           5]
        self.imag = subhalos['SubhaloStellarPhotometrics'][self.subhalo_index,
                                                           6]
        self.zmag = subhalos['SubhaloStellarPhotometrics'][self.subhalo_index,
                                                           7]

        self.gmag_apparent = gmag[ci]

        #self.total_redshift =

        return
            i = np.random.randint(len(objs[div]))
            print(objs[div][i])
        if i % tenpct == 0:
            print(i, 'of', len(objs[div]))

        #Loop over the simulation projections
        for p in projection:
            #Load data, has to be reloaded for reasons (IDK)
            f = h5py.File(objs[div][i], 'r')

            px = 1.0
            #get redshift and determine effective resolution of EAGLE
            if high_z:
                while px / 3600 > 0.00011:
                    rand_idx = np.random.randint(len(table))
                    kpc_as = cosmo.kpc_proper_per_arcmin(
                        table[z_col][rand_idx]).to(u.kpc / u.arcsec)
                    px = (((60 * u.kpc) / kpc_as) /
                          256).value  #calculate pixel size at redshift
            else:
                rand_idx = np.random.randint(len(table))
                kpc_as = cosmo.kpc_proper_per_arcmin(
                    table[z_col][rand_idx]).to(u.kpc / u.arcsec)
                px = (((60 * u.kpc) / kpc_as) /
                      256).value  #calculate pixel size at redshift
            wcs_egl.wcs.cdelt = np.array([px / 3600,
                                          px / 3600])  #update eagle WCS

            obj_name.append(objs[div][i][len(path) + len(div) + 1:-len(extn)] +
                            p)
            obj_zsft.append(table[z_col][rand_idx])
Example #3
0
def calculate_Pr_center2(fname):
    cat = {}
    hdulist = pyfits.open(fname)
    tdata = hdulist[1].data
    print(hdulist[1].columns)
    # help(hdulist[1])
    print(tdata)
    cat['p_cen'] = tdata.field('p_cen')
    cat['z_lambda'] = tdata.field('z_lambda')
    cat['ra_cen'] = tdata.field('ra_cen')
    cat['dec_cen'] = tdata.field('dec_cen')
    print(cat['p_cen'])
    probs = []
    d_kpcs = []
    non_center_avg = []
    non_center_avg_prob = []
    correct_prop = []
    radii_to_save = []
    print("progress")
    for i in range(len(cat['z_lambda'])):
    # for i in range(1000):
        z = cat['z_lambda'][i]
        a = 1.0/(1.0+z)
        prob = cat['p_cen'][i]
        ra = cat['ra_cen'][i]
        dec = cat['dec_cen'][i]
        d_r = rad_dist2(ra, dec, ra[0], dec[0])
        d_kpc = cosmowmap7.kpc_proper_per_arcmin(z).value * (60.0*d_r)
        probs += list(prob)
        d_kpcs += list(d_kpc)
        ## Converting from physical kpc to comoving kpc/h
        ## 1.27323
        non_center_avg.append(np.average(d_kpcs[1:])*0.7/a)
        non_center_avg_prob.append(np.average(d_kpc[1:], weights=prob[1:])*0.7/a)
        correct_prop.append(prob[0])
        radii_to_save.append(select_radius(prob, d_kpc)*0.7/a)
        if i%1000 == 0:
            print('\t{:.3f}\r'.format(i/len(cat['z_lambda'])), end='')
    print()
    save_radii('data/Pr_center/distribution.hdf5', np.array(radii_to_save)/1000)
    save_radii('data/Pr_center/distribution.0mpc.hdf5', np.zeros_like(radii_to_save))
    save_radii('data/Pr_center/distribution.1mpc.hdf5', np.ones_like(radii_to_save))
    probs = np.array(probs)
    d_kpcs = np.array(d_kpcs)
    
    # probs[d_kpcs>0.2] = 0
    plt.figure()
    plt.hist(probs, bins=np.linspace(0,1,100))
    plt.yscale('log')
    
    plt.figure()
    plt.hist(d_kpcs/1000.0, bins=64, histtype='step', label='unweighted')
    plt.hist(d_kpcs/1000.0, bins=64, weights=probs, histtype='step', label='weighted')
    plt.ylabel('Counts')
    plt.xlabel('Clustering Miscentering Distance\n[h$^{-1}$ Mpc Comoving]')
    plt.legend(framealpha=0.0)
    # plt.yscale('log')
    plt.tight_layout()

    num_clusters = len(cat['z_lambda'])
    avg_displaced = np.sum((probs*d_kpcs))/num_clusters
    print("Average Miscentering: {:2f} [comoving kpc/h]".format(avg_displaced))
    print("Average Distance when Miscentered: {:.2f} [comoving kpc/h]".format(np.average(non_center_avg_prob)))
    print("Miscentering Prob: {:.2f}".format(np.average(correct_prop)))