<commit_before>.. include:: references.txt

.. _astropy-coordinates:

*******************************************************
Astronomical Coordinate Systems (`astropy.coordinates`)
*******************************************************

Introduction
============

The `~astropy.coordinates` package provides classes for representing a
variety of celestial/spatial  coordinates, as well as tools for
converting between common coordinate systems in a uniform way.

.. note::

    If you have existing code that uses `~astropy.coordinates` functionality from
    Astropy version 0.3.x or earlier, please see the section on `Migrating from
    pre-v0.4 coordinates`_.  The interface has changed in ways that are not
    backward compatible in many circumstances.

Getting Started
===============

The simplest way to use `~astropy.coordinates` is to use the |skycoord|
class. |skycoord| objects are instantiated with a flexible and natural
approach that supports inputs provided in a number of convenient
formats.  The following ways of initializing a coordinate are all
equivalent::

    >>> from astropy import units as u
    >>> from astropy.coordinates import SkyCoord

    >>> c = SkyCoord(ra=10.625*u.degree, dec=41.2*u.degree, frame='icrs')
    >>> c = SkyCoord(10.625, 41.2, frame='icrs', unit='deg')
    >>> c = SkyCoord('00h42m30s', '+41d12m00s', frame='icrs')
    >>> c = SkyCoord('00h42.5m', '+41d12m', frame='icrs')
    >>> c = SkyCoord('00 42 30 +41 12 00', frame='icrs', unit=(u.hourangle, u.deg))
    >>> c = SkyCoord('00:42.5 +41:12', frame='icrs', unit=(u.hourangle, u.deg))
    >>> c
    <SkyCoord (ICRS): ra=10.625 deg, dec=41.2 deg>

The examples above illustrate a few simple rules to follow when creating a coordinate
object:

- Coordinate values can be provided either as unnamed positional arguments or
  via keyword arguments like ``ra``, ``dec``, ``l``, or ``b`` (depending on the frame).
- Coordinate frame value is optional and can be specified as a positional
  argument or via the ``frame`` keyword.
- Angle units must be specified, either in the values themselves
  (e.g. ``10.5*u.degree`` or ``'+41d12m00s'``) or via the ``unit`` keyword.

The individual components of equatorial coordinates are
`~astropy.coordinates.Longitude` or `~astropy.coordinates.Latitude`
objects, which are specialized versions of the general
`~astropy.coordinates.Angle` class.  The component values are accessed
using aptly named attributes::

    >>> c = SkyCoord(ra=10.68458*u.degree, dec=41.26917*u.degree)
    >>> c.ra  # doctest: +FLOAT_CMP
    <Longitude 10.68458 deg>
    >>> c.ra.hour  # doctest: +FLOAT_CMP
    0.7123053333333335
    >>> c.ra.hms  # doctest: +FLOAT_CMP
    hms_tuple(h=0.0, m=42.0, s=44.299200000000525)
    >>> c.dec  # doctest: +FLOAT_CMP
    <Latitude 41.26917 deg>
    >>> c.dec.degree  # doctest: +FLOAT_CMP
    41.26917
    >>> c.dec.radian  # doctest: +FLOAT_CMP
    0.7202828960652683

Coordinates can easily be converted to strings using the
:meth:`~astropy.coordinates.SkyCoord.to_string` method::

    >>> c = SkyCoord(ra=10.68458*u.degree, dec=41.26917*u.degree)
    >>> c.to_string('decimal')
    '10.6846 41.2692'
    >>> c.to_string('dms')
    '10d41m04.488s 41d16m09.012s'
    >>> c.to_string('hmsdms')
    '00h42m44.2992s +41d16m09.012s'

For more control over the string formatting, use the
`~astropy.coordinates.Angle.to_string` method of the individual
components::

    >>> c.ra.to_string(decimal=True)
    '10.6846'
    >>> c.dec.to_string(format='latex')
    '$41^\\circ16{}^\\prime09.012{}^{\\prime\\prime}$'
    >>> msg = 'My coordinates are: ra="{0}"" dec="{1}"'
    >>> msg.format(c.ra.to_string(sep=':'), c.dec.to_string(sep=':'))
    'My coordinates are: ra="10:41:04.488"" dec="41:16:09.012"'


Many of the above examples did not explicitly specify the coordinate frame.
This is fine if you do not need to transform to other frames or compare with
coordinates defined in a different frame.  However, to use the full power of
`~astropy.coordinates`, you should specify the reference frame your coordinates
are defined in::

    >>> c_icrs = SkyCoord(ra=10.68458*u.degree, dec=41.26917*u.degree, frame='icrs')

Once you've defined the frame of your coordinates, you can transform from that
frame to another frame.  You can do this a few different ways: if you just want
the default version of that frame, you can use attribute-style access::

    >>> from astropy.coordinates import FK5
    >>> c_icrs.galactic  # doctest: +FLOAT_CMP
    <SkyCoord (Galactic): l=121.174241811 deg, b=-21.5728855724 deg>

For more control, you can use the `~astropy.coordinates.SkyCoord.transform_to`
method, which accepts a frame name, frame class, or frame instance::

    >>> c_fk5 = c_icrs.transform_to('fk5')  # c_icrs.fk5 does the same thing
    >>> c_fk5  # doctest: +FLOAT_CMP
    <SkyCoord (FK5: equinox=J2000.000): ra=10.6845915393 deg, dec=41.2691714591 deg>
    >>> c_fk5.transform_to(FK5(equinox='J1975'))  # precess to a different equinox  # doctest: +FLOAT_CMP
    <SkyCoord (FK5: equinox=J1975.000): ra=10.3420913461 deg, dec=41.1323211229 deg>

This form of `~astropy.coordinates.SkyCoord.transform_to` also makes it
straightforward to convert from celestial coordinates to
`~astropy.coordinates.AltAz` coordinates, allowing the use of |skycoord|
as a tool for planning observations.  For a more complete example of
this, see :doc:`observing-example`.

|skycoord| and all other `~astropy.coordinates` objects also support
array coordinates.  These work the same as single-value coordinates, but
they store multiple coordinates in a single object.  When you're going
to apply the same operation to many different coordinates (say, from a
catalog), this is a better choice than a list of |skycoord| objects,
because it will be *much* faster than applying the operation to each
|skycoord| in a for loop.

::

    >>> SkyCoord(ra=[10, 11]*u.degree, dec=[41, -5]*u.degree)
    <SkyCoord (ICRS): (ra, dec) in deg
        [(10.0, 41.0), (11.0, -5.0)]>

So far we have been using a spherical coordinate representation in the all the
examples, and this is the default for the built-in frames.  Frequently it is
convenient to initialize or work with a coordinate using a different
representation such as cartesian or cylindrical.  This can be done by setting
the ``representation`` for either |SkyCoord| objects or low-level frame
coordinate objects::

    >>> c = SkyCoord(x=1, y=2, z=3, unit='kpc', frame='icrs', representation='cartesian')
    >>> c
    <SkyCoord (ICRS): x=1.0 kpc, y=2.0 kpc, z=3.0 kpc>
    >>> c.x, c.y, c.z
    (<Quantity 1.0 kpc>, <Quantity 2.0 kpc>, <Quantity 3.0 kpc>)

    >>> c.representation = 'cylindrical'
    >>> c  # doctest: +FLOAT_CMP
    <SkyCoord (ICRS): rho=2.2360679775 kpc, phi=63.4349488229 deg, z=3.0 kpc>
    >>> c.phi
    <Angle 63.434948... deg>
    >>> c.phi.to(u.radian)
    <Angle 1.107148... rad>

    >>> c.representation = 'spherical'
    >>> c  # doctest: +FLOAT_CMP
    <SkyCoord (ICRS): ra=63.4349488229 deg, dec=53.3007747995 deg, distance=3.74165738677 kpc>

    >>> c.representation = 'unitspherical'
    >>> c  # doctest: +FLOAT_CMP
    <SkyCoord (ICRS): ra=63.4349488229 deg, dec=53.3007747995 deg>

|skycoord| defines a number of convenience methods as well, like on-sky
separation between two coordinates and catalog matching (detailed in
:ref:`astropy-coordinates-matching`)::

    >>> c1 = SkyCoord(ra=10*u.degree, dec=9*u.degree, frame='icrs')
    >>> c2 = SkyCoord(ra=11*u.degree, dec=10*u.degree, frame='fk5')
    >>> c1.separation(c2)  # Differing frames handled correctly  # doctest: +FLOAT_CMP
    <Angle 1.4045335865905868 deg>


Distance from the origin (which is system-dependent, but often the Earth
center) can also be assigned to a |skycoord|. With two angles and a
distance, a unique point in 3D space is available, which also allows
conversion to the Cartesian representation of this location::<commit_msg>update index docs<commit_after>.. include:: references.txt

.. _astropy-coordinates:

*******************************************************
Astronomical Coordinate Systems (`astropy.coordinates`)
*******************************************************

Introduction
============

The `~astropy.coordinates` package provides classes for representing a
variety of celestial/spatial  coordinates, as well as tools for
converting between common coordinate systems in a uniform way.

.. note::

    If you have existing code that uses `~astropy.coordinates` functionality from
    Astropy version 0.3.x or earlier, please see the section on `Migrating from
    pre-v0.4 coordinates`_.  The interface has changed in ways that are not
    backward compatible in many circumstances.

Getting Started
===============

The simplest way to use `~astropy.coordinates` is to use the |skycoord|
class. |skycoord| objects are instantiated with a flexible and natural
approach that supports inputs provided in a number of convenient
formats.  The following ways of initializing a coordinate are all
equivalent::

    >>> from astropy import units as u
    >>> from astropy.coordinates import SkyCoord

    >>> c = SkyCoord(ra=10.625*u.degree, dec=41.2*u.degree, frame='icrs')
    >>> c = SkyCoord(10.625, 41.2, frame='icrs', unit='deg')
    >>> c = SkyCoord('00h42m30s', '+41d12m00s', frame='icrs')
    >>> c = SkyCoord('00h42.5m', '+41d12m', frame='icrs')
    >>> c = SkyCoord('00 42 30 +41 12 00', frame='icrs', unit=(u.hourangle, u.deg))
    >>> c = SkyCoord('00:42.5 +41:12', frame='icrs', unit=(u.hourangle, u.deg))
    >>> c
    <SkyCoord (ICRS): (ra, dec) in deg
        (10.625, 41.2)>

The examples above illustrate a few simple rules to follow when creating a coordinate
object:

- Coordinate values can be provided either as unnamed positional arguments or
  via keyword arguments like ``ra``, ``dec``, ``l``, or ``b`` (depending on the frame).
- Coordinate frame value is optional and can be specified as a positional
  argument or via the ``frame`` keyword.
- Angle units must be specified, either in the values themselves
  (e.g. ``10.5*u.degree`` or ``'+41d12m00s'``) or via the ``unit`` keyword.

The individual components of equatorial coordinates are
`~astropy.coordinates.Longitude` or `~astropy.coordinates.Latitude`
objects, which are specialized versions of the general
`~astropy.coordinates.Angle` class.  The component values are accessed
using aptly named attributes::

    >>> c = SkyCoord(ra=10.68458*u.degree, dec=41.26917*u.degree)
    >>> c.ra  # doctest: +FLOAT_CMP
    <Longitude 10.68458 deg>
    >>> c.ra.hour  # doctest: +FLOAT_CMP
    0.7123053333333335
    >>> c.ra.hms  # doctest: +FLOAT_CMP
    hms_tuple(h=0.0, m=42.0, s=44.299200000000525)
    >>> c.dec  # doctest: +FLOAT_CMP
    <Latitude 41.26917 deg>
    >>> c.dec.degree  # doctest: +FLOAT_CMP
    41.26917
    >>> c.dec.radian  # doctest: +FLOAT_CMP
    0.7202828960652683

Coordinates can easily be converted to strings using the
:meth:`~astropy.coordinates.SkyCoord.to_string` method::

    >>> c = SkyCoord(ra=10.68458*u.degree, dec=41.26917*u.degree)
    >>> c.to_string('decimal')
    '10.6846 41.2692'
    >>> c.to_string('dms')
    '10d41m04.488s 41d16m09.012s'
    >>> c.to_string('hmsdms')
    '00h42m44.2992s +41d16m09.012s'

For more control over the string formatting, use the
`~astropy.coordinates.Angle.to_string` method of the individual
components::

    >>> c.ra.to_string(decimal=True)
    '10.6846'
    >>> c.dec.to_string(format='latex')
    '$41^\\circ16{}^\\prime09.012{}^{\\prime\\prime}$'
    >>> msg = 'My coordinates are: ra="{0}"" dec="{1}"'
    >>> msg.format(c.ra.to_string(sep=':'), c.dec.to_string(sep=':'))
    'My coordinates are: ra="10:41:04.488"" dec="41:16:09.012"'


Many of the above examples did not explicitly specify the coordinate frame.
This is fine if you do not need to transform to other frames or compare with
coordinates defined in a different frame.  However, to use the full power of
`~astropy.coordinates`, you should specify the reference frame your coordinates
are defined in::

    >>> c_icrs = SkyCoord(ra=10.68458*u.degree, dec=41.26917*u.degree, frame='icrs')

Once you've defined the frame of your coordinates, you can transform from that
frame to another frame.  You can do this a few different ways: For more control,
you can use the `~astropy.coordinates.SkyCoord.transform_to` method, which
accepts a frame name, frame class, or frame instance::

    >>> from astropy.coordinates import FK5
    >>> c_icrs.galactic  # doctest: +FLOAT_CMP
    <SkyCoord (Galactic): (l, b) in deg
        (121.174241811, -21.5728855724)>
    >>> c_fk5 = c_icrs.transform_to('fk5')  # c_icrs.fk5 does the same thing
    >>> c_fk5  # doctest: +FLOAT_CMP
    <SkyCoord (FK5: equinox=J2000.000): (ra, dec) in deg
        (10.6845915393, 41.2691714591)>
    >>> c_fk5.transform_to(FK5(equinox='J1975'))  # precess to a different equinox  # doctest: +FLOAT_CMP
    <SkyCoord (FK5: equinox=J1975.000): (ra, dec) in deg
        (10.3420913461, 41.1323211229)>

This form of `~astropy.coordinates.SkyCoord.transform_to` also makes it
straightforward to convert from celestial coordinates to
`~astropy.coordinates.AltAz` coordinates, allowing the use of |skycoord|
as a tool for planning observations.  For a more complete example of
this, see :doc:`observing-example`.

|skycoord| and all other `~astropy.coordinates` objects also support
array coordinates.  These work the same as single-value coordinates, but
they store multiple coordinates in a single object.  When you're going
to apply the same operation to many different coordinates (say, from a
catalog), this is a better choice than a list of |skycoord| objects,
because it will be *much* faster than applying the operation to each
|skycoord| in a for loop.

::

    >>> SkyCoord(ra=[10, 11]*u.degree, dec=[41, -5]*u.degree)
    <SkyCoord (ICRS): (ra, dec) in deg
        [(10.0, 41.0), (11.0, -5.0)]>

So far we have been using a spherical coordinate representation in the all the
examples, and this is the default for the built-in frames.  Frequently it is
convenient to initialize or work with a coordinate using a different
representation such as cartesian or cylindrical.  This can be done by setting
the ``representation`` for either |SkyCoord| objects or low-level frame
coordinate objects::

    >>> c = SkyCoord(x=1, y=2, z=3, unit='kpc', frame='icrs', representation='cartesian')
    >>> c
    <SkyCoord (ICRS): (x, y, z) in kpc
        (1.0, 2.0, 3.0)>
    >>> c.x, c.y, c.z
    (<Quantity 1.0 kpc>, <Quantity 2.0 kpc>, <Quantity 3.0 kpc>)

    >>> c.representation = 'cylindrical'
    >>> c  # doctest: +FLOAT_CMP
    <SkyCoord (ICRS): (rho, phi, z) in (kpc, deg, kpc)
        (2.2360679775, 63.4349488229, 3.0)>
    >>> c.phi
    <Angle 63.434948... deg>
    >>> c.phi.to(u.radian)
    <Angle 1.107148... rad>

    >>> c.representation = 'spherical'
    >>> c  # doctest: +FLOAT_CMP
    <SkyCoord (ICRS): (ra, dec, distance) in (deg, deg, kpc)
        (63.4349488229, 53.3007747995, 3.74165738677)>

    >>> c.representation = 'unitspherical'
    >>> c  # doctest: +FLOAT_CMP
    <SkyCoord (ICRS): (ra, dec) in deg
        (63.4349488229, 53.3007747995)>

|skycoord| defines a number of convenience methods as well, like on-sky
separation between two coordinates and catalog matching (detailed in
:ref:`astropy-coordinates-matching`)::

    >>> c1 = SkyCoord(ra=10*u.degree, dec=9*u.degree, frame='icrs')
    >>> c2 = SkyCoord(ra=11*u.degree, dec=10*u.degree, frame='fk5')
    >>> c1.separation(c2)  # Differing frames handled correctly  # doctest: +FLOAT_CMP
    <Angle 1.4045335865905868 deg>


Distance from the origin (which is system-dependent, but often the Earth
center) can also be assigned to a |skycoord|. With two angles and a
distance, a unique point in 3D space is available, which also allows
conversion to the Cartesian representation of this location::
{'fraction_non_alphanumeric': 0.14118535296338242, 'fraction_numerical': 0.0684535044670945, 'mean_word_length': 4.07665282657298, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 307, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2613480', 'n_tokens_mistral': 5957, 'n_tokens_neox': 5168, 'n_words': 1724}