Skip to content

Commit

Permalink
Inherit FITS header comment cards.
Browse files Browse the repository at this point in the history
Have show_primary be more sticky.
  • Loading branch information
pllim committed May 18, 2022
1 parent d8f0977 commit 17f542c
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 62 deletions.
16 changes: 8 additions & 8 deletions jdaviz/configs/cubeviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from specutils import Spectrum1D

from jdaviz.core.registries import data_parser_registry
from jdaviz.utils import PRIHDR_KEY
from jdaviz.utils import standardize_metadata, PRIHDR_KEY

__all__ = ['parse_data']

Expand Down Expand Up @@ -117,9 +117,9 @@ def _parse_hdulist(app, hdulist, file_name=None):

flux = hdu.data << flux_unit

metadata = dict(hdu.header)
metadata = standardize_metadata(hdu.header)
if hdu.name != 'PRIMARY' and 'PRIMARY' in hdulist:
metadata[PRIHDR_KEY] = dict(hdulist['PRIMARY'].header)
metadata[PRIHDR_KEY] = standardize_metadata(hdulist['PRIMARY'].header)

try:
sc = Spectrum1D(flux=flux, wcs=wcs, meta=metadata)
Expand Down Expand Up @@ -167,9 +167,9 @@ def _parse_jwst_s3d(app, hdulist, data_label, ext='SCI', viewer_name='flux-viewe
flux = hdulist[ext].data << unit
wcs = WCS(hdulist['SCI'].header, hdulist) # Everything uses SCI WCS

metadata = dict(hdulist[ext].header)
metadata = standardize_metadata(hdulist[ext].header)
if hdulist[ext].name != 'PRIMARY' and 'PRIMARY' in hdulist:
metadata[PRIHDR_KEY] = dict(hdulist['PRIMARY'].header)
metadata[PRIHDR_KEY] = standardize_metadata(hdulist['PRIMARY'].header)

data = Spectrum1D(flux, wcs=wcs, meta=metadata)

Expand Down Expand Up @@ -205,10 +205,10 @@ def _parse_esa_s3d(app, hdulist, data_label, ext='DATA', viewer_name='flux-viewe
flux = np.moveaxis(flux, 0, -1)
flux = np.swapaxes(flux, 0, 1)

metadata = dict(hdulist[ext].header)
metadata = standardize_metadata(hdulist[ext].header)
metadata.update(wcs_dict) # To be internally consistent
if hdulist[ext].name != 'PRIMARY' and 'PRIMARY' in hdulist:
metadata[PRIHDR_KEY] = dict(hdulist['PRIMARY'].header)
metadata[PRIHDR_KEY] = standardize_metadata(hdulist['PRIMARY'].header)

data = Spectrum1D(flux, wcs=wcs, meta=metadata)

Expand Down Expand Up @@ -241,7 +241,7 @@ def _parse_spectrum1d_3d(app, file_obj, data_label=None):

flux = np.moveaxis(flux, 1, 0)

s1d = Spectrum1D(flux=flux, wcs=file_obj.wcs, meta=file_obj.meta)
s1d = Spectrum1D(flux=flux, wcs=file_obj.wcs, meta=standardize_metadata(file_obj.meta))

cur_data_label = f"{data_label}[{attr.upper()}]"
app.add_data(s1d, cur_data_label)
Expand Down
28 changes: 22 additions & 6 deletions jdaviz/configs/default/plugins/metadata_viewer/metadata_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import TemplateMixin, DatasetSelectMixin
from jdaviz.utils import PRIHDR_KEY
from jdaviz.utils import PRIHDR_KEY, COMMENTCARD_KEY

__all__ = ['MetadataViewer']

Expand All @@ -13,6 +13,7 @@ class MetadataViewer(TemplateMixin, DatasetSelectMixin):
has_metadata = Bool(False).tag(sync=True)
has_primary = Bool(False).tag(sync=True)
show_primary = Bool(False).tag(sync=True)
has_comments = Bool(False).tag(sync=True)
metadata = List([]).tag(sync=True)

def __init__(self, *args, **kwargs):
Expand All @@ -24,6 +25,7 @@ def reset(self):
self.has_metadata = False
self.has_primary = False
self.show_primary = False
self.has_comments = False
self.metadata = []

@observe("dataset_selected")
Expand All @@ -38,9 +40,9 @@ def show_metadata(self, event):
self.has_primary = True
else:
self.has_primary = False
self.show_primary = False

self.show_primary = False
self.find_public_metadata(data.meta, primary_only=False)
self.find_public_metadata(data.meta, primary_only=self.show_primary)

@observe("show_primary")
def handle_show_primary(self, event):
Expand All @@ -61,8 +63,7 @@ def find_public_metadata(self, meta, primary_only=False):
if PRIHDR_KEY in meta:
meta = meta[PRIHDR_KEY]
else:
self.metadata = []
self.has_metadata = False
self.reset()
return

d = flatten_nested_dict(meta)
Expand All @@ -73,8 +74,23 @@ def find_public_metadata(self, meta, primary_only=False):
if badkey in d:
del d[badkey]

if COMMENTCARD_KEY in meta:
self.has_comments = True

def get_comment(key):
if key in meta[COMMENTCARD_KEY]._header:
val = meta[COMMENTCARD_KEY][key]
else:
val = ''
return val
else:
self.has_comments = False

def get_comment(key):
return ''

# TODO: Option to not sort?
public_meta = sorted(zip(d.keys(), map(str, d.values())))
public_meta = sorted(zip(d.keys(), map(str, d.values()), map(get_comment, d.keys())))
if len(public_meta) > 0:
self.metadata = public_meta
self.has_metadata = True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
<v-row no-gutters>
<v-col cols=6><U>Key</U></v-col>
<v-col cols=6><U>Value</U></v-col>
<v-col v-if="has_comments" cols=6><U>Comment</U></v-col>
</v-row>
<v-row
v-for="item in metadata"
:key="item[0]"
no-gutters>
<v-col cols=6>{{ item[0] }}</v-col>
<v-col cols=6>{{ item[1] }}</v-col>
<v-col v-if="has_comments" cols=6>{{ item[2] }}</v-col>
</v-row>
</div>
<v-row v-else>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,82 @@
import numpy as np
import pytest
from astropy.io import fits
from astropy.nddata import NDData

from jdaviz.configs.default.plugins.metadata_viewer.metadata_viewer import MetadataViewer
from jdaviz.utils import PRIHDR_KEY


def test_view_dict(imviz_helper):
mv = MetadataViewer(app=imviz_helper.app)
arr = np.zeros((2, 2))
arr = np.zeros((2, 2), dtype=np.float32)
ndd_1 = NDData(arr, meta={
'EXTNAME': 'SCI', 'EXTVER': 1, 'BAR': 10.0, '_hidden': 'no show',
'HISTORY': 'Hmm', '': 'Invalid', 'FOO': '', 'COMMENT': 'a test', 'BOZO': None})
ndd_2 = NDData(arr, meta={
'EXTNAME': 'ASDF', 'REF': {'bar': 10.0, 'foo': {'1': '', '2': [1, 2]}}})
ndd_3 = NDData(arr, meta={
'EXTVER': 1, 'EXTNAME': 'SCI',
PRIHDR_KEY: {'EXTNAME': 'PRIMARY', 'APERTURE': '#TODO', 'COMMENT': 'a test'}})

# MEF
ndd_3 = fits.HDUList([fits.PrimaryHDU(), fits.ImageHDU(arr)])
ndd_3[1].name = 'DATA'
ndd_4 = fits.HDUList([fits.PrimaryHDU(), fits.ImageHDU(arr)])
ndd_4[0].header['APERTURE'] = ('#TODO', 'Aperture')
ndd_4[1].name = 'DATA'

imviz_helper.load_data(ndd_1, data_label='has_simple_meta')
imviz_helper.load_data(ndd_2, data_label='has_nested_meta')
imviz_helper.load_data(ndd_3, data_label='has_primary')
imviz_helper.load_data(ndd_4, data_label='has_primary_2')
imviz_helper.load_data(arr, data_label='no_meta')
assert mv.dataset.labels == ['has_simple_meta[DATA]', 'has_nested_meta[DATA]',
'has_primary[DATA]', 'no_meta']
'has_primary[DATA,1]', 'has_primary_2[DATA,1]', 'no_meta']

mv.dataset_selected = 'has_simple_meta[DATA]'
assert not mv.has_primary
assert not mv.show_primary
assert not mv.has_comments
assert mv.has_metadata
assert mv.metadata == [
('BAR', '10.0'), ('BOZO', 'None'), ('EXTNAME', 'SCI'),
('EXTVER', '1'), ('FOO', '')], mv.metadata
('BAR', '10.0', ''), ('BOZO', 'None', ''), ('EXTNAME', 'SCI', ''),
('EXTVER', '1', ''), ('FOO', '', '')]

mv.dataset_selected = 'has_nested_meta[DATA]'
assert not mv.has_primary
assert not mv.show_primary
assert not mv.has_comments
assert mv.has_metadata
assert mv.metadata == [
('EXTNAME', 'ASDF'), ('REF.bar', '10.0'),
('REF.foo.1', ''), ('REF.foo.2.0', '1'), ('REF.foo.2.1', '2')], mv.metadata
('EXTNAME', 'ASDF', ''), ('REF.bar', '10.0', ''),
('REF.foo.1', '', ''), ('REF.foo.2.0', '1', ''), ('REF.foo.2.1', '2', '')]

mv.dataset_selected = 'has_primary[DATA]'
mv.dataset_selected = 'has_primary[DATA,1]'
assert mv.has_primary
assert not mv.show_primary
assert mv.has_comments
assert mv.has_metadata
assert mv.metadata == [('EXTNAME', 'SCI'), ('EXTVER', '1')]
assert mv.metadata == [('BITPIX', '-32', 'array data type'),
('EXTNAME', 'DATA', 'extension name'),
('GCOUNT', '1', 'number of groups'),
('NAXIS', '2', 'number of array dimensions'),
('NAXIS1', '2', ''), ('NAXIS2', '2', ''),
('PCOUNT', '0', 'number of parameters'),
('XTENSION', 'IMAGE', 'Image extension')]
mv.show_primary = True
assert mv.metadata == [('APERTURE', '#TODO'), ('EXTNAME', 'PRIMARY')]
assert mv.metadata == [('BITPIX', '8', 'array data type'), ('EXTEND', 'True', ''),
('NAXIS', '0', 'number of array dimensions'),
('SIMPLE', 'True', 'conforms to FITS standard')]

mv.dataset_selected = 'has_primary_2[DATA,1]'
assert mv.show_primary # Make sure it sticks if possible
assert mv.has_comments
assert mv.metadata == [('APERTURE', '#TODO', 'Aperture'),
('BITPIX', '8', 'array data type'), ('EXTEND', 'True', ''),
('NAXIS', '0', 'number of array dimensions'),
('SIMPLE', 'True', 'conforms to FITS standard')]

mv.dataset_selected = 'no_meta'
assert not mv.has_primary
assert not mv.show_primary
assert not mv.has_comments
assert not mv.has_metadata
assert mv.metadata == []

Expand All @@ -65,5 +91,6 @@ def test_view_invalid(imviz_helper):
assert mv.dataset_selected == ''
assert not mv.has_primary
assert not mv.show_primary
assert not mv.has_comments
assert not mv.has_metadata
assert mv.metadata == []
10 changes: 5 additions & 5 deletions jdaviz/configs/imviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from jdaviz.core.registries import data_parser_registry
from jdaviz.core.events import SnackbarMessage
from jdaviz.utils import PRIHDR_KEY
from jdaviz.utils import standardize_metadata, PRIHDR_KEY

__all__ = ['parse_data']

Expand Down Expand Up @@ -230,7 +230,7 @@ def _jwst2data(file_obj, ext, data_label):
with AsdfInFits.open(file_obj) as af:
dm = af.tree
dm_meta = af.tree["meta"]
data.meta.update(dm_meta)
data.meta.update(standardize_metadata(dm_meta))

if unit_attr in dm_meta:
bunit = _validate_bunit(dm_meta[unit_attr], raise_error=False)
Expand Down Expand Up @@ -287,8 +287,8 @@ def _hdu2data(hdu, data_label, hdulist, include_wcs=True):

data = Data(label=new_data_label)
if hdulist is not None and hdu.name != 'PRIMARY' and 'PRIMARY' in hdulist:
data.meta[PRIHDR_KEY] = dict(hdulist['PRIMARY'].header)
data.meta.update(dict(hdu.header))
data.meta[PRIHDR_KEY] = standardize_metadata(hdulist['PRIMARY'].header)
data.meta.update(standardize_metadata(hdu.header))
if include_wcs:
data.coords = WCS(hdu.header, hdulist)
component = Component.autotyped(hdu.data, units=bunit)
Expand All @@ -310,7 +310,7 @@ def _nddata_to_glue_data(ndd, data_label):
comp_label = attrib.upper()
cur_label = f'{data_label}[{comp_label}]'
cur_data = Data(label=cur_label)
cur_data.meta.update(ndd.meta)
cur_data.meta.update(standardize_metadata(ndd.meta))
if ndd.wcs is not None:
cur_data.coords = ndd.wcs
raw_arr = arr
Expand Down
31 changes: 13 additions & 18 deletions jdaviz/configs/mosviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from jdaviz.configs.imviz.plugins.parsers import get_image_data_iterator
from jdaviz.core.registries import data_parser_registry
from jdaviz.core.events import SnackbarMessage
from jdaviz.utils import PRIHDR_KEY
from jdaviz.utils import standardize_metadata, PRIHDR_KEY

__all__ = ['mos_spec1d_parser', 'mos_spec2d_parser', 'mos_image_parser']

Expand Down Expand Up @@ -223,9 +223,7 @@ def mos_spec1d_parser(app, data_obj, data_labels=None):

for cur_data, cur_label in zip(data_obj, data_labels):
# Make metadata layout conform with other viz.
if 'header' in cur_data.meta:
cur_data.meta.update(cur_data.meta['header'])
del cur_data.meta['header']
cur_data.meta = standardize_metadata(cur_data.meta)

app.add_data(cur_data, cur_label, notify_done=False)

Expand Down Expand Up @@ -259,8 +257,8 @@ def _parse_as_spectrum1d(path):
data = hdulist[1].data
header = hdulist[1].header
wcs = WCS(header)
metadata = dict(header)
metadata[PRIHDR_KEY] = dict(hdulist[0].header)
metadata = standardize_metadata(header)
metadata[PRIHDR_KEY] = standardize_metadata(hdulist[0].header)
return Spectrum1D(data, wcs=wcs, meta=metadata)

# Coerce into list-like object
Expand Down Expand Up @@ -292,9 +290,7 @@ def _parse_as_spectrum1d(path):
data = _parse_as_spectrum1d(data)

# Make metadata layout conform with other viz.
if 'header' in data.meta:
data.meta.update(data.meta['header'])
del data.meta['header']
data.meta = standardize_metadata(data.meta)

# Set the instrument
# TODO: this should not be set to nirspec for all datasets
Expand Down Expand Up @@ -500,10 +496,10 @@ def _get_source_identifiers_by_hdu(hdus, filepaths=None, header_keys=['SOURCEID'
# Fallback 1: filepath if only one is given
# Fallback 2: filepath at indx, if list of files given
# Fallback 3: If nothing else, just our fallback value
src_name = \
os.path.basename(filepaths) if type(filepaths) is str \
else os.path.basename(filepaths[indx]) if type(filepaths) is list \
else FALLBACK_NAME
src_name = (
os.path.basename(filepaths) if type(filepaths) is str
else os.path.basename(filepaths[indx]) if type(filepaths) is list
else FALLBACK_NAME)
src_names.append(src_name)
except Exception:
# Source ID lookup shouldn't ever prevent target from loading. Downgrade all errors to
Expand Down Expand Up @@ -639,7 +635,8 @@ def mos_niriss_parser(app, data_dir, obs_label=""):
if temp[sci].header["SPORDER"] == 1:

data = temp[sci].data
meta = temp[sci].header
meta = standardize_metadata(temp[sci].header)
meta[PRIHDR_KEY] = standardize_metadata(temp[0].header)

# The wavelength is stored in a WAVELENGTH HDU. This is
# a 2D array, but in order to be able to use Spectrum1D
Expand Down Expand Up @@ -671,7 +668,7 @@ def mos_niriss_parser(app, data_dir, obs_label=""):
# TODO: Remove this once valid SRCTYPE values are present in all headers
for hdu in temp:
if ("SRCTYPE" in hdu.header and
(hdu.header["SRCTYPE"] in ["POINT", "EXTENDED"])):
(hdu.header["SRCTYPE"] in ("POINT", "EXTENDED"))):
pass
else:
hdu.header["SRCTYPE"] = "EXTENDED"
Expand All @@ -686,9 +683,7 @@ def mos_niriss_parser(app, data_dir, obs_label=""):

for spec in specs:
# Make metadata layout conform with other viz.
if 'header' in spec.meta:
spec.meta.update(spec.meta['header'])
del spec.meta['header']
spec.meta = standardize_metadata(spec.meta)

if spec.meta['SPORDER'] == 1 and spec.meta['EXTNAME'] == "EXTRACT1D":
label = f"{filter_name} Source {spec.meta['SOURCEID']} spec1d {orientation}"
Expand Down
Loading

0 comments on commit 17f542c

Please sign in to comment.