Skip to content

Instantly share code, notes, and snippets.

@smoh
Created October 17, 2018 10:44
Show Gist options
  • Save smoh/640c35352a477dfd5e930f6e0df2c7a2 to your computer and use it in GitHub Desktop.
Save smoh/640c35352a477dfd5e930f6e0df2c7a2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import astropy
astropy.__version__
'3.0.4'
import numpy as np
import astropy.units as u
import astropy.coordinates as coord
print(coord.ICRS.__doc__)
    A coordinate or frame in the ICRS system.

    If you're looking for "J2000" coordinates, and aren't sure if you want to
    use this or `~astropy.coordinates.FK5`, you probably want to use ICRS. It's
    more well-defined as a catalog coordinate and is an inertial system, and is
    very close (within tens of milliarcseconds) to J2000 equatorial.

    For more background on the ICRS and related coordinate transformations, see the
    references provided in the  :ref:`astropy-coordinates-seealso` section of the
    documentation.
    
    Parameters
    ----------
    data : `BaseRepresentation` subclass instance
        A representation object or ``None`` to have no data (or use the
        coordinate component arguments, see below).
    
    ra : `Angle`, optional, must be keyword
        The RA for this object (``dec`` must also be given and ``representation``
        must be None).
    dec : `Angle`, optional, must be keyword
        The Declination for this object (``ra`` must also be given and
        ``representation`` must be None).
    distance : `~astropy.units.Quantity`, optional, must be keyword
        The Distance for this object along the line-of-sight.
        (``representation`` must be None).

    pm_ra_cosdec : :class:`~astropy.units.Quantity`, optional, must be keyword
        The proper motion in Right Ascension (including the ``cos(dec)`` factor)
        for this object (``pm_dec`` must also be given).
    pm_dec : :class:`~astropy.units.Quantity`, optional, must be keyword
        The proper motion in Declination for this object (``pm_ra_cosdec`` must
        also be given).
    radial_velocity : :class:`~astropy.units.Quantity`, optional, must be keyword
        The radial velocity of this object.

    representation_type : `BaseRepresentation` subclass, str, optional
        A representation class or string name of a representation class. This
        sets the expected input representation class, thereby changing the
        expected keyword arguments for the data passed in. For example, passing
        ``representation_type='cartesian'`` will make the classes expect
        position data with cartesian names, i.e. ``x, y, z`` in most cases.
    differential_type : `BaseDifferential` subclass, str, dict, optional
        A differential class or dictionary of differential classes (currently
        only a velocity differential with key 's' is supported). This sets the
        expected input differential class, thereby changing the expected keyword
        arguments of the data passed in. For example, passing
        ``differential_type='cartesian'`` will make the classes expect velocity
        data with the argument names ``v_x, v_y, v_z``.
    copy : bool, optional
        If `True` (default), make copies of the input coordinate arrays.
        Can only be passed in as a keyword argument.
# ICRS cartesian initialization works as in docstring
c = coord.ICRS(1,2,3, representation_type='cartesian')
c = coord.ICRS(x=1,y=2,z=3, representation_type='cartesian')
# Issue: But it looks like velocities cannot be unitless and need to be at least [1 /time]
# however the error message is confusing...
c = coord.ICRS(x=1,y=2,z=3, v_x=1, v_y=2, v_z=3, representation_type='cartesian', differential_type='cartesian')
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-5-367a4f8fe60e> in <module>
      1 # Issue: But it looks like velocities cannot be unitless and need to be at least [1 /time]
      2 # however the error message is confusing...
----> 3 c = coord.ICRS(x=1,y=2,z=3, v_x=1, v_y=2, v_z=3, representation_type='cartesian', differential_type='cartesian')


~/miniconda3/envs/research/lib/python3.7/site-packages/astropy/coordinates/baseframe.py in __init__(self, copy, representation_type, differential_type, *args, **kwargs)
    557         if differential_data:
    558             self._data = representation_data.with_differentials(
--> 559                 {'s': differential_data})
    560         else:
    561             self._data = representation_data  # possibly None.


~/miniconda3/envs/research/lib/python3.7/site-packages/astropy/coordinates/representation.py in with_differentials(self, differentials)
    732                                  copy=False)
    733         new_rep._differentials.update(
--> 734             new_rep._validate_differentials(differentials))
    735 
    736         return new_rep


~/miniconda3/envs/research/lib/python3.7/site-packages/astropy/coordinates/representation.py in _validate_differentials(self, differentials)
    548                     raise ValueError("For differential object '{0}', expected "
    549                                      "unit key = '{1}' but received key = '{2}'"
--> 550                                      .format(repr(diff), expected_key, key))
    551 
    552             # For now, we are very rigid: differentials must have the same shape


ValueError: For differential object '<CartesianDifferential (d_x, d_y, d_z) [dimensionless]
    (1., 2., 3.)>', expected unit key = '' but received key = 's'
# The same docstring appears for coords.Galactic
# because the docstring templating gets it from `basefrome.py` `base_doc`.
# However, coords.Galactic cartesian coordinates are actually defined with names (u,v,w,U,V,W).

print(coord.Galactic.__doc__)
import inspect
print(inspect.getsource(coord.Galactic))
    A coordinate or frame in the Galactic coordinate system.

    This frame is used in a variety of Galactic contexts because it has as its
    x-y plane the plane of the Milky Way.  The positive x direction (i.e., the
    l=0, b=0 direction) points to the center of the Milky Way and the z-axis
    points toward the North Galactic Pole (following the IAU's 1958 definition
    [1]_). However, unlike the `~astropy.coordinates.Galactocentric` frame, the
    *origin* of this frame in 3D space is the solar system barycenter, not
    the center of the Milky Way.
    
    Parameters
    ----------
    data : `BaseRepresentation` subclass instance
        A representation object or ``None`` to have no data (or use the
        coordinate component arguments, see below).
    
    l : `Angle`, optional, must be keyword
        The Galactic longitude for this object (``b`` must also be given and
        ``representation`` must be None).
    b : `Angle`, optional, must be keyword
        The Galactic latitude for this object (``l`` must also be given and
        ``representation`` must be None).
    distance : `~astropy.units.Quantity`, optional, must be keyword
        The Distance for this object along the line-of-sight.

    pm_l_cosb : :class:`~astropy.units.Quantity`, optional, must be keyword
        The proper motion in Galactic longitude (including the ``cos(b)`` term)
        for this object (``pm_b`` must also be given).
    pm_b : :class:`~astropy.units.Quantity`, optional, must be keyword
        The proper motion in Galactic latitude for this object (``pm_l_cosb``
        must also be given).
    radial_velocity : :class:`~astropy.units.Quantity`, optional, must be keyword
        The radial velocity of this object.

    representation_type : `BaseRepresentation` subclass, str, optional
        A representation class or string name of a representation class. This
        sets the expected input representation class, thereby changing the
        expected keyword arguments for the data passed in. For example, passing
        ``representation_type='cartesian'`` will make the classes expect
        position data with cartesian names, i.e. ``x, y, z`` in most cases.
    differential_type : `BaseDifferential` subclass, str, dict, optional
        A differential class or dictionary of differential classes (currently
        only a velocity differential with key 's' is supported). This sets the
        expected input differential class, thereby changing the expected keyword
        arguments of the data passed in. For example, passing
        ``differential_type='cartesian'`` will make the classes expect velocity
        data with the argument names ``v_x, v_y, v_z``.
    copy : bool, optional
        If `True` (default), make copies of the input coordinate arrays.
        Can only be passed in as a keyword argument.
    
    Notes
    -----
    .. [1] Blaauw, A.; Gum, C. S.; Pawsey, J. L.; Westerhout, G. (1960), "The
       new I.A.U. system of galactic coordinates (1958 revision),"
       `MNRAS, Vol 121, pp.123 <http://adsabs.harvard.edu/abs/1960MNRAS.121..123B>`_.


class Galactic(BaseCoordinateFrame):
    """
    A coordinate or frame in the Galactic coordinate system.

    This frame is used in a variety of Galactic contexts because it has as its
    x-y plane the plane of the Milky Way.  The positive x direction (i.e., the
    l=0, b=0 direction) points to the center of the Milky Way and the z-axis
    points toward the North Galactic Pole (following the IAU's 1958 definition
    [1]_). However, unlike the `~astropy.coordinates.Galactocentric` frame, the
    *origin* of this frame in 3D space is the solar system barycenter, not
    the center of the Milky Way.
    """

    frame_specific_representation_info = {
        r.SphericalRepresentation: [
            RepresentationMapping('lon', 'l'),
            RepresentationMapping('lat', 'b')
        ],
        r.CartesianRepresentation: [
            RepresentationMapping('x', 'u'),
            RepresentationMapping('y', 'v'),
            RepresentationMapping('z', 'w')
        ],
        r.CartesianDifferential: [
            RepresentationMapping('d_x', 'U', u.km/u.s),
            RepresentationMapping('d_y', 'V', u.km/u.s),
            RepresentationMapping('d_z', 'W', u.km/u.s)
        ]
    }

    default_representation = r.SphericalRepresentation
    default_differential = r.SphericalCosLatDifferential

    # North galactic pole and zeropoint of l in FK4/FK5 coordinates. Needed for
    # transformations to/from FK4/5

    # These are from the IAU's definition of galactic coordinates
    _ngp_B1950 = FK4NoETerms(ra=192.25*u.degree, dec=27.4*u.degree)
    _lon0_B1950 = Angle(123, u.degree)

    # These are *not* from Reid & Brunthaler 2004 - instead, they were
    # derived by doing:
    #
    # >>> FK4NoETerms(ra=192.25*u.degree, dec=27.4*u.degree).transform_to(FK5)
    #
    # This gives better consistency with other codes than using the values
    # from Reid & Brunthaler 2004 and the best self-consistency between FK5
    # -> Galactic and FK5 -> FK4 -> Galactic. The lon0 angle was found by
    # optimizing the self-consistency.
    _ngp_J2000 = FK5(ra=192.8594812065348*u.degree, dec=27.12825118085622*u.degree)
    _lon0_J2000 = Angle(122.9319185680026, u.degree)
# this is not an issue when representation_data are given as arguments
c = coord.Galactic(1,2,3, representation_type='cartesian')
# but raises error which can be cryptic given the docstring
# same problem for velocities
c = coord.Galactic(x=1,y=2,z=3, representation_type='cartesian')
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-8-20c6f4ecd20b> in <module>
      1 # but raises error which can be cryptic given the docstring
      2 # same problem for velocities
----> 3 c = coord.Galactic(x=1,y=2,z=3, representation_type='cartesian')


~/miniconda3/envs/research/lib/python3.7/site-packages/astropy/coordinates/baseframe.py in __init__(self, copy, representation_type, differential_type, *args, **kwargs)
    580             raise TypeError(
    581                 'Coordinate frame got unexpected keywords: {0}'.format(
--> 582                     list(kwargs)))
    583 
    584         # We do ``is None`` because self._data might evaluate to false for


TypeError: Coordinate frame got unexpected keywords: ['x', 'y', 'z']
# works with correct names
c = coord.Galactic(u=1,v=2,w=3, representation_type='cartesian')
c = coord.Galactic(u=1,v=2,w=3, U=1/u.yr, V=1/u.yr, W=1/u.yr, representation_type='cartesian', differential_type='cartesian')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment