In [1]:
%pylab inline
from astropy.cosmology import Planck13
from astropy.io import fits
from simqso.sqgrids import *
from simqso import sqbase
from simqso.sqrun import buildSpectraBulk,buildQsoSpectrum,save_spectra,load_spectra,restore_qso_grid
from simqso.sqmodels import BOSS_DR9_PLEpivot,get_BossDr9_model_vars


Populating the interactive namespace from numpy and matplotlib

In [2]:
# cover 1000A to 20um at R=1000
wave = sqbase.fixed_R_dispersion(1000,20e4,1000)

In [3]:
# just make up a few random redshifts between z=2 and z=3, then assign apparent mags according 
# to the BOSS DR9 QLF
nqso = 10
np.random.seed(12345)
zin = 2.0 + np.random.rand(nqso)
kcorr = sqbase.ContinuumKCorr('DECam-r',1450,effWaveBand='SDSS-r')
qsos = generateQlfPoints(BOSS_DR9_PLEpivot(cosmo=Planck13),
                         (17,22),(2.0,3.0),
                         kcorr=kcorr,zin=zin,
                         qlfseed=12345,gridseed=67890)
scatter(qsos.z,qsos.appMag)
xlabel('z')
ylabel('r mag');



In [4]:
# add the fiducial quasar SED model from BOSS DR9
# need to set forestseed if the forest transmission sightlines are to be reproducible
sedVars = get_BossDr9_model_vars(qsos,wave,0,forestseed=192837465,verbose=1)
qsos.addVars(sedVars)


Generating 10 sightlines

In [5]:
# define photometry in DECam and WISE systems
qsos.loadPhotoMap([('DECam','DECaLS'),('WISE','AllWISE')])

In [6]:
# ready to generate spectra. iteration is necessary to converge on the per-object k-correction,
# after two steps the maximum error on the absolute mags is <<1%
_,spectra = buildSpectraBulk(wave,qsos,saveSpectra=True,maxIter=3,verbose=10)


simulating  10  quasar spectra
units are  flux
buildSpectra iteration  1  out of  3
--> delta mag mean = -0.2181253, rms = 0.0295074, |max| = 0.2719755
buildSpectra iteration  2  out of  3
--> delta mag mean = -0.0175531, rms = 0.0119108, |max| = 0.0362774
buildSpectra iteration  3  out of  3
--> delta mag mean = -0.0004554, rms = 0.0004496, |max| = 0.0012965

In [7]:
figure(figsize=(10,4))
plot(wave,(wave*spectra).transpose())
xscale('log')
yscale('log')
xlabel('wavelength [Ang]')
ylabel(r'$\lambda{f}_\lambda}$')
ylim(1e-14,1e-12);



In [8]:
# the parameter values for each spectrum
qsos.data


Out[8]:
<Table length=10>
absMagappMagzslopes [5]emLines [62,3]igmlossynMag [5]synFlux [5]
float32float32float32float32float32int32float32float32
-24.718620.47122.92962-1.23932 .. -0.5143251033.98 .. 288.205020.6803 .. 19.65185.34418 .. 13.7812
-25.420919.33512.31638-1.54657 .. -0.729591033.72 .. 304.236119.4162 .. 17.928417.1212 .. 67.3977
-22.568421.98652.18392-2.06576 .. -1.065271034.76 .. 303.599221.9921 .. 20.41171.59649 .. 6.84416
-22.859821.82632.20456-1.36616 .. -1.513561034.41 .. 261.102321.7853 .. 20.67781.93142 .. 5.35667
-23.474221.49472.56772-1.74318 .. -1.406611033.93 .. 292.392421.5135 .. 20.21712.48076 .. 8.18736
-24.281220.72882.59554-1.26906 .. -0.8752621033.42 .. 314.515520.7489 .. 19.50045.01702 .. 15.8432
-24.99620.26222.96451-1.57915 .. -1.455921034.49 .. 249.958620.371 .. 19.62787.10589 .. 14.0895
-25.021420.00582.65318-1.31468 .. -0.4656641033.05 .. 294.655720.1705 .. 19.04278.5466 .. 24.15
-25.778719.31372.74891-1.68028 .. -1.377261033.15 .. 430.311819.4531 .. 18.415216.5481 .. 43.046
-24.490620.54532.65357-1.57515 .. -0.8414151033.85 .. 310.385920.5687 .. 19.99125.92286 .. 10.0814

In [9]:
# save the simulation meta-data to a file
qsos.write('quickspeclib_meta',extname='quickspec',overwrite=True)

In [10]:
ff = fits.open('quickspeclib_meta.fits')
ff.info()


Filename: quickspeclib_meta.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU       4   ()      
  1  QUICKSPEC     1 BinTableHDU     66   10R x 8C   [E, E, E, 5E, 186E, J, 5E, 5E]   

In [11]:
# save the spectra to a fits bin table
save_spectra(wave,spectra,'quickspeclib')

In [12]:
# restore the spectra
wave,spec = load_spectra('quickspeclib')

In [13]:
# restore the simulation meta-data into a QsoSimObjects
qsos_restore = restore_qso_grid('quickspeclib_meta',wave,extname='quickspec')

In [14]:
# rebuild a single quasar spectrum
spec0 = buildQsoSpectrum(wave,qsos_restore.cosmo,
                         qsos_restore.getVars(SpectralFeatureVar),
                         qsos_restore.data[0])

In [15]:
# plot the original and restored spectra for comparison
figure(figsize=(12,4))
plot(wave,spectra[0],lw=1.4)
plot(wave,spec0.f_lambda,ls='--')
yscale('log')
ylim(3e-18,2e-16)
xlim(3400,5000);#1e4);



In [16]:
# restore another spectrum
spec1 = buildQsoSpectrum(wave,qsos_restore.cosmo,
                         qsos_restore.getVars(SpectralFeatureVar),
                         qsos_restore.data[1])

In [17]:
# and plot the comparison again
figure(figsize=(12,4))
plot(wave,spectra[1],lw=1.4)
plot(wave,spec1.f_lambda,ls='--')
yscale('log')
ylim(3e-17,10e-16)
xlim(3250,1e4);



In [18]:
# rebuild all of the spectra
_,allspec = buildSpectraBulk(wave,qsos_restore)

In [19]:
figure(figsize=(10,4))
plot(wave,(wave*spectra).transpose())
xscale('log')
yscale('log')
xlabel('wavelength [Ang]')
ylabel(r'$\lambda{f}_\lambda}$')
ylim(1e-14,1e-12);



In [ ]: