-
Notifications
You must be signed in to change notification settings - Fork 13
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
Spectrum1D translator update for NDCube version #36
Changes from all commits
010b9ab
2940e3d
1002ca8
cdd844d
7a01dbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
from astropy import units as u | ||
from astropy.wcs import WCSSUB_SPECTRAL | ||
from astropy.nddata import StdDevUncertainty, InverseVariance, VarianceUncertainty | ||
from gwcs import WCS as GWCS | ||
|
||
from glue_astronomy.spectral_coordinates import SpectralCoordinates | ||
|
||
|
@@ -21,26 +22,44 @@ | |
class Specutils1DHandler: | ||
|
||
def to_data(self, obj): | ||
coords = SpectralCoordinates(obj.spectral_axis) | ||
data = Data(coords=coords) | ||
data['flux'] = obj.flux | ||
data.get_component('flux').units = str(obj.unit) | ||
|
||
# Glue expects spectral axis first for cubes (opposite of specutils). | ||
# Swap the spectral axis to first here. to_object doesn't need this because | ||
# Spectrum1D does it automatically on initialization. | ||
if len(obj.flux.shape) == 3: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about cases where the data has 2 dimensions? (for example a 2D spectrum) |
||
data = Data(coords=obj.wcs.swapaxes(-1, 0)) | ||
data['flux'] = np.swapaxes(obj.flux, -1, 0) | ||
data.get_component('flux').units = str(obj.unit) | ||
else: | ||
# Don't use the dummy GWCS created by Spectrum1D initialized with spectral_axis | ||
if isinstance(obj.wcs, GWCS): | ||
data = Data(coords=SpectralCoordinates(obj.spectral_axis)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't really need this anymore, Data coords can be a GWCS as far as I know. |
||
else: | ||
data = Data(coords=obj.wcs) | ||
data['flux'] = obj.flux | ||
data.get_component('flux').units = str(obj.unit) | ||
|
||
# Include uncertainties if they exist | ||
if obj.uncertainty is not None: | ||
data['uncertainty'] = obj.uncertainty.quantity | ||
if len(obj.flux.shape) == 3: | ||
data['uncertainty'] = np.swapaxes(obj.uncertainty.quantity, -1, 0) | ||
else: | ||
data['uncertainty'] = obj.uncertainty.quantity | ||
data.get_component('uncertainty').units = str(obj.uncertainty.unit) | ||
data.meta.update({'uncertainty_type': obj.uncertainty.uncertainty_type}) | ||
|
||
# Include mask if it exists | ||
if obj.mask is not None: | ||
data['mask'] = obj.mask | ||
if len(obj.flux.shape) == 3: | ||
data['mask'] = np.swapaxes(obj.mask, -1, 0) | ||
else: | ||
data['mask'] = obj.mask | ||
|
||
data.meta.update(obj.meta) | ||
|
||
return data | ||
|
||
def to_object(self, data_or_subset, attribute=None, statistic='mean'): | ||
def to_object(self, data_or_subset, attribute=None, statistic=None): | ||
""" | ||
Convert a glue Data object to a Spectrum1D object. | ||
|
||
|
@@ -69,14 +88,16 @@ def to_object(self, data_or_subset, attribute=None, statistic='mean'): | |
# Find non-spectral axes | ||
axes = tuple(i for i in range(data.ndim) if i != spec_axis) | ||
|
||
kwargs = {'wcs': data.coords.sub([WCSSUB_SPECTRAL])} | ||
if statistic is not None: | ||
kwargs = {'wcs': data.coords.sub([WCSSUB_SPECTRAL])} | ||
else: | ||
kwargs = {'wcs': data.coords} | ||
|
||
elif isinstance(data.coords, SpectralCoordinates): | ||
|
||
kwargs = {'spectral_axis': data.coords.spectral_axis} | ||
|
||
else: | ||
|
||
raise TypeError('data.coords should be an instance of WCS ' | ||
'or SpectralCoordinates') | ||
|
||
|
@@ -112,7 +133,7 @@ def parse_attributes(attributes): | |
mask = ~mask | ||
|
||
# Collapse values and mask to profile | ||
if data.ndim > 1: | ||
if data.ndim > 1 and statistic is not None: | ||
# Get units and attach to value | ||
values = data.compute_statistic(statistic, attribute, axis=axes, | ||
subset_state=subset_state) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to the above comments it would be good to clarify here whether you mean for the WCS or Numpy array. The data array and WCS will have axes in opposite order - spectral cubes read with plain Astropy FITS and WCS or with SpectralCube are often stored in spectral,dec,ra for the Numpy data array and ra,dec,spectral for the WCS.
Glue can handle any axis order as long as x_att and y_att are set correctly but it would indeed be good to make sure SpectralCube and Spectrum1D return a similar glue data object.