Skip to content

Commit

Permalink
working towards support for L2 files, linking corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
bmorris3 committed Sep 27, 2024
1 parent ec3813c commit f0f44cf
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 40 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ New Features

- The standalone version of jdaviz now uses solara instead of voila, resulting in faster load times. [#2909]

- New configuration for ramp/Level 1 data products from Roman WFI and JWST [#3120, #3148, #3167, #3171]
- New configuration for ramp/Level 1 data products from Roman WFI and JWST [#3120, #3148, #3167, #3171, #3194]

- Unit columns are now visible by default in the results table in model fitting. [#3196]

Expand Down
19 changes: 14 additions & 5 deletions jdaviz/configs/default/plugins/data_quality/data_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ def _update_available_viewers(self):
def is_image_viewer(viewer):
from jdaviz.configs.imviz.plugins.viewers import ImvizImageView
from jdaviz.configs.cubeviz.plugins.viewers import CubevizImageView
from jdaviz.configs.rampviz.plugins.viewers import RampvizImageView

return isinstance(viewer, (ImvizImageView, CubevizImageView))
return isinstance(viewer, (ImvizImageView, CubevizImageView, RampvizImageView))

viewer_filter_names = [filt.__name__ for filt in self.viewer.filters]
if 'is_image_viewer' not in viewer_filter_names:
Expand Down Expand Up @@ -151,6 +152,9 @@ def load_default_flag_maps(self):

@property
def dq_layer_selected_flattened(self):
if not hasattr(self, 'dq_layer'):
return []

selected_dq = self.dq_layer.selected_obj
if not len(selected_dq):
return []
Expand All @@ -170,6 +174,7 @@ def unique_flags(self):
return []

dq = selected_dq[0].get_image_data()

return np.unique(dq[~np.isnan(dq)])

@property
Expand Down Expand Up @@ -210,11 +215,15 @@ def init_decoding(self, event={}, viewers=None):

# for cubeviz, also change uncert-viewer defaults to
# map the out-of-bounds regions to the cmap's `bad` color:
if self.app.config == 'cubeviz':
uncert_viewer = self.app.get_viewer(
self.app._jdaviz_helper._default_uncert_viewer_reference_name
if self.app.config in ('cubeviz', 'rampviz'):
viewer = self.app.get_viewer(
getattr(
self.app._jdaviz_helper,
'_default_uncert_viewer_reference_name', 'level-2'
)
)
for layer in uncert_viewer.layers:

for layer in viewer.layers:
# allow bad alpha for image layers, not subsets:
if not hasattr(layer, 'subset_array'):
layer.composite._allow_bad_alpha = True
Expand Down
4 changes: 3 additions & 1 deletion jdaviz/configs/default/plugins/plot_options/plot_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,10 +1164,12 @@ def _viewer_is_image_viewer(self):
from jdaviz.configs.imviz.plugins.viewers import ImvizImageView
from jdaviz.configs.cubeviz.plugins.viewers import CubevizImageView
from jdaviz.configs.mosviz.plugins.viewers import MosvizImageView, MosvizProfile2DView
from jdaviz.configs.rampviz.plugins.viewers import RampvizImageView

def _is_image_viewer(viewer):
return isinstance(viewer, (ImvizImageView, CubevizImageView,
MosvizImageView, MosvizProfile2DView))
MosvizImageView, MosvizProfile2DView,
RampvizImageView))

viewers = self.viewer.selected_obj
if not isinstance(viewers, list):
Expand Down
26 changes: 25 additions & 1 deletion jdaviz/configs/rampviz/helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from jdaviz.core.events import SliceSelectSliceMessage
from jdaviz.core.events import SliceSelectSliceMessage, NewViewerMessage
from jdaviz.core.helpers import CubeConfigHelper
from jdaviz.configs.rampviz.plugins.viewers import RampvizImageView

Expand Down Expand Up @@ -98,3 +98,27 @@ def get_data(self, data_label=None, spatial_subset=None,
return self._get_data(data_label=data_label, spatial_subset=spatial_subset,
temporal_subset=temporal_subset,
cls=cls, use_display_units=use_display_units)

def create_image_viewer(self, viewer_name=None, data=None):
"""
Create a new image viewer.
Parameters
----------
viewer_name : str or `None`
Viewer name/ID to use. If `None`, it is auto-generated.
Returns
-------
viewer : `~jdaviz.configs.imviz.plugins.viewers.ImvizImageView`
Image viewer instance.
"""
from jdaviz.configs.imviz.plugins.viewers import ImvizImageView

# Cannot assign data to real Data because it loads but it will
# not update checkbox in Data menu.

return self.app._on_new_viewer(
NewViewerMessage(ImvizImageView, data=None, sender=self.app),
vid=viewer_name, name=viewer_name)
42 changes: 14 additions & 28 deletions jdaviz/configs/rampviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ def _roman_3d_to_glue_data(

data_reshaped = move_group_axis_last(data)
diff_data_reshaped = move_group_axis_last(diff_data)
data_reshaped = data_reshaped - data_reshaped[..., 0]

# load these cubes into the cache:
app._jdaviz_helper.cube_cache[ramp_cube_data_label] = NDDataArray(
Expand Down Expand Up @@ -246,7 +245,18 @@ def _parse_hdulist(
hdu = hdulist[1] # extension containing the ramp

if hdu.header['NAXIS'] == 2:
# this may be a calibrated Level 2 image:
_parse_image_imviz(app, hdulist, data_label=file_name, ext=ext, parent=parent)
new_viewer_name = 'level-2'

# create a level-2 viewer if none exists:
if new_viewer_name not in app.get_viewer_reference_names():
app._jdaviz_helper.create_image_viewer(viewer_name=new_viewer_name)

# add the SCI extension to the level-2 viewer:
if 'SCI' in ext:
idx = len(ext) - ext.index('SCI')
app.add_data_to_viewer(new_viewer_name, app.data_collection[-idx].label)
return

elif hdu.header['NAXIS'] != 4:
Expand Down Expand Up @@ -283,27 +293,6 @@ def _parse_hdulist(
def _parse_ramp_cube(app, ramp_cube_data, flux_unit, file_name,
group_viewer_reference_name, diff_viewer_reference_name,
meta=None):

# Identify NIRSpec IRS2 detector mode, which needs special treatment.
# jdox: https://jwst-docs.stsci.edu/jwst-near-infrared-spectrograph/nirspec-instrumentation/
# nirspec-detectors/nirspec-detector-readout-modes-and-patterns/nirspec-irs2-detector-readout-mode
if 'meta.model_type' in meta:
# this is a Level1bModel, which has metadata in a Node rather
# than a dictionary:
from_jwst_nirspec_irs2 = (
meta.get('meta._primary_header.TELESCOP') == 'JWST' and
meta.get('meta._primary_header.INSTRUME') == 'NIRSPEC' and
'IRS2' in meta.get('meta._primary_header.READPATT', '')
)
else:
# assume this was parsed from FITS:
header = meta.get('_primary_header', {})
from_jwst_nirspec_irs2 = (
header.get('TELESCOP') == 'JWST' and
header.get('INSTRUME') == 'NIRSPEC' and
'IRS2' in header.get('READPATT', '')
)

# last axis is the group axis, first two are spatial axes:
diff_data = np.vstack([
# begin with a group of zeros, so
Expand All @@ -312,12 +301,9 @@ def _parse_ramp_cube(app, ramp_cube_data, flux_unit, file_name,
np.diff(ramp_cube_data, axis=0)
])

def move_axes(x):
return np.swapaxes(move_group_axis_last(x), 0, 1)

ramp_cube = NDDataArray(move_axes(ramp_cube_data), unit=flux_unit, meta=meta)
ramp_cube = ramp_cube.subtract(ramp_cube[..., :1])
diff_cube = NDDataArray(move_axes(diff_data), unit=flux_unit, meta=meta)
ramp_data = move_group_axis_last(ramp_cube_data)
ramp_cube = NDDataArray(ramp_data, unit=flux_unit, meta=meta)
diff_cube = NDDataArray(move_group_axis_last(diff_data), unit=flux_unit, meta=meta)

group_data_label = app.return_data_label(file_name, ext="DATA")
diff_data_label = app.return_data_label(file_name, ext="DIFF")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def integration_viewer(self):

def _on_data_added(self, msg={}):
# only perform the default collapse after the first data load:
if len(self.app.data_collection) == 2 and len(self.app._jdaviz_helper.cube_cache):
if len(self.app._jdaviz_helper.cube_cache) and not self.extraction_available:
self.extract(add_data=True)
self.integration_viewer._initialize_x_axis()

Expand Down
3 changes: 2 additions & 1 deletion jdaviz/configs/rampviz/plugins/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def _default_integration_viewer_reference_name(self):
def _initial_x_axis(self, *args):
# Make sure that the x_att is correct on data load
ref_data = self.state.reference_data
if ref_data and ref_data.ndim == 3:

if ref_data:
self.state.x_att = ref_data.id["Pixel Axis 0 [z]"]

def set_plot_axes(self):
Expand Down
2 changes: 1 addition & 1 deletion jdaviz/configs/rampviz/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ def test_load_nirspec_irs2(rampviz_helper, jwst_level_1b_rectangular_ramp):

parsed_cube_shape = rampviz_helper.app.data_collection[0].shape
assert parsed_cube_shape == (
original_cube_shape[1], original_cube_shape[2], original_cube_shape[0]
original_cube_shape[2], original_cube_shape[1], original_cube_shape[0]
)
2 changes: 1 addition & 1 deletion jdaviz/core/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def on_limits_change(self, *args):
to_lims = self._map_limits(self.viewer, viewer, from_lims)
matched_refdata = viewer.state.reference_data

if hasattr(viewer, '_get_fov'):
if hasattr(viewer, '_get_fov') and matched_refdata is not None:
to_fov_sky = viewer._get_fov(wcs=matched_refdata.coords)
else:
to_fov_sky = None
Expand Down

0 comments on commit f0f44cf

Please sign in to comment.