Ejemplo n.º 1
0
def plot_lgal_bgsSpec(): 
    galids = testGalIDs()

    fig = plt.figure(figsize=(15,5))
    sub = fig.add_subplot(111)
    for i, galid in enumerate(galids[:10]): 
        spec_source = lgal_sourceSpectra(galid, lib='bc03')
        spec_i = lgal_bgsSpec(galid, 0, lib='bc03', obs_sampling='spacefill')
        for band in ['b', 'r', 'z']: 
            sub.plot(spec_i.wave[band], spec_i.flux[band][0], lw=0.25, c='C'+str(i))
        sub.plot(spec_source['wave'], spec_source['flux'], c='k', ls='--', lw=0.5)
    sub.set_xlabel(r'Wavelenght [$\AA$]', fontsize=25) 
    sub.set_xlim([3600., 9800.]) 
    sub.set_ylabel(r'Flux [$10^{-17} ergs/s/cm^2/\AA$]', fontsize=25) 
    sub.set_ylim([0., 10.]) 
    fig.savefig(''.join([UT.fig_dir(), 'spectral_challenge.lgal_source_bgsSpec.png']), 
            bbox_inches='tight') 

    fig = plt.figure(figsize=(15,5))
    sub = fig.add_subplot(111)
    for i, galid in enumerate(galids[:10]): 
        spec_i = lgal_bgsSpec(galid, 0, lib='bc03', obs_sampling='spacefill')
        for band in ['b', 'r', 'z']: 
            sub.plot(spec_i.wave[band], spec_i.flux[band][0], lw=0.25, c='C'+str(i))
    sub.set_xlabel(r'Wavelenght [$\AA$]', fontsize=25) 
    sub.set_xlim([3600., 9800.]) 
    sub.set_ylabel(r'Flux [$10^{-17} ergs/s/cm^2/\AA$]', fontsize=25) 
    sub.set_ylim([0., 10.]) 
    fig.savefig(''.join([UT.fig_dir(), 'spectral_challenge.lgal_bgsSpec.png']), 
            bbox_inches='tight') 

    fig = plt.figure(figsize=(15,5))
    sub = fig.add_subplot(111)
    snrs = [] 
    for iobs in range(8): 
        spec_i = lgal_bgsSpec(galids[0], iobs, lib='bc03', obs_sampling='spacefill')
        band = 'b' 
        these = ((spec_i.wave[band] > np.mean(spec_i.wave[band])-50) &
                (spec_i.wave[band] < np.mean(spec_i.wave[band])+50) &
                (spec_i.flux[band][0] > 0))
        snr = np.median(spec_i.flux[band][0, these] * np.sqrt(spec_i.ivar[band][0, these]))
        snrs.append(snr) 
    i_sort = np.argsort(snrs) 
    for iobs in i_sort: 
        spec_i = lgal_bgsSpec(galids[0], iobs, lib='bc03', obs_sampling='spacefill')
        for band in ['b', 'r', 'z']: 
            sub.plot(spec_i.wave[band], spec_i.flux[band][0], lw=1, c='C'+str(iobs))
    sub.set_xlabel(r'Wavelenght [$\AA$]', fontsize=25) 
    sub.set_xlim([3600., 9800.]) 
    sub.set_ylabel(r'Flux [$10^{-17} ergs/s/cm^2/\AA$]', fontsize=25) 
    sub.set_ylim([0., 10.]) 

    fig.savefig(''.join([UT.fig_dir(), 'spectral_challenge.lgal_bgsSpec.iobs.png']), 
            bbox_inches='tight') 

    return None
Ejemplo n.º 2
0
def singleSP(iz, age=1., mtot=1e8, zred=0.01):
    ''' construct spectra of single stellar population with single metallicity, no dust, 
    no nonsense from firefly SSPs (for consistency). metallicity set by z_arr[iz] where z_arr is
    '''
    z_arr = np.array([0.5, 1.0, 2.0, 10**-1.301, 10**-1.302, 10**-2.301, 10**-2.302, 10**-0.6, 
        10**-0.9, 10**-1.2, 10**-1.6, 10**-1.9]) 
    z_strs = np.array(['z001', 'z002', 'z004', 'z0001.bhb', 'z0001.rhb', 'z10m4.bhb', 'z10m4.rhb', 
        'z-0.6', 'z-0.9', 'z-1.2', 'z-1.6', 'z-1.9']) 
    z_metal = z_arr[iz]

    f_ssp = os.environ['STELLARPOPMODELS_DIR']+'/data/SSP_M11_MILES/ssp_M11_MILES.cha'+z_strs[iz]
    model_age, model_wave, model_flux = np.loadtxt(f_ssp, unpack=True, usecols=[0,2,3]) 

    isage = (model_age == age)
    wave = model_wave[isage] # wavelength
    flux = model_flux[isage] # flux [ergs/s/A]
    
    cosmo = FlatLambdaCDM(70., 0.3) 
    wave *= (1. + zred)
    flux *= mtot / (4.*np.pi * cosmo.luminosity_distance(zred).to(U.cm).value**2)

    fig = plt.figure(figsize=(10,4))
    sub = fig.add_subplot(111)
    sub.plot(wave, flux, c='k', lw=1)
    sub.set_xlabel('observed-frame wavelength [$A$]', fontsize=15) 
    sub.set_xlim([3500., 1e4]) 
    sub.set_ylabel('flux [$ergs/s/cm^2/A$]', fontsize=15) 
    sub.set_ylim([0., flux[(wave > 3000.) & (wave < 1e4)].max()])
    fig.savefig(''.join([UT.fig_dir(), 'SSP.iz', str(iz), '.age', str(age), '.z', str(zred), '.png']), bbox_inches='tight') 

    # save spectra 
    f_spec = ''.join([UT.dat_dir(), 'SSP.iz', str(iz), '.age', str(age), '.z', str(zred), '.dat']) 
    np.savetxt(f_spec, np.vstack([wave, flux]).T)
    return None 
Ejemplo n.º 3
0
def ifsps_mcmc():
    ifsps = fitters.iFSPS(model_name='vanilla')
    # theta: mass, Z, t_age, dust2, tau
    tt_fid = np.array([9, np.log10(0.019), 10., 0., 2.])
    w, spec = ifsps.model(tt_fid, zred=0.1)
    wlim = (w > 3000.) & (w < 10000.)
    w = w[wlim]
    spec = spec[wlim] / 1e17
    spec_err = 0.1 * spec
    post = ifsps.mcmc(w, spec, spec_err, 0.1, nwalkers=100)
    chain = ifsps.sampler.flatchain

    print 'input', tt_fid
    print 'output', post['theta_med']
    lbls = [r'$\log M_*$', r'$\log Z$', r'$t_{\rm age}$', 'dust2', r'$\tau$']
    fig = DFM.corner(chain,
                     labels=lbls,
                     truths=tt_fid,
                     quantiles=[0.16, 0.5, 0.84],
                     levels=[0.68, 0.95],
                     range=ifsps.priors,
                     smooth=True,
                     bins=20,
                     plot_datapoints=False,
                     fill_contours=True,
                     plot_density=False,
                     color='C0')
    fig.savefig(os.path.join(UT.fig_dir(), 'ifsps_mcmc.test.png'),
                bbox_inches='tight')
    return None
Ejemplo n.º 4
0
def plot_obs_SkyBrightness():
    w_sky, skybrights = obs_SkyBrightness(sampling='spacefill')
    fig = plt.figure(figsize=(15,5))
    sub = fig.add_subplot(111)
    for i in range(skybrights.shape[0]):
        sub.plot(w_sky, skybrights[i,:], lw=1, c='C'+str(i)) 
    sub.set_xlabel(r'Wavelenght [$\AA$]', fontsize=25) 
    sub.set_xlim([3600., 9800.]) 
    sub.set_ylabel(r'Sky Surface Brightness', fontsize=25) 
    sub.set_ylim([0., 20.]) 

    fig.savefig(''.join([UT.fig_dir(), 'spectral_challenge.obs_SkyBrightness.png']), 
            bbox_inches='tight') 
    return None
Ejemplo n.º 5
0
def plot_obs_condition(): 
    obscond = obs_condition(sampling='spacefill') 
    airmass, seeing, exptime, moonfrac, moonalt, moonsep, sunalt, sunsep = obscond.T

    fig = plt.figure(figsize=(20,4))
    sub = fig.add_subplot(141)
    for i in range(len(airmass)): 
        sub.scatter([airmass[i]], [seeing[i]], c='C'+str(i), s=20) 
    sub.set_xlabel('Airmass', fontsize=20)  
    sub.set_xlim([1., 2.]) 
    sub.set_ylabel('Seeing', fontsize=20) 
    sub.set_ylim([0.95, 1.75]) 
    
    sub = fig.add_subplot(142)
    for i in range(len(airmass)): 
        sub.scatter([exptime[i]], [moonfrac[i]], c='C'+str(i), s=20) 
    sub.set_xlabel('Exposure Time', fontsize=20)  
    sub.set_xlim([400., 1200.]) 
    sub.set_ylabel('Moon Fraction', fontsize=20) 
    sub.set_ylim([0.5, 1.]) 
    
    sub = fig.add_subplot(143)
    for i in range(len(airmass)): 
        sub.scatter([moonsep[i]], [moonalt[i]], c='C'+str(i), s=20) 
    sub.set_xlabel('Moon Separation', fontsize=20)  
    sub.set_xlim([40., 120.]) 
    sub.set_ylabel('Moon Altitude', fontsize=20) 
    sub.set_ylim([-90, 90.]) 
    
    sub = fig.add_subplot(144)
    for i in range(len(airmass)): 
        sub.scatter([sunsep[i]], [sunalt[i]], c='C'+str(i), s=10) 
    sub.set_xlabel('Sun Separation', fontsize=20)  
    sub.set_xlim([40., 180.]) 
    sub.set_ylabel('Sun Altitude', fontsize=20) 
    sub.set_ylim([-90, 0.]) 
    
    fig.subplots_adjust(wspace=0.5)
    fig.savefig(''.join([UT.fig_dir(), 'spectral_challenge.obs_condition.png']), 
            bbox_inches='tight') 
    return None 
Ejemplo n.º 6
0
def singleSP_myfirefly(iz, age=1., mtot=1e8, zred=0.01):
    ''' fit the single stellar population file from `singleSP` using firefly 
    '''
    z_arr = np.array([0.5, 1.0, 2.0, 10**-1.301, 10**-1.302, 10**-2.301, 10**-2.302, 10**-0.6, 
        10**-0.9, 10**-1.2, 10**-1.6, 10**-1.9]) 

    f_spec = ''.join([UT.dat_dir(), 'SSP.iz', str(iz), '.age', str(age), '.z', str(zred), '.dat']) 
    wave, flux = np.loadtxt(f_spec, unpack=True, usecols=[0,1]) 
    wave_rest = wave/(1.+zred)
    err = flux * 0.001

    cosmo = FlatLambdaCDM(70., 0.3) 
    ffly = Fitters.myFirefly(cosmo, 
            model='m11', 
            model_lib='MILES', 
            imf='cha', 
            dust_corr=False,                        # no dust correction
            age_lim=[0., cosmo.age(zred).value],    # can't have older populations
            logZ_lim=[np.log10(z_arr[iz])-0.1, np.log10(z_arr[iz])+0.1])
    mask = ffly.emissionlineMask(wave_rest)
    ff_fit, ff_prop = ffly.Fit(wave, flux, err, zred, mask=mask, flux_unit=1.)
    
    print('total mass = %f' % ff_prop['logM_total']) 
    for i in range(ff_prop['n_ssp']): 
        print('--- SSP %i; weight=%f ---' % (i, ff_prop['weightLight_ssp_'+str(i)]))
        print('age = %f'% ff_prop['age_ssp_'+str(i)])
        print('log Z = %f'% ff_prop['logZ_ssp_'+str(i)])
        print('log M_tot = %f' % ff_prop['logM_total_ssp_'+str(i)])

    fig = plt.figure()
    sub = fig.add_subplot(111)
    sub.plot(ff_fit['wavelength'], ff_fit['flux'], label='SSP') 
    sub.plot(ff_fit['wavelength'], ff_fit['flux_model'], label='myFirefly best-fit') 
    sub.plot(ff_fit['wavelength'], ff_fit['flux'] - ff_fit['flux_model'], label='residual') 
    #sub.plot(wave_rest, flux, c='k', ls=':') 
    sub.set_xlim([3e3, 1e4]) 
    fig.savefig(''.join([UT.fig_dir(), 'myfirefly.SSP.iz', str(iz), '.age', str(age), '.z', str(zred), '.png']), 
            bbox_inches='tight')
    return None 
Ejemplo n.º 7
0
sub = plt.subplot(gs[0, :])
tt_i = (prior_max - prior_min) * np.random.uniform(size=len(prior_min))
tt_i[:6] = np.array([1.4, 2.5, 3., 3., 0.003, 0.003])
w, lum_i = sspLum(tt_i)
wobs = w * (1. + zred)
norm = Mstar / np.sum(lum_i)
flux = lum_i * norm * factor_flux
sub.plot(wobs, flux, c='C0')
sub.set_xlabel('observed-frame wavelength [$A$]', fontsize=20)
sub.set_xlim(3e3, 1e4)
sub.set_ylabel('flux [$erg/s/cm^2/A$]', fontsize=25)
sub.set_ylim(0, 1e-15)
# plot SFH
sub = plt.subplot(gs[1, 0])  # SFH
sfh = np.dot(tt_i[:Ncomp_sfh], nmf_sfh_basis)
sub.plot(t_lookback, sfh * norm)
sub.set_xlabel('lookback time [Gyr]', fontsize=20)
sub.set_xlim(0, 13)
sub.set_yscale('log')
sub.set_ylabel(r'SFH [$M_\odot$]', fontsize=20)
# plot SFH
sub = plt.subplot(gs[1, 1])  # SFH
zh = np.dot(tt_i[Ncomp_sfh:Ncomp_sfh + Ncomp_zh], nmf_zh_basis)
sub.plot(t_lookback, zh)
sub.set_xlabel('lookback time [Gyr]', fontsize=20)
sub.set_xlim(0, 13)
sub.set_yscale('log')
sub.set_ylabel("metallicity, $Z$", fontsize=20)
fig.savefig(os.path.join(UT.fig_dir(), 'fsps_wrapper_test.png'),
            bbox_inches='tight')
Ejemplo n.º 8
0
def singleSP_SFH_myfirefly(iz, zred=0.01):
    ''' construct spectra with single metallicity, with eagle SFH, no dust, no nonsense  
    '''
    z_arr = np.array([0.5, 1.0, 2.0, 10**-1.301, 10**-1.302, 10**-2.301, 10**-2.302, 10**-0.6, 
        10**-0.9, 10**-1.2, 10**-1.6, 10**-1.9]) 
    z_strs = np.array(['z001', 'z002', 'z004', 'z0001.bhb', 'z0001.rhb', 'z10m4.bhb', 'z10m4.rhb', 
        'z-0.6', 'z-0.9', 'z-1.2', 'z-1.6', 'z-1.9']) 
    z_metal = z_arr[iz]

    # read in SFH and M_* 
    f_eagle = h5py.File(''.join([UT.dat_dir(), '0EAGLE_SFRHs.hdf5']), 'r') 
    i_zmid = np.abs(z_arr[iz] - 10**np.array(logZ_mid)).argmin()
    logZ = logZ_mid[i_zmid] 
    t = age_bounds[:,1]
    dt = age_bounds[:,2] - age_bounds[:,0]
    sfh = f_eagle['SFRH'].value[0,:,i_zmid]

    f_ssp = os.environ['STELLARPOPMODELS_DIR']+'/data/SSP_M11_MILES/ssp_M11_MILES.cha'+z_strs[iz]
    model_age, model_wave, model_flux = np.loadtxt(f_ssp, unpack=True, usecols=[0,2,3]) 
    
    age_uniq = np.sort(np.unique(model_age))
    age_bin = np.concatenate([[0], 0.5*(age_uniq[1:] + age_uniq[:-1])])
    
    mtot_bin, flux_bin = [], [] 
    for i_a in range(len(age_bin)-1): 
        in_agebin = ((14.-t >= age_bin[i_a]) & (14-t < age_bin[i_a+1]))
        mtot_i = np.sum(dt[in_agebin] * sfh[in_agebin] * 1e9)
        mtot_bin.append(mtot_i) 

    assert np.sum(mtot_bin) == np.sum(sfh * dt * 1e9)
    print('log M_tot = %f' % np.log10(np.sum(mtot_bin)))

    # read in spectra 
    f_spec = ''.join([UT.dat_dir(), 'SSP_SFH.iz', str(iz), '.z', str(zred), '.dat']) 
    wave, flux = np.loadtxt(f_spec, unpack=True, usecols=[0,1]) 
    wave_rest = wave/(1.+zred)
    err = flux * 0.001

    cosmo = FlatLambdaCDM(70., 0.3) 
    ffly = Fitters.myFirefly(cosmo, 
            model='m11', 
            model_lib='MILES', 
            imf='cha', 
            dust_corr=False,                        # no dust correction
            age_lim=[0., cosmo.age(zred).value],    # can't have older populations
            logZ_lim=[np.log10(z_arr[iz])-0.1, np.log10(z_arr[iz])+0.1])
    mask = ffly.emissionlineMask(wave_rest)
    ff_fit, ff_prop = ffly.Fit(wave, flux, err, zred, mask=mask, flux_unit=1.)
    
    print('total mass = %f' % ff_prop['logM_total']) 
    ssp_age_bin0, ssp_age_bin1 = [], [] 
    ssp_mtot = [] 
    for i in range(ff_prop['n_ssp']): 
        print('--- SSP %i; weight=%f ---' % (i, ff_prop['weightLight_ssp_'+str(i)]))
        print('age = %f'% ff_prop['age_ssp_'+str(i)])
        print('log Z = %f'% ff_prop['logZ_ssp_'+str(i)])
        print('log M_tot = %f' % ff_prop['logM_total_ssp_'+str(i)])
        i_age_bin = np.abs(age_uniq - ff_prop['age_ssp_'+str(i)]).argmin()
        ssp_age_bin0.append(age_bin[i_age_bin]) 
        ssp_age_bin1.append(age_bin[i_age_bin+1])
        ssp_mtot.append(10**ff_prop['logM_total_ssp_'+str(i)]) 
    
    fig = plt.figure(figsize=(12,4))
    sub = fig.add_subplot(121)
    sub.bar(age_bin[:-1], mtot_bin, width=age_bin[:-1]-age_bin[1:], alpha=0.75)
    sub.bar(ssp_age_bin0, ssp_mtot, width=np.array(ssp_age_bin0)-np.array(ssp_age_bin1), alpha=0.75)
    sub.set_xlabel('Lookback Time [Gyr]', fontsize=25) 
    sub.set_xlim([0., 14]) 
    sub.set_ylabel("$M_\mathrm{form}$", fontsize=25)
    sub.set_yscale("log") 
    sub.set_ylim([1e7, 5e9]) 

    sub = fig.add_subplot(122)
    sub.plot(ff_fit['wavelength'], ff_fit['flux'], label='SSP') 
    sub.plot(ff_fit['wavelength'], ff_fit['flux_model'], label='myFirefly best-fit') 
    sub.plot(ff_fit['wavelength'], ff_fit['flux'] - ff_fit['flux_model'], label='residual') 
    #sub.plot(wave_rest, flux, c='k', ls=':') 
    sub.set_xlim([3e3, 1e4]) 
    fig.savefig(''.join([UT.fig_dir(), 'myfirefly.SSP_SFH.iz', str(iz), '.z', str(zred), '.png']), 
            bbox_inches='tight')
    return None 
Ejemplo n.º 9
0
def singleSP_SFH(iz, zred=0.01):
    ''' construct spectra with single metallicity, with eagle SFH, no dust, no nonsense  
    '''
    z_arr = np.array([0.5, 1.0, 2.0, 10**-1.301, 10**-1.302, 10**-2.301, 10**-2.302, 10**-0.6, 
        10**-0.9, 10**-1.2, 10**-1.6, 10**-1.9]) 
    z_strs = np.array(['z001', 'z002', 'z004', 'z0001.bhb', 'z0001.rhb', 'z10m4.bhb', 'z10m4.rhb', 
        'z-0.6', 'z-0.9', 'z-1.2', 'z-1.6', 'z-1.9']) 
    z_metal = z_arr[iz]

    # read in SFH and M_* 
    f_eagle = h5py.File(''.join([UT.dat_dir(), '0EAGLE_SFRHs.hdf5']), 'r') 
    i_zmid = np.abs(z_arr[iz] - 10**np.array(logZ_mid)).argmin()
    logZ = logZ_mid[i_zmid] 
    t = age_bounds[:,1]
    dt = age_bounds[:,2] - age_bounds[:,0]
    sfh = f_eagle['SFRH'].value[0,:,i_zmid]

    f_ssp = os.environ['STELLARPOPMODELS_DIR']+'/data/SSP_M11_MILES/ssp_M11_MILES.cha'+z_strs[iz]
    model_age, model_wave, model_flux = np.loadtxt(f_ssp, unpack=True, usecols=[0,2,3]) 
    
    age_uniq = np.sort(np.unique(model_age))
    age_bin = np.concatenate([[0], 0.5*(age_uniq[1:] + age_uniq[:-1])])
    
    mtot_bin, flux_bin = [], [] 
    for i_a in range(len(age_bin)-1): 
        in_agebin = ((14.-t >= age_bin[i_a]) & (14-t < age_bin[i_a+1]))
        mtot_i = np.sum(dt[in_agebin] * sfh[in_agebin] * 1e9)
        mtot_bin.append(mtot_i) 

        isage = (model_age == age_uniq[i_a])
        flux_i = model_flux[isage] * mtot_i
        flux_bin.append(flux_i)
        if i_a == 0: 
            wave = model_wave[isage] # wavelength
            flux = flux_i 
        else: 
            flux += flux_i 
    flux_bin = np.array(flux_bin)

    cosmo = FlatLambdaCDM(70., 0.3) 
    wave *= (1. + zred)
    flux /= (4.*np.pi * cosmo.luminosity_distance(zred).to(U.cm).value**2)
    flux_bin /= (4.*np.pi * cosmo.luminosity_distance(zred).to(U.cm).value**2)

    assert np.sum(mtot_bin) == np.sum(sfh * dt * 1e9)
    print('log M_tot = %f' % np.log10(np.sum(mtot_bin)))
    
    fig = plt.figure(figsize=(12,4))
    sub = fig.add_subplot(121)
    sub.bar(age_bin[:-1], mtot_bin, width=age_bin[:-1]-age_bin[1:])
    sub.bar(14.-age_bounds[:,0], sfh * dt * 1e9, width=dt)
    sub.set_xlabel('Lookback Time [Gyr]', fontsize=25) 
    sub.set_xlim([0., 14]) 
    sub.set_ylabel("$M_\mathrm{form}$", fontsize=25)
    sub.set_yscale("log") 
    sub.set_ylim([1e7, 5e9]) 

    sub = fig.add_subplot(122)
    for i in range(flux_bin.shape[0]): 
        sub.plot(wave, flux_bin[i])
    sub.plot(wave, flux, c='k', lw=3) 
    sub.set_xlabel('observed-frame wavelength [$A$]', fontsize=20) 
    sub.set_xlim([3500., 1e4]) 
    sub.set_ylabel('flux [$ergs/s/cm^2/A$]', fontsize=20) 
    fig.savefig(''.join([UT.fig_dir(), 'SSP_SFH.iz', str(iz), '.z', str(zred), '.png']), bbox_inches='tight') 

    # save spectra 
    f_spec = ''.join([UT.dat_dir(), 'SSP_SFH.iz', str(iz), '.z', str(zred), '.dat']) 
    np.savetxt(f_spec, np.vstack([wave, flux]).T)
    return None 
Ejemplo n.º 10
0
def Lgal_nonoisePhoto(lib='bc03', dust=False, overwrite=False, validate=False): 
    ''' generate noiseless photometry from noiseless spectral_challenge Lgal spectra 
    generated by Rita. Super simple code that convolves the spectra with the bandpass 
    decam g, r, z, W1, W2, W3, W4 filtes. 
    '''
    fsource = f_nonoise(4, lib=lib)
    fsource = os.path.basename(fsource).rsplit('.', 1)[0].rsplit('_BGS_template_')[1]
    if dust: str_dust = 'dust'
    else: str_dust = 'nodust'
    fphot = os.path.join(UT.dat_dir(), 'spectral_challenge', 'bgs','photo.%s.%s.nonoise.dat' % (fsource, str_dust))

    if os.path.isfile(fphot) and not overwrite: 
        _mags = np.loadtxt(fphot, unpack=True, skiprows=1, usecols=range(1,8))
        mags = _mags.T
    else: 
        galids = np.unique(testGalIDs()) # IDs of spectral_challenge galaxies 
            
        fluxes = np.zeros((len(galids), 8))
        fluxes[:,0] = galids
        for i, gid in enumerate(galids): 
            # read ins ource spectra
            spec_source = Lgal_nonoiseSpectra(gid, lib=lib)
            if dust: 
                flux = spec_source['flux_dust_nonoise']
            else: 
                flux = spec_source['flux_nodust_nonoise']
            # apply filters
            filter_response = specFilter.load_filters('decam2014-g', 'decam2014-r', 'decam2014-z','wise2010-W1', 'wise2010-W2', 'wise2010-W3', 'wise2010-W4')
            fluxes_i = filter_response.get_ab_maggies(np.atleast_2d(flux)*U.Watt/U.m**2/U.Angstrom, spec_source['wave']*U.Angstrom)
            fluxes[i,1:] = 1e9 * np.array([fluxes_i[0][0], fluxes_i[0][1], fluxes_i[0][2], fluxes_i[0][3], fluxes_i[0][4], fluxes_i[0][5], fluxes_i[0][6]]) # nanomaggies
        fmt = ['%.5e' for i in range(8)]
        fmt[0] = '%i'
        np.savetxt(fphot, fluxes, header='galid, g, r, z, W1, W2, W3, W4 fluxes in nanomaggies', fmt=fmt)
        mags = fUT.flux2mag(fluxes[:,1:], method='log') 

    if validate: # compare the Lgal nonoise photometry to photometry from GAMA-Legacy 
        cats = Cats.GamaLegacy() 
        gleg = cats.Read('g15') 

        fig = plt.figure(figsize=(20,4)) 
        for i_b, band in enumerate(['g', 'r', 'z', 'w1', 'w2']): 
            sub = fig.add_subplot(1,5,i_b+1) 
            sub.hist(fUT.flux2mag(gleg['legacy-photo']['flux_%s' % band]), color='k', range=(14,25), density=True, label='Legacy')
            sub.hist(mags[:,i_b], color='C1', range=(14, 25), density=True, alpha=0.5, label='LGal\nSpectral\nChallenge')
            sub.set_xlabel('%s magnitude' % band, fontsize=20) 
            sub.set_xlim(14, 25) 
        sub.legend(loc='upper right', fontsize=15) 
        fig.savefig(os.path.join(UT.fig_dir(), os.path.basename(fphot).replace('dat', 'hist.png')), bbox_inches='tight') 
        plt.close() 
    
        # g-r vs r-z
        fig = plt.figure(figsize=(6,6)) 
        sub = fig.add_subplot(111) 
        g_r = fUT.flux2mag(gleg['legacy-photo']['flux_g']) - fUT.flux2mag(gleg['legacy-photo']['flux_r'])
        r_z = fUT.flux2mag(gleg['legacy-photo']['flux_r']) - fUT.flux2mag(gleg['legacy-photo']['flux_z'])
        sub.scatter(g_r, r_z, c='k', s=1, label='Legacy')
        g_r = mags[:,0] - mags[:,1]
        r_z = mags[:,1] - mags[:,2]
        sub.scatter(g_r, r_z, c='C1', s=4, zorder=10, label='LGal\nSpectral Challenge')
        sub.set_xlabel('$g - r$', fontsize=20) 
        sub.set_xlim(-1., 3.)   
        sub.set_ylabel('$r - z$', fontsize=20) 
        sub.set_ylim(-1., 3.)   
        sub.legend(loc='upper right', handletextpad=0.2, markerscale=3, fontsize=15) 
        fig.savefig(os.path.join(UT.fig_dir(), os.path.basename(fphot).replace('dat', 'color.png')), bbox_inches='tight') 
        plt.close() 
    return mags 
Ejemplo n.º 11
0
def Lgal_noisePhoto(lib='bc03', dust=False, overwrite=False, validate=False): 
    ''' generate photometry from noiseless spectral_challenge Lgal spectra 
    generated by Rita then add realistic noise from Legacy survey.
    '''
    fsource = f_nonoise(4, lib=lib)
    fsource = os.path.basename(fsource).rsplit('.', 1)[0].rsplit('_BGS_template_')[1]
    if dust: str_dust = 'dust'
    else: str_dust = 'nodust'
    fphot = os.path.join(UT.dat_dir(), 'spectral_challenge', 'bgs', 
            'mag.%s.%s.legacy_noise.dat' % (fsource, str_dust))

    if os.path.isfile(fphot) and not overwrite: 
        _mags = np.loadtxt(fphot, unpack=True, skiprows=1, usecols=[-7, -6, -5, -4, -3, -2, -1])
        mags = _mags.T
    else: 
        galids = np.unique(testGalIDs()) # IDs of unique spectral_challenge spectra 
        
        mags = np.zeros((len(galids), 7)) 
        for i, gid in enumerate(galids): 
            # read ins ource spectra
            spec_source = Lgal_nonoiseSpectra(gid, lib=lib)
            if dust: 
                flux = spec_source['flux_dust_nonoise']
            else: 
                flux = spec_source['flux_nodust_nonoise']
            # apply filters
            filter_response = specFilter.load_filters('decam2014-g', 'decam2014-r', 'decam2014-z', 
                    'wise2010-W1', 'wise2010-W2', 'wise2010-W3', 'wise2010-W4')
            mags_i = filter_response.get_ab_magnitudes(flux*U.Watt/U.m**2/U.Angstrom, spec_source['wave']*U.Angstrom)
            mags[i,:] = np.array([mags_i[0][0], mags_i[0][1], mags_i[0][2], mags_i[0][3], 
                mags_i[0][4], mags_i[0][5], mags_i[0][6]]) # this is because mags_i is an astropy table. ugh 
        fluxs = fUT.mag2flux(mags, method='log') 

        # add in noise from legacy 
        cats = Cats.GamaLegacy() 
        gleg = cats.Read('g15') 
        for ib, band in enumerate(['g', 'r', 'z', 'w1', 'w2', 'w3', 'w4']): 
            _flux = gleg['legacy-photo']['flux_%s' % band] 
            _ivar = gleg['legacy-photo']['flux_ivar_%s' % band]
            if ib == 0: 
                gleg_fluxs = np.zeros((len(_flux), 7))
                gleg_ivars = np.zeros((len(_flux), 7))

            gleg_fluxs[:,ib] = _flux
            gleg_ivars[:,ib] = _ivar
            
        tree = KDTree(gleg_fluxs[:,:5]) 
        dist, indx = tree.query(fluxs[:,:5])
        ivars = np.zeros_like(mags)
        ivars = gleg_ivars[indx,:] 
        # now lets apply noise to thse mags 
        sig = ivars**-0.5
        fluxs += sig * np.random.randn(sig.shape[0], sig.shape[1]) # noisy flux 
        noisy_mags = fUT.flux2mag(fluxs) 
        
        hdr = 'galid, flux_g, flux_r, flux_z, flux_w1, flux_w2, flux_w3, flux_w4, flux_ivar_g, flux_ivar_r, flux_ivar_z, flux_ivar_w1, flux_ivar_w2, flux_ivar_w3, flux_ivar_w4, mag_g, mag_r, mag_z, mag_w1, mag_w2, mag_w3, mag_w4'
        fmt = ['%.5e' for i in range(22)]
        fmt[0] = '%i'
        np.savetxt(fphot, np.vstack([galids, fluxs.T, ivars.T, noisy_mags.T]).T, header=hdr, 
                fmt=fmt) 

    if validate: # compare the Lgal nonoise photometry to photometry from GAMA-Legacy 
        cats = Cats.GamaLegacy() 
        gleg = cats.Read('g15') 
        # histogram of magnitudes
        fig = plt.figure(figsize=(20,4)) 
        for i_b, band in enumerate(['g', 'r', 'z', 'w1', 'w2']): 
            sub = fig.add_subplot(1,5,i_b+1) 
            sub.hist(fUT.flux2mag(gleg['legacy-photo']['flux_%s' % band]), color='k', range=(14,25), density=True, label='Legacy')
            sub.hist(mags[:,i_b], color='C1', range=(14, 25), density=True, alpha=0.5, label='LGal\nSpectral\nChallenge')
            sub.set_xlabel('%s magnitude' % band, fontsize=20) 
            sub.set_xlim(14, 25) 
        sub.legend(loc='upper right', fontsize=15) 
        fig.savefig(os.path.join(UT.fig_dir(), os.path.basename(fphot).replace('dat', 'hist.png')), bbox_inches='tight') 
        plt.close() 

        # g-r vs r-z
        fig = plt.figure(figsize=(6,6)) 
        sub = fig.add_subplot(111) 
        g_r = fUT.flux2mag(gleg['legacy-photo']['flux_g']) - fUT.flux2mag(gleg['legacy-photo']['flux_r'])
        r_z = fUT.flux2mag(gleg['legacy-photo']['flux_r']) - fUT.flux2mag(gleg['legacy-photo']['flux_z'])
        sub.scatter(g_r, r_z, c='k', s=1, label='Legacy')
        g_r = mags[:,0] - mags[:,1]
        r_z = mags[:,1] - mags[:,2]
        sub.scatter(g_r, r_z, c='C1', s=4, zorder=10, label='LGal\nSpectral Challenge')
        sub.set_xlabel('$g - r$', fontsize=20) 
        sub.set_xlim(-1., 3.)   
        sub.set_ylabel('$r - z$', fontsize=20) 
        sub.set_ylim(-1., 3.)   
        sub.legend(loc='upper right', handletextpad=0.2, markerscale=3, fontsize=15) 
        fig.savefig(os.path.join(UT.fig_dir(), os.path.basename(fphot).replace('dat', 'color.png')), bbox_inches='tight') 
        plt.close() 
    return mags 
Ejemplo n.º 12
0
def ifsps():
    ifsps = fitters.iFSPS(model_name='vanilla')
    # theta: mass, Z, t_age, dust2, tau
    tt_fid = np.array([1e9, 0.019, 10., 0., 2.])
    tt_tau_p = np.array([1e9, 0.019, 10., 0., 3.])
    tt_tau_pp = np.array([1e9, 0.019, 10., 0., 5.])
    tt_tau_ppp = np.array([1e9, 0.019, 10., 0., 7.])
    ws, specs = ifsps.model(np.array([tt_fid, tt_tau_p, tt_tau_pp,
                                      tt_tau_ppp]),
                            zred=0.1)

    tt_tage_p = np.array([1e9, 0.019, 11., 0., 2.])
    tt_tage_pp = np.array([1e9, 0.019, 12., 0., 2.])
    tt_tage_ppp = np.array([1e9, 0.019, 13., 0., 2.])
    _ws, _specs = ifsps.model(np.array(
        [tt_fid, tt_tage_p, tt_tage_pp, tt_tage_ppp]),
                              zred=0.1)

    tt_dust_p = np.array([1e9, 0.019, 10., 1., 2.])
    tt_dust_pp = np.array([1e9, 0.019, 10., 2., 2.])
    tt_dust_ppp = np.array([1e9, 0.019, 10., 3., 2.])
    __ws, __specs = ifsps.model(np.array(
        [tt_fid, tt_dust_p, tt_dust_pp, tt_dust_ppp]),
                                zred=0.1)

    fig = plt.figure(figsize=(10, 12))
    sub = fig.add_subplot(311)
    for w, spec in zip(ws, specs):
        sub.plot(w, spec)
    sub.text(0.95,
             0.95,
             r'varying $\tau$',
             ha='right',
             va='top',
             transform=sub.transAxes,
             fontsize=20)
    sub.set_xlim(3e3, 1e4)
    sub.set_ylim(0, 2e-17)
    sub = fig.add_subplot(312)
    for w, spec in zip(_ws, _specs):
        sub.plot(w, spec, ls='--')
    sub.text(0.95,
             0.95,
             r'varying $t_{\rm age}$',
             ha='right',
             va='top',
             transform=sub.transAxes,
             fontsize=20)
    sub.set_xlim(3e3, 1e4)
    sub.set_ylim(0, 2e-17)
    sub = fig.add_subplot(313)
    for w, spec in zip(__ws, __specs):
        sub.plot(w, spec, ls='--')
    sub.text(0.95,
             0.95,
             'varying dust2',
             ha='right',
             va='top',
             transform=sub.transAxes,
             fontsize=20)
    sub.set_xlabel('Wavelength [Angstrom]', fontsize=25)
    sub.set_xlim(3e3, 1e4)
    sub.set_ylim(0, 2e-17)
    fig.savefig(os.path.join(UT.fig_dir(), 'ifsps.test.png'),
                bbox_inches='tight')
    return None
Ejemplo n.º 13
0
def firefly_Mstar(lib='bc03', obs_sampling='spacefill', model='m11', model_lib='MILES', imf='cha', hpf_mode='on'):
    ''' see how well Mstar inferred from firefly reproduces the 
    input mstar 
    '''
    obscond = obs_condition(sampling=obs_sampling) 
    galids = testGalIDs()
    obs = [0]#range(8)

    mtot_ffly_source, mtot_ffly_source_nodust, mtot_ffly_obs = [], [], [] 
    mstar_ffly_source, mstar_ffly_source_nodust, mstar_ffly_obs = [], [], [] 
    mtot_input, mdisk_input, mbulg_input = [], [], [] 
    for iobs in obs: 
        mstar_ffly, mtot_ffly = [], []
        for galid in galids: 
            # read firefly fitting output  
            f_bgs = f_BGSspec(galid, iobs, lib=lib, obs_sampling=obs_sampling, nobs=obscond.shape[0])
            f_firefly = ''.join([UT.dat_dir(), 'spectral_challenge/bgs/', 
                'firefly.', model, '.', model_lib, '.imf_', imf, '.dust_', hpf_mode, '__', 
                f_bgs.rsplit('/', 1)[1].rsplit('.fits', 1)[0], '.hdf5']) 
            ffly_out, ffly_prop = UT.readFirefly(f_firefly) 
            mstar_ffly.append(10**ffly_prop['stellar_mass'])
            
            if iobs == 0: 
                gal_input = LGalInput(galid) 
                mdisk_input.append(np.sum(gal_input['sfh_disk']))
                mbulg_input.append(np.sum(gal_input['sfh_bulge']))
                mtot_input.append(np.sum(gal_input['sfh_disk'])+np.sum(gal_input['sfh_bulge']))

                # source firefly file 
                f_specin = f_Source(galid, lib=lib)
                f_firefly_s = ''.join([UT.dat_dir(), 'spectral_challenge/bgs/', 
                    'firefly.', model, '.', model_lib, '.imf_', imf, '.dust_', hpf_mode, '__', 
                    f_specin.rsplit('/', 1)[1].rsplit('.fits', 1)[0], '.hdf5']) 
                ffly_out, ffly_prop = UT.readFirefly(f_firefly_s) 
                mtot_ffly_source.append(10**ffly_prop['total_mass'])
                mstar_ffly_source.append(10**ffly_prop['stellar_mass'])
                
                # source no dust firefly file 
                f_firefly_s = ''.join([UT.dat_dir(), 'spectral_challenge/bgs/', 
                    'firefly.', model, '.', model_lib, '.imf_', imf, '.dust_', hpf_mode, '__', 
                    f_specin.rsplit('/', 1)[1].rsplit('.fits', 1)[0], '.nodust.hdf5']) 
                ffly_out, ffly_prop = UT.readFirefly(f_firefly_s) 
                mtot_ffly_source_nodust.append(10**ffly_prop['total_mass'])
                mstar_ffly_source_nodust.append(10**ffly_prop['stellar_mass'])

        mtot_ffly_obs.append(mstar_ffly) 
        mstar_ffly_obs.append(mstar_ffly) 
    
    fig = plt.figure(figsize=(7,7))
    sub = fig.add_subplot(111)
    sub.hist(np.log10(mtot_input), range=(9,12), bins=20, histtype='step', color='k', linewidth=2, label='input')  
    sub.hist(np.log10(mstar_ffly_source), range=(9,12), bins=20, histtype='step', 
            color='k', linewidth=1, linestyle=':', label='source')
    sub.hist(np.log10(mstar_ffly_source_nodust), range=(9,12), bins=20, histtype='step', 
            color='r', linewidth=1, linestyle=':', label='source (no dust)')
    for iobs in obs: 
        sub.hist(np.log10(mstar_ffly_obs[iobs]), range=(9,12), bins=20, histtype='step', 
                color='C'+str(iobs), linewidth=1, label=r'bgs; $i_\mathrm{obs}='+str(iobs+1)+'$')
    sub.set_xlabel(r'$\log(\,M_*$ [$M_\odot$]\,)', fontsize=25) 
    sub.set_xlim([9, 12])
    sub.legend(loc='upper right', frameon=True, fontsize=20) 
    fig.savefig(''.join([UT.fig_dir(), 
        'mstar_hist.', f_firefly.rsplit('/', 1)[1].rsplit('__', 1)[0], '.png']), bbox_inches='tight') 

    fig = plt.figure(figsize=(7,7))
    sub = fig.add_subplot(111)
    sub.scatter(mtot_input, mtot_ffly_source, s=10, color='k', label='source')
    sub.scatter(mtot_input, mtot_ffly_source_nodust, s=10, color='r', label='source (no dust)')
    for iobs in obs: 
        sub.scatter(mtot_input, mtot_ffly_obs[iobs], s=5, color='C'+str(iobs), label=r'bgs; $i_\mathrm{obs}='+str(iobs+1)+'$')
    sub.plot([1e8, 1e12], [1e8,1e12], c='k', ls='--')
    sub.set_xlabel(r'$M_*^\mathrm{(input)}$ [$M_\odot$]', fontsize=25) 
    sub.set_xscale('log')
    sub.set_xlim([1e8, 1e12])
    sub.set_ylabel(r'$M_*^\mathrm{(firefly)}$ [$M_\odot$]', fontsize=25) 
    sub.set_yscale('log')
    sub.set_ylim([1e8, 1e12])
    sub.legend(loc='upper left', markerscale=5, handletextpad=0, fontsize=20) 
    fig.savefig(''.join([UT.fig_dir(), 
        'mstar_comparison.', f_firefly.rsplit('/', 1)[1].rsplit('__', 1)[0], '.png']), bbox_inches='tight') 

    fig = plt.figure(figsize=(7,7))
    sub = fig.add_subplot(111)
    sub.hist(np.log10(mtot_input) - np.log10(mstar_ffly_source), range=(-1,5), bins=20, color='k', histtype='stepfilled', alpha=0.5, label='source')
    sub.hist(np.log10(mtot_input) - np.log10(mstar_ffly_source_nodust), range=(-1,5), bins=20, color='r', histtype='stepfilled', alpha=0.5, label='source (no dust)')
    for iobs in obs: 
        sub.hist(np.log10(mtot_input) - np.log10(mstar_ffly_obs[iobs]), range=(-1,5), bins=20, histtype='stepfilled', alpha=0.5,
                color='C'+str(iobs), label=r'bgs; $i_\mathrm{obs}='+str(iobs+1)+'$')
    sub.set_xlabel(r'$\log(\,M_*^\mathrm{(input)}\,)-\,\log(\,M_*^\mathrm{(firefly)}\,)$ ', fontsize=25) 
    sub.set_xlim([-1, 5])
    sub.legend(loc='upper right', fontsize=20) 
    fig.savefig(''.join([UT.fig_dir(), 
        'dmstar_hist.', f_firefly.rsplit('/', 1)[1].rsplit('__', 1)[0], '.png']), bbox_inches='tight') 
    return None