Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unit conversion from flux to surface brightness #2888

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading