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 reading of FITS files with SkyCoord columns #2488

Merged
merged 2 commits into from
May 8, 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
15 changes: 11 additions & 4 deletions glue/core/data_factories/fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import warnings
from os.path import basename
from collections import OrderedDict

from astropy.coordinates import SkyCoord
from glue.core.coordinates import coordinates_from_header, WCSCoordinates
from glue.core.data import Component, Data
from glue.config import data_factory, cli_parser
Expand Down Expand Up @@ -151,9 +151,16 @@ def new_data(suffix=True):
if column.ndim != 1:
warnings.warn("Dropping column '{0}' since it is not 1-dimensional".format(column_name))
continue
component = Component.autotyped(column, units=column.unit)
data.add_component(component=component,
label=column_name)
if isinstance(column, SkyCoord):
for attribute_name in column.get_representation_component_names():
values = getattr(column, attribute_name)
component = Component.autotyped(values, units=values.unit)
data.add_component(component=component,
label=f"{column_name}.{attribute_name}")
else:
component = Component.autotyped(column, units=column.unit)
data.add_component(component=component,
label=column_name)

if close_hdulist:
hdulist.close()
Expand Down
32 changes: 32 additions & 0 deletions glue/core/data_factories/tests/test_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import numpy as np
from numpy.testing import assert_array_equal

from astropy.table import Table
from astropy.coordinates import SkyCoord
from astropy import units as u

from glue.core import data_factories as df

from glue.tests.helpers import requires_astropy, make_file
Expand Down Expand Up @@ -229,3 +233,31 @@ def test_coordinate_component_units():
assert d.get_component(wid[2]).units == 'deg'
assert wid[3].label == 'Right Ascension'
assert d.get_component(wid[3]).units == 'deg'


@requires_astropy
def test_mixin_columns(tmp_path):

t = Table()
t['coords'] = SkyCoord([1, 2, 3] * u.deg, [4, 5, 6] * u.deg, frame='galactic')
t['coords_with_distance'] = SkyCoord([1, 2, 3] * u.deg, [4, 5, 6] * u.deg, [7, 8, 9] * u.kpc, frame='fk5')

t.write(tmp_path / 'table.fits', format='fits')

d = fits_reader(tmp_path / 'table.fits')[0]

assert len(d.main_components) == 6

assert_array_equal(d['coords.l'], [1, 2, 3])
assert d.get_component('coords.l').units == 'deg'
assert_array_equal(d['coords.b'], [4, 5, 6])
assert d.get_component('coords.b').units == 'deg'
assert_array_equal(d['coords.distance'], [1, 1, 1])
assert d.get_component('coords.distance').units == ''

assert_array_equal(d['coords_with_distance.ra'], [1, 2, 3])
assert d.get_component('coords_with_distance.ra').units == 'deg'
assert_array_equal(d['coords_with_distance.dec'], [4, 5, 6])
assert d.get_component('coords_with_distance.dec').units == 'deg'
assert_array_equal(d['coords_with_distance.distance'], [7, 8, 9])
assert d.get_component('coords_with_distance.distance').units == 'kpc'
Loading