diff --git a/jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py b/jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py index 20beb89de9..c446847feb 100644 --- a/jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py +++ b/jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py @@ -347,7 +347,7 @@ def collapse_to_spectrum(self, add_data=True, **kwargs): # per https://jwst-docs.stsci.edu/jwst-near-infrared-camera/nircam-performance/nircam-absolute-flux-calibration-and-zeropoints # noqa if 'PIXAR_SR' in spectral_cube.meta: - pix_scale_factor = self.aperture.scale_factor * 10e6 * spectral_cube.meta['PIXAR_SR'] + pix_scale_factor = self.aperture.scale_factor * spectral_cube.meta['PIXAR_SR'] collapsed_spec.meta['_pixel_scale_factor'] = pix_scale_factor return collapsed_spec @@ -528,13 +528,24 @@ def _live_update(self, event={}): mark.visible = True def translate_units(self, collapsed_spec): - # MJy/sr -> Jy/pix - if collapsed_spec._unit == u.MJy / u.sr: + sb_to_flux = { + u.MJy / u.sr: u.MJy / u.pix, + u.Jy / u.sr: u.Jy / u.pix, + u.erg / (u.cm**2*u.s*u.AA*u.sr): u.erg / (u.cm**2*u.s*u.AA), + u.erg / (u.s*u.cm**2*u.sr): u.erg / (u.s*u.cm**2), + u.W / (u.m**2*u.Hz*u.sr): u.W / (u.m**2*u.Hz) + } + + flux_to_sb = {value: key for key, value in sb_to_flux.items()} + + # remove sr + if collapsed_spec._unit in sb_to_flux: collapsed_spec._data *= collapsed_spec.meta['_pixel_scale_factor'] - collapsed_spec._unit = u.Jy / u.pix - # Jy/pix -> MJy/sr - elif collapsed_spec._unit == u.Jy / u.pix: + collapsed_spec._unit = sb_to_flux[collapsed_spec._unit] + # add sr + elif collapsed_spec._unit in flux_to_sb: collapsed_spec._data /= collapsed_spec.meta['_pixel_scale_factor'] - collapsed_spec._unit = u.MJy / u.sr + collapsed_spec._unit = flux_to_sb[collapsed_spec._unit] else: - raise ValueError(f"Units '{collapsed_spec._unit}' are not MJy/sr nor Jy/pix.") + raise ValueError(f"Units '{collapsed_spec._unit}'" + f"are unrecognized surface brightness or flux units.") diff --git a/jdaviz/configs/cubeviz/plugins/spectral_extraction/tests/test_spectral_extraction.py b/jdaviz/configs/cubeviz/plugins/spectral_extraction/tests/test_spectral_extraction.py index 4b2cd9aa2e..c1b5be9538 100644 --- a/jdaviz/configs/cubeviz/plugins/spectral_extraction/tests/test_spectral_extraction.py +++ b/jdaviz/configs/cubeviz/plugins/spectral_extraction/tests/test_spectral_extraction.py @@ -382,7 +382,7 @@ def test_unit_translation(cubeviz_helper): w = WCS(wcs_dict) flux = np.zeros((30, 20, 3001), dtype=np.float32) flux[5:15, 1:11, :] = 1 - cube = Spectrum1D(flux=flux * u.Jy / u.pix, wcs=w, meta=wcs_dict) + cube = Spectrum1D(flux=flux * u.MJy / u.pix, wcs=w, meta=wcs_dict) cubeviz_helper.load_data(cube, data_label="test") center = PixCoord(5, 10) @@ -409,12 +409,12 @@ def test_unit_translation(cubeviz_helper): assert collapsed_spec._unit == u.MJy / u.sr # some value in MJy/sr that we know the outcome after translation - assert np.allclose(collapsed_spec._data[0], 8751.653) + assert np.allclose(collapsed_spec._data[0], 8.7516529e10) extract_plg._obj.translate_units(collapsed_spec) # translating again returns the original units - assert collapsed_spec._unit == u.Jy / u.pix + assert collapsed_spec._unit == u.MJy / u.pix # returns to the original values # which is a value in Jy/pix that we know the outcome after translation assert np.allclose(collapsed_spec._data[0], mjy_sr_data1)