Skip to content

Commit

Permalink
adjust from incorrect scalar scale factor to correct array implementa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
gibsongreen committed May 23, 2024
1 parent 0b3e17b commit 1c10f96
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
86 changes: 53 additions & 33 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from astropy.nddata import NDData
from astropy.io import fits
from astropy.time import Time
from astropy.units import Quantity
from echo import CallbackProperty, DictCallbackProperty, ListCallbackProperty
from ipygoldenlayout import GoldenLayout
from ipysplitpanes import SplitPanes
Expand Down Expand Up @@ -106,48 +107,67 @@ def to_unit(self, data, cid, values, original_units, target_units):
if cid.label == "flux":
try:
spec = data.get_object(cls=Spectrum1D)

except RuntimeError:
eqv = []
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)
eqv = []
# if spectrum data collection item is in Surface Brightness units
if u.sr in spec.unit.bases and len(values) != 2:
# 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 * np.array(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 and len(values) != 2:
# 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 / np.array(spec.meta['_pixel_scale_factor'])),
lambda x: x)]

else:
# Surface Brightness -> Flux
eqv = u.spectral_density(spec.spectral_axis)
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)

if '_pixel_scale_factor' in spec.meta:
# get min and max scale factors, to use with min and max of spec for
# y-limits. Make sure they are Quantities (can be numpy.float64).
pixel_scale_min = (Quantity(spec.meta['_pixel_scale_factor'][0])).value
pixel_scale_max = (Quantity(spec.meta['_pixel_scale_factor'][-1])).value
min_max = [pixel_scale_min, pixel_scale_max]

if (u.sr in u.Unit(original_units).bases) and \
(u.sr not in u.Unit(target_units).bases):
eqv += [(u.MJy,
u.MJy / u.sr,
lambda x: x * np.array(min_max),
lambda x: x)]
elif (u.sr not in u.Unit(original_units).bases) and \
(u.sr in u.Unit(target_units).bases):
eqv += [(u.MJy / u.sr,
u.MJy,
lambda x: x / np.array(min_max),
lambda x: x)]

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def test_unit_translation(cubeviz_helper):
collapsed_spec = extract_plg.extract()

# test that the scale factor was set
assert collapsed_spec.meta['_pixel_scale_factor'] != 1
assert np.all(collapsed_spec.meta['_pixel_scale_factor'] != 1)

# When the dropdown is displayed, this ensures the loaded
# data collection item units will be used for translations.
Expand Down

0 comments on commit 1c10f96

Please sign in to comment.