Skip to content

Commit

Permalink
Fix unit conversion from flux to surface brightness
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofrog committed May 22, 2024
1 parent e1e0a80 commit 46addab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
52 changes: 21 additions & 31 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,37 +111,27 @@ def to_unit(self, data, cid, values, original_units, target_units):
else:
# Ensure a spectrum passed through Spectral Extraction plugin
if '_pixel_scale_factor' in spec.meta:
# if spectrum data collection item is in Surface Brightness units
if u.sr in spec.unit.bases:
# Data item in data collection does not update from conversion/translation.
# App wide orginal data units are used for conversion, orginal_units and
# target_units dicate the conversion to take place.
if (u.sr in u.Unit(original_units).bases) and \
(u.sr not in u.Unit(target_units).bases):
# Surface Brightness -> Flux
eqv = [(u.MJy / u.sr,
u.MJy,
lambda x: (x * spec.meta['_pixel_scale_factor']),
lambda x: x)]
else:
# Flux -> Surface Brightness
eqv = u.spectral_density(spec.spectral_axis)

# if spectrum data collection item is in Flux units
elif u.sr not in spec.unit.bases:
# Data item in data collection does not update from conversion/translation.
# App wide orginal data units are used for conversion, orginal_units and
# target_units dicate the conversion to take place.
if (u.sr not in u.Unit(original_units).bases) and \
(u.sr in u.Unit(target_units).bases):
# Flux -> Surface Brightness
eqv = [(u.MJy,
u.MJy / u.sr,
lambda x: (x / spec.meta['_pixel_scale_factor']),
lambda x: x)]
else:
# Surface Brightness -> Flux
eqv = u.spectral_density(spec.spectral_axis)

# Data item in data collection does not update from conversion/translation.
# App wide original data units are used for conversion, original and
# target_units dictate the conversion to take place.

if (u.sr in u.Unit(original_units).bases) and \
(u.sr not in u.Unit(target_units).bases):
# Surface Brightness -> Flux
eqv = [(u.MJy / u.sr,
u.MJy,
lambda x: (x * spec.meta['_pixel_scale_factor']),
lambda x: x)]
elif (u.sr not in u.Unit(original_units).bases) and \
(u.sr in u.Unit(target_units).bases):
# Flux -> Surface Brightness
eqv = [(u.MJy,
u.MJy / u.sr,
lambda x: (x / spec.meta['_pixel_scale_factor']),
lambda x: x)]
else:
eqv = u.spectral_density(spec.spectral_axis)

elif len(values) == 2:
# Need this for setting the y-limits
Expand Down
7 changes: 7 additions & 0 deletions jdaviz/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,19 @@ def test_to_unit(cubeviz_helper):
cid = cubeviz_helper.app.data_collection[0].data.find_component_id('flux')
data = cubeviz_helper.app.data_collection[-1].data
values = 1

# Surface brightness to flux

original_units = u.MJy / u.sr
target_units = u.MJy

value = uc.to_unit(cubeviz_helper, data, cid, values, original_units, target_units)

assert np.allclose(value, 4.7945742429049767e-11)

# Flux to surface brightness

original_units = u.MJy
target_units = u.MJy / u.sr

value = uc.to_unit(cubeviz_helper, data, cid, values, original_units, target_units)

0 comments on commit 46addab

Please sign in to comment.