Source code for lezargus.data._make.make_standard_spectra
"""Make functions to create the LezargusSpectrum objects for star spectra.
This module is just the part of the data making procedure to make the star
spectra objects. We only support a limited selection of stars because we also
load other metadata from the tables.
"""
# isort: split
# Import required to remove circular dependencies from type checking.
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from lezargus.library import hint
# isort: split
import astropy.table
import numpy as np
import lezargus
from lezargus.data._make import functionality
from lezargus.library import logging
[docs]
def make_standard_spectrum(basename: str) -> hint.LezargusSpectrum:
"""Load a spectrum data file and make a LezargusSpectrum class from it.
Parameters
----------
basename : str
The basename of the internal data file of the spectrum. The paths are
handled automatically.
Returns
-------
spectrum : LezargusSpectrum
The spectrum class.
"""
# We need to load the data file.
spectrum_table = load_spectrum_mrt_file(basename=basename)
wavelength = np.asarray(spectrum_table["wavelength"])
data = np.asarray(spectrum_table["data"])
uncertainty = np.asarray(spectrum_table["uncertainty"])
# We also need to load any potential metadata.
header_table = load_spectrum_header_file()
basename_tag = lezargus.library.path.get_filename_without_extension(
pathname=basename,
)
try:
header_keys = tuple(header_table["key"])
header_values = tuple(header_table[basename_tag])
except KeyError:
logging.critical(
critical_type=logging.DevelopmentError,
message=(
f"Internal header data for spectrum {basename_tag} does not"
" exist in header table."
),
)
header = {}
else:
header = dict(zip(header_keys, header_values, strict=True))
# Creating the spectrum object.
spectrum = lezargus.library.container.LezargusSpectrum(
wavelength=wavelength,
data=data,
uncertainty=uncertainty,
wavelength_unit="m",
data_unit="W m^-2 m^-1",
spectral_scale=header.get("LZDSPECS", None),
pixel_scale=header.get("LZDPIXPS", None),
slice_scale=header.get("LZDSLIPS", None),
mask=None,
flags=None,
header=header,
)
return spectrum
[docs]
def load_spectrum_mrt_file(basename: str) -> hint.Table:
"""Load a AAS MRT spectrum file to a standard table format to use.
Parameters
----------
basename : str
The MRT spectrum filename to load, paths are handled automatically.
Returns
-------
spectrum_table : Table
The Astropy table of the spectrum object.
"""
# Parsing the filename.
spectrum_filename = functionality.parse_basename(basename=basename)
mrt_table = astropy.table.Table.read(spectrum_filename, format="ascii.mrt")
# We format it to a standard table.
wavelength_column = mrt_table["wavelength"]
data_column = mrt_table["flux"]
uncertainty_column = mrt_table["uncertainty"]
# Parsing the table.
spectrum_table = astropy.table.Table(
[wavelength_column, data_column, uncertainty_column],
names=("wavelength", "data", "uncertainty"),
)
return spectrum_table