diff --git a/jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py b/jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py index c632bffeb5..4aa5b43a0f 100644 --- a/jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py +++ b/jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py @@ -206,11 +206,6 @@ def _update_mark_scale(self, *args): else: self.aperture.scale_factor = self.slice_wavelength/self.reference_wavelength - spectral_cube = self._app._jdaviz_helper._loaded_flux_cube - - if spectral_cube: - spectral_cube.meta['_pixel_scale_factor'] = self.aperture.scale_factor - if not self.bg_wavelength_dependent: self.background.scale_factor = 1.0 else: @@ -350,6 +345,11 @@ def collapse_to_spectrum(self, add_data=True, **kwargs): sender=self) self.hub.broadcast(snackbar_message) + # 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'] + collapsed_spec.meta['_pixel_scale_factor'] = pix_scale_factor + return collapsed_spec def get_aperture(self): @@ -527,35 +527,14 @@ def _live_update(self, event={}): mark.update_xy(sp.spectral_axis.value, sp.flux.value) mark.visible = True - def translate_units(self, spectral_cube, collapsed_spec): + def translate_units(self, collapsed_spec): + # MJy/sr -> Jy/pix if collapsed_spec._unit == u.MJy / u.sr: - # MJy to Jy - collapsed_spec._data *= 10e6 - - # spectral_cube.meta['_pixel_scale_factor'] / 1, where 1 = default scale factor - collapsed_spec._data *= spectral_cube.meta['_pixel_scale_factor'] - - # per https://jwst-docs.stsci.edu/jwst-near-infrared-camera/nircam-performance/nircam-absolute-flux-calibration-and-zeropoints # noqa - # MJy/sr is default, to measure photometry in MJy, convert the image to units - # of MJy/pixel by multiplying by the average area of a pixel in sr, a constant - # provided in the FITS header keyword PIXAR_SR - collapsed_spec._data *= spectral_cube.meta['PIXAR_SR'] - - # manually change units + 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: - # Jy to MJy - collapsed_spec._data /= 10e6 - - # 1 / spectral_cube.meta['_pixel_scale_factor'], where 1 = default scale factor - collapsed_spec._data /= spectral_cube.meta['_pixel_scale_factor'] - - # refer to above if-statement regarding meta['PIXAR_SR'] - collapsed_spec._data /= spectral_cube.meta['PIXAR_SR'] - - # manually change units + collapsed_spec._data /= collapsed_spec.meta['_pixel_scale_factor'] collapsed_spec._unit = u.MJy / u.sr - - elif collapsed_spec._unit != u.Jy / u.pix and collapsed_spec._unit != u.MJy / u.sr: + else: raise ValueError(f"Units '{collapsed_spec._unit}' are not MJy/sr nor Jy/pix.") 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 1b76e0b11e..9220740580 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 @@ -400,21 +400,18 @@ def test_unit_translation(cubeviz_helper): # collapse to spectrum, now we can get pixel scale factor collapsed_spec = extract_plg.collapse_to_spectrum() - # metadata needed, PIXAR_SR and _pixel_scale_factor - spectral_cube = cubeviz_helper.app._jdaviz_helper._loaded_flux_cube - - assert spectral_cube.meta['_pixel_scale_factor'] != 1 + assert collapsed_spec.meta['_pixel_scale_factor'] != 1 # store to test second time after calling translate_units mjy_sr_data1 = collapsed_spec._data[0] - extract_plg.translate_units(spectral_cube, collapsed_spec) + extract_plg.translate_units(collapsed_spec) 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) - extract_plg.translate_units(spectral_cube, collapsed_spec) + extract_plg.translate_units(collapsed_spec) # translating again returns the original units assert collapsed_spec._unit == u.Jy / u.pix