rubix.spectra.ssp package#
Submodules#
rubix.spectra.ssp.factory module#
- rubix.spectra.ssp.factory.get_ssp_template(template: str) SSPGrid[source]#
Return a configured SSP template grid.
- Parameters:
template (str) – Template key defined in
config["ssp"]["templates"].- Returns:
Loaded stellar population grid.
- Return type:
- Raises:
ValueError – If the template name or source format is not supported.
Example:
>>> from rubix.spectra.ssp.factory import get_ssp_template >>> ssp = get_ssp_template("FSPS") >>> ssp.age.shape
rubix.spectra.ssp.fsps_grid module#
Use python-fsps to retrieve a block of Simple Stellar Population data.
Adapted from the DSPS retrieve_fsps_data.py workflow.
- rubix.spectra.ssp.fsps_grid.retrieve_ssp_data_from_fsps(add_neb_emission: bool = True, imf_type: int = 2, zmet: int | None = None, tage: float = 0.0, peraa: bool = True, **kwargs) SSPGrid[source]#
Use python-fsps to populate arrays and matrices of data for the default simple stellar populations (SSPs) in the shapes expected by DSPS adapted from ArgonneCPAC/dsps
- Parameters:
add_neb_emission (bool, optional) – Whether to enable nebular emission in
fsps.StellarPopulation. Defaults toTrue.imf_type (int, optional) – IMF type identifier passed to
fsps.StellarPopulation. Defaults to2(Chabrier 2003). See https://dfm.io/python-fsps/current/stellarpop_api/#example for more details.zmet (int | None, optional) – Metallicity index for
fsps.StellarPopulation. WhenNonethe default FSPS grid is used.tage (float, optional) – SSP age in Gyr. Defaults to
0.0.peraa (bool, optional) – If
Truereturn spectra in L$_{odot}$/AA, otherwise L$_{odot}$/Hz. Defaults toTrue.**kwargs – Additional keyword arguments forwarded to
fsps.StellarPopulation.
- Returns:
- Grid containing age, metallicity, wavelength, and flux arrays
in the shapes expected by downstream DSPS consumers.
- Return type:
- Raises:
AssertionError – If
python-fspsis not installed.
Notes
The retrieve_ssp_data_from_fsps function is just a wrapper around python-fsps without any other dependencies. This standalone function should be straightforward to modify to use python-fsps to build alternate SSP data blocks.
All DSPS functions operate on plain ndarrays, so user-supplied data storing alternate SSP models is supported. You will just need to pack your SSP data into arrays with shapes matching the shapes of the arrays returned by this function.
- rubix.spectra.ssp.fsps_grid.write_fsps_data_to_disk(outname: str, file_location: str | PathLike = '/home/annalena/rubix/rubix/spectra/ssp/templates', add_neb_emission: bool = True, imf_type: int = 2, peraa: bool = True, **kwargs) None[source]#
Write FSPS ssp template data to disk in HDF5 format. adapted from ArgonneCPAC/dsps
- Parameters:
outname (str) – Output filename, relative to
file_location.file_location (str | os.PathLike, optional) – Directory for the generated file. Defaults to
TEMPLATE_PATH.add_neb_emission (bool, optional) – Passed through to
retrieve_ssp_data_from_fsps(). Defaults toTrue.imf_type (int, optional) – IMF type forwarded to
retrieve_ssp_data_from_fsps(). Defaults to2.peraa (bool, optional) – Spectral units flag forwarded to
retrieve_ssp_data_from_fsps(). Defaults toTrue.**kwargs – Additional parameters forwarded to
retrieve_ssp_data_from_fsps().
- Returns:
None
rubix.spectra.ssp.grid module#
- class rubix.spectra.ssp.grid.HDF5SSPGrid(age: Float[Array, 'age_bins'], metallicity: Float[Array, 'metallicity_bins'], wavelength: Float[Array, 'wavelength_bins'], flux: Float[Array, 'metallicity_bins age_bins wavelength_bins'])[source]#
Bases:
SSPGridClass for SSP models stored in HDF5 format. Useful for custom collections of Bruzual & Charlot (2003) and MILES models.
- age#
SSP ages in Gyr.
- Type:
Float[Array, AGE_AXIS]
- metallicity#
SSP metallicities.
- Type:
Float[Array, METALLICITY_AXIS]
- wavelength#
Wavelength grid in Angstrom.
- Type:
Float[Array, WAVELENGTH_AXIS]
- flux#
SSP fluxes in Lsun/Angstrom.
- Type:
Float[Array, FLUX_AXES]
- Parameters:
age (Float[Array, AGE_AXIS]) – SSP ages in Gyr.
metallicity (Float[Array, METALLICITY_AXIS]) – SSP metallicities.
wavelength (Float[Array, WAVELENGTH_AXIS]) – Wavelength grid in Angstrom.
flux (Float[Array, FLUX_AXES]) – SSP fluxes in Lsun/Angstrom.
Example
>>> config = { ... "name": "Bruzual & Charlot (2003)", ... "format": "HDF5", ... "source": "https://www.bruzual.org/bc03/", ... "file_name": "BC03lr.h5", ... "fields": { ... "age": {"name": "age", "units": "Gyr", ... "in_log": False}, ... "metallicity": {"name": "metallicity", "units": "", ... "in_log": False}, ... "wavelength": {"name": "wavelength", ... "units": "Angstrom", "in_log": False}, ... "flux": {"name": "flux", "units": "Lsun/Angstrom", ... "in_log": False} ... } ... } >>> from rubix.spectra.ssp.grid import HDF5SSPGrid >>> ssp = HDF5SSPGrid.from_file( ... config, file_location="../rubix/spectra/ssp/templates" ... )
>>> ssp.age.shape >>> ssp.metallicity.shape >>> ssp.wavelength.shape >>> ssp.flux.shape
- classmethod from_file(config: dict, file_location: str) SSPGrid[source]#
Load a SSP grid from a HDF5 file.
- Parameters:
config (dict) – Configuration dictionary.
file_location (str) – Location of the file.
- Returns:
The SSP grid in the correct units.
- Return type:
- Raises:
ValueError – If the configured file format is not HDF5.
- class rubix.spectra.ssp.grid.SSPGrid(age, metallicity, wavelength, flux, _logger=None)[source]#
Bases:
objectBase class for all SSP models.
- static checkout_SSP_template(config: dict, file_location: str) str[source]#
Check if the SSP template exists on disk, if not download it from the given URL in the configuration dictionary.
- Parameters:
config (dict) – Configuration dictionary.
file_location (str) – Location to save the template file.
- Returns:
The path to the file as str.
- Raises:
FileNotFoundError – If the template file cannot be found or downloaded.
- static convert_units(data: Float[Array, '...'] | Int[Array, '...'], from_units: str, to_units: str) Float[Array, '...'][source]#
Convert
datafromfrom_unitstoto_units.- Parameters:
data (Union[Float[Array, "..."], Int[Array, "..."]]) – Values to convert.
from_units (str) – Original unit string understood by Astropy.
to_units (str) – Target unit string.
- Returns:
Converted data.
- Return type:
Float[Array, “…”]
- classmethod from_file(config: dict, file_location: str)[source]#
Template function to load a SSP grid from a file.
- Parameters:
config (dict) – Configuration dictionary.
file_location (str) – Location of the file.
- Returns:
The SSP grid SSPGrid in the correct units.
- get_lookup_interpolation(method: str = 'cubic', extrap: int | float | bool | tuple[float, float] = 0) Partial[source]#
Return a 2D interpolation function for the SSP grid.
The function can be called with metallicity and age arguments to get the flux at that metallicity and age.
- Parameters:
method (str) – Interpolation kernel passed to
interp2d(). Defaults to"cubic".extrap (ExtrapValue) – Value returned for points outside the interpolation domain. Defaults to
0. Refer to theinterpax.Interpolator2Ddocumentation for details (https://interpax.readthedocs.io/en/latest/_api/interpax.Interpolator2D.html#interpax.Interpolator2D).
- Returns:
Interpolation function
f(metallicity, age).- Return type:
Partial
Examples
>>> grid = SSPGrid(...) >>> lookup = grid.get_lookup_interpolation() >>> metallicity = 0.02 >>> age = 1e9 >>> flux = lookup(metallicity, age)
>>> import matplotlib.pyplot as plt >>> from rubix.spectra.ssp.templates import BruzualCharlot2003 >>> ssp = BruzualCharlot2003 >>> wave = ssp.wavelength >>> age_index = 0 >>> met_index = 3 >>> delta_age = ssp.age[age_index + 1] - ssp.age[age_index] >>> target_age = ssp.age[age_index] + 0.5 * delta_age >>> delta_met = ( ... ssp.metallicity[met_index + 1] ... - ssp.metallicity[met_index] ... ) >>> target_met = ssp.metallicity[met_index] + 0.5 * delta_met >>> lookup = ssp.get_lookup_interpolation() >>> spec_calc = lookup(target_met, target_age) >>> spec_true = ssp.flux[met_index, age_index, :] >>> plt.plot(wave, spec_calc, label="calc") >>> plt.plot(wave, spec_true, label="true") >>> plt.legend() >>> plt.yscale("log")
- class rubix.spectra.ssp.grid.pyPipe3DSSPGrid(age: Float[Array, 'age_bins'], metallicity: Float[Array, 'metallicity_bins'], wavelength: Float[Array, 'wavelength_bins'], flux: Float[Array, 'metallicity_bins age_bins wavelength_bins'])[source]#
Bases:
SSPGridClass for all SSP models supported by the pyPipe3D project. See http://ifs.astroscu.unam.mx/pyPipe3D/templates/ for more information.
- age#
SSP ages in Gyr.
- Type:
Float[Array, AGE_AXIS]
- metallicity#
SSP metallicities.
- Type:
Float[Array, METALLICITY_AXIS]
- wavelength#
Wavelength grid in Angstrom.
- Type:
Float[Array, WAVELENGTH_AXIS]
- flux#
SSP fluxes in Lsun/Angstrom.
- Type:
Float[Array, FLUX_AXES]
- Parameters:
age (Float[Array, AGE_AXIS]) – SSP ages in Gyr.
metallicity (Float[Array, METALLICITY_AXIS]) – SSP metallicities.
wavelength (Float[Array, WAVELENGTH_AXIS]) – Wavelength grid in Angstrom.
flux (Float[Array, FLUX_AXES]) – SSP fluxes in Lsun/Angstrom.
Example
>>> config = { ... "name": "Mastar Charlot & Bruzual (2019)", ... "format": "pyPipe3D", ... "source": ... "https://ifs.astroscu.unam.mx/pyPipe3D/templates/", ... "file_name": "MaStar_CB19.slog_1_5.fits.gz", ... "fields": { ... "age": {"name": "age", "units": "Gyr", ... "in_log": False}, ... "metallicity": {"name": "metallicity", "units": "", ... "in_log": False}, ... "wavelength": {"name": "wavelength", ... "units": "Angstrom", "in_log": False}, ... "flux": {"name": "flux", "units": "Lsun/Angstrom", ... "in_log": False} ... } ... }
>>> from rubix.spectra.ssp.grid import pyPipe3DSSPGrid >>> ssp = pyPipe3DSSPGrid.from_file( ... config, file_location="../rubix/spectra/ssp/templates" ... )
- classmethod from_file(config: dict, file_location: str) SSPGrid[source]#
Load a SSP grid from a fits file in pyPipe3D format.
- Parameters:
config (dict) – Configuration dictionary.
file_location (str) – Location of the file.
- Returns:
The SSP grid in the correct units.
- Return type:
- Raises:
ValueError – If the configured file format is not pyPipe3D.
- static get_tZ_models(header: Header | dict, n_models: int) tuple[Float[Array, '...'], Float[Array, '...'], Float[Array, '...']][source]#
Read age, metallicity, and mass-to-light ratio at the normalization flux from a FITS file.
- Parameters:
header (Union[fits.Header, dict]) – FITS header (or dict-like) with spectral data.
n_models (int) – Number of models in the SSP grid.
- Returns:
Unique ages (Gyr), metallicities, and mass-to-light ratios at the normalization wavelength.
- Return type:
Tuple[Array, Array, Array]
Notes
Adapted from the
TNG_MaNGA_mockssin_ifu_clean.pyscript. See reginasar/TNG_MaNGA_mocks blob/3229dd47b441aef380ef7dbfdf110f39e5c5a77c/sin_ifu_clean.py#L1466
- static get_wavelength_from_header(header: Header | dict, wave_axis: int | None = None) Array[source]#
Generate a wavelength array using
header(anastropy.io.fits.header.Headerinstance) at axiswave_axis.The calculation follows:
wavelengths = CRVAL + CDELT * ([0, 1, ..., NAXIS] + 1 - CRPIX)
- Parameters:
header (Union[fits.Header, dict]) – FITS header (or dict-like) with spectral data.
wave_axis (int | None, optional) – Axis storing the wavelength metadata (CRVAL, CDELT, NAXIS, CRPIX). If
Nonethe function will default to axis 1.
- Returns:
CRVAL + CDELT * ([0, 1, ..., NAXIS] + 1 - CRPIX).- Return type:
Array
Notes
Adapted from the
TNG_MaNGA_mockssin_ifu_clean.pyscript. See reginasar/TNG_MaNGA_mocks blob/3229dd47b441aef380ef7dbfdf110f39e5c5a77c/sin_ifu_clean.py#L1466
rubix.spectra.ssp.templates module#
This module contains the supported templates for the SSP grid.
Example:
>>> from rubix.spectra.ssp.templates import BruzualCharlot2003
>>> BruzualCharlot2003
>>> print(BruzualCharlot2003.age)