diff --git a/CHANGES.rst b/CHANGES.rst index 0cc07544ed..b0efc9ec08 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,7 +7,7 @@ New Features Cubeviz ^^^^^^^ -- Enable spectral unit conversion in cubeviz. [#2758] +- Enable spectral unit conversion in cubeviz. [#2758, #2803] Imviz ^^^^^ diff --git a/jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py b/jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py index 8ac8928506..ac98d7cbcc 100644 --- a/jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py +++ b/jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py @@ -126,6 +126,10 @@ def user_api(self): 'output_unit', 'reference_wavelength', 'add_results', 'calculate_moment')) + @property + def slice_display_unit_name(self): + return 'spectral' + @observe('is_active') def _is_active_changed(self, msg): for pos, mark in self.continuum_marks.items(): @@ -239,6 +243,12 @@ def calculate_moment(self, add_data=True): # slice out desired region # TODO: should we add a warning for a composite spectral subset? spec_min, spec_max = self.spectral_subset.selected_min_max(cube) + display_spectral_axis_unit = self.app._get_display_unit(self.slice_display_unit_name) + + # Convert units of min and max if necessary + if display_spectral_axis_unit and display_spectral_axis_unit != str(spec_min.unit): + spec_min = spec_min.to(display_spectral_axis_unit, equivalencies=u.spectral()) + spec_max = spec_max.to(display_spectral_axis_unit, equivalencies=u.spectral()) slab = manipulation.spectral_slab(cube, spec_min, spec_max) # Calculate the moment and convert to CCDData to add to the viewers diff --git a/jdaviz/configs/cubeviz/plugins/moment_maps/tests/test_moment_maps.py b/jdaviz/configs/cubeviz/plugins/moment_maps/tests/test_moment_maps.py index 62a7229f14..912c546d6d 100644 --- a/jdaviz/configs/cubeviz/plugins/moment_maps/tests/test_moment_maps.py +++ b/jdaviz/configs/cubeviz/plugins/moment_maps/tests/test_moment_maps.py @@ -7,7 +7,9 @@ from astropy.nddata import CCDData from astropy.wcs import WCS from astroquery.mast import Observations +import numpy as np from numpy.testing import assert_allclose +from glue.core.roi import XRangeROI def test_user_api(cubeviz_helper, spectrum1d_cube): @@ -188,6 +190,27 @@ def test_moment_velocity_calculation(cubeviz_helper, spectrum1d_cube): "204.9997755344 27.0001999998 (deg)") +def test_moment_frequency_unit_conversion(cubeviz_helper, spectrum1d_cube_larger): + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message="No observer defined on WCS.*") + cubeviz_helper.load_data(spectrum1d_cube_larger, data_label='test') + + uc = cubeviz_helper.plugins['Unit Conversion'] + mm = cubeviz_helper.plugins['Moment Maps'] + viewer = cubeviz_helper.app.get_viewer('spectrum-viewer') + viewer.apply_roi(XRangeROI(4.624e-07, 4.627e-07)) + + uc.spectral_unit = 'Hz' + mm.spectral_subset = 'Subset 1' + mm.continuum = 'Surrounding' + mm.n_moment = 1 + mm.output_unit = 'Spectral Unit' + moment_1_data = mm.calculate_moment() + + # Check to make sure there are no nans + assert len(np.where(moment_1_data.data > 0)[0]) == 8 + + def test_write_momentmap(cubeviz_helper, spectrum1d_cube, tmp_path): ''' Test writing a moment map out to a FITS file on disk '''