astropy / astropy

Astronomy and astrophysics core library
https://www.astropy.org
BSD 3-Clause "New" or "Revised" License
4.39k stars 1.75k forks source link

Some coordinate frame transformations fail for zero-size arrays #17008

Open lpsinger opened 2 weeks ago

lpsinger commented 2 weeks ago

Description

Some coordinate conversions (for example, ICRS -> ITRS) fail unexpectedly for zero-length arrays.

Expected behavior

Transformation should result in a new SkyCoord of object of the desired frame and with zero length.

How to Reproduce

>>> from astropy.coordinates import SkyCoord
>>> from astropy.time import Time
>>> from astropy import units as u
>>> s = SkyCoord([] * u.deg, [] * u.deg, obstime=Time([], format='iso'))
>>> s.galactic
<SkyCoord (Galactic): (l, b) in deg
    []>
>>> s.itrs
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/lpsinger/Library/Python/3.11/lib/python/site-packages/astropy/coordinates/sky_coordinate.py", line 896, in __getattr__
    return self.transform_to(attr)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lpsinger/Library/Python/3.11/lib/python/site-packages/astropy/coordinates/sky_coordinate.py", line 704, in transform_to
    new_coord = trans(self.frame, generic_frame)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lpsinger/Library/Python/3.11/lib/python/site-packages/astropy/coordinates/transformations/composite.py", line 113, in __call__
    curr_coord = t(curr_coord, curr_toframe)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lpsinger/Library/Python/3.11/lib/python/site-packages/astropy/coordinates/transformations/function.py", line 175, in __call__
    return supcall(fromcoord, toframe)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lpsinger/Library/Python/3.11/lib/python/site-packages/astropy/coordinates/builtin_frames/icrs_cirs_transforms.py", line 34, in icrs_to_cirs
    astrom = erfa_astrom.get().apco(cirs_frame)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lpsinger/Library/Python/3.11/lib/python/site-packages/astropy/coordinates/erfa_astrom.py", line 54, in apco
    xp, yp = get_polar_motion(obstime)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lpsinger/Library/Python/3.11/lib/python/site-packages/astropy/coordinates/builtin_frames/utils.py", line 46, in get_polar_motion
    xp, yp, status = iers_table.pm_xy(time, return_status=True)
    ^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 3, got 0)

Versions

macOS-14.6.1-arm64-arm-64bit
Python 3.11.10 (main, Sep  7 2024, 05:47:53) [Clang 15.0.0 (clang-1500.1.0.2.5)]
astropy 6.1.3
Numpy 2.1.1
pyerfa 2.0.1.4
Scipy 1.14.1
Matplotlib 3.9.2
mhvk commented 1 week ago

Ai, another case where a "smart" short-circuit caused problems: we special-cased size 0 and it doesn't always return the right number of elements. Fix in #17013...