Skip to content

Commit ef5b123

Browse files
authored
Merge pull request #1266 from rosteen/no-masked-spectral-axis-from-table
Fix JWST WFSS read to handle spectrum padding
2 parents 3ca4d5c + 4f8372d commit ef5b123

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Bug Fixes
2222
- Add appropriate axes_names to resulting GWCS when a ``Spectrum`` is created with a spectral axis
2323
specified and no WCS, based on the spectral axis units. [#1264]
2424

25+
- Fix spectra resulting from new JWST WFSS format having `MaskedQuantity` spectral_axis. [#1266]
26+
2527
2.1.0 (2025-07-30)
2628
------------------
2729

specutils/io/default_loaders/jwst_reader.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@ def _jwst_spectrum_from_table(data, hdu_header, primary_header, flux_col=None, s
296296

297297
wavelength = Quantity(data['WAVELENGTH'])
298298

299+
unpadded_indices = None
300+
if hasattr(wavelength, 'mask'):
301+
# In this case the spectra have been padded to make their arrays have equal length
302+
unpadded_indices = np.where(~wavelength.mask)[0]
303+
wavelength = wavelength[unpadded_indices].unmasked
304+
299305
# Determine if FLUX or SURF_BRIGHT column should be returned
300306
# based on whether it is point or extended source. This will be overridden
301307
# if flux_col is input by the user.
@@ -349,6 +355,11 @@ def _jwst_spectrum_from_table(data, hdu_header, primary_header, flux_col=None, s
349355
if 'SOURCE_ID' in data.colnames:
350356
meta['source_id'] = data['SOURCE_ID']
351357

358+
if unpadded_indices is not None:
359+
# In this case the spectra arrays have been padded to make them have consistent length
360+
flux = flux[unpadded_indices].unmasked
361+
uncertainty = uncertainty[unpadded_indices]
362+
352363
return Spectrum(flux=flux, spectral_axis=wavelength,
353364
uncertainty=uncertainty, meta=meta)
354365

specutils/io/default_loaders/tests/test_jwst_reader.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
from astropy.io import fits
77
from astropy.io.registry import IORegistryError
88
from astropy.modeling import models
9-
from astropy.table import Table
9+
from astropy.table import Table, QTable
1010
from astropy.utils.exceptions import AstropyUserWarning
11+
from astropy.utils.masked import Masked
1112
from gwcs.wcs import WCS
1213

1314
from specutils import Spectrum, SpectrumList
@@ -93,12 +94,21 @@ def spec_multi(request):
9394

9495
def create_wfss_hdu(name='EXTRACT1D'):
9596
# Each row contains arrays defining the spectrum of a single source
96-
data = [[1, 2, 3], [[10, 20, 30] * u.um] * 3, [[2, 3, 4] * u.Jy] * 3,
97+
data = [[1, 2, 3],
98+
[Masked([10, 20, 30] * u.um, mask=[False, False, True]),
99+
Masked([10, 20, 30] * u.um, mask=[True, False, False]),
100+
Masked([10, 20, 30] * u.um, mask=[False, False, False])],
101+
[Masked([2, 3, 4] * u.Jy, mask=[False, False, True]),
102+
Masked([2, 3, 4] * u.Jy, mask=[True, False, False]),
103+
Masked([2, 3, 4] * u.Jy, mask=[False, False, False])],
97104
[[0.1, 0.1, 0.1] * u.Jy] * 3,
98-
[[2, 3, 4 ]* u.MJy/u.sr] * 3, [[0.1, 0.1, 0.1] * u.MJy/u.sr] * 3,
105+
[Masked([2, 3, 4] * u.MJy/u.sr, mask=[False, False, True]),
106+
Masked([2, 3, 4] * u.MJy/u.sr, mask=[True, False, False]),
107+
Masked([2, 3, 4] * u.MJy/u.sr, mask=[False, False, False])],
108+
[[0.1, 0.1, 0.1] * u.MJy/u.sr] * 3,
99109
[0, 0, 0], ['POINT', 'POINT', 'EXTENDED']]
100110

101-
table = Table(data=data, names=['SOURCE_ID','WAVELENGTH', 'FLUX', 'FLUX_ERROR', 'SURF_BRIGHT',
111+
table = QTable(data=data, names=['SOURCE_ID','WAVELENGTH', 'FLUX', 'FLUX_ERROR', 'SURF_BRIGHT',
102112
'SB_ERROR', 'DQ', 'SOURCE_TYPE'])
103113

104114
hdu = fits.BinTableHDU(table, name=name)
@@ -164,9 +174,9 @@ def test_jwst_wfss_multi_reader(tmp_path, spec_multi_new, format):
164174
for item in data:
165175
assert isinstance(item, Spectrum)
166176

167-
assert data[0].shape == (3,)
177+
assert data[0].shape == (2,)
168178
assert data[0].flux.unit == u.Jy
169-
assert data[1].shape == (3,)
179+
assert data[1].shape == (2,)
170180
assert data[1].flux.unit == u.Jy
171181
assert data[2].shape == (3,)
172182
assert data[2].flux.unit == u.MJy/u.sr

0 commit comments

Comments
 (0)