diff --git a/sunkit_instruments/response/tests/test_source_spectra.py b/sunkit_instruments/response/tests/test_source_spectra.py new file mode 100644 index 00000000..4ae5f401 --- /dev/null +++ b/sunkit_instruments/response/tests/test_source_spectra.py @@ -0,0 +1,40 @@ +""" +Smoke tests for SourceSpectra +""" +import numpy as np +import pytest + +import astropy.units as u + +from sunkit_instruments.response import SourceSpectra + + +@pytest.mark.parametrize(('density', 'meta'), [ + (None, None), + (1e15*u.K/u.cm**3, None), + (None, {'abundance_model': 'test'}), + (1e15*u.K/u.cm**3, {'abundance_model': 'test'}), +]) +def test_create_source_spectra(density, meta): + temperature = np.logspace(4, 9, 100) * u.K + wavelength = np.linspace(1, 1000, 50) * u.Angstrom + if density is not None: + density = density / temperature + data_shape = temperature.shape + wavelength.shape + data = np.random.rand(*data_shape) * u.Unit("photon cm3 s-1 sr-1 Angstrom-1") + spec = SourceSpectra( + temperature, + wavelength, + data, + density=density, + meta=meta, + ) + assert isinstance(spec.meta, dict) + assert isinstance(spec.data, u.Quantity) + assert isinstance(spec.wavelength, u.Quantity) + assert isinstance(spec.temperature, u.Quantity) + if density is not None: + assert isinstance(spec.density, u.Quantity) + else: + with pytest.raises(ValueError, match="No density data available."): + spec.density diff --git a/sunkit_instruments/response/thermal.py b/sunkit_instruments/response/thermal.py index 6dc6a89d..7de91ee5 100644 --- a/sunkit_instruments/response/thermal.py +++ b/sunkit_instruments/response/thermal.py @@ -108,7 +108,7 @@ def meta(self, x): if x is None: self._meta = {} elif isinstance(x, dict): - self._mata = x + self._meta = x else: raise TypeError(f'Unsupported metadata type {type(x)}') @@ -122,6 +122,14 @@ def temperature(self) -> u.K: def wavelength(self) -> u.Angstrom: return u.Quantity(self._da.wavelength.data, self._da.wavelength.attrs["unit"]) + @property + @u.quantity_input + def density(self) -> u.cm**(-3): + if "density" in self._da.coords: + return u.Quantity(self._da.density.data, self._da.density.attrs["unit"]) + else: + raise ValueError("No density data available.") + @property @u.quantity_input def data(self) -> u.photon * u.cm**3 / (u.s * u.Angstrom * u.steradian):