Skip to content

Commit

Permalink
Merge pull request #2488 from astrofrog/fix-skycoord-fits
Browse files Browse the repository at this point in the history
Fix reading of FITS files with SkyCoord columns
  • Loading branch information
astrofrog authored May 8, 2024
2 parents 21bee7b + e7ac491 commit 7a834e7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
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'

0 comments on commit 7a834e7

Please sign in to comment.