Spectroscopy¶
If you have an instrument with a wavelength array you can produce spectroscopy from any spectra generated by Synthesizer.
For this you simply need a galaxy or component with spectra and an instrument defining a wavelength array. Below we set up the galaxy and generate it’s spectra.
[1]:
import numpy as np
from unyt import Mpc, Msun, Myr, cm, kelvin, yr
from synthesizer.emission_models import (
BimodalPacmanEmission,
Blackbody,
)
from synthesizer.emission_models.attenuation import PowerLaw
from synthesizer.grid import Grid
from synthesizer.parametric import SFH, ZDist
from synthesizer.parametric import Stars as ParametricStars
from synthesizer.particle import BlackHoles, Galaxy
from synthesizer.particle.stars import sample_sfzh
# Get the grids which we'll need for extraction
grid_name = "test_grid"
grid = Grid(grid_name)
nlr_grid = Grid("test_grid_agn-nlr")
blr_grid = Grid("test_grid_agn-blr")
# Get the stellar pacman model
pc_model = BimodalPacmanEmission(
grid=grid,
tau_v_ism=1.0,
tau_v_birth=0.7,
dust_curve_ism=PowerLaw(slope=-1.3),
dust_curve_birth=PowerLaw(slope=-0.7),
dust_emission_ism=Blackbody(temperature=100 * kelvin),
dust_emission_birth=Blackbody(temperature=30 * kelvin),
fesc=0.2,
fesc_ly_alpha=0.9,
label="stellar_total",
)
pc_model.save_spectra("reprocessed", "attenuated", "stellar_total")
# Define the metallicity history
zh = ZDist.DeltaConstant(metallicity=0.01)
# Define the star formation history
sfh_p = {"max_age": 100 * Myr}
sfh = SFH.Constant(**sfh_p)
# Initialise the parametric Stars object
param_stars = ParametricStars(
grid.log10ages,
grid.metallicities,
sf_hist=sfh,
metal_dist=zh,
initial_mass=10**9 * Msun,
)
# Define the number of stellar particles we want
n = 10000
# Sample the parametric SFZH, producing a particle Stars object
# we will also pass some keyword arguments for some example attributes
part_stars = sample_sfzh(
sfzh=param_stars.sfzh,
log10ages=param_stars.log10ages,
log10metallicities=param_stars.log10metallicities,
nstar=n,
current_masses=np.full(n, 10**8.7 / n) * Msun,
redshift=1,
)
# Make fake properties
n = 4
masses = 10 ** np.random.uniform(low=7, high=9, size=n) * Msun
coordinates = np.random.normal(0, 1.5, (n, 3)) * Mpc
accretion_rates = 10 ** np.random.uniform(low=-2, high=1, size=n) * Msun / yr
metallicities = np.full(n, 0.01)
# And get the black holes object
blackholes = BlackHoles(
masses=masses,
coordinates=coordinates,
accretion_rates=accretion_rates,
metallicities=metallicities,
ionisation_parameter_nlr=0.01,
hydrogen_density_nlr=1e4 * cm**-3,
ionisation_parameter_blr=0.1,
hydrogen_density_blr=1e10 * cm**-3,
)
# And create the galaxy
galaxy = Galaxy(
stars=part_stars,
black_holes=blackholes,
redshift=1,
)
# Get the stellar spectra
galaxy.get_spectra(pc_model)
[1]:
<synthesizer.emissions.sed.Sed at 0x70b519a47eb0>
Next we need an instrument capable of spectroscopy. For this purpose we’ll construct a simple SpectroscopicInstrument with the wavelength array we want to observe on.
[2]:
from unyt import angstrom
from synthesizer.instruments import SpectroscopicInstrument
# Instantiate the spectroscopic instrument
inst = SpectroscopicInstrument(
label="Spectrometer", lam=np.logspace(3, 4, 100) * angstrom
)
print(inst)
+----------------------------------------------------------------------------------+
| INSTRUMENT |
+------------------------------------+---------------------------------------------+
| Attribute | Value |
+------------------------------------+---------------------------------------------+
| label | 'Spectrometer' |
+------------------------------------+---------------------------------------------+
| lsf_resample_factor | 1 |
+------------------------------------+---------------------------------------------+
| template_on_detector | True |
+------------------------------------+---------------------------------------------+
| can_do_imaging | False |
+------------------------------------+---------------------------------------------+
| can_do_line_mapping | False |
+------------------------------------+---------------------------------------------+
| can_do_lsf_spectroscopy | False |
+------------------------------------+---------------------------------------------+
| can_do_noisy_imaging | False |
+------------------------------------+---------------------------------------------+
| can_do_noisy_line_mapping | False |
+------------------------------------+---------------------------------------------+
| can_do_noisy_resolved_spectroscopy | False |
+------------------------------------+---------------------------------------------+
| can_do_noisy_spectroscopy | False |
+------------------------------------+---------------------------------------------+
| can_do_photometry | False |
+------------------------------------+---------------------------------------------+
| can_do_psf_imaging | False |
+------------------------------------+---------------------------------------------+
| can_do_psf_line_mapping | False |
+------------------------------------+---------------------------------------------+
| can_do_psf_spectroscopy | False |
+------------------------------------+---------------------------------------------+
| can_do_resolved_spectroscopy | False |
+------------------------------------+---------------------------------------------+
| can_do_spectroscopy | True |
+------------------------------------+---------------------------------------------+
| instrument_type | 'spectroscopic' |
+------------------------------------+---------------------------------------------+
| lam (100,) | 1.00e+03 Å -> 1.00e+04 Å (Mean: 3.92e+03 Å) |
+------------------------------------+---------------------------------------------+
Finally, to actually generate the spectroscopy we call one of two methods with our instrument, on either the galaxy or a component. These differ in how the instrument’s wavelength array is interpreted:
get_spectroscopy_lnutreats the instrument wavelengths as rest frame wavelengths. The results are stored in thespectroscopy_lnuattribute.get_spectroscopy_fnutreats the instrument wavelengths as observer frame wavelengths. This needs the observed spectra (see below) and the results are stored in thespectroscopy_fnuattribute.
First, the rest frame spectroscopy.
[3]:
galaxy.get_spectroscopy_lnu(inst)
print(galaxy.stars.spectroscopy_lnu)
{'Spectrometer': {'stellar_total': <synthesizer.emissions.sed.Sed object at 0x70b584f96e30>, 'reprocessed': <synthesizer.emissions.sed.Sed object at 0x70b584f61f30>, 'attenuated': <synthesizer.emissions.sed.Sed object at 0x70b584f63160>}}
We can plot the rest frame spectroscopy using the plot_spectroscopy method on the galaxy or component.
[4]:
galaxy.plot_spectroscopy(
inst.label,
stellar_spectra=True,
show=True,
)
[4]:
(<Figure size 350x500 with 1 Axes>,
<Axes: xlabel='$\\lambda/[\\mathrm{\\AA}]$', ylabel='$L_{\\nu}/[\\mathrm{\\rm{erg} \\ / \\ \\rm{Hz \\cdot \\rm{s}}}]$'>)
For observer frame spectroscopy we first need the observed spectra. We can then call get_spectroscopy_fnu, which resamples the fluxes directly onto the instrument wavelengths, so the observed wavelengths of the result are exactly the instrument’s. The rest frame luminosities are carried along on the matching rest frame wavelengths (the instrument wavelengths divided by \(1 + z\)). Once generated, this can be plotted using the plot_observed_spectroscopy method on a galaxy.
Note that get_spectroscopy (and the spectroscopy attribute) is deprecated and will be removed in version 1.4.0. It is an alias for get_spectroscopy_fnu (and spectroscopy_fnu).
[5]:
from astropy.cosmology import Planck18 as cosmo
# Get the observed spectra
galaxy.get_observed_spectra(cosmo=cosmo)
# Generate the observer frame spectroscopy
galaxy.get_spectroscopy_fnu(inst)
# Plot the observed spectra
galaxy.plot_observed_spectroscopy(
inst.label,
stellar_spectra=True,
show=True,
)
[5]:
(<Figure size 350x500 with 1 Axes>,
<Axes: xlabel='$\\lambda_\\mathrm{obs}/[\\mathrm{\\AA}]$', ylabel='$F_{\\nu}/[\\mathrm{\\rm{nJy}}]$'>)
Line spread functions¶
A spectroscopic instrument broadens the lines in a spectrum, which is described by its line spread function (LSF). An instrument applies the kernels passed to it as lsfs. get_lsfs_from_resolving_power makes Gaussian kernels whose full width at half maximum is the wavelength divided by a resolving power. LSFs can also be given to an existing instrument with its set_lsfs method.
The LSF is applied on wavelength bins lsf_resample_factor times finer than the instrument’s wavelength array, which keeps the positions and shapes of narrow lines. The kernels must be sampled on those bins, so the same factor is passed to get_lsfs_from_resolving_power and to the instrument. We recommend a factor of a few when the LSF is only a few wavelength elements wide.
For an instrument with an LSF, the spectroscopy methods above return the spectroscopy with the LSF applied, which is stored in spectroscopy_lsf_lnu or spectroscopy_lsf_fnu and is what the plotting methods show. The spectroscopy without the LSF is still stored in spectroscopy_lnu or spectroscopy_fnu.
[6]:
from synthesizer.instruments import get_lsfs_from_resolving_power
# Make Gaussian LSFs from a resolving power
lsf_lam = np.logspace(4, 4.3, 200) * angstrom
lsfs = get_lsfs_from_resolving_power(lsf_lam, 100, lsf_resample_factor=4)
# Instantiate a spectroscopic instrument with these LSFs
lsf_inst = SpectroscopicInstrument(
label="LowResSpectrometer",
lam=lsf_lam,
lsfs=lsfs,
lsf_resample_factor=4,
)
# Generate the observer frame spectroscopy, now with the LSF applied
galaxy.get_spectroscopy_fnu(lsf_inst)
# Plot the observed spectra
galaxy.plot_observed_spectroscopy(
lsf_inst.label,
stellar_spectra=True,
show=True,
)
[6]:
(<Figure size 350x500 with 1 Axes>,
<Axes: xlabel='$\\lambda_\\mathrm{obs}/[\\mathrm{\\AA}]$', ylabel='$F_{\\nu}/[\\mathrm{\\rm{nJy}}]$'>)
Noise¶
An instrument with a depth and snrs adds Gaussian noise to its spectroscopy. Without a noise template the noise is flat, with a standard deviation of depth / snrs at every wavelength, and independent between wavelength elements. The units of depth set which spectrum gets the noise: a flux density (e.g. nJy) adds it to the observer frame fluxes, for use with get_spectroscopy_fnu, while a luminosity density adds it to the rest frame luminosities, for use with
get_spectroscopy_lnu.
The spectroscopy methods apply the noise automatically for an instrument with a depth and SNR. Each noisy spectrum stores the noise added as noise_arr, and the inverse variance of each element as weight_map.
Here we set the depth to have S/N = 1 for a source of 0.1 micro Jy.
[7]:
from unyt import uJy
# Instantiate a spectroscopic instrument with flat noise
# Set the depth so that a 0.1 uJy source has S/N = 1
noisy_inst = SpectroscopicInstrument(
label="NoisySpectrometer",
lam=inst.lam,
depth=0.1 * uJy,
snrs=1,
)
# Generate the observer frame spectroscopy, now with noise
galaxy.get_spectroscopy_fnu(noisy_inst)
# Plot the noisy observed spectra
galaxy.plot_observed_spectroscopy(
noisy_inst.label,
stellar_spectra=True,
show=True,
)
[7]:
(<Figure size 350x500 with 1 Axes>,
<Axes: xlabel='$\\lambda_\\mathrm{obs}/[\\mathrm{\\AA}]$', ylabel='$F_{\\nu}/[\\mathrm{\\rm{nJy}}]$'>)