Converting scalar SkyCoord to length-1 SkyCoord array
See original GitHub issueI have an use case (in photutils
) of putting SkyCoord
objects in a QTable
. However, there is an edge case where the SkyCoord
object could either be scalar (only 1 detected source) or contain an array of coordinates (many detected sources). The issue is that scalar objects (including SkyCoord
) cannot be added as a table column. So I tried np.atleast_1d(skycoord)
, but that returns a length-1 ndarray
whereas I want a length-1 SkyCoord
array (so the table column is always a SkyCoord object regardless of whether it contains 1 or many sources). So my workaround is a bit ugly: SkyCoord(np.atleast_1d(skycoord))
:
>>> import numpy as np
>>> from astropy.coordinates import SkyCoord
>>> sc = SkyCoord(10, 10, unit='deg') # but could also contain an array of coords
>>> sc1 = np.atleast_1d(sc)
>>> sc1 # not what I want
array([<SkyCoord (ICRS): (ra, dec) in deg
(10., 10.)>], dtype=object)
>>> sc2 = SkyCoord(np.atleast_1d(sc))
>>> sc2 # good!
<SkyCoord (ICRS): (ra, dec) in deg
[(10., 10.)]>
sc1
and sc2
produce different table columns:
>>> from astropy.table import QTable
>>> tbl = QTable()
>>> tbl['col1'] = sc1
>>> tbl['col2'] = sc2
>>> tbl
col1 col2
deg,deg
-------------------------------------------------- ---------
<SkyCoord (ICRS): (ra, dec) in deg
(10., 10.)> 10.0,10.0
I always want the column to be a SkyCoord
object (in the case of 1 or many objects).
I discussed this a bit offline with @eteq and he suggested adding a atleast_nd(dim)
method to SkyCoord
objects to turn a scalar SkyCoord
into a one-element n-dimensional SkyCoord
array (I like this suggestion).
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (11 by maintainers)
Top GitHub Comments
Oh, sure. But that part is unlikely to get resolved this week, so I removed the milestone.
Arrggh, that reminds me of #8610 - really would be lovely if
np.atleast_1d
just worked, and with__array_function__
, it can…