astropy: Inconsistent handling of 2D coordinates for transformations with an origin shift

Description

2D coordinates (i.e., no distance component for the spherical representation) are currently not treated consistently in our transformation framework. A 2D coordinate cannot formally be transformed between two coordinate frames that do not share the same origin without some assumption. However, some – but not all! – of our transformations make the assumption that 2D coordinates are infinitely far away. This inconsistency is confusing and may lead to surprising results.

I propose removing the assumption of infinite distance from these transformations. It’d be make sense to create a context manager to so that a user can choose to apply this assumption liberally, but I think it should be an active choice.

Expected behavior

Here’s an example for how a 2D coordinate cannot be transformed from ICRS (origin is the solar-system barycenter) to HCRS (origin is Sun center):

>>> import astropy.units as u
>>> from astropy.coordinates import SkyCoord
>>> coord = SkyCoord(0*u.deg, 0*u.deg, frame='icrs')
>>> coord.hcrs
...
UnitsError: The input ICRS coordinates do not have length units. This probably means you created coordinates with lat/lon but no distance.  Heliocentric<->ICRS transforms cannot function in this case because there is an origin shift.

How to Reproduce

However, some of our transformations (including ICRS<->AltAz, ICRS<->GCRS, ICRS<->CIRS) assume that the 2D coordinate points at something infinitely far away. Thus, it is possible to transform a 2D coordinate from ICRS (origin is solar-system barycenter) to GCRS (origin is Earth center), despite the wildly different origin:

>>> coord.gcrs
<SkyCoord (GCRS: obstime=J2000.000, obsgeoloc=(0., 0., 0.) m, obsgeovel=(0., 0., 0.) m / s): (ra, dec) in deg
    (359.99903994, -0.00041623)>

Furthermore, there exists a GCRS->HCRS transformation that also makes this assumption, so it is possible to transform a 2D coordinate along the path ICRS->GCRS->HCRS even though ICRS->HCRS errors:

>>> coord.hcrs
...
UnitsError: The input ICRS coordinates do not have length units. This probably means you created coordinates with lat/lon but no distance.  Heliocentric<->ICRS transforms cannot function in this case because there is an origin shift.

>>> coord.gcrs.hcrs
<SkyCoord (HCRS: obstime=J2000.000): (ra, dec) in deg
    (360., -4.11433227e-12)>

Versions

import astropy
try:
    astropy.system_info()
except AttributeError:
    import platform; print(platform.platform())
    import sys; print("Python", sys.version)
    import astropy; print("astropy", astropy.__version__)
    import numpy; print("Numpy", numpy.__version__)
    import erfa; print("pyerfa", erfa.__version__)
    try:
        import scipy
        print("Scipy", scipy.__version__)
    except ImportError:
        print("Scipy not installed")
    try:
        import matplotlib
        print("Matplotlib", matplotlib.__version__)
    except ImportError:
        print("Matplotlib not installed")
platform
--------
platform.platform() = 'Windows-11-10.0.26100-SP0'
platform.version() = '10.0.26100'
platform.python_version() = '3.12.5'

packages
--------
astropy              7.0.0.dev810+gb982985335.d20240911
numpy                2.1.1
scipy                1.14.1
matplotlib           3.9.2
pandas               2.2.2
pyerfa               2.0.1.4

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 15 (10 by maintainers)

Most upvoted comments

I’m fine with what @mhvk wrote above. There may be some sticking points to resolve once there’s an implementation to test out, but I don’t foresee any showstoppers.

@ayshih - yes, that all makes sense. It sounds to me like we should implement origin_mismatch with a default that is set via a ScienceState.

I like re-using origin_mismatch! It seems about right to get a warning on the attribute short-cut if the origin shifts but no distances are given. (In principle, we could still use a ScienceState to set the default, but likely this is overkill.)

what if we set a standard distance rather taking it as infinite?

Gosh, no, this is the opposite direction of what I would like! I do not want it to be possible to transform a 2D coordinate from ICRS to HCRS.