diff --git a/jdaviz/app.py b/jdaviz/app.py index e65de0270e..8e54eb7ea7 100644 --- a/jdaviz/app.py +++ b/jdaviz/app.py @@ -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 diff --git a/jdaviz/tests/test_app.py b/jdaviz/tests/test_app.py index 9e923ab301..91566e6e70 100644 --- a/jdaviz/tests/test_app.py +++ b/jdaviz/tests/test_app.py @@ -224,13 +224,22 @@ 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 + + value = 1 original_units = u.MJy / u.sr target_units = u.MJy - value = uc.to_unit(cubeviz_helper, data, cid, values, original_units, target_units) + value = uc.to_unit(cubeviz_helper, data, cid, value, 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, value, original_units, target_units) + + assert np.allclose(value, 1)