Skip to content

Commit

Permalink
remove hard-coded sci notation, add additional flux/sb units
Browse files Browse the repository at this point in the history
  • Loading branch information
gibsongreen committed Mar 22, 2024
1 parent 299308e commit b01aa90
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit b01aa90

Please sign in to comment.