Skip to content

Commit

Permalink
adding FITS L2 support in rampviz
Browse files Browse the repository at this point in the history
  • Loading branch information
bmorris3 committed Sep 27, 2024
1 parent c9425b8 commit ec3813c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion jdaviz/configs/imviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def _parse_image(app, file_obj, data_label, ext=None, parent=None):
for data, data_label in data_iter:

# if the science extension hasn't been identified yet, do so here:
if sci_ext is None and ('[DATA' in data_label or '[SCI' in data_label):
if sci_ext is None and data.ndim == 2 and ('[DATA' in data_label or '[SCI' in data_label):
sci_ext = data_label

if isinstance(data.coords, GWCS) and (data.coords.bounding_box is not None):
Expand Down
5 changes: 3 additions & 2 deletions jdaviz/configs/rampviz/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ def load_data(self, data, data_label=None, **kwargs):
def _set_x_axis(self, msg={}):
group_viewer = self.app.get_viewer(self._default_group_viewer_reference_name)
ref_data = group_viewer.state.reference_data
group_viewer.state.x_att = ref_data.id["Pixel Axis 0 [z]"]
group_viewer.state.y_att = ref_data.id["Pixel Axis 1 [y]"]
if ref_data:
group_viewer.state.x_att = ref_data.id["Pixel Axis 0 [z]"]
group_viewer.state.y_att = ref_data.id["Pixel Axis 1 [y]"]

def select_group(self, group_index):
"""
Expand Down
43 changes: 29 additions & 14 deletions jdaviz/configs/rampviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
standardize_metadata, download_uri_to_path,
PRIHDR_KEY, standardize_roman_metadata
)
from jdaviz.configs.imviz.plugins.parsers import _parse_image as _parse_image_imviz

try:
from roman_datamodels import datamodels as rdd
Expand All @@ -24,7 +25,7 @@

@data_parser_registry("ramp-data-parser")
def parse_data(app, file_obj, data_type=None, data_label=None,
parent=None, cache=None, local_path=None, timeout=None,
ext=None, parent=None, cache=None, local_path=None, timeout=None,
integration=0):
"""
Attempts to parse a data file and auto-populate available viewers in
Expand Down Expand Up @@ -104,7 +105,9 @@ def parse_data(app, file_obj, data_type=None, data_label=None,

with fits.open(file_obj) as hdulist:
_parse_hdulist(
app, hdulist, file_name=data_label or file_name,
app, hdulist,
ext=ext, parent=parent,
file_name=data_label or file_name,
integration=integration,
group_viewer_reference_name=group_viewer_reference_name,
diff_viewer_reference_name=diff_viewer_reference_name,
Expand Down Expand Up @@ -190,9 +193,13 @@ def _roman_3d_to_glue_data(
ramp_cube_data_label = f"{data_label}[DATA]"
ramp_diff_data_label = f"{data_label}[DIFF]"

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(
move_group_axis_last(data)
data_reshaped
)
app._jdaviz_helper.cube_cache[ramp_diff_data_label] = NDDataArray(
move_group_axis_last(diff_data)
Expand All @@ -204,14 +211,14 @@ def _roman_3d_to_glue_data(
# load these cubes into the app:
_parse_ndarray(
app,
file_obj=move_group_axis_last(data),
file_obj=data_reshaped,
data_label=ramp_cube_data_label,
viewer_reference_name=group_viewer_reference_name,
meta=meta
)
_parse_ndarray(
app,
file_obj=move_group_axis_last(diff_data),
file_obj=diff_data_reshaped,
data_label=ramp_diff_data_label,
viewer_reference_name=diff_viewer_reference_name,
meta=meta
Expand All @@ -224,7 +231,9 @@ def _roman_3d_to_glue_data(


def _parse_hdulist(
app, hdulist, file_name=None,
app, hdulist,
ext=None, parent=None,
file_name=None,
integration=None,
group_viewer_reference_name=None,
diff_viewer_reference_name=None,
Expand All @@ -235,8 +244,13 @@ def _parse_hdulist(
file_name = file_name or "Unknown HDU object"

hdu = hdulist[1] # extension containing the ramp
if hdu.header['NAXIS'] != 4:
raise ValueError(f"Expected a ramp with NAXIS=4 (with axes:"

if hdu.header['NAXIS'] == 2:
_parse_image_imviz(app, hdulist, data_label=file_name, ext=ext, parent=parent)
return

elif hdu.header['NAXIS'] != 4:
raise ValueError(f"Expected a ramp with NAXIS=4 (with dimensions: "
f"integrations, groups, x, y), but got "
f"NAXIS={hdu.header['NAXIS']}.")

Expand Down Expand Up @@ -298,14 +312,11 @@ def _parse_ramp_cube(app, ramp_cube_data, flux_unit, file_name,
np.diff(ramp_cube_data, axis=0)
])

if from_jwst_nirspec_irs2:
# JWST/NIRSpec in IRS2 readout needs an additional axis swap for x and y:
def move_axes(x):
return np.swapaxes(move_group_axis_last(x), 0, 1)
else:
move_axes = move_group_axis_last
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)

group_data_label = app.return_data_label(file_name, ext="DATA")
Expand All @@ -322,6 +333,10 @@ def move_axes(x):
# load these cubes into the cache:
app._jdaviz_helper.cube_cache[data_label] = data_entry

# set as reference data in this viewer
viewer = app.get_viewer(viewer_ref)
viewer.state.reference_data = app.data_collection[data_label]

app._jdaviz_helper._loaded_flux_cube = app.data_collection[group_data_label]


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:
if len(self.app.data_collection) == 2 and len(self.app._jdaviz_helper.cube_cache):
self.extract(add_data=True)
self.integration_viewer._initialize_x_axis()

Expand Down

0 comments on commit ec3813c

Please sign in to comment.