Skip to content

Commit

Permalink
move scale factor to collapsed_spec, combine sci notation and pixar_s…
Browse files Browse the repository at this point in the history
…r with scale factor
  • Loading branch information
gibsongreen committed Mar 19, 2024
1 parent 3c06508 commit 02363c5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.")

Check warning on line 540 in jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py#L540

Added line #L540 was not covered by tests
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 02363c5

Please sign in to comment.