question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Converting scalar SkyCoord to length-1 SkyCoord array

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
bsipoczcommented, Oct 24, 2019

Oh, sure. But that part is unlikely to get resolved this week, so I removed the milestone.

1reaction
mhvkcommented, Oct 24, 2019

Arrggh, that reminds me of #8610 - really would be lovely if np.atleast_1d just worked, and with __array_function__, it can…

Read more comments on GitHub >

github_iconTop Results From Across the Web

SkyCoord — Astropy v5.2
Inputs may be scalars or lists/tuples/arrays, yielding scalar or array coordinates (can ... Convert this coordinate to pixel coordinates using a WCS object....
Read more >
Astropy Documentation - Read the Docs
from astropy.coordinates import SkyCoord ... to the difference between a scalar 1 (length 0) and an array like np.array([1]) with length 1.
Read more >
galpy.orbit.Orbits — galpy v1.7.2 documentation
cannot be combined with Quantity lists (2 and 3 above) 5) lists of scalar ... of the fact that array of SkyCoords is...
Read more >
Using the SkyCoord High-level Class - Astropy Documentation
List or numpy array of such coordinate strings; List of (LON, LAT) tuples, where each LON and LAT are scalars (not arrays); N...
Read more >
python-astropy-4.0.2-bp153.1.10 - SUSE Package Hub
[#10696] * astropy.coordinates Ensure that for size-1 array SkyCoord and coordinate frames the attributes also properly become scalars when indexed with 0.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found