Skip to content

Commit

Permalink
Fix unit conversion plugin
Browse files Browse the repository at this point in the history
but still needs unreleased glue

[ci skip] [rtd skip]
  • Loading branch information
pllim committed Mar 29, 2023
1 parent 46024af commit f64890b
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 702 deletions.
15 changes: 3 additions & 12 deletions docs/specviz/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,8 @@ To export the table into the notebook via the API, call
Unit Conversion
===============

.. note::

This plugin is temporarily disabled. Effort to improve it is being
tracked at https://github.com/spacetelescope/jdaviz/issues/1972 .

The spectral flux density and spectral axis units can be converted
using the Unit Conversion plugin. The Spectrum1D object to be
converted is the currently selected spectrum in the spectrum viewer :guilabel:`Data`
icon in the viewer toolbar.
using the Unit Conversion plugin.

Select the frequency, wavelength, or energy unit in the
:guilabel:`New Spectral Axis Unit` pulldown
Expand All @@ -136,10 +129,8 @@ Select the flux density unit in the :guilabel:`New Flux Unit` pulldown
(e.g., Jansky, W/(Hz/m2), ph/(Angstrom cm2 s)).

The :guilabel:`Apply` button will convert the flux density and/or
spectral axis units and create a new Spectrum1D object that
is automatically switched to in the spectrum viewer.
The name of the new Spectrum1D object is "_units_copy_" plus
the flux and spectral units of the spectrum.
spectral axis units being displayed in the spectrum viewer, if applicable.
This does not affect the underlying data.

.. _line-lists:

Expand Down
2 changes: 0 additions & 2 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ def equivalent_units(self, data, cid, units):
'W / (m2 Hz)', 'W / (Hz m2)', # Order is different in astropy v5.3
'eV / (s m2 Hz)', 'eV / (Hz s m2)',
'erg / (s cm2)',
'erg / (s cm2 um)', 'erg / (um s cm2)',
'erg / (s cm2 Angstrom)', 'erg / (Angstrom s cm2)',
'erg / (s cm2 Hz)', 'erg / (Hz s cm2)',
'ph / (s cm2 um)', 'ph / (um s cm2)',
'ph / (s cm2 Angstrom)', 'ph / (Angstrom s cm2)',
'ph / (s cm2 Hz)', 'ph / (Hz s cm2)'
])
Expand Down
3 changes: 2 additions & 1 deletion jdaviz/configs/default/plugins/line_lists/line_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ def _on_viewer_data_changed(self, msg=None):
self._on_spectrum_viewer_limits_changed() # will also trigger _auto_slider_step

# set the choices (and default) for the units for new custom lines
self.custom_unit_choices = create_spectral_equivalencies_list(viewer_data)
self.custom_unit_choices = create_spectral_equivalencies_list(
viewer_data.spectral_axis.unit)
self.custom_unit = str(viewer_data.spectral_axis.unit)

def _parse_redshift_msg(self, msg):
Expand Down
50 changes: 27 additions & 23 deletions jdaviz/configs/imviz/plugins/coords_info/coords_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,10 @@ def _image_viewer_update(self, viewer, x, y):

def _spectrum_viewer_update(self, viewer, x, y):
def _cursor_fallback():
statistic = getattr(viewer.state, 'function', None)
cache_key = (viewer.state.layers[0].layer.label, statistic)
sp = self.app._get_object_cache.get(cache_key, viewer.data()[0])
self._dict['axes_x'] = x
self._dict['axes_x:unit'] = sp.spectral_axis.unit.to_string()
self._dict['axes_x:unit'] = viewer.state.x_display_unit
self._dict['axes_y'] = y
self._dict['axes_y:unit'] = sp.flux.unit.to_string()
self._dict['axes_y:unit'] = viewer.state.y_display_unit
self._dict['data_label'] = ''

def _copy_axes_to_spectral():
Expand All @@ -418,8 +415,6 @@ def _copy_axes_to_spectral():
self.row3_text = ''
self.icon = 'mdi-cursor-default'
self.marks[viewer._reference_id].visible = False
# get the units from the first layer
# TODO: replace with display units once implemented
_cursor_fallback()
_copy_axes_to_spectral()
return
Expand Down Expand Up @@ -462,17 +457,22 @@ def _copy_axes_to_spectral():
subset_to_apply=subset_label)
self.app._get_object_cache[cache_key] = sp

# Calculations have to happen in the frame of viewer display units.
disp_wave = sp.spectral_axis.to_value(viewer.state.x_display_unit, u.spectral())
disp_flux = sp.flux.to_value(viewer.state.y_display_unit,
u.spectral_density(sp.spectral_axis))

# Out of range in spectral axis.
if (self.dataset.selected != lyr.layer.label and
(x < sp.spectral_axis.value.min() or x > sp.spectral_axis.value.max())):
(x < disp_wave.min() or x > disp_wave.max())):
continue

cur_i = np.argmin(abs(sp.spectral_axis.value - x))
cur_wave = sp.spectral_axis[cur_i]
cur_flux = sp.flux[cur_i]
cur_i = np.argmin(abs(disp_wave - x))
cur_wave = disp_wave[cur_i]
cur_flux = disp_flux[cur_i]

dx = cur_wave.value - x
dy = cur_flux.value - y
dx = cur_wave - x
dy = cur_flux - y
cur_distance = math.sqrt(dx * dx + dy * dy)
if (closest_distance is None) or (cur_distance < closest_distance):
closest_distance = cur_distance
Expand All @@ -497,30 +497,34 @@ def _copy_axes_to_spectral():
return

self.row2_title = 'Wave'
self.row2_text = f'{closest_wave.value:10.5e} {closest_wave.unit.to_string()}'
self._dict['axes_x'] = closest_wave.value
self._dict['axes_x:unit'] = closest_wave.unit.to_string()
if closest_wave.unit != u.pix:
self.row2_text = f'{closest_wave:10.5e} {viewer.state.x_display_unit}'
self._dict['axes_x'] = closest_wave
self._dict['axes_x:unit'] = viewer.state.x_display_unit
if viewer.state.x_display_unit != u.pix:
self.row2_text += f' ({int(closest_i)} pix)'
if self.app.config == 'cubeviz':
# float to be compatible with nan
self._dict['slice'] = float(closest_i)
self._dict['spectral_axis'] = closest_wave.value
self._dict['spectral_axis:unit'] = closest_wave.unit.to_string()
self._dict['spectral_axis'] = closest_wave
self._dict['spectral_axis:unit'] = viewer.state.x_display_unit
else:
# float to be compatible with nan
self._dict['index'] = float(closest_i)

if viewer.state.y_display_unit is None:
flux_unit = ""
else:
flux_unit = viewer.state.y_display_unit
self.row3_title = 'Flux'
self.row3_text = f'{closest_flux.value:10.5e} {closest_flux.unit.to_string()}'
self._dict['axes_y'] = closest_flux.value
self._dict['axes_y:unit'] = closest_flux.unit.to_string()
self.row3_text = f'{closest_flux:10.5e} {flux_unit}'
self._dict['axes_y'] = closest_flux
self._dict['axes_y:unit'] = viewer.state.y_display_unit

if closest_icon is not None:
self.icon = closest_icon
else:
self.icon = ""

self.marks[viewer._reference_id].update_xy([closest_wave.value], [closest_flux.value]) # noqa
self.marks[viewer._reference_id].update_xy([closest_wave], [closest_flux])
self.marks[viewer._reference_id].visible = True
_copy_axes_to_spectral()
Loading

0 comments on commit f64890b

Please sign in to comment.