Skip to content

Commit

Permalink
Backport PR #2576: Specviz Parser handling HDULists
Browse files Browse the repository at this point in the history
  • Loading branch information
gibsongreen authored and meeseeksmachine committed Dec 7, 2023
1 parent e999bfa commit 46124f6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Mosviz
Specviz
^^^^^^^

- Fixed parser bug where an HDUList would load as SpectrumList, even though it was a Spectrum1D. [#2576]

Specviz2d
^^^^^^^^^

Expand Down
11 changes: 8 additions & 3 deletions jdaviz/configs/specviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
from astropy.io.registry import IORegistryError
from astropy.nddata import StdDevUncertainty
from astropy.io import fits
from specutils import Spectrum1D, SpectrumList, SpectrumCollection

from jdaviz.core.events import SnackbarMessage
Expand Down Expand Up @@ -39,7 +40,6 @@ def specviz_spectrum1d_parser(app, data, data_label=None, format=None, show_in_v
# If no data label is assigned, give it a unique name
if not data_label:
data_label = app.return_data_label(data, alt_name="specviz_data")

if isinstance(data, SpectrumCollection):
raise TypeError("SpectrumCollection detected."
" Please provide a Spectrum1D or SpectrumList")
Expand All @@ -50,7 +50,13 @@ def specviz_spectrum1d_parser(app, data, data_label=None, format=None, show_in_v
elif isinstance(data, SpectrumList):
pass
elif isinstance(data, list):
data = SpectrumList.read(data, format=format)
# special processing for HDUList
if isinstance(data, fits.HDUList):
data = [Spectrum1D.read(data)]
data_label = [app.return_data_label(data_label, alt_name="specviz_data")]
else:
# list treated as SpectrumList if not an HDUList
data = SpectrumList.read(data, format=format)

Check warning on line 59 in jdaviz/configs/specviz/plugins/parsers.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/specviz/plugins/parsers.py#L59

Added line #L59 was not covered by tests
else:
path = pathlib.Path(data)

Expand Down Expand Up @@ -127,7 +133,6 @@ def specviz_spectrum1d_parser(app, data, data_label=None, format=None, show_in_v

with app.data_collection.delay_link_manager_update():
for i, spec in enumerate(data):

# note: if SpectrumList, this is just going to be the last unit when
# combined in the next block. should put a check here to make sure
# units are all the same or collect them in a list?
Expand Down
21 changes: 21 additions & 0 deletions jdaviz/configs/specviz/tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import pytest
from astropy import units as u
from astropy.io import fits
from astropy.tests.helper import assert_quantity_allclose
from glue.core.roi import XRangeROI
from glue.core.edit_subset_mode import OrMode, AndMode, AndNotMode
Expand Down Expand Up @@ -36,6 +37,26 @@ def test_load_spectrum1d(self):

assert isinstance(data, Spectrum1D)

def test_load_hdulist(self):
# Create a fake fits file with a 1D spectrum for testing.
primary_header = fits.Header({'TELESCOP': 'Fake Telescope'})
primary_hdu = fits.PrimaryHDU(header=primary_header)

# Image extension HDU with a 1D spectrum
wavelength = np.linspace(5000, 6000, 100)
flux = np.ones(100)
spectrum_table = fits.BinTableHDU.from_columns([
fits.Column(name='WAVELENGTH', array=wavelength, format='E', unit="Angstrom"),
fits.Column(name='FLUX', array=flux, format='E', unit="Jy")
])
spectrum_table.header['INSTRUME'] = 'Fake Instrument'
fake_hdulist = fits.HDUList([primary_hdu, spectrum_table])
self.label = "Test 1D Spectrum"
self.spec_app.load_data(fake_hdulist)
data = self.spec_app.get_data(data_label=self.label)
# HDUList should load as Spectrum1D
assert isinstance(data, Spectrum1D)

def test_load_spectrum_list_no_labels(self):
# now load three more spectra from a SpectrumList, without labels
self.spec_app.load_data(self.spec_list)
Expand Down

0 comments on commit 46124f6

Please sign in to comment.