Basics#

import numpy as np
import matplotlib.pyplot as plt
import astropy.units as u
from astropy.utils.data import clear_download_cache
clear_download_cache()

from spextra import Spextrum, spextra_database, SpecLibrary, FilterSystem

Examining the database#

sdb = spextra_database

Information of the database#

print(sdb)
Spextra Database:
  Remote URL: https://scopesim.univie.ac.at/spextra/database/
  Local path: /home/docs/.spextra_cache
Database contents:
├─libraries: 
│ ├─ref: A library of reference stars
│ ├─kc96: Kinney-Calzetti Atlas
│ ├─pickles: Pickles Stellar Library
│ ├─dobos: SDSS galaxy composite spectra
│ ├─irtf: IRTF spectral library
│ ├─agn: AGN templates
│ ├─nebulae: Emission line nebulae
│ ├─brown: Galaxy SEDs from the UV to the Mid-IR
│ ├─kurucz: Subset of Kurucz 1993 Models
│ ├─sne: Supernova Legacy Survey
│ ├─moehler: flux/telluric standards with X-Shooter
│ ├─madden: High-Resolution Spectra of Habitable Zone Planets
│ ├─bosz/hr: BOSZ stellar atmosphere Grid - High Resolution
│ ├─bosz/mr: BOSZ stellar atmosphere Grid - Medium Resolution
│ ├─bosz/lr: BOSZ stellar atmosphere Grid - Low Resolution
│ ├─assef: Low-resolution spectral templates for AGN and galaxies
│ ├─sky: Paranal sky background spectra
│ ├─shapley: Rest-Frame Ultraviolet Spectra of z ∼ 3 Lyman Break Galaxies
│ ├─etc/kinney: ESO ETC version of the Kinney-Calzetti Atlas
│ ├─etc/kurucz: ESO ETC subset of the Kurucz 1993 models
│ ├─etc/marcs/p: ESO ETC subset of the MARCS Stellar Models with Plane Parallel Geometry
│ ├─etc/marcs/s: ESO ETC subset of the MARCS Stellar Models with Spherical Geometry
│ ├─etc/misc: Other templates, nubulae and qso
│ └─etc/pickles: ESO ETC subset of the Pickles stellar library
├─extinction_curves: 
│ ├─gordon: LMC and SMC extinction laws
│ ├─cardelli: MW extinction laws
│ └─calzetti: extragalactic attenuation curves
└─filter_systems: 
  ├─elt/micado: MICADO filters
  ├─elt/metis: METIS filters
  └─etc: ESO ETC standard filters
# Which templates are available?

print(SpecLibrary("sne"))
Downloading file 'libraries/sne/index.yml' from 'https://scopesim.univie.ac.at/spextra/database/libraries/sne/index.yml' to '/home/docs/.spextra_cache'.
  0%|                                              | 0.00/1.03k [00:00<?, ?B/s]
  0%|                                              | 0.00/1.03k [00:00<?, ?B/s]
100%|██████████████████████████████████████| 1.03k/1.03k [00:00<00:00, 861kB/s]
Spectral Library 'sne': Supernova spectral library
  spectral coverage: uv, vis, nir
  wave_unit: Angstrom
  flux_unit: FLAM
  Templates: sn1a, sn1b, sn1c, sn2l, sn2p, sn2n, hyper, pop3_3d, pop3_15d

Extinction curves and Filters#

print(sdb["extinction_curves"])
print(sdb["filter_systems"])
{'gordon': 'LMC and SMC extinction laws', 'cardelli': 'MW extinction laws', 'calzetti': 'extragalactic attenuation curves'}
{'elt/micado': 'MICADO filters', 'elt/metis': 'METIS filters', 'etc': 'ESO ETC standard filters'}
print(FilterSystem("micado"))
Filter system 'micado': <untitled>
  spectral coverage: 
  wave_unit: Angstrom
  filters: 

Retrieving the spectra#

sp1 = Spextrum("kc96/s0")
sp1.plot()
Downloading file 'libraries/kc96/s0.fits' from 'https://scopesim.univie.ac.at/spextra/database/libraries/kc96/s0.fits' to '/home/docs/.spextra_cache'.
  0%|                                              | 0.00/20.2k [00:00<?, ?B/s]
  0%|                                              | 0.00/20.2k [00:00<?, ?B/s]
100%|█████████████████████████████████████| 20.2k/20.2k [00:00<00:00, 17.4MB/s]

../_images/6e4d49e883ab7afd626c74cb4f1617b17a9b1abcf2e05b404ee9616cface0023.png
# another spectrum

sp2 = Spextrum("agn/qso")
sp2.plot()
Downloading file 'libraries/agn/index.yml' from 'https://scopesim.univie.ac.at/spextra/database/libraries/agn/index.yml' to '/home/docs/.spextra_cache'.
  0%|                                              | 0.00/2.19k [00:00<?, ?B/s]
  0%|                                              | 0.00/2.19k [00:00<?, ?B/s]
100%|█████████████████████████████████████| 2.19k/2.19k [00:00<00:00, 1.74MB/s]
Downloading file 'libraries/agn/qso.fits' from 'https://scopesim.univie.ac.at/spextra/database/libraries/agn/qso.fits' to '/home/docs/.spextra_cache'.
  0%|                                              | 0.00/23.0k [00:00<?, ?B/s]
 31%|███████████▌                         | 7.17k/23.0k [00:00<00:00, 65.0kB/s]
  0%|                                              | 0.00/23.0k [00:00<?, ?B/s]
100%|█████████████████████████████████████| 23.0k/23.0k [00:00<00:00, 20.1MB/s]

../_images/8b657eb214b1e8643ea5212dad2ca20561ea845811e9a69c2f32132850a72e42.png

Aritmetics#

simple arithmetics are possible

sp = sp1 + 3*sp2

sp.plot()
../_images/a47ad8fd3923828b5b3554a4c4567dc122194cd61e0044cc36dd56fc228be0e1.png

Adding emission lines#

It is possible to add emission lines, either individually or as a list. Parameters are center, flux and fwhm

sp3 = sp1.add_emi_lines(center=4000,flux=4e-13, fwhm=5*u.AA)
sp3.plot(left=3500, right=4500)
../_images/2c9dbdaeeb5142c7c8ef054091134e52966d26556468dc7bc8eeaa8888d731ef.png

Redshifting spectra#

fig = plt.figure(figsize=(10,7))
sp4 = sp2.redshift(z=1)

wave = sp2.waveset
flux = sp2(wave, flux_unit="FLAM")

plt.plot(wave, flux)

plt.plot(sp4.waveset, 
         sp4(sp4.waveset, flux_unit="FLAM"))

plt.legend(['z=0', 'z=1'], loc='upper right')
<matplotlib.legend.Legend at 0x7f7129065850>
../_images/a2f588a1a10837eca10ca7542a9ea0bd779c8bcd0d5b0ba6078421020c7d59c6.png

Or using velocity#

fig = plt.figure(figsize=(10,6))

sp1 = Spextrum("nebulae/orion")

vel = -1000 * u.km / u.s
sp2 = sp1.redshift(vel=vel)

plt.plot(sp1.waveset, 
         sp1(sp1.waveset, flux_unit="FLAM"))
plt.plot(sp2.waveset, 
         sp2(sp2.waveset, flux_unit="FLAM"))
plt.legend(['vel=0', 'vel=-1000 km/s'], loc='upper right')
plt.xlim(3000,5000)
Downloading file 'libraries/nebulae/index.yml' from 'https://scopesim.univie.ac.at/spextra/database/libraries/nebulae/index.yml' to '/home/docs/.spextra_cache'.
  0%|                                              | 0.00/1.51k [00:00<?, ?B/s]
  0%|                                              | 0.00/1.51k [00:00<?, ?B/s]
100%|█████████████████████████████████████| 1.51k/1.51k [00:00<00:00, 1.16MB/s]
Downloading file 'libraries/nebulae/orion.fits' from 'https://scopesim.univie.ac.at/spextra/database/libraries/nebulae/orion.fits' to '/home/docs/.spextra_cache'.
  0%|                                               | 0.00/170k [00:00<?, ?B/s]
  8%|███▎                                   | 14.3k/170k [00:00<00:01, 133kB/s]
 25%|█████████▊                             | 43.0k/170k [00:00<00:00, 208kB/s]
 38%|██████████████▊                        | 64.5k/170k [00:00<00:00, 202kB/s]
 59%|███████████████████████▌                | 100k/170k [00:00<00:00, 251kB/s]
 92%|████████████████████████████████████▉   | 157k/170k [00:00<00:00, 345kB/s]
  0%|                                               | 0.00/170k [00:00<?, ?B/s]
100%|████████████████████████████████████████| 170k/170k [00:00<00:00, 139MB/s]

(3000.0, 5000.0)
../_images/1e1142961af153e7ebe09c8efe2184a65cd3a0d47896c3ce57d49a5e55d4aaf4.png

Flat spectrum in any photometric system#

(aka reference spectrum if mag=0)

sp_vega = Spextrum.flat_spectrum(10*u.mag)
sp_ab = Spextrum.flat_spectrum(10*u.ABmag)
sp_st = Spextrum.flat_spectrum(10*u.STmag)


fig = plt.figure(figsize=(10,7))
wave = sp_vega.waveset
plt.plot(wave, sp_vega(wave, flux_unit="FLAM"), label="Vega")
plt.plot(wave, sp_ab(wave, flux_unit="FLAM"), label="AB")
plt.plot(wave, sp_st(wave, flux_unit="FLAM"), label="ST")

plt.xlim(3000,1e4)
plt.ylim(0,0.2e-11)
plt.xlabel("wavelength")
plt.ylabel("flux (FLAM)")
plt.legend()
Downloading file 'libraries/ref/vega.fits' from 'https://scopesim.univie.ac.at/spextra/database/libraries/ref/vega.fits' to '/home/docs/.spextra_cache'.
  0%|                                               | 0.00/276k [00:00<?, ?B/s]
  3%|▉                                     | 7.17k/276k [00:00<00:04, 66.2kB/s]
  5%|█▉                                    | 14.3k/276k [00:00<00:03, 65.6kB/s]
 16%|██████                                 | 43.0k/276k [00:00<00:01, 155kB/s]
 21%|████████▍                              | 59.4k/276k [00:00<00:01, 153kB/s]
 34%|█████████████▎                         | 94.2k/276k [00:00<00:00, 212kB/s]
 45%|██████████████████                      | 125k/276k [00:00<00:00, 235kB/s]
 62%|████████████████████████▉               | 172k/276k [00:00<00:00, 298kB/s]
 80%|████████████████████████████████        | 221k/276k [00:00<00:00, 346kB/s]
  0%|                                               | 0.00/276k [00:00<?, ?B/s]
100%|████████████████████████████████████████| 276k/276k [00:00<00:00, 275MB/s]

<matplotlib.legend.Legend at 0x7f7128ffb490>
../_images/74038a0297b9ccb389295cb1339893e3f0fc0b95aab01bdd632a42d4b3f60acf.png

Scaling to a magnitude#

sp1 = Spextrum("kc96/s0").scale_to_magnitude(amplitude=13 * u.ABmag, filter_curve="g")
sp2 = sp1.scale_to_magnitude(amplitude=15 * u.ABmag, filter_curve="g")

sig = plt.figure(figsize=(10,7))
plt.plot(sp1.waveset, 
         sp1(sp1.waveset, flux_unit="FLAM"))
plt.plot(sp2.waveset, 
         sp2(sp2.waveset, flux_unit="FLAM"))
plt.legend(['mag=13', 'mag=15'], loc='upper right')
plt.xlim(4000,7000)
(4000.0, 7000.0)
../_images/440fadc83a1bba8f5058deff701f23987d6d63fffce7dc0dc76146dfc49eaa25.png

Obtaining magnitudes from spectra#

print("Magnitude spectra 1:", sp1.get_magnitude(filter_curve="g"), 
      sp1.get_magnitude(filter_curve="g", system_name="Vega"), "Vega")
print("Magnitude spectra 2:", sp2.get_magnitude(filter_curve="g"), 
      sp2.get_magnitude(filter_curve="g", system_name="Vega"), "Vega")
Magnitude spectra 1: 13.0 mag(AB) 13.095325163298817 mag Vega
Magnitude spectra 2: 15.0 mag(AB) 15.095325163298817 mag Vega

Rebin spectra#

a new wavelength array must be passed

sp1 = Spextrum("agn/qso")
new_waves = np.linspace(np.min(sp1.waveset),
                        np.max(sp1.waveset),
                        100)
sp2 = sp1.rebin_spectra(new_waves=new_waves)

sig = plt.figure(figsize=(10,7))
plt.plot(sp1.waveset, 
         sp1(sp1.waveset, flux_unit="FLAM"))
plt.plot(sp2.waveset, 
         sp2(sp2.waveset, flux_unit="FLAM"))
plt.xlim(1000,4000)
(1000.0, 4000.0)
../_images/a7171e5337bc5f7765a09eae0dee3361fddffc2536ea4fccfeaeba9aa3406247.png

Smooth the spectral with a velocity kernel#

sp1 = Spextrum("nebulae/pn")

sigma = 500*(u.km / u.s)
sp2 = sp1.smooth(sigma=sigma)

fig = plt.figure(figsize=(10,7))
plt.plot(sp1.waveset, 
         sp1(sp1.waveset, flux_unit="FLAM"), label="original")
plt.plot(sp2.waveset, 
         sp2(sp2.waveset, flux_unit="FLAM"), label="broadened with 500 km/s")

plt.xlim(4800,5200)
plt.legend()
Downloading file 'libraries/nebulae/pn.fits' from 'https://scopesim.univie.ac.at/spextra/database/libraries/nebulae/pn.fits' to '/home/docs/.spextra_cache'.
  0%|                                               | 0.00/170k [00:00<?, ?B/s]
  8%|███▎                                   | 14.3k/170k [00:00<00:01, 133kB/s]
 25%|█████████▊                             | 43.0k/170k [00:00<00:00, 208kB/s]
 45%|█████████████████▍                     | 75.8k/170k [00:00<00:00, 249kB/s]
 72%|████████████████████████████▋           | 122k/170k [00:00<00:00, 316kB/s]
  0%|                                               | 0.00/170k [00:00<?, ?B/s]
100%|████████████████████████████████████████| 170k/170k [00:00<00:00, 258MB/s]

<matplotlib.legend.Legend at 0x7f712ad8b590>
../_images/0c01d9b09076d3dfbf9f43500f4e9b6f74cb0f28b1a05c3af13b69a3c0beda9b.png

Blackbody spectrum and extinction curves#

sp1 = Spextrum.black_body_spectrum(temperature=5500, 
                                   amplitude=10 * u.ABmag, 
                                   filter_curve="r")
sp2 = sp1.redden("gordon/smc_bar", Ebv=0.15)


fig = plt.figure(figsize=(10,7))
plt.plot(sp1.waveset, 
         sp1(sp1.waveset, flux_unit="FLAM"), label="original")
plt.plot(sp2.waveset, 
         sp2(sp2.waveset, flux_unit="FLAM"), label="attenuated")

plt.xlim(1800,15200)
plt.legend()
Downloading data from 'http://svo2.cab.inta-csic.es/theory/fps3/fps.php?ID=SLOAN/SDSS.r' to file '/home/docs/.spextra_cache/svo_filters/SLOAN/SDSS.r'.
SHA256 hash of downloaded file: 570fae17c0ea52ffa9e6edff4761e6712e40c2bbbb9894cbb8575fc20b2eef3e
Use this value as the 'known_hash' argument of 'pooch.retrieve' to ensure that the file hasn't changed if it is downloaded again in the future.
Downloading file 'extinction_curves/gordon/index.yml' from 'https://scopesim.univie.ac.at/spextra/database/extinction_curves/gordon/index.yml' to '/home/docs/.spextra_cache'.
  0%|                                                | 0.00/502 [00:00<?, ?B/s]
  0%|                                                | 0.00/502 [00:00<?, ?B/s]
100%|██████████████████████████████████████████| 502/502 [00:00<00:00, 469kB/s]
Downloading file 'extinction_curves/gordon/smc_bar.fits' from 'https://scopesim.univie.ac.at/spextra/database/extinction_curves/gordon/smc_bar.fits' to '/home/docs/.spextra_cache'.
  0%|                                              | 0.00/8.64k [00:00<?, ?B/s]
  0%|                                              | 0.00/8.64k [00:00<?, ?B/s]
100%|█████████████████████████████████████| 8.64k/8.64k [00:00<00:00, 7.75MB/s]

<matplotlib.legend.Legend at 0x7f71290fd9d0>
../_images/ea286211406f2cb626715297b3f502b85f824c01173447c579f70d2fa62bec28.png

Photons within a filter#

(or between wmin or wmax)

n_photons = sp2.photons_in_range(area=2*u.m**2,
                                 filter_curve="V")

print(n_photons)
Downloading file 'filter_systems/etc/index.yml' from 'https://scopesim.univie.ac.at/spextra/database/filter_systems/etc/index.yml' to '/home/docs/.spextra_cache'.
  0%|                                                | 0.00/609 [00:00<?, ?B/s]
  0%|                                                | 0.00/609 [00:00<?, ?B/s]
100%|██████████████████████████████████████████| 609/609 [00:00<00:00, 553kB/s]
Downloading file 'filter_systems/etc/V.dat' from 'https://scopesim.univie.ac.at/spextra/database/filter_systems/etc/V.dat' to '/home/docs/.spextra_cache'.
  0%|                                              | 0.00/3.18k [00:00<?, ?B/s]
  0%|                                              | 0.00/3.18k [00:00<?, ?B/s]
100%|█████████████████████████████████████| 3.18k/3.18k [00:00<00:00, 2.78MB/s]
1150148.4504138283 ct / s