Пример #1
0
def me(theta_Es, e_theta_Es, zlenses, zsources):

    ntarg = theta_Es.size
    M_E = numpy.zeros(ntarg)
    e_M_E = numpy.zeros(ntarg)
    vdisp = numpy.zeros(ntarg)
    e_vdisp = numpy.zeros(ntarg)

    for targ in numpy.arange(ntarg):

        zsource = zsources[targ]
        zlens = zlenses[targ]
        theta_E = theta_Es[targ]
        e_theta_E = e_theta_Es[targ]

        # luminosity distances
        d_LS = cosmo.luminosity_distance(zsource).value * 3.08e24
        d_LL = cosmo.luminosity_distance(zlens).value * 3.08e24

        # comoving distances
        d_MS = d_LS / (1 + zsource)
        d_ML = d_LL / (1 + zlens)

        # angular diameter distances
        d_ALS = 1 / (1 + zsource) * ( d_MS - d_ML )
        d_AL = d_LL / (1 + zlens)**2
        d_AS = d_LS / (1 + zsource)**2

        # einstein radius in cm (7.1 kpc/" at z=0.7)
        theta_E_cm = theta_E / 206265. * d_AL
        e_theta_E_cm = e_theta_E / 206265. * d_AL

        # get a distribution of Einstein radii
        niters = 1e3
        theta_E_iters = numpy.random.normal(loc = theta_E_cm, \
                scale = e_theta_E_cm, size = niters)

        # compute the mass enclosed within the Einstein radius
        c = 3e10
        G = 6.67e-8
        sigma_crit = c**2 / 4 / pi / G * d_AS / d_AL / d_ALS
        M_E_iters = pi * sigma_crit * theta_E_iters**2 / 2e33
        M_E[targ] = numpy.mean(M_E_iters)
        e_M_E[targ] = numpy.std(M_E_iters)

        vdisp2 = theta_E_iters / d_AL / 4 / pi * c**2 * d_AS / d_ALS
        vdisp[targ] = numpy.mean(numpy.sqrt(vdisp2) / 1e5)
        e_vdisp[targ] = numpy.std(numpy.sqrt(vdisp2) / 1e5)

    return M_E, e_M_E, vdisp, e_vdisp
Пример #2
0
def cosmoDL(redshift,
            WMAP9=False,
            H0=70.0,
            Om0=0.30,
            Planck15=False,
            kpc=False):
    """
    Get the Luminosity Distance at redshift=z.

    This is simply a wrapper of astropy.cosmology
    The input redsfhit can be an array
    """
    if WMAP9:
        from astropy.cosmology import WMAP9 as cosmo
    elif Planck15:
        from astropy.cosmology import Planck15 as cosmo
    else:
        from astropy.cosmology import FlatLambdaCDM
        cosmo = FlatLambdaCDM(H0=H0, Om0=Om0)

    dl = cosmo.luminosity_distance(redshift)

    if not kpc:
        return dl.value
    else:
        return dl.to(u.kpc).value
Пример #3
0
    def get_spectrum(self, outwave=None, filters=None, component=-1, **params):
        """
        """
        # Spectrum in Lsun/Hz per solar mass formed, restframe
        wave, spectrum, mfrac = self.get_galaxy_spectrum(**params)

        # Redshifting + Wavelength solution
        # We do it ourselves.
        a = 1 + self.params.get('zred', 0)
        af = a
        b = 0.0

        if 'wavecal_coeffs' in self.params:
            x = wave - wave.min()
            x = 2.0 * (x / x.max()) - 1.0
            c = np.insert(self.params['wavecal_coeffs'], 0, 0)
            # assume coeeficients give shifts in km/s
            b = chebval(x, c) / (lightspeed*1e-13)

        wa, sa = wave * (a + b), spectrum * af  # Observed Frame
        if outwave is None:
            outwave = wa

        # Observed frame photometry, as absolute maggies
        if filters is not None:
            # Magic to only do filter projections for unique filters, and get a
            # mapping back into this list of unique filters
            # note that this may scramble order of unique_filters
            fnames = [f.name for f in filters]
            unique_names, uinds, filter_ind = np.unique(fnames, return_index=True, return_inverse=True)
            unique_filters = np.array(filters)[uinds]
            mags = getSED(wa, lightspeed/wa**2 * sa * to_cgs, unique_filters)
            phot = np.atleast_1d(10**(-0.4 * mags))
        else:
            phot = 0.0

        # Distance dimming and unit conversion
        zred = self.params.get('zred', 0.0)
        if (zred == 0) or ('lumdist' in self.params):
            # Use 10pc for the luminosity distance (or a number
            # provided in the dist key in units of Mpc)
            dfactor = (self.params.get('lumdist', 1e-5) * 1e5)**2
        else:
            lumdist = cosmo.luminosity_distance(zred).value
            dfactor = (lumdist * 1e5)**2

        # Spectrum will be in maggies
        sa *= to_cgs / dfactor / (3631*jansky_cgs)

        # Convert from absolute maggies to apparent maggies
        phot /= dfactor

        # Mass normalization
        mass = np.atleast_1d(self.params['mass'])
        mass = np.squeeze(mass.tolist() + [mass.sum()])

        sa = (sa * mass[:, None])
        phot = (phot * mass[:, None])[component, filter_ind]

        return sa, phot, mfrac
def vel_kms_to_masyr(vel=1000.0, z=1.0):

    vel = vel * u.kilometer / u.second

    vel_mas_per_year = (vel.to(u.kpc / u.yr) *
                        cosmo.arcsec_per_kpc_proper(z).to(u.mas / u.kpc))
    if z < 0.1:
        d = cosmo.angular_diameter_distance(z).to(u.kpc)
        loc_vel_mas_per_year = ((vel.to(u.kpc / u.yr) / d) * (u.rad)).to(
            u.mas / u.yr)

    #table 6 says 0.7 mas is max resolution, for 93GHz band

    print("D_A (z=%g) = " % z, cosmo.angular_diameter_distance(z))
    print("D_L (z=%g) = " % z, cosmo.luminosity_distance(z))
    print("arcsec/kpc (z=%g) =" % z, cosmo.arcsec_per_kpc_proper(z))
    print("mas/kpc (z=%g) =" % z,
          cosmo.arcsec_per_kpc_proper(z).to(u.mas / u.kpc))
    print("physical res for 10 mas (z=%g) = " % z,
          10 * u.mas / (cosmo.arcsec_per_kpc_proper(z).to(u.mas / u.pc)))
    print("vel [km/s] = %g" % vel.to_value())
    print("vel [km/s] =", vel)
    print("vel [pc/Myr] =", vel.to(u.pc / u.Myr))
    print(vel)
    print("vel [kpc/yr] =", vel.to(u.kpc / u.yr))
    print("vel [mas/yr] = ", vel_mas_per_year)
    if z < 0.1: print("loc vel [mas/yr] = ", loc_vel_mas_per_year)
    print("motion in 10 yr lifetime = ", vel_mas_per_year * 10 * u.yr)

    return vel_mas_per_year
Пример #5
0
def Strain_Conv(source, natural_f, natural_h):
    """Converts frequency and strain in natural units (G=c=1) to Hertz and dimensionless, respectively.

    Parameters
    ----------
    source
        Instance of gravitational wave source class
    natural_f : array [Mf]
        the frequency of the source in natural units (G=c=1)
    natural_h : array [Mf]
        the strain of the source in natural units (G=c=1)

    """
    DL = cosmo.luminosity_distance(source.z)
    DL = DL.to('m')

    m_conv = const.G / const.c**3  #Converts M = [M] to M = [sec]
    M_redshifted_time = source.M.to('kg') * (1 + source.z) * m_conv

    #frequency and strain of source in detector frame
    freq_conv = 1 / M_redshifted_time
    #Normalized factor to match Stationary phase approx at low frequencies?
    #Changed from sqrt(5/16/pi)
    strain_conv = np.sqrt(
        1 / 4 / np.pi) * (const.c / DL) * M_redshifted_time**2

    f = natural_f * freq_conv
    h_f = natural_h * strain_conv
    return [f, h_f]
Пример #6
0
 def findDist(self, **kwargs: {'calc': False}):
     try:
         if kwargs.get('calc') == True:
             #c=299792.458* u.km / u.second
             dh = const.c / cosmo.H(0)
             om = 0.27
             ok = 1 - om
             oa = 0
             #E=lambda x: sqrt(om*(1+x)**3+ok*(1+x)**2+oa)
             dc = dh * integrate.quad(
                 lambda x: 1 / (sqrt(om * (1 + x)**3 + ok *
                                     (1 + x)**2 + oa)), 0, self.z)
             dm = dh * (1 / sqrt(ok)) * (
                 (np.exp(sqrt(ok) * (dc / dh)) - np.exp(-sqrt(ok) *
                                                        (dc / dh))) / 2)
             dl = dm * (1 + self.z)
             return dl
         else:
             a = cosmo.luminosity_distance(self.z)  #.value * u.Mpc
             return a
     except:
         s3 = 'The findDist() function failed for plate-ifu: ' + self.s
         log.error(s3)
         print(s3)
         return 0.0
 def __init__(self, name, z, x, y):
     self.name = name
     self.z = z
     self.x = x
     self.y = y
     self.lDcm = cosmo.luminosity_distance(self.z)*u.Mpc.to(u.cm) / u.Mpc
     self.radToKpc = conv.arcsec_per_kpc_proper(self.z)*0.05/u.arcsec*u.kpc
Пример #8
0
def calculate_Ke_marscher(nu_ssa_ghz, theta_obs_mas,
                          flux_extrapol_nu_ssa_obs_jy, delta, z, p):
    """
    Calculate Ke using (3) from Marscher 1983 (1983ApJ...264..296M).

   :param nu_ssa_ghz:
        Frequency of observation (it is SSA frequency for radio core).
    :param theta_obs_mas:
        Width of the VLBI core (see paper for discussion).
    :param flux_extrapol_nu_ssa_obs_jy:
        Core flux density extrapolated from optical thin spectrum [Jy].
    :param delta:
        Doppler factor.
    :param z:
        Redshift.
    :param p:
        Exponent of particles energy distribution. Must be 1.5, 2.0, 2.5 or 3.

    :return:
        Value of B [G].
    """
    if p not in (2.5, 3.0, 3.5):
        raise Exception("p must be 1.5, 2.0, 2.5 or 3")
    alpha = (p - 1.0) / 2
    n = {0.25: 7.9, 0.5: 0.27, 0.75: 0.012, 1.0: 0.00059}
    D_L_Gpc = WMAP9.luminosity_distance(z).to(u.Gpc).value
    return n[alpha] * D_L_Gpc**(-1) ** theta_obs_mas**(-4*alpha-7.0) *\
           nu_ssa_ghz**(-4*alpha-5.0) * flux_extrapol_nu_ssa_obs_jy**(2*alpha+3.0) *\
           (1+z)**(2*alpha+6.0) * delta*(-2*alpha-4.0)
Пример #9
0
    def __init__(self, lcspath):

        #self.jmass=fits.getdata(lcspath+'tables/LCS_Spirals_all_fsps_v2.4_miles_chab_charlot_sfhgrid01.fits')
        # use jmass.mstar_50 and jmass.mstar_err

        #self.agc=fits.getdata(lcspath+'tables/LCS_Spirals_AGC.fits')

        self.s = fits.getdata(lcspath + 'tables/LCS_all_size.fits')

        self.gim2d = fits.getdata(lcspath + 'tables/LCS_all.gim2d.tab1.fits')

        # dictionary to look up galaxies by NSAID
        self.nsadict = dict(
            (a, b) for a, b in zip(self.s.NSAID, np.arange(len(self.s.NSAID))))
        self.NUVr = self.s.ABSMAG[:, 1] - self.s.ABSMAG[:, 4]
        self.upperlimit = self.s[
            'RE_UPPERLIMIT']  # converts this to proper boolean array

        self.MAG24 = 2.5 * np.log10(3631. / (self.s.FLUX24 * 1.e-6))

        self.dL = cosmo.luminosity_distance(self.s.ZDIST)
        self.distmod_ZDIST = 5 * np.log10(self.dL.value * 1.e6) - 5
        self.ABSMAG24 = self.MAG24 - self.distmod_ZDIST
        self.NUV24 = self.s.ABSMAG[:, 1] - self.ABSMAG24
        if __name__ != '__main__':
            self.setup()
        self.logstellarmass = self.s.MSTAR_50
        self.clusterflag = (self.s.CLUSTER == b'Coma') | (
            self.s.CLUSTER == b'A2063'
        )  # | (self.s.CLUSTER == b'Hercules') | (self.s.CLUSTER == b'A1367')  # | (self.s.CLUSTER == b'A2052')
Пример #10
0
def get_catalog(params, map_struct):

    cat, = Vizier.get_catalogs('J/ApJS/199/26/table3')

    completeness = 0.5
    alpha = -1.0
    MK_star = -23.55
    MK_max = MK_star + 2.5 * np.log10(gammaincinv(alpha + 2, completeness))

    z = (u.Quantity(cat['cz']) / c.c).to(u.dimensionless_unscaled)
    MK = cat['Ktmag'] - cosmo.distmod(z)
    keep = (z > 0) & (MK < MK_max)

    cat = cat[keep]
    z = z[keep]

    r = cosmo.luminosity_distance(z).to('Mpc').value
    theta = 0.5 * np.pi - cat['DEJ2000'].to('rad').value
    phi = cat['RAJ2000'].to('rad').value
    ipix = hp.ang2pix(map_struct["nside"], theta, phi)

    if "distnorm" in map_struct:
        dp_dV = map_struct["prob"][ipix] * map_struct["distnorm"][ipix] * norm(
            map_struct["distmu"][ipix],
            map_struct["distsigma"][ipix]).pdf(r) / map_struct["pixarea"]
        top50 = cat[np.flipud(np.argsort(dp_dV))][:50]
    else:
        top50 = cat[np.flipud(np.argsort(map_struct["prob"][ipix]))][:50]

    catalogfile = os.path.join(params["outputDir"], 'catalog.dat')
    top50['RAJ2000', 'DEJ2000', 'Ktmag'].write(catalogfile, format='ascii')

    return top50
Пример #11
0
def generate_mass_estimates(galaxy_names, info_table, mini_tot_values,
                            mcor_tot_values):
    """Return a table containing the galaxy names, redshifts, present masses,
    and processed solar masses over a galaxy's lifetime

    Arguments:
        galaxy_names    (list) : A list of all galaxy names' whose mass must be
                                 calculated
        info_table      (table): The data table with PISCO galaxy information
        mini_tot_values (list) : A list of mini_tot values needed to calculate the processed solar
                                 masses over a galaxy's lifetime
        mcor_tot_values (list) : A list of mcor_tot values needed to calculate the present solar
                                 masses in a galaxy

    Returns:
        A table containing the galaxy names, redshifts, present masses, and
        processed solar masses over a galaxy's lifetime
        """

    # solar_luminosity units : erg / sec
    solar_luminosity = 3.826e33

    redshifts = []
    all_present_galaxy_mass = []
    all_processed_solar_masses = []

    for i, name in enumerate(galaxy_names):
        redshift = np.asarray(info_table['z'])[np.where(
            np.asarray(info_table['GALname']) == name)[0]]
        luminosity_distance = cosmo.luminosity_distance(
            redshift) * 3.0857e24  # Unit: cm

        # mini_tot_values units        : cm^2 * sec * solar mass / erg
        # mcor_tot_values units        : cm^2 * sec * solar mass / erg
        # present_galaxy_mass units    : solar mass
        # processed_solar_masses units : solar mass
        present_galaxy_mass = mcor_tot_values[i] * 4 * np.pi * (
            luminosity_distance**2) / solar_luminosity
        processed_solar_masses = mini_tot_values[i] * 4 * np.pi * (
            luminosity_distance**2) / solar_luminosity

        redshifts.append(redshift[0])
        all_present_galaxy_mass.append('%.3E' %
                                       Decimal(present_galaxy_mass[0].value))
        all_processed_solar_masses.append(
            '%.3E' % Decimal(processed_solar_masses[0].value))

    table = Table(
        {
            'Galaxy Name': galaxy_names,
            'Redshift': redshifts,
            'STARLIGHT Present Galaxy Mass': all_present_galaxy_mass,
            'STARLIGHT Processed Solar Masses': all_processed_solar_masses
        },
        names=[
            'Galaxy Name', 'Redshift', 'Present Galaxy Mass',
            'Processed Solar Masses'
        ])

    return table
Пример #12
0
def cosmoDL(redshift,
            WMAP9=False,
            H0=70.0,
            Om0=0.30,
            Planck15=False,
            kpc=False):
    """
    Get the Luminosity Distance at redshift=z.

    This is simply a wrapper of astropy.cosmology
    The input redsfhit can be an array
    """
    if WMAP9:
        from astropy.cosmology import WMAP9 as cosmo
    elif Planck15:
        from astropy.cosmology import Planck15 as cosmo
    else:
        from astropy.cosmology import FlatLambdaCDM
        cosmo = FlatLambdaCDM(H0=H0, Om0=Om0)

    dl = cosmo.luminosity_distance(redshift)

    if not kpc:
        return dl.value
    else:
        return dl.to(u.kpc).value
Пример #13
0
def Templates(i):
    print("galaxy", i + 1, "/", len(comb), comb[i])
    ###GALAXY GENERATION
    t0 = time.time()

    ind = comb[i][0]  #Sersic Index n
    b = get_bn(ind)  #Normalisation Based on n
    dx = comb[i][1]
    dy = comb[i][2]
    ell = comb[i][3]
    a = comb[i][4]
    Re = comb[i][5] * u.pc  #size of galaxy half light
    Re = Re.to(u.Mpc)
    ReAng = (
        (Re * (1 + Z)**2) / cosmo.luminosity_distance(Z)
    ) * 57.2958 * 3600  #size of galaxy in arcsec (converting from rad) via angular distance
    galhalflight = ReAng / res
    headers = '%s' % comb[i]
    stamps = makeSersic(ind, b, galhalflight, ell, a, stampsize, -dy, -dx)
    Galaxy = stamps
    np.savetxt('GalaxyTemplates/%s_%s.txt' % (name, i + 1),
               Galaxy,
               header=headers)
    #tot+=1
    t1 = time.time()
    t01 = time.time()
    print("time taken", t1 - t0)
    tottime = t01 - t00
    print(i, "galaxies in", tottime, "total time")
Пример #14
0
def Mlim(z, M2L):
    """
    Given some mass-to-light ratio, M2L, and redshift, z, use the flux_limit=10**-16 erg/cm2/s defined above to determine the corresponding stellar mass limit
    """
    Mlimit = (4. * np.pi * (cosmo.luminosity_distance(z).value * Mpc)**2 *
              flux_limit * M2L) / Msol

    return Mlimit
Пример #15
0
def int_gb(x,T,N_y,N_gb,alpha,z):
    pi=scipy.constants.pi
    d_l = cosmo.luminosity_distance(z)
    d_l =  d_l.to(u.m)
    L_ir = 4*pi*(d_l**2)*integrate.quad(gb_model,8e-6,1000e-6,args=(T,N_y,N_gb,alpha))
    # Unit conversion: from Jy Hz m^2, to W, to erg/s:
    L_ir = L_ir*1e26*1e-7
    return L_ir
Пример #16
0
 def findLum(self, **kwargs: {'use_mRatio': False, 'nod': False}):
     #Might have to convert distance to something else
     try:
         if kwargs.get('nod') == True:
             f = cosmo.luminosity_distance(self.z).value * u.Mpc
             x = f.to(u.cm)
             if self.efail:
                 return 0.0
             fl = self.fluxFind()
             if fl == 0.0:
                 return 0.0
             if fl == 'Fail':
                 return 'Fail'  # * u.erg / (u.cm ** 2 * u.second)
             L = fl * 4 * pi * x**2
             return L.value
         if kwargs.get('use_mRatio') == True:
             f = cosmo.luminosity_distance(self.z)  #.value * u.Mpc
             x = f.to(u.cm)
             if self.efail:
                 return 0.0
             fl = self.fluxFind(use_mRatio=True) * u.erg / (u.cm**2 *
                                                            u.second)
             if fl == 0.0:
                 return 0.0
             if fl == 'Fail':
                 return 'Fail'
             L = fl * 4 * pi * x**2
             return L
         else:
             f = cosmo.luminosity_distance(self.z)  #.value * u.Mpc
             x = f.to(u.cm)
             if self.efail:
                 return 0.0
             fl = self.fluxFind()  #* u.erg / (u.cm ** 2 * u.second)
             if fl == 0.0:
                 return 0.0
             if fl == 'Fail':
                 return 'Fail'
             L = fl * 4 * pi * x**2
             return L
     except:
         es = 'The findLum() function failed for plate-ifu: ' + self.s
         print(es)
         log.error(es)
         return 0.0
Пример #17
0
def mag_in_z(spec, z, f):
    d_L = cosmo.luminosity_distance(z).to('cm')
    k_cosmo = units.Lsun.to('erg/s') / (4 * np.pi * np.power(d_L, 2))

    model_spec = zcor(spec, z)
    model_spec['flux'] *= k_cosmo
    x = spec2filterset(f.filterset, model_spec)

    return x
Пример #18
0
def cal_distance_p_from_powerlaw(distance,
                                 peakredshift=0.1,
                                 powerlawindex=-0.3):

    from astropy.cosmology import WMAP9 as cosmo
    peakd = cosmo.luminosity_distance(
        peakredshift).value  ##returned value is in Mpc
    pdis = np.abs(peakd - distance)**powerlawindex
    return pdis
Пример #19
0
def Strain_Conv(
    source, freqs, strain, inverse=False, in_frame="source", out_frame="observer"
):
    """Converts frequency and strain in natural units (G=c=1) to Hertz and raw Fourier strain amplitude (1/Hertz), respectively.
    If inverse is true, it does the reverse and assumes the strain and frequency are given in the detector frame.

    Parameters
    ----------
    source
        Instance of gravitational wave source class
    freqs: array
        the frequency of the source in either natural units (G=c=1) or Hertz
    strain: array
        the strain of the source in natural units (G=c=1) or raw Fourier strain amplitude (1/Hertz)
    inverse: bool, optional
        Converts non-naturalized (Hertz and dimensionless) frequency and strain to ``Mf`` and strain in G=c=1 units
    in_frame: str, {'source','observer'}
        If inverse is true, determines whether the source frequency ``f_gw`` is in the source or observer frame.
    out_frame: str, {'observer','source'}
        Determines whether the returned frequency is in the source or observer frame.
    """
    DL = cosmo.luminosity_distance(source.z)
    DL = DL.to("m")

    m_conv = const.G / const.c ** 3  # Converts M = [M] to M = [sec]
    M_time = source.M.to("kg") * m_conv
    # M_redshifted_time = source.M.to("kg") * (1 + source.z) * m_conv

    # frequency and strain of source in source frame
    freq_conv = 1 / M_time
    # Normalized factor to match Stationary phase approx at low frequencies
    strain_conv = np.sqrt(5 / 24 / np.pi) * (const.c / DL) * M_time ** 2
    if inverse:
        if in_frame == "source":
            # converted to source frame natural units
            conv_freqs = freqs / freq_conv
            conv_strain = strain / strain_conv
        elif in_frame == "observer":
            # converted to source frame natural units
            conv_freqs = (freqs / freq_conv) * (1 + source.z)
            conv_strain = strain / strain_conv / (1 + source.z) ** 2
        else:
            raise ValueError("The reference frame can only be observer or source.")
    else:
        if out_frame == "source":
            # converted to source frame physical units
            conv_freqs = freqs * freq_conv
            conv_strain = strain * strain_conv
        elif out_frame == "observer":
            # converted to detector frame physical units
            conv_freqs = (freqs * freq_conv) / (1 + source.z)
            conv_strain = strain * strain_conv * (1 + source.z) ** 2
        else:
            raise ValueError("The reference frame can only be observer or source.")

    return [conv_freqs, conv_strain]
 def lum_dist_error(self, val, err):
     
     '''
     Use a black box error method to find the uncertanty in
     astropy.cosmology.WMAP9.luminosity_distance().
     
     Args:
         val (float): A redshift value
         err (float): Error in the redshift
     
     Returns:
         error (float): Error in the luminosity distance
     '''
     
     diff = (cosmo.luminosity_distance(val + err).cgs
             - cosmo.luminosity_distance(val - err).cgs)
     
     error = abs(.5 * diff.value)
     return(error)
Пример #21
0
    def __init__(self, ra, dec, z):

        #Set the position and redshift of the source
        self.pos = [ra, dec]
        self.z = z
        self.d = (cosmo.luminosity_distance(z)).value*3.086e24
        self.d2 = 4.*np.pi*(self.d*self.d)

        #Give the source a rest-frame SED:
        self.sed = sed()
Пример #22
0
def main(args):
    """
    :param args: command line arguments from argparse
    """
    # Read in the CSV file of parameters
    if args.type == "S":
        kcorr_df = pd.read_csv("data/kcorr_sgrbs.csv", index_col="GRB")
    elif args.type == "L":
        kcorr_df = pd.read_csv("data/kcorr_lgrbs.csv", index_col="GRB")
    else:
        print("Please provide a valid type argument.\n" \
              "The type of GRB: L - long, S - short")
        sys.exit(2)

    print(f"--> Loading the {args.type}GRB properties...")
    grbs = kcorr_df.index.tolist()    # Get list of GRB names

    # Calculate the luminosity distance in cm and add to data frame
    kcorr_df["dl_cm"] = (cosmo.luminosity_distance(kcorr_df["z"]).value
                         * 3.08568e24)

    # Loop over GRBs
    for grb in grbs:

        # Create filepaths
        if args.type == "S":
            infile = os.path.join("data", "SGRBS", grb, "".join([grb, ".csv"]))
            outfile = os.path.join("data", "SGRBS", grb, "".join([grb,
                                   "_k.csv"]))
        elif args.type == "L":
            infile = os.path.join("data", "LGRBS", grb, "".join([grb, ".csv"]))
            outfile = os.path.join("data", "LGRBS", grb, "".join([grb,
                                                                  "_k.csv"]))
        else:
            print("Please provide a valid type argument.\n" \
                  "The type of GRB: L - long, S - short")
            sys.exit(2)

        # Read in GRB data file
        print(f"--> Loading data for: {grb}...")
        data = pd.read_csv(infile, index_col=False)

        # Perform k-correction
        print("--> Performing k-correction...")
        k_data = k_correction(data, kcorr_df["Gamma"][grb],
                              kcorr_df["sigma"][grb], kcorr_df["z"][grb],
                              kcorr_df["dl_cm"][grb])

        # Calculate geometric mean of asymmetric errors and add to data frame
        k_data["Lum50err"] = gmean([k_data["Lum50pos"].values,
                                    np.abs(k_data["Lum50neg"].values)])

        # Output corrected to data
        print("--> Writing to output file...")
        k_data.to_csv(outfile, index=False)
Пример #23
0
    def get_spectrum(self, outwave=None, filters=None, peraa=False, **params):
        """Given a theta vector, generate spectroscopy, photometry and any
        extras (e.g. stellar mass).

        :param theta:
            ndarray of parameter values.

        :param sps:
            A python-fsps StellarPopulation object to be used for
            generating the SED.

        :returns spec:
            The restframe spectrum in units of maggies.

        :returns phot:
            The apparent (redshifted) observed frame maggies in each of the
            filters.

        :returns extras:
            A list of the ratio of existing stellar mass to total mass formed
            for each component, length ncomp.
        """
        self.params.update(**params)
        # Pass the model parameters through to the sps object
        ncomp = len(self.params['mass'])
        for ic in range(ncomp):
            s, p, x = self.one_sed(component_index=ic, filterlist=filters)
            try:
                spec += s
                maggies += p
                extra += [x]
            except(NameError):
                spec, maggies, extra = s, p, [x]
        # `spec` is now in Lsun/Hz

        if outwave is not None:
            w = self.csp.wavelengths
            spec = np.interp(outwave, w, spec)
        # Distance dimming and unit conversion
        if self.params['zred'] == 0:
            # Use 10pc for the luminosity distance (or a number
            # provided in the lumdist key in units of Mpc)
            dfactor = (self.params.get('lumdist', 1e-5) * 1e5)**2
        else:
            lumdist = cosmo.luminosity_distance(self.params['zred']).value
            dfactor = (lumdist * 1e5)**2 / (1 + self.params['zred'])
        if peraa:
            # spectrum will be in erg/s/cm^2/AA
            spec *= to_cgs / dfactor * lightspeed / outwave**2
        else:
            # Spectrum will be in maggies
            spec *= to_cgs / dfactor / 1e3 / (3631*jansky_mks)

        return spec, maggies / dfactor, extra
 def luminosityDistance(self, objectID):
     self.getID()
     self.getRedshift()
     lumDist = 0
     if (objectID in self.objID):
         for i in range(0, len(self.objID)):
             if (objectID == self.objID[i]):
                 lumDist = cosmo.luminosity_distance(self.redshift[i])
                 return lumDist
     else:
         raise ValueError(objectID, 'is not a valid object identifier.')
Пример #25
0
def dist2z(dist):
    zs = np.arange(0.01, 10, 0.01)
    dists = cosmo.luminosity_distance(zs).value
    f = interpolate.interp1d(dists,
                             zs,
                             bounds_error=False,
                             fill_value="extrapolate",
                             kind='linear')
    z = f(dist)

    return z
Пример #26
0
def omega_rnu(shift_mas, nu1_ghz, nu2_ghz, z, k_r):
    """
    Core-position offset measure (see Lobanov 1998).

    :param shift_mas:
        Core shift [mas] between frequencies ``nu1_ghz`` and ``nu2_ghz`` [GHz].
    """
    D_L_pc = WMAP9.luminosity_distance(z).to(u.pc).value
    nu2 = max([nu1_ghz, nu2_ghz])
    nu1 = min([nu1_ghz, nu2_ghz])
    return 4.85e-09 * shift_mas * D_L_pc * (nu1**(1 / k_r) * nu2**(1 / k_r) /
                                            (nu2**(1 / k_r) - nu1**
                                             (1 / k_r))) / (1 + z)**2
Пример #27
0
 def findDist(self):
     dh = 299792.458 / cosmo.H(0)
     om = 0.27
     ok = 1 - om
     oa = 0
     #E=lambda x: sqrt(om*(1+x)**3+ok*(1+x)**2+oa)
     dc = dh * integrate.quad(
         lambda x: 1 / (sqrt(om * (1 + x)**3 + ok *
                             (1 + x)**2 + oa)), 0, self.z)
     dm = dh * (1 / sqrt(ok) * sinh(sqrt(ok) * (dc / dh)))
     dl = dm * (1 + self.z)
     a = cosmo.luminosity_distance(self.z).value
     return a
Пример #28
0
 def __init__(self, name, z, x, y):
     self.z = z
     self.y = y
     self.x = x
     self.name = name
     #lDcm gives the luminosity distance in centimeters
     #self.lDcm = cosmo.luminosity_distance(0.603)*u.Mpc.to(u.cm)/u.Mpc
     #self.lDcm = cosmo.luminosity_distance(self.z)*u.Mpc.to(u.cm) / u.Mpc
     #pixtoKpc is the conversion betweem pixels to rad to kpc based on z value
     #self.pixtoKpc = conv.arcsec_per_kpc_proper(self.z)*0.05/u.arcsec*u.kpc
     self.lDcm = cosmo.luminosity_distance(self.z) * u.Mpc.to(u.cm) / u.Mpc
     self.radToKpc = conv.arcsec_per_kpc_proper(
         self.z) * 0.05 / u.arcsec * u.kpc
Пример #29
0
def get_column(zin, cosmo, Mpc_cm=3.08568025e+24, z_r=6.0, delz=0.1):
    '''
	Returns
	-------
	HI column density of IGM at zin, in cm^-3.
	'''
    z = np.arange(z_r, zin, delz)
    try:
        nH = np.zeros(len(z), dtype='float')
    except:
        nH = 0

    # From Cen & Haiman 2000
    nH = 8.5e-5 * ((1. + z) / 8)**3  # in cm^-3
    NH = 0
    for zz in range(len(z)):
        d1 = cosmo.luminosity_distance(z[zz] - delz).value  #, **cosmo)
        d2 = cosmo.luminosity_distance(z[zz] + delz).value  #, **cosmo)
        dx = (d2 - d1) * Mpc_cm
        NH += nH[zz] * dx / (1. + z[zz])

    return NH
Пример #30
0
def mips_to_lir(mips_flux,z):

    '''
    input flux must be in mJy
    output is in Lsun
    L_IR [Lsun] = fac_<band>(redshift) * flux [milliJy]
    '''
    
    # if we're at higher redshift, interpolate
    # it decrease error due to redshift relative to rest-frame template (good)
    # but adds nonlinear error due to distances (bad)
    # else, scale the nearest conversion factor by the 
    # ratio of luminosity distances, since nonlinear error due to distances will dominate
    if z > 0.1:
        intfnc = interp1d(conversion['Redshift'],conversion['fac_MIPS24um'], bounds_error = False, fill_value = 0)
        fac = intfnc(z)
    else:
        near_idx = np.abs(conversion['Redshift']-z).argmin()
        lumdist_ratio = (WMAP9.luminosity_distance(z).value / WMAP9.luminosity_distance(conversion['Redshift'][near_idx]).value)**2
        zfac_ratio = (1.+conversion['Redshift'][near_idx]) / (1.+z)
        fac = conversion['fac_MIPS24um'][near_idx]*lumdist_ratio*zfac_ratio

    return fac*mips_flux
Пример #31
0
    def get_fnu(
            self,
            cosmo,
            z,
            include_IGM=True):  # flux nJy, depends on redshift and cosmology

        self.lamz = self.lam * (1. + z)

        self.fnu = 1E23 * 1E9 * self.lnu * (1. + z) / (
            4 * np.pi * cosmo.luminosity_distance(z).to('cm').value**2)  # nJy

        if include_IGM:

            self.fnu *= IGM.madau(self.lamz, z)
Пример #32
0
def get_intrinsic_mag(sn_name, Z, B, AB):
    MB = pandas.DataFrame(index=sn_name, columns=['MB'])
    MB_arr = numpy.zeros(len(sn_name))
    for i, n in enumerate(sn_name):
        try:
            mb = B.loc[n]
            d = cosmo.luminosity_distance(Z.loc[n])  #
            ab = AB.loc[n]
            mu = 5 * numpy.log10(d / (10 * u.pc))
            MB_arr[i] = mb - mu - ab
        except:
            MB_arr[i] = numpy.nan
    MB['MB'] = MB_arr
    return MB  #Mpandas.DataFrame(index=sn_name, data=MB)
Пример #33
0
def calculate_B_zdzr2015(h_u, nu1_u, nu2_u, z, delta, dr_core_ang_u,
                         hoangle_rad, theta_los_rad, flux_u, p):
    """
    Calculate B using (8) from paper Zdziarski et al. 2015 (10.1093/mnras/stv986).
    Arguments with ``u`` postfix are values with units:

    >>> import astropy.units as u
    >>> flux_u = 1.0*u.Jy
    >>> nu1_u = 15.4*u.GHz

    :param h_u:
        Distance from BH.
    :param nu1_u:
        Lower frequency.
    :param nu2_u:
        Higher frequency.
    :param z:
        redshift.
    :param delta:
        Doppler factor.
    :param dr_core_ang_u:
        Core shift.
    :param hoangle_rad:
        Half-opening angle [rad].
    :param theta_los_rad:
        LOS angle [rad]
    :param flux_u:
        Flux.
    :param p:
        Exponent of particles energy distribution. Must be 2 or 3 here.

    :return:
        Value of B [G].

    """
    if p not in (2.0, 3.0):
        raise Exception("p must be 2.0 or 3.0")
    c3 = {2: 3.61, 3: 2.10, 4: 1.61}
    c1 = {2: 1.14, 3: 1}
    c2 = {2: 2. / 3, 3: 1}
    D_L = WMAP9.luminosity_distance(z)
    # B_cr = 2*np.pi*(const.m_e**2*const.c**3/(const.e.gauss*const.h)).cgs.value
    result = (D_L * delta * (const.h / const.m_e)**7 /
              (h_u * ((1 + z) * np.sin(theta_los_rad))**3 * const.c**12) *
              (2 * np.pi * (const.m_e**2 * const.c**3 /
                            (const.e.gauss * const.h)) * dr_core_ang_u /
               (nu1_u**(-1) - nu2_u**(-1)))**5 *
              (const.alpha * c1[p] * c3[p] * np.tan(hoangle_rad) /
               (flux_u * 24 * np.pi**3 * c2[p]))**2).cgs
    return result.value
Пример #34
0
def add_6df(simplifiedmastercat, sixdf, tol=1*u.arcmin):
    """
    Adds entries in the catalog for the 6dF survey, or updates v when missing
    """
    from astropy import table
    from astropy.coordinates import SkyCoord
    from astropy.constants import c

    ckps = c.to(u.km/u.s).value

    catcoo = SkyCoord(simplifiedmastercat['RA'].view(np.ndarray)*u.deg, simplifiedmastercat['Dec'].view(np.ndarray)*u.deg)
    sixdfcoo = SkyCoord(sixdf['obsra'].view(np.ndarray)*u.deg, sixdf['obsdec'].view(np.ndarray)*u.deg)

    idx, dd, d3d = sixdfcoo.match_to_catalog_sky(catcoo)
    msk = dd < tol

    sixdfnomatch = sixdf[~msk]

    t = table.Table()
    t.add_column(table.MaskedColumn(name='RA', data=sixdfnomatch['obsra']))
    t.add_column(table.MaskedColumn(name='Dec', data=sixdfnomatch['obsdec']))
    t.add_column(table.MaskedColumn(name='PGC#', data=-np.ones(len(sixdfnomatch), dtype=int), mask=np.ones(len(sixdfnomatch), dtype=bool)))
    t.add_column(table.MaskedColumn(name='NSAID', data=-np.ones(len(sixdfnomatch), dtype=int), mask=np.ones(len(sixdfnomatch), dtype=bool)))
    t.add_column(table.MaskedColumn(name='othername', data=sixdfnomatch['targetname']))
    t.add_column(table.MaskedColumn(name='vhelio', data=sixdfnomatch['z_helio']*ckps))
    #t.add_column(table.MaskedColumn(name='vhelio_err', data=sixdfnomatch['zfinalerr']*ckps))
    t.add_column(table.MaskedColumn(name='distance', data=WMAP9.luminosity_distance(sixdfnomatch['z_helio']).value))

    #fill in anything else needed with -999 and masked
    for nm in simplifiedmastercat.colnames:
        if nm not in t.colnames:
            t.add_column(table.MaskedColumn(name=nm, data=-999*np.ones(len(sixdfnomatch), dtype=int), mask=np.ones(len(sixdfnomatch), dtype=bool)))

    t = table.vstack([simplifiedmastercat, t], join_type='exact')

    #now update anything that *did* match but doesn't have another velocity
    tcoo = SkyCoord(t['RA'].view(np.ndarray)*u.deg, t['Dec'].view(np.ndarray)*u.deg)

    idx, dd, d3d = sixdfcoo.match_to_catalog_sky(tcoo)
    msk = dd < tol

    catmatch = t[idx[msk]]
    sixdfmatch = sixdf[msk]

    msk2 = t['vhelio'][idx[msk]].mask
    t['vhelio'][idx[msk&msk2]] = sixdf['z_helio'][msk2]*ckps

    return t
Пример #35
0
def OP(deproj, nmc=1000):
    # Standard onion peeling
    prof = deproj.profile
    nbin = prof.nbin
    rinam = prof.bins - prof.ebins
    routam = prof.bins + prof.ebins
    area = np.pi * (routam**2 - rinam**2)  # full area in arcmin^2

    # Projection volumes
    if deproj.z is not None and deproj.cf is not None:
        amin2kpc = cosmo.kpc_proper_per_arcmin(deproj.z).value
        rin_cm = (prof.bins - prof.ebins) * amin2kpc * Mpc / 1e3
        rout_cm = (prof.bins + prof.ebins) * amin2kpc * Mpc / 1e3
        x = MyDeprojVol(rin_cm, rout_cm)
        vol = np.transpose(x.deproj_vol())
        dlum = cosmo.luminosity_distance(deproj.z).value * Mpc
        K2em = 4. * np.pi * 1e14 * dlum**2 / (1 +
                                              deproj.z)**2 / nhc / deproj.cf

        # Projected emission measure profiles
        em0 = prof.profile * K2em * area
        e_em0 = prof.eprof * K2em * area
        corr = EdgeCorr(nbin, rin_cm, rout_cm, em0)
    else:
        x = MyDeprojVol(rinam, routam)
        vol = np.transpose(x.deproj_vol()).T
        em0 = prof.profile * area
        e_em0 = prof.profile * area
        corr = EdgeCorr(nbin, rinam, routam)

    # Deproject and propagate error using Monte Carlo
    emres = np.repeat(e_em0, nmc).reshape(nbin, nmc) * np.random.randn(
        nbin, nmc) + np.repeat(em0, nmc).reshape(nbin, nmc)
    ct = np.repeat(corr, nmc).reshape(nbin, nmc)
    allres = np.linalg.solve(vol, emres * (1. - ct))
    ev0 = np.std(allres, axis=1)
    v0 = np.median(allres, axis=1)
    bsm = medsmooth(v0)

    deproj.sb = bsm
    deproj.sb_lo = bsm - ev0
    deproj.sb_hi = bsm + ev0

    deproj.dens = medsmooth(np.sign(bsm) * np.sqrt(np.abs(bsm)))
    edens = 0.5 / np.sqrt(np.abs(bsm)) * ev0
    deproj.dens_lo = deproj.dens - edens
    deproj.dens_hi = deproj.dens + edens
Пример #36
0
def Get_Mono_Strain(source, f_gw, strain_const='Averaged'):
    """Calculates the strain from a binary black hole.

    Parameters
    ----------
    f_gw : float
        The source frequency of the gravitational wave.
    strain_const : {'Averaged','Optimal'}
        'Averaged' gives the sky and inclination averaged strain from Robson et al. 2019 (eqn 27) <https://arxiv.org/pdf/1803.01944.pdf>
        'Optimal' gives the optimally oriented, face-on, inclination (ie. inc=0) value

    Returns
    -------
    float
        the strain of a monochromatic source in the dector frame

    """
    f_gw = utils.make_quant(f_gw, 'Hz')
    if isinstance(strain_const, str):
        DL = cosmo.luminosity_distance(source.z)
        DL = DL.to('m')

        #Converts M = [M] to M = [sec]
        m_conv = const.G / const.c**3

        eta = source.q / (1 + source.q)**2
        M_redshifted_time = source.M.to('kg') * (1 + source.z) * m_conv
        M_chirp = eta**(3 / 5) * M_redshifted_time

        if strain_const == 'Optimal':
            inc = 0.0
            a = 1 + np.cos(inc)**2
            b = -2 * np.cos(inc)
            const_val = 2 * np.sqrt(.5 * (a**2 + b**2))
        elif strain_const == 'Averaged':
            const_val = 8 / np.sqrt(5)
        else:
            raise ValueError(
                'Can only use "Averaged", or "Optimal" monochromatic strain calculation.'
            )

        return const_val * (const.c / DL) * (np.pi * f_gw)**(
            2. / 3.) * M_chirp**(5. / 3.)
    else:
        raise ValueError(
            'Can only use "Averaged", or "Optimal" monochromatic strain calculation.'
        )
Пример #37
0
 def __init__(self, p,z,e_erg,epsilon_e,epsilon_B,n_0,a_s,t_sec):
     self.p = p
     self.z = z
     self.e_erg = e_erg
     self.e_52 = self.e_erg * 1E-52
     self.epsilon_e = epsilon_e
     self.epsilon_B = epsilon_B
     self.n_0 = n_0
     self.a_s = a_s
     self.t_sec = t_sec
     self.t_min = self.t_sec / 60.
     self.t_hr = self.t_sec / 3600.
     self.t_day = self.t_sec / 86400.
     self.epsilon_e_bar = self.epsilon_e*(self.p-2.)/(self.p-1.)
     self.d_L = cosmo.luminosity_distance(self.z) #Weinberg, 1972, pp 420-424; Weedman, 1986, pp 60-62.
     self.d_L_Mpc = self.d_L.value
     self.d_L_cm = self.d_L.to(u.cm).value
     self.d_L28 = self.d_L_cm * 1E-28
Пример #38
0
def manually_tweak_simplified_catalog(simplifiedcat):
    """
    This just updates a few entries in the catalog that seem to be missing
    velocities for unclear reasons.

    No longer needed with `add_6df`: they are present.
    """
    from astropy.coordinates import SkyCoord
    from astropy.constants import c

    infolines = """
    46.480833  54.266111   2051.8    No velocity in NED PGC 011632
    46.704583  54.588333   2859.3    No velocity in NED
    50.288333  66.921667   2637.1    Velocity in NED, but no LEDA entry
    99.794583 -1.5075000   2887.9    No velocity in NED
    102.57375 -2.8605556   2699.9    No velocity in NED
    102.93875 -3.6077778   2867.4    No velocity in NED
    114.40708 -26.746667   2964.5    Velocity in NED, LEDA source, but not HyperLeda
    116.32750 -32.516667   2162.0    Velocity in NED, LEDA source, but not HyperLeda
    123.74833 -30.866667   1761.0    Velocity in NED, PGC 023091 (note in NED re; position error?)
    """.split('\n')[1:-1]

    ras = []
    decs = []
    vels = []
    for l in infolines:
        ls = l.split()
        ras.append(float(ls[0]))
        decs.append(float(ls[1]))
        vels.append(float(ls[2]))

    updatecoos = SkyCoord(ras*u.deg, decs*u.deg)
    catcoos = SkyCoord(simplifiedcat['RA'].view(np.ndarray)*u.deg,
                   simplifiedcat['Dec'].view(np.ndarray)*u.deg)
    idx, dd, d3d = updatecoos.match_to_catalog_sky(catcoos)

    simplifiedcat = simplifiedcat.copy()
    simplifiedcat['vhelio'][idx] = vels = np.array(vels)
    simplifiedcat['distance'][idx] = WMAP9.luminosity_distance(
        vels / c.to(u.km / u.s).value).to(u.Mpc).value

    return simplifiedcat
Пример #39
0
 def __init__(self, e52, n0, p, epsilonB, epsilonE, dL28=None, z=None, gamma0=100., iceffect=False, eblmodel=None):
     self.energy = e52 * 1E52 * u.erg
     self.ndensity = n0 * u.cm**-3
     self.p = p
     self.epsilonB = epsilonB
     self.epsilonE = epsilonE
     self.epsilonE_bar = epsilonE * (self.p-2.) / (self.p-1.)
     if dL28==None and z==None:
         logger.error('Distance and redshift are neither provided!!!')
         dL28 = 5.
     if dL28 is not None and z==None:
         self.dL = dL28 * 1E28 * u.cm
         distance = Distance(value=self.dL)
         self.z = distance.z
     elif dL28 is not None and z is not None:
         self.dL = dL28 * 1E28 * u.cm
         self.z = z
     else:
         self.z = z
         self.dL = cosmo.luminosity_distance(self.z)
     logger.debug('EBL model: {0}'.format(eblmodel))
     self.ebl = InterpolateEBLmodels.read_model(eblmodel)[0] if eblmodel is not None else None
     self.gamma0 = gamma0
     self.iceffect = iceffect
Пример #40
0
        print "new age: " + str(newages[newageind[arg]]) + ", EBV: " + str(EBV)
        for i in range(arg, arg+1):
            for j in range(len(zarr)):
                newzspectrum = np.zeros(len(newwavspec)*2, dtype="float")
                newzspectrum.shape = (len(newwavspec), 2)
                newzspectrum[:,0] = newwavspec
                newzspectrum[:,1] = newspecdata[newageind[i]+1,:]*3.826*10**33 #luminosity in erg/s/A
                newzspectrum[:,1] = newzspectrum[:,1]*10**(-EBV*newcoef/2.5)
                for k in range(len(newzspectrum)):
                    if newzspectrum[k,0] < 912.:
                        newzspectrum[k,1] = 0.
                    elif newzspectrum[k,0] > 912. and newzspectrum[k,0] < 1026.:
                        newzspectrum[k,1] = newzspectrum[k,1]*(1-D[j,1])
                    elif newzspectrum[k,0] > 1026. and newzspectrum[k,0] < 1216.:
                        newzspectrum[k,1] = newzspectrum[k,1]*(1-D[j,0])
                newzspectrum[:,1] = newzspectrum[:,1]/(4*np.pi*(cosmo.luminosity_distance(zarr[j])*3.086*10**24)**2) #convert to observed flux at given redshift in erg/s/A/cm^2
                newzspectrum[:,1] = newzspectrum[:,1]/(1+zarr[j]) #reduce flux by a factor of 1/(1+z) to account for redshifting
                newagenorms[i, j] = np.mean(rebin_to_filter(newzspectrum, normfilt))
                newzspectrum[:,1] = newzspectrum[:,1]/newagenorms[i, j] #reduce flux by a factor of 1/(1+z) to account for redshifting
                newzspectrum[:,0] = newzspectrum[:,0]*(1+zarr[j]) #change wavelength values to account for redshifting
                newoutput[:,j] = rebin_to_filter(newzspectrum, objfilter)
                
            np.savetxt("../../models/spec/const/age_" + str(newages[newageind[i]]) + "_EBV_" + str(EBV) + ".txt", newoutput)
            np.savetxt("../../models/spec/const/agenorms_" + str(newages[newageind[i]]) + "_EBV_" + str(EBV) + ".txt", newagenorms[i,:])
         
else:
    for e in range(41):
        EBV = 0.025*e
        print "old age: " + str(oldages[oldageind[arg-8]]) + ", EBV: " + str(EBV)
        for i in range(arg-8, arg-7):
            for j in range(len(zarr)):
Пример #41
0
    def get_spectrum(self, outwave=None, filters=None, peraa=False, **params):
        """Get a spectrum and SED for the given params.

        :param outwave:
            Desired *vacuum* wavelengths.  Defaults to the values in
            sps.ssp.wavelength.

        :param peraa: (default: False)
            If `True`, return the spectrum in erg/s/cm^2/AA instead of AB
            maggies.

        :returns spec:
            Observed frame spectrum in AB maggies, unless `peraa=True` in which
            case the units are erg/s/cm^2/AA.

        :returns phot:
            Observed frame photometry in AB maggies.

        :returns mass_frac:
            The ratio of the surviving stellar mass to the total mass formed.
        """
        # Spectrum in Lsun/Hz per solar mass formed, restframe
        wave, spectrum, mfrac = self.get_galaxy_spectrum(**params)

        # Redshifting + Wavelength solution
        if 'zred' in self.reserved_params:
            # We do it ourselves.
            a = 1 + self.params.get('zred', 0)
            af = a
            b = 0.0
        else:
            a, b = 1.0, 0.0
            # FSPS shifts the wavelength vector but we need to decrease the
            # flux by (1+z) here.
            af = 1 + self.params.get('zred', 0)

        if 'wavecal_coeffs' in self.params:
            x = wave - wave.min()
            x = 2.0 * (x / x.max()) - 1.0
            c = np.insert(self.params['wavecal_coeffs'], 0, 0)
            # assume coeeficients give shifts in km/s
            b = chebval(x, c) / (lightspeed*1e-13)

        wa, sa = wave * (a + b), spectrum * af  # Observed Frame
        if outwave is None:
            outwave = wa

        # Observed frame photometry, as absolute maggies
        if filters is not None:
            mags = getSED(wa, lightspeed/wa**2 * sa * to_cgs, filters)
            phot = np.atleast_1d(10**(-0.4 * mags))
        else:
            phot = 0.0

        # Spectral smoothing.
        do_smooth = (('sigma_smooth' in self.params) and
                     ('sigma_smooth' in self.reserved_params))
        if do_smooth:
            # We do it ourselves.
            smspec = self.smoothspec(wa, sa, self.params['sigma_smooth'],
                                     outwave=outwave, **self.params)
        elif outwave is not wa:
            # Just interpolate
            smspec = np.interp(outwave, wa, sa, left=0, right=0)
        else:
            # no interpolation necessary
            smspec = sa

        # Distance dimming and unit conversion
        if (self.params['zred'] == 0) or ('lumdist' in self.params):
            # Use 10pc for the luminosity distance (or a number
            # provided in the dist key in units of Mpc)
            dfactor = (self.params.get('lumdist', 1e-5) * 1e5)**2
        else:
            lumdist = cosmo.luminosity_distance(self.params['zred']).value
            dfactor = (lumdist * 1e5)**2
        if peraa:
            # spectrum will be in erg/s/cm^2/AA
            smspec *= to_cgs / dfactor * lightspeed / outwave**2
        else:
            # Spectrum will be in maggies
            smspec *= to_cgs / dfactor / 1e3 / (3631*jansky_mks)

        # Convert from absolute maggies to apparent maggies
        phot /= dfactor

        # Mass normalization
        mass = np.sum(self.params.get('mass', 1.0))
        if np.all(self.params.get('mass_units', 'mstar') == 'mstar'):
            # Convert from current stellar mass to mass formed
            mass /= mfrac

        return smspec * mass, phot * mass, mfrac
Пример #42
0
    def plot_fit(self, i_obj, ax=None, is_simulation=False, starlight_input=None, percentile=16):

        if ax is None:
            fig = plt.figure(1)
            fig.clf()
            ax = fig.add_axes((0.1,0.1,.8,.8))

        if self.l is None:
            raise MAGALException('You must init the object w/ a Library!')

        bmx_z, bmx_T = self.m.i_BMX[i_obj]
        if bmx_z == 0:  #FIXME:
            i_z = np.argmin((self.l.z - self.i.z[0])**2)
        else:
            i_z = bmx_z

        aux_model = self.l.library[i_z, bmx_T]['m_ab'] + self.m.s[i_obj, bmx_z, bmx_T]

        if self.filters_exclude:
            filterset_mask = ~np.sum([k == self.l.filterset['ID_filter'] for k in self.filters_exclude], axis=0, dtype=np.bool)
        else:
            filterset_mask = np.ones(len(self.l.filterset), dtype=np.bool)

        if is_simulation: #FIXME
            aux_inp = self.i.library[0, i_obj]['m_ab'] - 2.5 * self.i.properties[i_obj]['Mini_fib'] #FIXME
            aux_err = self.i.library[0, i_obj]['e_ab'] #FIXME
        else:
            aux_inp = self.i.data.value[i_obj]['m_ab']
            aux_err = self.i.data.value[i_obj]['e_ab']

        filterset_mask = np.bitwise_and(filterset_mask, np.isfinite(aux_model))
        filterset_mask = np.bitwise_and(filterset_mask, np.isfinite(aux_inp))

        aux_lib = np.ma.masked_invalid(self.l.library['m_ab'])

        aux_model_tot = [
            ((aux_lib[i_z,:,i_mag] + self.m.s[i_obj, i_z]) * self.m.likelihood[i_obj, i_z]).sum() / self.m.likelihood[i_obj, i_z][np.argwhere(~aux_lib[i_z,:,i_mag].mask)].sum()
            for i_mag in np.argwhere(filterset_mask)]

        aux_l = self.l.filterset['wl_central'][filterset_mask]

        z_lib = self.l.z[i_z]
        if z_lib == 0:
            d_L_lib = 3.08567758e19  # 10 parsec in cm
        else:
            d_L_lib = cosmo.luminosity_distance(z_lib).to('cm')
        # k_cosmo = 1/(4 * np.pi * np.power(d_L, 2)) #FIXME:

        # aux_l2 = aux_l**-2
        # ax.plot(aux_l, aux_l2 * 10**(-.4 * (aux_inp + 2.41)) * k_cosmo.value, '.', color='blue')
        # ax.plot(aux_l, aux_l2 * 10**(-.4 * (aux_model[filterset_mask] + 2.41)) * k_cosmo.value, '.', color='red')

        # ax.plot(aux_l, aux_inp+aux_err, color='black')
        # ax.plot(aux_l, aux_inp-aux_err, color='black')

        plt.plot(aux_l, aux_model_tot)
        n_percentile = self.m.get('/Alhambra_24/1/statistics/model/n_percentiles')[i_obj][str(percentile)]
        # print np.argsort(self.m.likelihood[0])[0][::-n_percentile]
        print n_percentile
        # for i_T in np.argsort(self.m.likelihood[0])[0][::-1][1:n_percentile]:
        #     aux_model = self.l.library[i_z, i_T]['m_ab'] + self.m.s[i_obj, bmx_z, i_T]
        #     plt.plot(aux_l, aux_model[filterset_mask], '.', color='black', linewidth=1, alpha=.1)
        #     # print 'i', i


        if starlight_input:
            z_sl = self.i.properties['z'][i_obj]
            if z_sl == 0:
                d_L = 3.08567758e19  # 10 parsec in cm
            else:
                d_L = cosmo.luminosity_distance(z_sl).to('cm')
            k_cosmo = (1+z_sl)**3 / (4 * np.pi * np.power(d_L.value, 2)) #FIXME:
            k_cosmo *= (4 * np.pi * np.power(d_L_lib.value, 2))

            try:
                print 'Trying to load', starlight_input
                aux_slight = np.loadtxt(starlight_input, dtype=np.dtype([('wl', '<f8'), ('flux', '<f8'), ('error', '<f8'), ('flag', '<i8')])) #, )
                wl_ = aux_slight['wl'] * (1+z_lib)
                fl_ = aux_slight['flux'] * np.power(z_sl + 1., 2) / (1+z_lib)
                # plt.plot(wl_, -2.5 * np.log10(aux_slight['flux'] * 1e-17 * wl_**2 / k_cosmo ) - 2.41, alpha=.3)
            except IOError:
                pass

        zp_error = self.m.input_config.getfloat('FitGeneral', 'zp_error') #FIXME
        plt.errorbar(aux_l, aux_inp[filterset_mask], fmt='o', color='green', linewidth=2, yerr=np.sqrt(aux_err[filterset_mask]**2+zp_error**2))
        plt.plot(aux_l, aux_model[filterset_mask], 'o', color='red', linewidth=2)
    def create_tables(self, uv_type, directory, radius, print_progress = False):
        
        '''
        Perform photometry on a directory of .fits files and create a list
        of two tables. The first table contains the redshift, exposure time,
        luminosity, and surface brightness for various supernova, along with
        the associated error values. The second table is a log outlining any
        files that do not contain a supernova or are missing checkfiles. If
        print_progress is set equal to true, the path each fits file will be
        printed before performing photometry on along with the number of
        remaining files.
        
        Args:
            uv_type        (str)  : Specifies which type of uv to create a table
                                        for. Use either "NUV" or "FUV".
            directory      (str)  : A directory containing .fits files
            radius         (float): Radius of desired photometry aperture in kpc
            print_progress (bool) : Whether or not to print the file path of each
                                        fits file

        Returns:
            results (list): [Data table (Table), Log table (Table)]
        '''
        
        #Make sure we have redshift and coordinates of each supernova
        if self.cord_dict.keys() != self.red_dict.keys():
            raise ValueError('''Keys in coordinate and redshift dictionaries
                                (self.cord_dict, self.red_dict) do not match''')
        
        label = uv_type + " " + str(radius) + "kpc "

        #Define the tables that will be returned by the function
        log = Table(names = ["File Path", "Issue"], dtype = [object, object])
        out = Table(names = ["sn",
                             "Redshift",
                             "Redshift Error",
                             label + "Exposure Time",
                             "Flux",
                             "Flux Error",
                             label + "Luminosity",
                             label + "Luminosity Error",
                             label + "Surface Brightness",
                             label + "Surface Brightness Error"],

                    dtype = ("S70", "float64", "float64", "float64", "float64",
                             "float64", "float64", "float64", "float64", "float64"))

        out["Redshift"].unit = u.dimensionless_unscaled
        out["Redshift Error"].unit = u.dimensionless_unscaled
        out[label + "Exposure Time"].unit = u.s
        out["Flux"].unit =  u.erg / u.s / u.Angstrom / u.kpc / u.kpc / u.cm / u.cm / np.pi
        out["Flux Error"].unit = u.erg / u.s / u.Angstrom / u.kpc / u.kpc / u.cm / u.cm / np.pi
        out[label + "Luminosity"].unit = u.erg / u.s / u.Angstrom / u.kpc / u.kpc
        out[label + "Luminosity Error"].unit = u.erg / u.s / u.Angstrom / u.kpc / u.kpc
        out[label + "Surface Brightness"].unit = u.erg / u.s / u.Angstrom / u.arcsec / u.arcsec
        out[label + "Surface Brightness Error"].unit = u.erg / u.s / u.Angstrom / u.arcsec / u.arcsec

        #Set parameters that are specific to NUV or FUV observations
        if "N" in uv_type.upper():
            file_key = "nd-int" #A string distinguing galex file types
            flux_conv = 2.06 * 1e-16 #A conversion factor from counts per second to flux

        elif "F" in uv_type.upper():
            file_key = "fd-int"
            flux_conv = 1.40 * 1e-15

        #Create a list of files to perform photometry on
        file_list = []
        for path, subdirs, files in os.walk(directory):
            for name in files:
                if file_key in name and len(name.split(".")) < 3:
                    file_list.append(os.path.join(path, name))
                    
        count = len(file_list)
        #Perform photometry on each .fits file
        for fits_file in file_list:
            if print_progress == True:
                print(count, ":", fits_file, flush = True)
                count -= 1

            p = self.photometry(fits_file, radius)
            for elt in p:
                if elt[0] == "error":
                    log.add_row([elt[1], elt[2]])
                    
                    if print_progress == True:
                        print("error", elt[2], "\n", flush = True)

                else:
                    #We calculate the values to be entered in the table
                    redshift = float(self.red_dict[elt[0]])
                    peculiar_redshift = np.sqrt((1 + (300 / 299792.458)) / (1 - (300 / 299792.458))) - 1
                    redshift_err = np.sqrt((redshift / 1000)**2 + (peculiar_redshift)**2)

                    arcmin = cosmo.kpc_comoving_per_arcmin(redshift).value**2 #kpc^2 per arcmin^2
                    arcmin_err = self.conv_error(redshift, redshift_err)

                    photom = elt[2] #The photometry value
                    photom_err = elt[3]

                    flux = flux_conv * photom #convert cps to flux using the conversion factor
                    flux_err = self.flux_error(photom, photom_err, flux_conv)

                    ldist = cosmo.luminosity_distance(redshift).cgs.value #Luminosity Distance (cm)
                    ldist_err = self.lum_dist_error(redshift, redshift_err)

                    lum = flux * 4 * np.pi * (ldist**2) #luminosity = flux*4*pi*r^2
                    lum_err = self.luminosity_error(flux, flux_err, ldist, ldist_err)

                    sbrightness = lum * arcmin / 3600
                    sbrightness_err = self.surf_brightness_error(lum, lum_err, arcmin, arcmin_err)

                    out.add_row([elt[0], redshift, redshift_err, elt[1], flux, flux_err,
                                 lum, lum_err, sbrightness, sbrightness_err])

        out.sort(label + "Surface Brightness Error")
        out_unique = unique(out, keys = "sn")
        out_unique.sort("sn")

        return([out_unique, log])
Пример #44
0
    def get_spectrum(self, outwave=None, filters=None, peraa=False, **params):
        """Given a theta vector, generate spectroscopy, photometry and any
        extras (e.g. stellar mass).

        :param theta:
            ndarray of parameter values.

        :param sps:
            A python-fsps StellarPopulation object to be used for
            generating the SED.

        :returns spec:
            The restframe spectrum in units of maggies.

        :returns phot:
            The apparent (redshifted) observed frame maggies in each of the
            filters.

        :returns extras:
            A list of the ratio of existing stellar mass to total mass formed
            for each component, length ncomp.
        """
        self.params.update(**params)
        # Pass the model parameters through to the sps object
        ncomp = len(self.params['mass'])
        for ic in range(ncomp):
            s, p, x = self.one_sed(component_index=ic, filterlist=filters)
            try:
                spec += s
                maggies += p
                extra += [x]
            except(NameError):
                spec, maggies, extra = s, p, [x]
        # `spec` is now in Lsun/Hz, with the wavelength array being the
        # observed frame wavelengths.  Flux array (and maggies) have not been
        # increased by (1+z) due to cosmological redshift

        if outwave is not None:
            w = self.csp.wavelengths
            spec = np.interp(outwave, w, spec)
        # Distance dimming and unit conversion
        if (self.params['zred'] == 0) or ('lumdist' in self.params):
            # Use 10pc for the luminosity distance (or a number provided in the
            # lumdist key in units of Mpc).  Do not apply cosmological (1+z)
            # factor to the flux.
            dfactor = (self.params.get('lumdist', 1e-5) * 1e5)**2
            a = 1.0
        else:
            # Use the comsological luminosity distance implied by this
            # redshift.  Incorporate cosmological (1+z) factor on the flux.
            lumdist = cosmo.luminosity_distance(self.params['zred']).value
            dfactor = (lumdist * 1e5)**2
            a = (1 + self.params['zred'])
        if peraa:
            # spectrum will be in erg/s/cm^2/AA
            spec *= to_cgs * a / dfactor * lightspeed / outwave**2
        else:
            # Spectrum will be in maggies
            spec *= to_cgs * a / dfactor / 1e3 / (3631*jansky_mks)

        # Convert from absolute maggies to apparent maggies
        maggies *= a / dfactor
            
        return spec, maggies, extra
Пример #45
0
def test_z(x,c):
    # c = true lumdist
    # x = redshift
    return WMAP9.luminosity_distance(x).value-c
Пример #46
0
    def get_spectrum(self, outwave=None, filters=None, peraa=False, **params):
        """Get a spectrum and SED for the given params.
        ripped from SSPBasis
        addition: check for flag nebeminspec. if not true,
        add emission lines directly to photometry
        """

        # Spectrum in Lsun/Hz per solar mass formed, restframe
        wave, spectrum, mfrac = self.get_galaxy_spectrum(**params)

        # Redshifting + Wavelength solution
        # We do it ourselves.
        a = 1 + self.params.get('zred', 0)
        af = a
        b = 0.0

        if 'wavecal_coeffs' in self.params:
            x = wave - wave.min()
            x = 2.0 * (x / x.max()) - 1.0
            c = np.insert(self.params['wavecal_coeffs'], 0, 0)
            # assume coeeficients give shifts in km/s
            b = chebval(x, c) / (lightspeed*1e-13)

        wa, sa = wave * (a + b), spectrum * af  # Observed Frame
        if outwave is None:
            outwave = wa
        
        spec_aa = lightspeed/wa**2 * sa # convert to perAA
        # Observed frame photometry, as absolute maggies
        if filters is not None:
            mags = observate.getSED(wa, spec_aa * to_cgs, filters)
            phot = np.atleast_1d(10**(-0.4 * mags))
        else:
            phot = 0.0

        ### if we don't have emission lines, add them
        if (not self.params['nebemlineinspec']) and self.params['add_neb_emission']:
            phot += self.nebline_photometry(filters,a-1)*to_cgs

        # Spectral smoothing.
        do_smooth = (('sigma_smooth' in self.params) and
                     ('sigma_smooth' in self.reserved_params))
        if do_smooth:
            # We do it ourselves.
            smspec = self.smoothspec(wa, sa, self.params['sigma_smooth'],
                                     outwave=outwave, **self.params)
        elif outwave is not wa:
            # Just interpolate
            smspec = np.interp(outwave, wa, sa, left=0, right=0)
        else:
            # no interpolation necessary
            smspec = sa

        # Distance dimming and unit conversion
        zred = self.params.get('zred', 0.0)
        if (zred == 0) or ('lumdist' in self.params):
            # Use 10pc for the luminosity distance (or a number
            # provided in the dist key in units of Mpc)
            dfactor = (self.params.get('lumdist', 1e-5) * 1e5)**2
        else:
            lumdist = WMAP9.luminosity_distance(zred).value
            dfactor = (lumdist * 1e5)**2
        if peraa:
            # spectrum will be in erg/s/cm^2/AA
            smspec *= to_cgs / dfactor * lightspeed / outwave**2
        else:
            # Spectrum will be in maggies
            smspec *= to_cgs / dfactor / 1e3 / (3631*jansky_mks)

        # Convert from absolute maggies to apparent maggies
        phot /= dfactor

        # Mass normalization
        mass = np.sum(self.params.get('mass', 1.0))
        if np.all(self.params.get('mass_units', 'mstar') == 'mstar'):
            # Convert from current stellar mass to mass formed
            mass /= mfrac

        return smspec * mass, phot * mass, mfrac
Пример #47
0
def read_magphys_output(objname=None):

  '''
  two output files

  GALAXY_ID.fit has observed fluxes+uncertainties,
  index of best-fit IR + optical models, chi^2, values of
  best-fit parameters, fluxes in each filter, and likelihood
  distribution of all parameters

  GALAXY_ID.sed has main parameters of the best-fit model,
  and the SED of the best-fit model.
  '''
  from astropy.cosmology import WMAP9
  project_info = {
                  'idfile': os.getenv('APPS')+"/threedhst_bsfh/data/brownseds_data/photometry/namelist.txt",
                  'datname':os.getenv('APPS')+'/threedhst_bsfh/data/brownseds_data/photometry/table1.fits',
                  'photname':os.getenv('APPS')+'/threedhst_bsfh/data/brownseds_data/photometry/table3.fits',
                  'extinctname':os.getenv('APPS')+'/threedhst_bsfh/data/brownseds_data/photometry/table4.fits',
                  'herschname':os.getenv('APPS')+'/threedhst_bsfh/data/brownseds_data/photometry/kingfish.brownapertures.flux.fits',
                  'output': '/Users/joel/code/magphys/output'
                 }

  if objname is None:
    objname = np.loadtxt(project_info['idfile'],delimiter='#',dtype = str)[0]

  #### first file ####
  file1 = slice_name(objname.replace(' ',''))[:8]+'.fit'
  with open(project_info['output']+'/'+file1[:10], 'r') as f:

    # skip first line
    f.readline()

    ##### observations #####
    # flux in Lsun/Hz
    obs = {}
    obs['filters'] = np.array(f.readline().split()[1:],dtype=str)
    obs['flux'] = np.array(f.readline().split(),dtype=float)
    obs['flux_unc'] = np.array(f.readline().split(),dtype=float)
    obs['phot_mask'] = (obs['flux'] != 0) | (obs['flux_unc'] != 0)

    # skip four lines
    for kk in xrange(4): f.readline()

    ##### metadata #####
    metadata = {}
    line = np.array(f.readline().split(),dtype=float)
    metadata['bestfit_opt_ind'] = line[0]
    metadata['bestfit_ir_ind'] = line[1]
    metadata['chisq'] = line[2]
    metadata['redshift'] = line[3]

    ##### best-fit model #####
    # in Lsun/Hz
    model = {}
    model['parnames'] = np.array(f.readline().replace('.',' ').split()[1:],dtype=str)
    model['parameters'] = np.array(f.readline().split(),dtype=float)
    f.readline()
    model['flux'] = np.array(f.readline().split(),dtype=float)

    # skip three lines
    for kk in xrange(3): f.readline()

    ##### parameter PDFs #####
    pdfs = {'x_interp_perc': np.array([0.025,0.16,0.5,0.84,0.975]),
            'percentiles': {},
            'likelihood_distr': {},
            'likelihood_x': {}
            }
    for par in model['parnames']:

      for line in f:
        # ignore comments
        # if it's the last comment, switch to new parname
        if line.find('#') != -1 and line.find('....') != -1:
          continue
        elif line.find('#') != -1:
          break

        _ = np.array(line.split(),dtype=float)

        if _.shape[0] == 2:
          pdfs['likelihood_x'][par] = np.append(pdfs['likelihood_x'].get(par,[]),_[0])
          pdfs['likelihood_distr'][par] = np.append(pdfs['likelihood_distr'].get(par,[]),_[1])
        else:
          pdfs['percentiles'][par] = _
      
  ##### second file #####
  file2 = slice_name(objname.replace(' ',''))[:8]+'.sed'
  with open(project_info['output']+'/'+file2[:10], 'r') as f:

    # skip two lines
    for i in xrange(2): f.readline()

    ##### read model parameters
    model['full_parnames'] = np.array(f.readline().replace('.',' ').split(),dtype=str)[1:]
    model['full_parameters'] = np.array(f.readline().split(),dtype=float)
    f.readline()
    model['full_parnames'] = np.append(model['full_parnames'],np.array(f.readline().replace('.',' ').split(),dtype=str)[1:])
    model['full_parameters'] = np.append(model['full_parameters'],np.array(f.readline().split(),dtype=float))

    # skip three lines
    for i in xrange(3): f.readline()

    ##### read spectrum ##### 
    # starts in obslam
    for line in f:
      _ = np.array(line.split(),dtype=float)
      model['lam'] = np.append(model.get('lam',[]),10**_[0])#*(1+metadata['redshift']))
      model['spec'] = np.append(model.get('spec',[]),_[1])
      model['spec_nodust'] = np.append(model.get('spec_nodust',[]),_[2])

  ##### units of [log(Lsun/angstrom)], change to maggies
  # first save output as speclum
  model['speclum'] = 10**model['spec']
  model['speclum_nodust'] = 10**model['spec_nodust']

  # now convert to Lsun/Hz
  c = 3e18 # Angstroms / s
  model['spec'] = (10**model['spec']) * model['lam']**2 / c
  model['spec_nodust'] = (10**model['spec_nodust']) * model['lam']**2 / c

  # from Lsun/Hz to maggies
  lsun = 3.846e33  # ergs/s
  jansky_cgs = 1e-23
  to_maggies = lsun / (3631*jansky_cgs)

  # cm^2
  pc = 3.085677581467192e18  # cm
  dfactor = 4*np.pi*(pc*WMAP9.luminosity_distance(metadata['redshift']).value *
                    1e6)**2 / (1+metadata['redshift'])

  model['spec'] *= to_maggies / dfactor
  model['spec_nodust'] *= to_maggies / dfactor
  obs['flux'] *= to_maggies / dfactor
  obs['flux_unc'] *= to_maggies / dfactor
  model['flux']  *= to_maggies / dfactor

  ##### load SFH
  metind = model['full_parnames'] == 'Z/Zo'
  age,sfr = read_magphys_sfh(int(metadata['bestfit_opt_ind']),
                           float(model['full_parameters'][metind]))
  sfh = {'age':age,'sfr':sfr}

  ##### add SFR_10 from best-fitting SFH
  # in Msun/yr
  magmass = model['full_parameters'][[model['full_parnames'] == 'M*/Msun']]
  magsfr = sfh['sfr']*magmass
  magtime = np.abs(np.max(sfh['age']) - sfh['age'])

  sfr_10 = prosp_dutils.integral_average(magtime,magsfr,0,1e7)
  if np.isfinite(sfr_10) == False:
    print 1/0
  model['full_parameters'] = np.append(model['full_parameters'],sfr_10)
  model['full_parnames'] = np.append(model['full_parnames'],'SFR_10')

  magphys = {'model': model,
             'obs': obs, 
             'metadata': metadata,
             'pdfs': pdfs,
             'sfh': sfh}

  return magphys
Пример #48
0
def cosmo_distance_extra(zred):
	return cosmo.luminosity_distance(zred).value**(-2) * (1+zred)**2
Пример #49
0
#width MUST be odd.
width = 15
pixr = int((width-1)/2)

# specify the position of the science target and the size of the region around the science target to consider
filters = np.array([475, 814, 1600]) #*u.nm
galaxies = ['J0826', 'J0901', 'J0905', 'J0944', 'J1107', 'J1219', 'J1341', 'J1506', 'J1558', 'J1613', 'J2116', 'J2140']
xcen = [3628, 3933, 3386.5, 3477.5, 3573, 3802, 3886, 4149, 3787, 4174, 3565, 4067]
ycen = [4153, 4136, 3503.2, 3404.3, 3339, 4169, 4164, 3921, 4187, 3826, 3434, 4054]

fluxvalues = [[0 for x in range(len(wavelengths))] for y in range(len(galaxies))]

zs = [0.603, 0.459, 0.712, 0.514, 0.467, 0.451, 0.658, 0.608, 0.402, 0.449, 0.728, 0.752]

#Ldcm is the luminosity distance in cm, even though astropy thinks it is in Mpc. 
Ldcm = cosmo.luminosity_distance(zs)*u.Mpc.to(u.cm) / u.Mpc
totalphotons = 0

# define the radii to be used for aperture photometry
radii = np.arange(40)+1
area = [0 for x in range(len(radii))]

# percunc specifies the percent of variation we expect from systematic error... 
# For now, we have chosen 0.05, or 5%.
percUnc = 0.05

#calculate area of each bagel
for i in range(0, len(area)):
    if i == 0:
        area[i] = math.pi*math.pow(radii[0],2)
    else:
Пример #50
0
    for j in range(69, 197):
        print "Calculating colours for age: " + str(ages[j]) + " and Tau: " + tauvals[t]
        spectrum = np.array([specdata[0, :], specdata[j+1, :]]).T

        synmags = np.zeros(13*500, dtype="float")
        synmags.shape = (500, 13)
        synmags[:,0] = np.arange(0.01, 5.01, 0.01)

        for i in range(1, 501):
            z = 0.01*i
            if ages[j]*(10**-9) < 14.00 - cosmo.lookback_time(z).value:

                zspectrum = np.copy(spectrum)
                zspectrum[:,1] = zspectrum[:,1]*3.826*10**33 #luminosity in erg/s/A
                zspectrum[:,1] = zspectrum[:,1]/(4*np.pi*(cosmo.luminosity_distance(z).value*3.086*10**24)**2) #convert to observed flux at given redshift in erg/s/A/cm^2
                zspectrum[:,1] = zspectrum[:,1]/(1+z) #reduce flux by a factor of 1/(1+z) to account for redshifting
                
                agenorms[j, i-1] = 25.*np.sum(rebin_to_filter(zspectrum, normfilt))
                zspectrum[:,1] = zspectrum[:,1]/agenorms[j,i-1] 
                zspectrum[:,0] = zspectrum[:,0]*(1+z) #change wavelength values to account for redshifting

                output[:,j] = rebin_to_filter(zspectrum, objfilter)
            else:
                output[:,j] = np.zeros(len(objfilter))

        np.savetxt("models/spec/" + tauvals[t] + "/age_" + str(ages[j]) + ".txt", output)
        np.savetxt("models/spec/" + tauvals[t] + "/agenorms_" + str(ages[j]) + ".txt", agenorms[j,:])


Пример #51
0
def collate_data(runname, runname_sample, filename=None, regenerate=False, **opts):
    
    ### if it's already made, load it and give it back
    # else, start with the making!
    if os.path.isfile(filename) and (regenerate == False):
        with open(filename, "r") as f:
            outdict=hickle.load(f)
            return outdict

    ### define output containers
    out = {'objname':[]}
    entries = ['ha_ew_obs', 'ha_ew_mod', 'ha_flux_mod', 'ha_flux_obs']
    qvals = ['q50', 'q16', 'q84']
    for e in entries:
        out[e] = {}
        if 'obs' in e:
            out[e]['val'] = []
            out[e]['err'] = []
        else:
            for q in qvals: out[e][q] = []

    ### load up ancillary dataset
    basenames, _, _ = prosp_dutils.generate_basenames(runname)
    field = [name.split('/')[-1].split('_')[0] for name in basenames]
    nii_ha_fnc = nii_ha_ratio()

    ancil = []
    allfields = np.unique(field).tolist()
    for f in allfields:
        ancil.append(td_io.load_ancil_data(runname_sample,f))

    for i, name in enumerate(basenames):

        ### if H-alpha is not detected, dump and continue
        objfield = name.split('/')[-2]
        objnumber = int(name.split('_')[-1])
        fidx = allfields.index(objfield)
        oidx = ancil[fidx]['phot_id'] == objnumber

        if (ancil[fidx]['Ha_FLUX'][oidx][0] == -99):
            continue

        #### load input
        # make sure all files exist
        try:
            prosp = load_prospector_extra(name)
            print name.split('/')[-1]+' loaded.'
        except:
            continue
        if prosp is None:
            continue
        out['objname'].append(name.split('/')[-1])
        objnumber = int(out['objname'][-1].split('_')[1])

        # fill in model data
        # comes out in rest-frame EW and Lsun
        for q in qvals: out['ha_ew_mod'][q].append(prosp['obs']['elines']['H alpha 6563']['ew'][q])
        for q in qvals: out['ha_flux_mod'][q].append(prosp['obs']['elines']['H alpha 6563']['flux'][q])

        # fill in observed data
        # comes out in observed-frame EW and (10**-17 ergs / s / cm**2)
        # account for NII / Halpha ratio, distance
        zred = ancil[fidx]['z_best'][oidx][0]
        mass = np.log10(prosp['extras']['stellar_mass']['q50'])
        nii_correction = float(1-nii_ha_fnc(mass,zred))
        lumdist = WMAP9.luminosity_distance(zred).value
        dfactor = 4*np.pi*(u.Mpc.to(u.cm) * lumdist)**2

        # fill in and march on
        out['ha_flux_obs']['val'].append(ancil[fidx]['Ha_FLUX'][oidx][0] * 1e-17 * dfactor / 3.828e33 * nii_correction)
        out['ha_flux_obs']['err'].append(ancil[fidx]['Ha_FLUX_ERR'][oidx][0] * 1e-17 * dfactor / 3.828e33 * nii_correction)
        out['ha_ew_obs']['val'].append(ancil[fidx]['Ha_EQW'][oidx][0]/(1+zred) * nii_correction)
        out['ha_ew_obs']['err'].append(ancil[fidx]['Ha_EQW_ERR'][oidx][0]/(1+zred) * nii_correction)

    for key in out.keys():
        if type(out[key]) == dict:
            for key2 in out[key].keys(): out[key][key2] = np.array(out[key][key2])
        else:
            out[key] = np.array(out[key])

    ### dump files and return
    hickle.dump(out,open(filename, "w"))
    return out