wave = wave * u.AA vs = wave.to(u.Hz, equivalencies=u.spectral()) Z = [] gunn = ['u', 'g', 'r', 'i', 'z', 'y'] classic = ['U', 'G', 'R', 'B', 'V', 'I'] for i, x in enumerate(flux): interim = x * u.erg / u.s / u.cm / u.cm / u.AA Fv = interim.to(u.erg / u.s / u.cm / u.cm / u.Hz, equivalencies=u.spectral_density(wave)) Fv *= 1.e-17 mags = get_appmags(vs.value[::-1], Fv.value[::-1], filters, printit=False) if meta['AGE'][i] < 0.3: ## Z.append([mags[i] for i in gunn + isubaru]) Z.append([mags[i] for i in gunn]) ## Z = np.array(Z) print(Z) print('\n\n') sm = SOMFactory().build(Z, normalization='var', initialization='random', component_names=gunn)
## Age [Myr]; vs [Hz]; SEDS [ergs/s/Hz]; ls [A]; Ll [ergs/s/angstrom]. ## Ordered by wavelength. ages, vs, Fv, ls, Ll = read_ised( 'GALAXEV/models/Padova1994/salpeter/bc2003_hr_m72_salp_ssp.ised', AgeMyr=25., printit=False) ## Wavelengths for output. waves = np.arange(1., 1.5e4, 1.) ## Prep. filters. filters = prep_filters() if template: mags = get_appmags(vs, Fv, filters, printit=False) Ll *= 10.**(0.4 * mags['i'] - 0.4 * 22.5) owaves = np.arange(1., 1.e4, 0.1) interp = interp1d(ls.value, Ll.value, kind='linear', bounds_error=False, fill_value=0.0, assume_sorted=False) ## np.savetxt('../quickspectra/spectra/BC03/restframe/spec-BC03-z%.1lf.dat' % 0.0, np.c_[waves, interp(waves)],\ ## fmt='%.6le', header=' WAVELENGTH FLUX\n--------------------------') ## Twinned with unredshifted spectra for redrock template. .fits output. ## create_template(owaves, interp(owaves) / 1.e-17, 'LBG', printit = False)
def BC03_maker(ngal=None, restframe=False, printit=False, test=False, seed=314, redshifts=None, magnitudes=None, alliseds=False,\ calzetti=False, madau=False, hiwave=1.e4): np.random.seed(seed) if type(redshifts) == type(None): redshifts = 1.5 + np.linspace(0., 4.0, 20) if type(magnitudes) == type(None): magnitudes = 20.0 + np.arange(0., 7.0, 0.1) if printit: print('\n\nSolving for redshifts:\n' + ''.join('%.3lf, ' % x for x in redshifts)) print('\n\nSolving for magnitudes:\n' + ''.join('%.3lf, ' % x for x in magnitudes)) filters = prep_filters() dbands = ['u', 'g', 'r', 'i', 'z', 'y', 'J'] dfilters = OrderedDict(zip(dbands, [filters[x] for x in dbands])) owave = np.arange(3.6e3, hiwave, 0.1) ## Matched to gal. maker. count = 0 flux = [] ## Meta. objtypes = [] subtypes = [] meta_zs = [] meta_dbands = [] meta_ages = [] meta_imfs = [] meta_taus = [] meta_metals = [] meta_mags = [] meta_calz = [] root = os.environ['BEAST'] + '/gal_maker/dat/BC03/salpeter/' spectra = [] ## Save metallicity, tau and ages for every retained template. Ms = [] Is = [] Ts = [] Ages = [] files = [] if alliseds: files = glob.glob(os.environ['BEAST'] + '/gal_maker/dat/BOWLER/ised/*') else: ## Single stellar population. for metallicity in ['m42', 'm62']: files.append(root + '/bc2003_hr_%s_salp_ssp.ised' % metallicity) for fname in files: ## SED ordered by increasing frequency [ergs/s/Hz], Age [GYr], (Monotonically increasing) vs [Hertz]; (Monotonically decreasing) Waves [AA]. seds, ages, vs, ls = read_ised(fname) if alliseds: metallicity, imf, dummy, xx, xxx = fname.split( 'bc2003_lr_')[1].split('.ised')[0].split('_') tau = np.float(xx + '.' + xxx) else: metallicity = fname.split('_')[3] imf = 'salp' tau = np.NaN for ii, sed in enumerate(seds.T): if (ages[ii] > 0.01) & (ii % 25 == 0): ## Sample of Ages and metallicities. ## vs * Fv = ls * Fl. Fig. 13.5 of Cosmological Physics. if metallicity == 'm42': Ms.append(0.5) ## Zo elif metallicity == 'm62': Ms.append(1.0) ## Zo else: raise UserWarning( 'Hard code metallicity for given BC03 mXX: %s' % metallicity) Is.append(imf) Ts.append(tau) Ages.append(ages[ii]) spectra.append(sed) nmax = len(redshifts) * len(spectra) * len(magnitudes) if (ngal is None): ngal = nmax elif (ngal > nmax): ngal = nmax raise ValueWarning( 'Requested LBG number is larger than that available.') else: pass runs = [[z, specid, mag] for mag in magnitudes for z in redshifts for specid in np.arange(len(spectra))] if test: runs = [[2.3, 0, 22.], [2.3, 1, 22.], [1.7, 2, 22.], [1.7, 3, 22.], [2.2, 4, 22.]] printit = True ngal = 5 if restframe: ngal = len(spectra) runs = [[0.0, specid, 23.5] for specid in np.arange(len(spectra))] printit = True print(runs) ## nmax random selection of galaxies. runs = np.array(runs) runs = runs[np.random.choice(runs.shape[0], ngal, replace=False)] ## flux_factors = 10.0 ** (0.4 * (magnitudes[0] - magnitudes)) for run in runs: redshift = np.float(run[0]) specid = np.rint(run[1]).astype(np.int) magnitude = np.float(run[2]) EBV = 0.1 * choice(5, 1, replace=True) if printit: print('\nSolving for redshift: %.3lf, mag: %.3lf, spec. Age: %s, Spec. Metallicity: %s, EBV: %.3lf' % (redshift, magnitude,\ str(Ages[specid]), Ms[specid],\ EBV)) ## Spectra wavelength is very large. Cut to 2.e4 AA. waves = ls[ls < 2.e4] Fv = spectra[specid][ls < 2.e4] if calzetti: Av = 4.05 * EBV waves = waves.astype(np.double) Fv = ext_apply(calzetti00(waves, a_v=Av, r_v=4.05, unit='aa'), Fv) if madau & (not restframe): Fv = lephare_madau(waves[::-1], redshift)[::-1] * Fv waves = waves * u.AA Fv = Fv * u.erg / u.s / u.cm / u.cm / u.Hz Fl = Fv.to(u.erg / u.s / u.cm / u.cm / u.AA, equivalencies=u.spectral_density(waves)) ## Change wavelength sampling to linear with dlambda = 0.1A. xx = np.arange(waves.value.min(), waves.value.max(), 0.1) ## Note: reverse to monotically increasing wavelength for resample_flux. Fl = resample_flux(xx, waves.value[::-1], Fl.value[::-1]) * u.erg / u.s / u.cm / u.cm / u.AA waves = xx * u.AA Fv = Fl.to(u.erg / u.s / u.cm / u.cm / u.Hz, equivalencies=u.spectral_density(waves)) vs = waves.to(u.Hz, equivalencies=u.spectral()) if not restframe: ## Redshift spectra for quickspectra input. 'Else', leave unredshifted for redrock template. vs, Fv = redshift_spectra(vs.value, Fv.value, redshift) vs = vs * u.Hz waves = vs.to( u.AA, equivalencies=u.spectral()) ## Redshifted wavelength. Fv = Fv * u.erg / u.s / u.cm / u.cm / u.Hz mags = get_appmags(vs.value[::-1], Fv.value[::-1], filters, printit=printit) dband = get_detband(redshift, dfilters, printit=False, restframe=restframe) Fv *= 10.**(0.4 * (mags[dband] - magnitude) ) ## Normalise to magnitudes[0], e.g. 20.0 in g. ## And check. ## mags = get_appmags(vs, Fv, filters, printit = True) ## F_lambda from F_nu. Fl = Fv.to(u.erg / u.s / u.cm / u.cm / u.AA, equivalencies=u.spectral_density( waves)) ## [ ergs / s / sq. cm / A] if restframe: wave = waves.value Fl /= 1.e-17 owave = xx else: Fl /= 1.e-17 ## [1.e-17 ergs / s / sq. cm / A] assert np.all(np.diff(owave) > 0.0) Fl = resample_flux(owave, waves.value, Fl.value) * u.erg / u.s / u.cm / u.cm / u.AA ## Magnitude is arbitrary for redrock template. objtypes.append('BC03') if alliseds: subtypes.append('ALL') else: subtypes.append('SSP') meta_zs.append(redshift) meta_dbands.append(dband) meta_mags.append(magnitude) meta_ages.append(Ages[specid]) meta_metals.append(Ms[specid]) meta_imfs.append(Is[specid]) meta_taus.append(Ts[specid]) meta_calz.append(EBV[0]) flux.append(Fl.value) count += 1 if count >= ngal: flux = tuple(flux) flux = np.column_stack(flux) flux = flux.T meta = Table([objtypes, subtypes, meta_zs, meta_ages, meta_metals, meta_imfs, meta_taus, meta_calz, meta_dbands, meta_mags],\ names=('OBJTYPE', 'SUBTYPE', 'REDSHIFT', 'AGE', 'METALLICITY', 'IMF', 'Tau', 'CalzEBV', 'dband', 'AB mag'),\ meta={'name': 'BC03 MAKER META'}) if flux.min() < 0.0: raise ValueError('ERROR: z: %.3lf, spectra: %s' % (redshift, specname)) return flux, owave, meta
import numpy as np import astropy.constants as const from app_mags import get_appmags from magABsource import magAB from prep_filters import prep_filters magnitude = 26.5 dlambda = 0.1 ls = np.arange(3.e3, 1.e4, dlambda) ## Restframe [A]. vs = (1.e10 / ls) * const.c.to('m/s').value ## ls [AA]; vs [Hz]; c [m/s.] vs, Fv = magAB(vs, magnitude) ## Flat Fv filters = prep_filters(['LSST', 'VIDEO', 'STEIDEL', 'SUBARU', 'JKC'], normed=False) mags = get_appmags(vs, Fv, filters, printit=True)
def lbg_maker(ngal=None, restframe=False, printit=False, test=False, seed=314, redshifts=None, magnitudes=None, hiwave=1.0e4): np.random.seed(seed) if type(redshifts) == type(None): redshifts = 1.5 + np.linspace(0., 4.0, 20) if type(magnitudes) == type(None): magnitudes = 20.0 + np.arange(0., 7.0, 0.1) if restframe: ## EW magnitudes for quantiles of Shapley spectra; Left col. of pg. 8 (https://arxiv.org/pdf/astro-ph/0301230.pdf). Includes absorption edge. eqv_width = [-14.92, -1.10, 11.00, 14.30, 52.63] spectra = [ 'Q0_199.fits', 'Q2_198.fits', 'Q3_199.fits', 'QX_811.fits', 'Q4_198.fits' ] else: ## No need to sample both Q3 and QX in LBG generation. eqv_width = [-14.92, -1.10, 11.00, 52.63] spectra = ['Q0_199.fits', 'Q2_198.fits', 'Q3_199.fits', 'Q4_198.fits'] ## Order by decreasing EW. eqv_width = eqv_width[::-1] spectra = spectra[::-1] eqv_width = OrderedDict(zip(spectra, eqv_width)) ## Cut on EW considered. ## spectra = spectra[:2] if printit: print('\n\nSolving for redshifts:\n' + ''.join('%.3lf, ' % x for x in redshifts)) print('\n\nSolving for magnitudes:\n' + ''.join('%.3lf, ' % x for x in magnitudes)) root = os.environ['BEAST'] + '/gal_maker/dat/composites/' filters = prep_filters(names=['LSST', 'VIDEO']) ## Used to set detection band. dbands = ['u', 'g', 'r', 'i', 'z', 'y', 'J'] dfilters = OrderedDict(zip(dbands, [filters[x] for x in dbands])) wave = np.arange(3.6e3, hiwave, 0.2) ## Matched to gal. maker. count = 0 flux = [] ## Meta. objtypes = [] subtypes = [] meta_zs = [] meta_eqws = [] meta_mags = [] meta_dbands = [] nmax = len(redshifts) * len(spectra) * len(magnitudes) if (ngal is None): ngal = nmax elif (ngal > nmax): ngal = nmax raise ValueWarning( 'Requested LBG number is larger than that available.') else: pass runs = [[z, spec, mag] for mag in magnitudes for z in redshifts for spec in spectra] if test: ## EW magnitudes for quantiles of Shapley spectra; Left col. of pg. 8 (https://arxiv.org/pdf/astro-ph/0301230.pdf). Includes absorption edge. eqv_width = [-14.92, -1.10, 11.00, 14.30, 52.63] spectra = [ 'Q0_199.fits', 'Q2_198.fits', 'Q3_199.fits', 'QX_811.fits', 'Q4_198.fits' ] eqv_width = OrderedDict(zip(spectra, eqv_width)) runs = [[5.5, spectra[0], 19.5], [4.5, spectra[2], 20.5], [3.5, spectra[3], 21.5], [2.5, spectra[4], 21.5]] printit = True ngal = 4 if restframe: ## One for each Shapley composite. ngal = 5 runs = [[0.0, spec, 23.5] for spec in spectra] printit = True print(runs) ## Cut on number of galaxies requested. runs = np.array(runs) runs = runs[np.random.choice(runs.shape[0], ngal, replace=False)] ## flux_factors = 10.0 ** (0.4 * (magnitudes[0] - magnitudes)) for run in runs: redshift = np.float(run[0]) specname = run[1] magnitude = np.float(run[2]) if printit: print('Solving for redshift: %.3lf, mag: %.3lf, spec: %s' % (redshift, magnitude, specname)) fname = root + specname if fname.split('.')[-1] == 'fits': dat = fits.open(fname) ## WAT1_001 = 'wtype=linear label=Wavelength units=Angstroms'. See http://www-wfau.roe.ac.uk/6dFGS/fits.html waves = u.AA * ( dat[0].header['CRVAL1'] - (dat[0].header['CRPIX1'] - np.arange(dat[0].header['NAXIS1'])) * dat[0].header['CDELT1']) vs = waves.to(u.Hz, equivalencies=u.spectral()) ## AA to Hz. Fv = dat[ 0].data * u.erg / u.s / u.cm / u.cm / u.Hz ## [erg / s / sq. cm / Hz]. ## Flux enforced to be positive definite. Fv[Fv < 0.0] = 0.0 * u.erg / u.s / u.cm / u.cm / u.Hz else: dat = np.loadtxt(fname) waves = u.AA * dat[0] vs = waves.to(u.Hz, equivalencies=u.spectral()) ## AA to Hz. Fl = dat[1] * 1.e-17 * u.erg / u.s / u.cm / u.cm / u.AA Fv = Fl.to(u.erg / u.s / u.cm / u.cm / u.Hz, equivalencies=u.spectral_density(waves)) ## Extend break and flat Fv. redend = np.mean(Fv.value[(1.6e3 < waves.value) & (waves.value < 1.8e3)]) dwave = waves[1].value - waves[0].value lowext = np.arange(1.e2, waves.value.min() + dwave, dwave) hiext = np.arange(waves.value.max() + dwave, 2.e4, dwave) waves = np.concatenate([lowext, waves.value, hiext]) * u.AA Fv = np.concatenate([ np.zeros_like(lowext), Fv.value, redend * np.ones_like(hiext) ]) * u.erg / u.s / u.cm / u.cm / u.Hz vs = waves.to(u.Hz, equivalencies=u.spectral()) if not restframe: ## Redshift spectra for quickspectra input. 'Else', leave unredshifted for redrock template. vs, Fv = redshift_spectra(vs.value, Fv.value, redshift) vs = vs * u.Hz Fv = Fv * u.erg / u.s / u.cm / u.cm / u.Hz waves = vs.to( u.AA, equivalencies=u.spectral()) ## Redshifted wavelength. mags = get_appmags(vs.value[::-1], Fv.value[::-1], filters, printit=False) dband = get_detband(redshift, dfilters, printit=False, restframe=restframe) Fv *= 10.**(0.4 * (mags[dband] - magnitude) ) ## Normalise to magnitudes[0], e.g. 20.0 in g. ## And check. ## mags = get_appmags(vs, Fv, filters, printit = True) ## F_lambda from F_nu. Fl = Fv.to(u.erg / u.s / u.cm / u.cm / u.AA, equivalencies=u.spectral_density( waves)) ## [ ergs / s / sq. cm / A] if restframe: wave = waves.value Fl /= 1.e-17 else: Fl /= 1.e-17 ## [1.e-17 ergs / s / sq. cm / A] assert np.all(np.diff(wave) >= 0.0) Fl = resample_flux(wave, waves.value, Fl.value) * u.erg / u.s / u.cm / u.cm / u.AA ## Magnitude is arbitrary for redrock template. objtypes.append('LBG') subtypes.append(specname.split('_')[0]) meta_zs.append(redshift) meta_eqws.append(eqv_width[specname]) meta_mags.append(magnitude) meta_dbands.append(dband) flux.append(Fl.value) count += 1 if count >= ngal: flux = tuple(flux) flux = np.column_stack(flux) flux = flux.T meta = Table([objtypes, subtypes, meta_zs, meta_eqws, meta_mags, meta_dbands],\ names=('OBJTYPE', 'SUBTYPE', 'REDSHIFT', 'Lya-EW', 'AB mag', 'dbands'),\ meta={'name': 'LBG MAKER META'}) if flux.min() < 0.0: raise ValueError('ERROR: z: %.3lf, spectra: %s' % (redshift, specname)) return flux, wave, meta
band = 'u' mags = [] ls = np.arange(3.e3, 1.e4, 0.1) * u.AA vs = ls.to(u.Hz, equivalencies=u.spectral()).value filters = prep_filters(['LSST', 'VIDEO', 'STEIDEL', 'SUBARU', 'JKC', 'HUBBLE'], normed=False) for x in np.arange(0, 500, 1): vs, nFv = magAB(vs, mag = depths[band]) ## eqn. (10.2) of Chromey, Introduction to Observational Astronomy. planck = 6.62606885e-27 ## ergs * s. lightspeed = 2.9979e10 ## cm/s. phi = np.rint(vs * nFv / planck / lightspeed).astype(np.int) nphi = np.random.poisson(phi, len(vs)) nFv = planck * lightspeed * nphi / vs xx = get_appmags(vs, nFv, {band: filters[band]}, printit=False) mags.append(xx[band]) mags = np.array(mags) pl.hist(mags, bins=50) pl.axvline(depths[band], c='r') pl.show()
vs = wave.to(u.Hz, equivalencies=u.spectral()) ## y=x. xs = np.arange(-1.e2, 1.e2, 0.1) pl.plot(xs, xs, 'k-') X = [] for i, x in enumerate(flux): interim = x * u.erg / u.s / u.cm / u.cm / u.AA Fv = interim.to(u.erg / u.s / u.cm / u.cm / u.Hz, equivalencies=u.spectral_density(wave)) Fv *= 1.e-17 mags = get_appmags(vs.value, Fv.value, filters, printit=False) ACS_beta = 5.30 * (mags['ACS_F775W'] - mags['ACS_F850LP']) - 2.04 LSST_beta = 5.30 * (mags['i'] - mags['z']) - 2.04 X.append([ACS_beta, LSST_beta]) pl.plot(ACS_beta, LSST_beta, 'ko', markersize=3) X = np.array(X) X1 = X[:, 0].reshape(-1, 1) X2 = X1**2. Y1 = X[:, 1].reshape(-1, 1)
waves = np.arange(np.min(waves.value), 1.5e4, 0.1) * u.AA ## [Angstroms]. vs = waves.to(u.Hz, equivalencies=u.spectral()) Fv = Fv( vs.value ) * u.erg / u.s / u.cm / u.cm / u.Hz ## [erg / s / sq. cm / Hz]. if redshifted: ## Redshift spectra for quickspectra input. 'Else', leave unredshifted for redrock template. vs, Fv = redshift_spectra(vs, Fv, redshift) waves = vs.to( u.AA, equivalencies=u.spectral()) ## Redshifted wavelength. mags = get_appmags(vs, Fv, filters, printit=False) Fv *= 10.**(0.4 * (mags['r'] - magnitudes[0]) ) ## Normalise to 21.5 mag. in g. ## And check. ## mags = get_appmags(vs, Fv, filters, printit = True) ## F_lambda from F_nu. Fl = Fv.to(u.erg / u.s / u.cm / u.cm / u.AA, equivalencies=u.spectral_density( waves)) ## [ ergs / s / sq. cm / A] Fl /= 1.e-17 ## [1.e-17 ergs / s / sq. cm / A] Fl = interp1d(waves.value, Fl.value, kind='linear',