Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Recreate JWST MJD-OBS if it is missing #1004

Merged
merged 4 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Bug Fixes
Cubeviz
^^^^^^^

- Missing MJD-OBS in JWST data will no longer crash Cubeviz as long as
it has MJD-BEG or DATE-OBS. [#1004]

Imviz
^^^^^

Expand Down
39 changes: 30 additions & 9 deletions jdaviz/configs/cubeviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ def parse_data(app, file_obj, data_type=None, data_label=None):
telescop = prihdr.get('TELESCOP', '').lower()
filetype = prihdr.get('FILETYPE', '').lower()
if telescop == 'jwst' and filetype == '3d ifu cube':
# TODO: What about ERR, DQ, and WMAP?
data_label = f'{file_name}[SCI]'
_parse_jwst_s3d(app, hdulist, data_label)
for ext, viewer_name in (('SCI', 'flux-viewer'),
('ERR', 'uncert-viewer'),
('DQ', 'mask-viewer')):
data_label = f'{file_name}[{ext}]'
_parse_jwst_s3d(app, hdulist, data_label, ext=ext, viewer_name=viewer_name)
else:
_parse_hdu(app, hdulist, file_name=data_label or file_name)

Expand Down Expand Up @@ -127,21 +129,40 @@ def _parse_hdu(app, hdulist, file_name=None):
app.add_data_to_viewer('spectrum-viewer', data_label)


def _parse_jwst_s3d(app, hdulist, data_label):
def _parse_jwst_s3d(app, hdulist, data_label, ext='SCI', viewer_name='flux-viewer'):
from specutils import Spectrum1D

unit = u.Unit(hdulist[1].header.get('BUNIT', 'count'))
flux = hdulist[1].data << unit
wcs = WCS(hdulist[1].header, hdulist)
# Manually inject MJD-OBS until we can support GWCS, see
# https://github.com/spacetelescope/jdaviz/issues/690 and
# https://github.com/glue-viz/glue-astronomy/issues/59
if ext == 'SCI' and 'MJD-OBS' not in hdulist[ext].header:
for key in ('MJD-BEG', 'DATE-OBS'): # Possible alternatives
if key in hdulist[ext].header:
if key.startswith('MJD'):
hdulist[ext].header['MJD-OBS'] = hdulist[ext].header[key]
break
else:
from astropy.time import Time
t = Time(hdulist[ext].header[key])
hdulist[ext].header['MJD-OBS'] = t.mjd
break

if ext == 'DQ': # DQ flags have no unit
flux = hdulist[ext].data << u.dimensionless_unscaled
else:
unit = u.Unit(hdulist[ext].header.get('BUNIT', 'count'))
flux = hdulist[ext].data << unit
wcs = WCS(hdulist['SCI'].header, hdulist) # Everything uses SCI WCS
data = Spectrum1D(flux, wcs=wcs)

# NOTE: Tried to only pass in sliced WCS but got error in Glue.
# sliced_wcs = wcs[:, 0, 0] # Only want wavelengths
# data = Spectrum1D(flux, wcs=sliced_wcs)

app.add_data(data, data_label)
app.add_data_to_viewer('flux-viewer', data_label)
app.add_data_to_viewer('spectrum-viewer', data_label)
app.add_data_to_viewer(viewer_name, data_label)
if viewer_name == 'flux-viewer':
app.add_data_to_viewer('spectrum-viewer', data_label)


def _parse_spectrum1d_3d(app, file_obj):
Expand Down
2 changes: 1 addition & 1 deletion jdaviz/configs/cubeviz/plugins/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def image_hdu_obj():
'CTYPE1': 'RA---TAN', 'CTYPE2': 'DEC--TAN', 'CTYPE3': 'WAVE-LOG',
'CRVAL1': 205.4384, 'CRVAL2': 27.004754, 'CRVAL3': 3.62159598486E-07,
'LONPOLE': 180.0, 'LATPOLE': 27.004754, 'MJDREFI': 0.0,
'MJDREFF': 0.0, 'DATE-OBS': '2014-03-30', 'MJD-OBS': 56746.0,
'MJDREFF': 0.0, 'DATE-OBS': '2014-03-30',
'RADESYS': 'FK5', 'EQUINOX': 2000.0
})

Expand Down