
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "generated/examples/coordinates/rv-to-gsr.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_generated_examples_coordinates_rv-to-gsr.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_generated_examples_coordinates_rv-to-gsr.py:


================================================================
Convert a radial velocity to the Galactic Standard of Rest (GSR)
================================================================

Radial or line-of-sight velocities of sources are often reported in a
Heliocentric or Solar-system barycentric reference frame. A common
transformation incorporates the projection of the Sun's motion along the
line-of-sight to the target, hence transforming it to a Galactic rest frame
instead (sometimes referred to as the Galactic Standard of Rest, GSR). This
transformation depends on the assumptions about the orientation of the Galactic
frame relative to the bary- or Heliocentric frame. It also depends on the
assumed solar velocity vector. Here we'll demonstrate how to perform this
transformation using a sky position and barycentric radial-velocity.


*By: Adrian Price-Whelan*

*License: BSD*

.. GENERATED FROM PYTHON SOURCE LINES 25-26

Import the required Astropy packages:

.. GENERATED FROM PYTHON SOURCE LINES 26-30

.. code-block:: default


    import astropy.coordinates as coord
    import astropy.units as u








.. GENERATED FROM PYTHON SOURCE LINES 31-32

Use the latest convention for the Galactocentric coordinates

.. GENERATED FROM PYTHON SOURCE LINES 32-34

.. code-block:: default

    coord.galactocentric_frame_defaults.set('latest')





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none


    <ScienceState galactocentric_frame_defaults: {'galcen_coord': <ICRS Coordinate: (ra, dec) in deg...>



.. GENERATED FROM PYTHON SOURCE LINES 35-38

For this example, let's work with the coordinates and barycentric radial
velocity of the star HD 155967, as obtained from
`Simbad <https://simbad.u-strasbg.fr/simbad/>`_:

.. GENERATED FROM PYTHON SOURCE LINES 38-41

.. code-block:: default

    icrs = coord.SkyCoord(ra=258.58356362*u.deg, dec=14.55255619*u.deg,
                          radial_velocity=-16.1*u.km/u.s, frame='icrs')








.. GENERATED FROM PYTHON SOURCE LINES 42-48

We next need to decide on the velocity of the Sun in the assumed GSR frame.
We'll use the same velocity vector as used in the
`~astropy.coordinates.Galactocentric` frame, and convert it to a
`~astropy.coordinates.CartesianRepresentation` object using the
``.to_cartesian()`` method of the
`~astropy.coordinates.CartesianDifferential` object ``galcen_v_sun``:

.. GENERATED FROM PYTHON SOURCE LINES 48-50

.. code-block:: default

    v_sun = coord.Galactocentric().galcen_v_sun.to_cartesian()








.. GENERATED FROM PYTHON SOURCE LINES 51-54

We now need to get a unit vector in the assumed Galactic frame from the sky
position in the ICRS frame above. We'll use this unit vector to project the
solar velocity onto the line-of-sight:

.. GENERATED FROM PYTHON SOURCE LINES 54-58

.. code-block:: default

    gal = icrs.transform_to(coord.Galactic)
    cart_data = gal.data.to_cartesian()
    unit_vector = cart_data / cart_data.norm()








.. GENERATED FROM PYTHON SOURCE LINES 59-60

Now we project the solar velocity using this unit vector:

.. GENERATED FROM PYTHON SOURCE LINES 60-62

.. code-block:: default

    v_proj = v_sun.dot(unit_vector)








.. GENERATED FROM PYTHON SOURCE LINES 63-65

Finally, we add the projection of the solar velocity to the radial velocity
to get a GSR radial velocity:

.. GENERATED FROM PYTHON SOURCE LINES 65-69

.. code-block:: default

    rv_gsr = icrs.radial_velocity + v_proj
    print(rv_gsr)






.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    123.30460087379765 km / s




.. GENERATED FROM PYTHON SOURCE LINES 70-72

We could wrap this in a function so we can control the solar velocity and
re-use the above code:

.. GENERATED FROM PYTHON SOURCE LINES 72-108

.. code-block:: default

    def rv_to_gsr(c, v_sun=None):
        """Transform a barycentric radial velocity to the Galactic Standard of Rest
        (GSR).

        The input radial velocity must be passed in as a

        Parameters
        ----------
        c : `~astropy.coordinates.BaseCoordinateFrame` subclass instance
            The radial velocity, associated with a sky coordinates, to be
            transformed.
        v_sun : `~astropy.units.Quantity`, optional
            The 3D velocity of the solar system barycenter in the GSR frame.
            Defaults to the same solar motion as in the
            `~astropy.coordinates.Galactocentric` frame.

        Returns
        -------
        v_gsr : `~astropy.units.Quantity`
            The input radial velocity transformed to a GSR frame.

        """
        if v_sun is None:
            v_sun = coord.Galactocentric().galcen_v_sun.to_cartesian()

        gal = c.transform_to(coord.Galactic)
        cart_data = gal.data.to_cartesian()
        unit_vector = cart_data / cart_data.norm()

        v_proj = v_sun.dot(unit_vector)

        return c.radial_velocity + v_proj


    rv_gsr = rv_to_gsr(icrs)
    print(rv_gsr)




.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    123.30460087379765 km / s





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.013 seconds)


.. _sphx_glr_download_generated_examples_coordinates_rv-to-gsr.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: rv-to-gsr.py <rv-to-gsr.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: rv-to-gsr.ipynb <rv-to-gsr.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
