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 of limits from Fnu to Flam #2889

Merged
merged 4 commits into from
May 31, 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
20 changes: 14 additions & 6 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ def to_unit(self, data, cid, values, original_units, target_units):
eqv = []
else:
eqv = []

# If there are only two values, this is likely the limits being converted, so then
# in case we need to use the spectral density equivalency, we need to provide only
# to spectral axis values. If there is only one value
if not np.isscalar(values) and len(values) == 2:
pllim marked this conversation as resolved.
Show resolved Hide resolved
spectral_values = spec.spectral_axis[0]
else:
spectral_values = spec.spectral_axis

# Ensure a spectrum passed through Spectral Extraction plugin
if '_pixel_scale_factor' in spec.meta and len(values) != 2:

Expand All @@ -120,26 +129,25 @@ def to_unit(self, data, cid, values, original_units, target_units):
# 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):
(u.sr not in u.Unit(target_units).bases):
# Surface Brightness -> Flux
eqv = [(u.MJy / u.sr,
u.MJy,
lambda x: (x * np.array(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):
(u.sr in u.Unit(target_units).bases):
# Flux -> Surface Brightness
eqv = [(u.MJy,
u.MJy / u.sr,
lambda x: (x / np.array(spec.meta['_pixel_scale_factor'])),
lambda x: x)]
else:
eqv = u.spectral_density(spec.spectral_axis)
eqv = u.spectral_density(spectral_values)

elif len(values) == 2:
# Need this for setting the y-limits
spec_limits = [spec.spectral_axis[0].value, spec.spectral_axis[-1].value]
eqv = u.spectral_density(spec_limits * spec.spectral_axis.unit)
eqv = u.spectral_density(spectral_values)

if '_pixel_scale_factor' in spec.meta:
# get min and max scale factors, to use with min and max of spec for
Expand All @@ -162,7 +170,7 @@ def to_unit(self, data, cid, values, original_units, target_units):
lambda x: x)]

else:
eqv = u.spectral_density(spec.spectral_axis)
eqv = u.spectral_density(spectral_values)

else: # spectral axis
eqv = u.spectral() + u.pixel_scale(1*u.pix)
Expand Down
29 changes: 29 additions & 0 deletions jdaviz/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,32 @@ def test_to_unit(cubeviz_helper):
# will be a uniform array since not wavelength dependent
# so test first value in array
assert np.allclose(value[0], 4.800000041882413e-08)

# Change from Fnu to Flam (with values shape matching spectral axis)

values = np.ones(3001)
original_units = u.MJy
target_units = u.erg / u.cm**2 / u.s / u.AA

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

assert np.allclose(new_values,
(values * original_units)
.to_value(target_units,
equivalencies=u.spectral_density(cube.spectral_axis)))

# Change from Fnu to Flam (with a shape (2,) array of values indicating we
# are probably converting the limits)

values = [1, 2]
original_units = u.MJy
target_units = u.erg / u.cm**2 / u.s / u.AA

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

# In this case we do a regular spectral density conversion, but using the
# first value in the spectral axis for the equivalency
assert np.allclose(new_values,
pllim marked this conversation as resolved.
Show resolved Hide resolved
([1, 2] * original_units)
.to_value(target_units,
equivalencies=u.spectral_density(cube.spectral_axis[0])))
Loading