From 546cdbaca93e9713a9da7ebc4b673cfc8155729f Mon Sep 17 00:00:00 2001 From: "Pey Lian Lim (Github)" <2090236+pllim@users.noreply.github.com> Date: Tue, 14 Dec 2021 09:54:12 -0500 Subject: [PATCH 1/4] BUG: Recreate JWST MJD-OBS if it is missing --- CHANGES.rst | 3 +++ jdaviz/configs/cubeviz/plugins/parsers.py | 23 +++++++++++++++---- .../cubeviz/plugins/tests/test_parsers.py | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index a661cc3a93..e93c7c5d1b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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, DATE-OBS, or DATE-BEG. [#1004] + Imviz ^^^^^ diff --git a/jdaviz/configs/cubeviz/plugins/parsers.py b/jdaviz/configs/cubeviz/plugins/parsers.py index 35bf85b511..e2fafe56ce 100644 --- a/jdaviz/configs/cubeviz/plugins/parsers.py +++ b/jdaviz/configs/cubeviz/plugins/parsers.py @@ -127,12 +127,27 @@ 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=1): 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 'MJD-OBS' not in hdulist[ext].header: + for key in ('MJD-BEG', 'DATE-OBS', 'DATE-BEG'): # 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 + + unit = u.Unit(hdulist[ext].header.get('BUNIT', 'count')) + flux = hdulist[ext].data << unit + wcs = WCS(hdulist[ext].header, hdulist) data = Spectrum1D(flux, wcs=wcs) # NOTE: Tried to only pass in sliced WCS but got error in Glue. diff --git a/jdaviz/configs/cubeviz/plugins/tests/test_parsers.py b/jdaviz/configs/cubeviz/plugins/tests/test_parsers.py index c7e545f8cc..d4172365c9 100644 --- a/jdaviz/configs/cubeviz/plugins/tests/test_parsers.py +++ b/jdaviz/configs/cubeviz/plugins/tests/test_parsers.py @@ -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 }) From 9aeceac6898716a0bc77bd59316358de785fe5dc Mon Sep 17 00:00:00 2001 From: "Pey Lian Lim (Github)" <2090236+pllim@users.noreply.github.com> Date: Tue, 14 Dec 2021 10:24:59 -0500 Subject: [PATCH 2/4] Actually I don't think DATE-BEG is a thing. --- CHANGES.rst | 2 +- jdaviz/configs/cubeviz/plugins/parsers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index e93c7c5d1b..306ba9934d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -26,7 +26,7 @@ Cubeviz ^^^^^^^ - Missing MJD-OBS in JWST data will no longer crash Cubeviz as long as - it has MJD-BEG, DATE-OBS, or DATE-BEG. [#1004] + it has MJD-BEG or DATE-OBS. [#1004] Imviz ^^^^^ diff --git a/jdaviz/configs/cubeviz/plugins/parsers.py b/jdaviz/configs/cubeviz/plugins/parsers.py index e2fafe56ce..966d4b5c6d 100644 --- a/jdaviz/configs/cubeviz/plugins/parsers.py +++ b/jdaviz/configs/cubeviz/plugins/parsers.py @@ -134,7 +134,7 @@ def _parse_jwst_s3d(app, hdulist, data_label, ext=1): # https://github.com/spacetelescope/jdaviz/issues/690 and # https://github.com/glue-viz/glue-astronomy/issues/59 if 'MJD-OBS' not in hdulist[ext].header: - for key in ('MJD-BEG', 'DATE-OBS', 'DATE-BEG'): # Possible alternatives + 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] From bc931062619daa6e47899154880085272e4afdf9 Mon Sep 17 00:00:00 2001 From: "Pey Lian Lim (Github)" <2090236+pllim@users.noreply.github.com> Date: Thu, 16 Dec 2021 18:05:13 -0500 Subject: [PATCH 3/4] Also load ERR and DQ of s3d --- jdaviz/configs/cubeviz/plugins/parsers.py | 24 ++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/jdaviz/configs/cubeviz/plugins/parsers.py b/jdaviz/configs/cubeviz/plugins/parsers.py index 966d4b5c6d..393cb9183f 100644 --- a/jdaviz/configs/cubeviz/plugins/parsers.py +++ b/jdaviz/configs/cubeviz/plugins/parsers.py @@ -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) @@ -127,7 +129,7 @@ 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, ext=1): +def _parse_jwst_s3d(app, hdulist, data_label, ext='SCI', viewer_name='flux-viewer'): from specutils import Spectrum1D # Manually inject MJD-OBS until we can support GWCS, see @@ -145,9 +147,12 @@ def _parse_jwst_s3d(app, hdulist, data_label, ext=1): hdulist[ext].header['MJD-OBS'] = t.mjd break - unit = u.Unit(hdulist[ext].header.get('BUNIT', 'count')) - flux = hdulist[ext].data << unit - wcs = WCS(hdulist[ext].header, hdulist) + 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. @@ -155,8 +160,9 @@ def _parse_jwst_s3d(app, hdulist, data_label, ext=1): # 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): From bb753f738100a04b0ed3e55d064006a29394658b Mon Sep 17 00:00:00 2001 From: "Pey Lian Lim (Github)" <2090236+pllim@users.noreply.github.com> Date: Thu, 16 Dec 2021 18:07:49 -0500 Subject: [PATCH 4/4] No need to inject non-SCI ext --- jdaviz/configs/cubeviz/plugins/parsers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jdaviz/configs/cubeviz/plugins/parsers.py b/jdaviz/configs/cubeviz/plugins/parsers.py index 393cb9183f..998f797eaa 100644 --- a/jdaviz/configs/cubeviz/plugins/parsers.py +++ b/jdaviz/configs/cubeviz/plugins/parsers.py @@ -135,7 +135,7 @@ def _parse_jwst_s3d(app, hdulist, data_label, ext='SCI', viewer_name='flux-viewe # 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 'MJD-OBS' not in hdulist[ext].header: + 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'):