# Create a new coordinate class (for the Sagittarius stream)¶

This document describes in detail how to subclass and define a custom spherical coordinate frame, as discussed in Defining a New Frame and the docstring for BaseCoordinateFrame. In this example, we will define a coordinate system defined by the plane of orbit of the Sagittarius Dwarf Galaxy (hereafter Sgr; as defined in Majewski et al. 2003). The Sgr coordinate system is often referred to in terms of two angular coordinates, $$\Lambda,B$$.

To do this, wee need to define a subclass of BaseCoordinateFrame that knows the names and units of the coordinate system angles in each of the supported representations. In this case we support SphericalRepresentation with “Lambda” and “Beta”. Then we have to define the transformation from this coordinate system to some other built-in system. Here we will use Galactic coordinates, represented by the Galactic class.

## See Also¶

By: Adrian Price-Whelan, Erik Tollerud

License: BSD

Make print work the same in all versions of Python, set up numpy, matplotlib, and use a nicer set of plot parameters:

import numpy as np
import matplotlib.pyplot as plt
from astropy.visualization import astropy_mpl_style
plt.style.use(astropy_mpl_style)


Import the packages necessary for coordinates

from astropy.coordinates import frame_transform_graph
from astropy.coordinates.matrix_utilities import rotation_matrix, matrix_product, matrix_transpose
import astropy.coordinates as coord
import astropy.units as u


The first step is to create a new class, which we’ll call Sagittarius and make it a subclass of BaseCoordinateFrame:

class Sagittarius(coord.BaseCoordinateFrame):
"""
A Heliocentric spherical coordinate system defined by the orbit
of the Sagittarius dwarf galaxy, as described in
http://adsabs.harvard.edu/abs/2003ApJ...599.1082M
and further explained in
http://www.stsci.edu/~dlaw/Sgr/.

Parameters
----------
representation : BaseRepresentation or None
A representation object or None to have no data (or use the other keywords)
Lambda : Angle, optional, must be keyword
The longitude-like angle corresponding to Sagittarius' orbit.
Beta : Angle, optional, must be keyword
The latitude-like angle corresponding to Sagittarius' orbit.
distance : Quantity, optional, must be keyword
The Distance for this object along the line-of-sight.
pm_Lambda_cosBeta : :class:~astropy.units.Quantity, optional, must be keyword
The proper motion along the stream in Lambda (including the
cos(Beta) factor) for this object (pm_Beta must also be given).
pm_Beta : :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.

"""

default_representation = coord.SphericalRepresentation
default_differential = coord.SphericalCosLatDifferential

frame_specific_representation_info = {
coord.SphericalRepresentation: [
coord.RepresentationMapping('lon', 'Lambda'),
coord.RepresentationMapping('lat', 'Beta'),
coord.RepresentationMapping('distance', 'distance')]
}


Breaking this down line-by-line, we define the class as a subclass of BaseCoordinateFrame. Then we include a descriptive docstring. The final lines are class-level attributes that specify the default representation for the data, default differential for the velocity information, and mappings from the attribute names used by representation objects to the names that are to be used by the Sagittarius frame. In this case we override the names in the spherical representations but don’t do anything with other representations like cartesian or cylindrical.

Next we have to define the transformation from this coordinate system to some other built-in coordinate system; we will use Galactic coordinates. We can do this by defining functions that return transformation matrices, or by simply defining a function that accepts a coordinate and returns a new coordinate in the new system. Because the transformation to the Sagittarius coordinate system is just a spherical rotation from Galactic coordinates, we’ll just define a function that returns this matrix. We’ll start by constructing the transformation matrix using pre-deteremined Euler angles and the rotation_matrix helper function:

SGR_PHI = (180 + 3.75) * u.degree # Euler angles (from Law & Majewski 2010)
SGR_THETA = (90 - 13.46) * u.degree
SGR_PSI = (180 + 14.111534) * u.degree

# Generate the rotation matrix using the x-convention (see Goldstein)
D = rotation_matrix(SGR_PHI, "z")
C = rotation_matrix(SGR_THETA, "x")
B = rotation_matrix(SGR_PSI, "z")
A = np.diag([1.,1.,-1.])
SGR_MATRIX = matrix_product(A, B, C, D)


Since we already constructed the transformation (rotation) matrix above, and the inverse of a rotation matrix is just its transpose, the required transformation functions are very simple:

@frame_transform_graph.transform(coord.StaticMatrixTransform, coord.Galactic, Sagittarius)
def galactic_to_sgr():
""" Compute the transformation matrix from Galactic spherical to
heliocentric Sgr coordinates.
"""
return SGR_MATRIX


The decorator @frame_transform_graph.transform(coord.StaticMatrixTransform, coord.Galactic, Sagittarius) registers this function on the frame_transform_graph as a coordinate transformation. Inside the function, we simply return the previously defined rotation matrix.

We then register the inverse transformation by using the transpose of the rotation matrix (which is faster to compute than the inverse):

@frame_transform_graph.transform(coord.StaticMatrixTransform, Sagittarius, coord.Galactic)
def sgr_to_galactic():
""" Compute the transformation matrix from heliocentric Sgr coordinates to
spherical Galactic.
"""
return matrix_transpose(SGR_MATRIX)


Now that we’ve registered these transformations between Sagittarius and Galactic, we can transform between any coordinate system and Sagittarius (as long as the other system has a path to transform to Galactic). For example, to transform from ICRS coordinates to Sagittarius, we would do:

icrs = coord.ICRS(280.161732*u.degree, 11.91934*u.degree)
sgr = icrs.transform_to(Sagittarius)
print(sgr)


Out:

<Sagittarius Coordinate: (Lambda, Beta) in deg
(346.81830652, -39.28360407)>


Or, to transform from the Sagittarius frame to ICRS coordinates (in this case, a line along the Sagittarius x-y plane):

sgr = Sagittarius(Lambda=np.linspace(0, 2*np.pi, 128)*u.radian,
Beta=np.zeros(128)*u.radian)
icrs = sgr.transform_to(coord.ICRS)
print(icrs)


Out:

<ICRS Coordinate: (ra, dec) in deg
[(284.03876751, -29.00408353), (287.24685769, -29.44848352),
(290.48068369, -29.81535572), (293.7357366 , -30.1029631 ),
(297.00711066, -30.30991693), (300.28958688, -30.43520293),
(303.57772919, -30.47820084), (306.86598944, -30.43869669),
(310.14881715, -30.31688708), (313.42076929, -30.11337526),
(316.67661568, -29.82915917), (319.91143548, -29.46561215),
(323.12070147, -29.02445708), (326.30034928, -28.50773532),
(329.44683007, -27.9177717 ), (332.55714589, -27.257137  ),
(335.62886847, -26.52860943), (338.66014233, -25.73513624),
(341.64967439, -24.87979679), (344.59671212, -23.96576781),
(347.50101283, -22.99629167), (350.36280652, -21.97464811),
(353.18275454, -20.90412969), (355.96190618, -19.78802107),
(358.70165491, -18.62958199), (  1.40369557, -17.43203397),
(  4.06998374, -16.19855028), (  6.70269788, -14.93224899),
(  9.30420479, -13.63618882), ( 11.87702861, -12.31336727),
( 14.42382347, -10.96672102), ( 16.94734952,  -9.59912794),
( 19.45045241,  -8.21341071), ( 21.93604568,  -6.81234162),
( 24.40709589,  -5.39864845), ( 26.86661004,  -3.97502106),
( 29.31762493,  -2.54411871), ( 31.76319801,  -1.10857781),
( 34.20639942,   0.32898001), ( 36.65030466,   1.76593955),
( 39.09798768,   3.19968374), ( 41.55251374,   4.6275852 ),
( 44.01693189,   6.04699804), ( 46.49426651,   7.45524993),
( 48.98750752,   8.84963453), ( 51.4995989 ,  10.22740448),
( 54.03342512,  11.58576509), ( 56.59179508,  12.92186896),
( 59.17742314,  14.23281165), ( 61.79290712,  15.51562883),
( 64.44070278,  16.76729487), ( 67.12309478,  17.98472356),
( 69.84216409,  19.16477088), ( 72.59975183,  20.30424045),
( 75.39742013,  21.3998918 ), ( 78.23641033,  22.44845192),
( 81.11759966,  23.44663022), ( 84.04145735,  24.39113719),
( 87.00800203,  25.27870692), ( 90.01676196,  26.10612335),
( 93.06674057,  26.87025019), ( 96.15638947,  27.56806406),
( 99.28359159,  28.19669038), (102.44565666,  28.75344107),
(105.63933131,  29.23585315), (108.86082534,  29.64172698),
(112.105855  ,  29.96916281), (115.36970341,  30.21659414),
(118.64729687,  30.38281659), (121.93329519,  30.46701088),
(125.22219273,  30.46875885), (128.50842634,  30.38805179),
(131.78648572,  30.22529063), (135.05102157,  29.98127794),
(138.29694697,  29.6572022 ), (141.51952827,  29.2546151 ),
(144.71446203,  28.77540295), (147.87793614,  28.22175338),
(151.00667382,  27.59611901), (154.09796066,  26.90117914),
(157.14965528,  26.13980125), (160.16018547,  25.31500315),
(163.12853176,  24.42991703), (166.05420084,  23.48775622),
(168.93719133,  22.49178507), (171.77795423,  21.44529257),
(174.57735037,  20.35156967), (177.33660656,  19.21389046),
(180.05727218,  18.03549704), (182.74117737,  16.81958784),
(185.39039367,  15.56930924), (188.00719783,  14.28774998),
(190.59403895,  12.97793826), (193.15350938,  11.64284103),
(195.68831902,  10.28536518), (198.20127316,   8.90836046),
(200.69525342,   7.51462369), (203.17320154,   6.10690412),
(205.63810576,   4.6879097 ), (208.09298919,   3.26031403),
(210.54090002,   1.82676397), (212.984903  ,   0.38988751),
(215.42807182,  -1.04769799), (217.87348209,  -2.48337744),
(220.32420429,  -3.91452965), (222.7832966 ,  -5.338519  ),
(225.25379684,  -6.75268736), (227.73871349,  -8.15434631),
(230.24101506,  -9.54076983), (232.76361762, -10.90918763),
(235.30937003, -12.25677927), (237.88103647, -13.58066929),
(240.48127601, -14.87792359), (243.11261883, -16.14554723),
(245.777439  , -17.38048408), (248.47792364, -18.57961852),
(251.2160385 , -19.7397795 ), (253.9934903 , -20.85774736),
(256.81168612, -21.93026371), (259.67169071, -22.95404466),
(262.57418275, -23.92579758), (265.51941137, -24.84224172),
(268.50715471, -25.70013256), (271.53668252, -26.49628998),
(274.6067251 , -27.22762983), (277.71545113, -27.89119849),
(280.86045662, -28.48420985), (284.03876751, -29.00408353)]>


As an example, we’ll now plot the points in both coordinate systems:

fig, axes = plt.subplots(2, 1, figsize=(8, 10),
subplot_kw={'projection': 'aitoff'})

axes[0].set_title("Sagittarius")
axes[0].plot(sgr.Lambda.wrap_at(180*u.deg).radian, sgr.Beta.radian,
linestyle='none', marker='.')

axes[1].set_title("ICRS")
axes[1].plot(icrs.ra.wrap_at(180*u.deg).radian, icrs.dec.radian,
linestyle='none', marker='.')

plt.show()


This particular transformation is just a spherical rotation, which is a special case of an Affine transformation with no vector offset. The transformation of velocity components is therefore natively supported as well:

sgr = Sagittarius(Lambda=np.linspace(0, 2*np.pi, 128)*u.radian,
Beta=np.zeros(128)*u.radian,
pm_Lambda_cosBeta=np.random.uniform(-5, 5, 128)*u.mas/u.yr,
pm_Beta=np.zeros(128)*u.mas/u.yr)
icrs = sgr.transform_to(coord.ICRS)
print(icrs)

fig, axes = plt.subplots(3, 1, figsize=(8, 10), sharex=True)

axes[0].set_title("Sagittarius")
axes[0].plot(sgr.Lambda.degree,
sgr.pm_Lambda_cosBeta.value,
linestyle='none', marker='.')
axes[0].set_xlabel(r"$\Lambda$ [deg]")
axes[0].set_ylabel(r"$\mu_\Lambda \, \cos B$ [{0}]"
.format(sgr.pm_Lambda_cosBeta.unit.to_string('latex_inline')))

axes[1].set_title("ICRS")
axes[1].plot(icrs.ra.degree, icrs.pm_ra_cosdec.value,
linestyle='none', marker='.')
axes[1].set_ylabel(r"$\mu_\alpha \, \cos\delta$ [{0}]"
.format(icrs.pm_ra_cosdec.unit.to_string('latex_inline')))

axes[2].set_title("ICRS")
axes[2].plot(icrs.ra.degree, icrs.pm_dec.value,
linestyle='none', marker='.')
axes[2].set_xlabel("RA [deg]")
axes[2].set_ylabel(r"$\mu_\delta$ [{0}]"
.format(icrs.pm_dec.unit.to_string('latex_inline')))

plt.show()


Out:

<ICRS Coordinate: (ra, dec) in deg
[(284.03876751, -29.00408353), (287.24685769, -29.44848352),
(290.48068369, -29.81535572), (293.7357366 , -30.1029631 ),
(297.00711066, -30.30991693), (300.28958688, -30.43520293),
(303.57772919, -30.47820084), (306.86598944, -30.43869669),
(310.14881715, -30.31688708), (313.42076929, -30.11337526),
(316.67661568, -29.82915917), (319.91143548, -29.46561215),
(323.12070147, -29.02445708), (326.30034928, -28.50773532),
(329.44683007, -27.9177717 ), (332.55714589, -27.257137  ),
(335.62886847, -26.52860943), (338.66014233, -25.73513624),
(341.64967439, -24.87979679), (344.59671212, -23.96576781),
(347.50101283, -22.99629167), (350.36280652, -21.97464811),
(353.18275454, -20.90412969), (355.96190618, -19.78802107),
(358.70165491, -18.62958199), (  1.40369557, -17.43203397),
(  4.06998374, -16.19855028), (  6.70269788, -14.93224899),
(  9.30420479, -13.63618882), ( 11.87702861, -12.31336727),
( 14.42382347, -10.96672102), ( 16.94734952,  -9.59912794),
( 19.45045241,  -8.21341071), ( 21.93604568,  -6.81234162),
( 24.40709589,  -5.39864845), ( 26.86661004,  -3.97502106),
( 29.31762493,  -2.54411871), ( 31.76319801,  -1.10857781),
( 34.20639942,   0.32898001), ( 36.65030466,   1.76593955),
( 39.09798768,   3.19968374), ( 41.55251374,   4.6275852 ),
( 44.01693189,   6.04699804), ( 46.49426651,   7.45524993),
( 48.98750752,   8.84963453), ( 51.4995989 ,  10.22740448),
( 54.03342512,  11.58576509), ( 56.59179508,  12.92186896),
( 59.17742314,  14.23281165), ( 61.79290712,  15.51562883),
( 64.44070278,  16.76729487), ( 67.12309478,  17.98472356),
( 69.84216409,  19.16477088), ( 72.59975183,  20.30424045),
( 75.39742013,  21.3998918 ), ( 78.23641033,  22.44845192),
( 81.11759966,  23.44663022), ( 84.04145735,  24.39113719),
( 87.00800203,  25.27870692), ( 90.01676196,  26.10612335),
( 93.06674057,  26.87025019), ( 96.15638947,  27.56806406),
( 99.28359159,  28.19669038), (102.44565666,  28.75344107),
(105.63933131,  29.23585315), (108.86082534,  29.64172698),
(112.105855  ,  29.96916281), (115.36970341,  30.21659414),
(118.64729687,  30.38281659), (121.93329519,  30.46701088),
(125.22219273,  30.46875885), (128.50842634,  30.38805179),
(131.78648572,  30.22529063), (135.05102157,  29.98127794),
(138.29694697,  29.6572022 ), (141.51952827,  29.2546151 ),
(144.71446203,  28.77540295), (147.87793614,  28.22175338),
(151.00667382,  27.59611901), (154.09796066,  26.90117914),
(157.14965528,  26.13980125), (160.16018547,  25.31500315),
(163.12853176,  24.42991703), (166.05420084,  23.48775622),
(168.93719133,  22.49178507), (171.77795423,  21.44529257),
(174.57735037,  20.35156967), (177.33660656,  19.21389046),
(180.05727218,  18.03549704), (182.74117737,  16.81958784),
(185.39039367,  15.56930924), (188.00719783,  14.28774998),
(190.59403895,  12.97793826), (193.15350938,  11.64284103),
(195.68831902,  10.28536518), (198.20127316,   8.90836046),
(200.69525342,   7.51462369), (203.17320154,   6.10690412),
(205.63810576,   4.6879097 ), (208.09298919,   3.26031403),
(210.54090002,   1.82676397), (212.984903  ,   0.38988751),
(215.42807182,  -1.04769799), (217.87348209,  -2.48337744),
(220.32420429,  -3.91452965), (222.7832966 ,  -5.338519  ),
(225.25379684,  -6.75268736), (227.73871349,  -8.15434631),
(230.24101506,  -9.54076983), (232.76361762, -10.90918763),
(235.30937003, -12.25677927), (237.88103647, -13.58066929),
(240.48127601, -14.87792359), (243.11261883, -16.14554723),
(245.777439  , -17.38048408), (248.47792364, -18.57961852),
(251.2160385 , -19.7397795 ), (253.9934903 , -20.85774736),
(256.81168612, -21.93026371), (259.67169071, -22.95404466),
(262.57418275, -23.92579758), (265.51941137, -24.84224172),
(268.50715471, -25.70013256), (271.53668252, -26.49628998),
(274.6067251 , -27.22762983), (277.71545113, -27.89119849),
(280.86045662, -28.48420985), (284.03876751, -29.00408353)]
(pm_ra_cosdec, pm_dec) in mas / yr
[( 4.87483729, -8.42067061e-01), (-2.108039  ,  3.05039198e-01),
(-0.47931891,  5.57515318e-02), ( 1.52253264, -1.33435779e-01),
(-3.42488825,  2.01221774e-01), ( 3.67234473, -1.09146318e-01),
( 0.01155208, -7.12501157e-06), ( 1.63821269,  4.66712319e-02),
(-4.48626871, -2.58073899e-01), ( 1.51863209,  1.31241225e-01),
( 1.44690921,  1.66545901e-01), (-0.52024562, -7.46587985e-02),
(-4.21800053, -7.23631204e-01), (-3.80859729, -7.58631026e-01),
(-3.31937239, -7.51283032e-01), (-0.05591907, -1.41432056e-02),
( 4.69623357,  1.30974993e+00), ( 1.49249791,  4.53991212e-01),
(-0.01249348, -4.10693314e-03), ( 3.34022185,  1.17731404e+00),
(-0.65382184, -2.45407382e-01), (-4.3714728 , -1.73679243e+00),
( 0.69217281,  2.89527385e-01), ( 4.10101837,  1.79727287e+00),
( 3.17001412,  1.44914207e+00), (-3.34388253, -1.58805015e+00),
( 2.22428902,  1.09328557e+00), (-1.94726056, -9.87120563e-01),
(-2.61194525, -1.36106836e+00), ( 0.35459201,  1.89346630e-01),
(-1.00419713, -5.47858277e-01), ( 0.91058676,  5.06119905e-01),
( 0.21086228,  1.19075200e-01), (-2.78987867, -1.59640429e+00),
(-0.27264979, -1.57678627e-01), ( 3.74018904,  2.18056946e+00),
(-0.97324327, -5.70588193e-01), (-1.64016029, -9.64586108e-01),
( 2.16306219,  1.27295209e+00), (-3.13017196, -1.83880645e+00),
(-0.03879761, -2.26949982e-02), ( 3.06717614,  1.78215225e+00),
(-2.79414236, -1.60858650e+00), ( 1.83248712,  1.04259390e+00),
(-0.39167002, -2.19651887e-01), (-0.41297372, -2.27670124e-01),
( 0.03862453,  2.08739025e-02), (-3.08061622, -1.62732189e+00),
(-0.95883004, -4.93574224e-01), (-3.0660292 , -1.53310364e+00),
(-1.25768675, -6.08804975e-01), (-1.61588616, -7.54490988e-01),
(-1.94158132, -8.71055255e-01), (-0.57336434, -2.46113767e-01),
(-2.82887307, -1.15646498e+00), ( 0.83168525,  3.22169845e-01),
(-2.71102394, -9.89480937e-01), (-0.87077839, -2.97549619e-01),
( 0.23535751,  7.47472499e-02), ( 1.087297  ,  3.18249609e-01),
( 2.9348568 ,  7.83869104e-01), ( 4.72953835,  1.13900151e+00),
(-1.7538578 , -3.75282921e-01), (-0.16867746, -3.14753032e-02),
(-3.10015485, -4.92207622e-01), (-1.12647919, -1.47060040e-01),
( 1.02265377,  1.04319456e-01), ( 4.81351266,  3.52441234e-01),
( 4.89480744,  2.16597747e-01), ( 0.02467226,  3.74367079e-04),
( 3.02163207, -4.21228705e-02), ( 3.7355668 , -1.60705443e-01),
( 4.7903066 , -3.44878185e-01), (-1.47866624,  1.49039914e-01),
( 2.964977  , -3.83505795e-01), (-3.4823747 ,  5.48754562e-01),
( 1.67022649, -3.09710454e-01), ( 2.95698595, -6.29323744e-01),
(-0.55033398,  1.31915681e-01), ( 2.92487119, -7.77985313e-01),
( 4.77438051, -1.39233893e+00), (-2.60615047,  8.24979590e-01),
( 1.54688396, -5.27023750e-01), ( 3.94353716, -1.43550954e+00),
( 2.98265376, -1.15261789e+00), (-2.34832504,  9.57927293e-01),
(-1.65677202,  7.09761327e-01), ( 1.9313066 , -8.64901838e-01),
(-4.50627108,  2.10068360e+00), ( 3.25005656, -1.57096227e+00),
(-2.93705226,  1.46669745e+00), (-3.92827504,  2.01979371e+00),
(-1.2914167 ,  6.81481321e-01), ( 1.9208135 , -1.03712723e+00),
(-0.92922799,  5.11875854e-01), (-3.78172803,  2.11940572e+00),
( 2.65435498, -1.50935486e+00), ( 3.009026  , -1.73152020e+00),
(-3.69470321,  2.14604097e+00), ( 3.48102458, -2.03577859e+00),
( 0.13754569, -8.07901534e-02), ( 2.3544171 , -1.38552765e+00),
( 3.37169218, -1.98306258e+00), (-0.01377665,  8.07837768e-03),
( 2.24647082, -1.31009192e+00), (-0.27318334,  1.58049353e-01),
( 0.56879233, -3.25633626e-01), (-2.27474127,  1.28534763e+00),
( 3.28768511, -1.82868167e+00), (-2.05411145,  1.12160631e+00),
( 1.75760769, -9.39444749e-01), ( 3.71471477, -1.93784030e+00),
( 1.67817458, -8.51763192e-01), ( 3.98392589, -1.96088328e+00),
(-3.59736815,  1.71105070e+00), ( 0.15192671, -6.95697450e-02),
( 0.40769324, -1.79006997e-01), (-3.07188031,  1.28759263e+00),
( 3.09813984, -1.23371218e+00), ( 0.10378312, -3.90527768e-02),
(-3.13434378,  1.10784293e+00), (-2.74338785,  9.04628500e-01),
( 2.13982024, -6.53153345e-01), ( 1.61089456, -4.51017179e-01),
( 1.2703579 , -3.22716663e-01), ( 0.98577045, -2.24234227e-01),
(-1.65293337,  3.31164206e-01), (-1.97324132,  3.40852713e-01)]>


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

Gallery generated by Sphinx-Gallery