Note
Go to the end to download the full example code
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, we 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#
The gala package, which defines a number of Astropy coordinate frames for stellar stream coordinate systems.
Majewski et al. 2003, “A Two Micron All Sky Survey View of the Sagittarius Dwarf Galaxy. I. Morphology of the Sagittarius Core and Tidal Arms”, https://arxiv.org/abs/astro-ph/0304198
Law & Majewski 2010, “The Sagittarius Dwarf Galaxy: A Model for Evolution in a Triaxial Milky Way Halo”, https://arxiv.org/abs/1003.1132
David Law’s Sgr info page https://www.stsci.edu/~dlaw/Sgr/
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 matplotlib.pyplot as plt
import numpy as np
from astropy.visualization import astropy_mpl_style
plt.style.use(astropy_mpl_style)
Import the packages necessary for coordinates
import astropy.coordinates as coord
import astropy.units as u
from astropy.coordinates import frame_transform_graph
from astropy.coordinates.matrix_utilities import matrix_transpose, rotation_matrix
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
https://ui.adsabs.harvard.edu/abs/2003ApJ...599.1082M
and further explained in
https://www.stsci.edu/~dlaw/Sgr/.
Parameters
----------
representation : `~astropy.coordinates.BaseRepresentation` or None
A representation object or None to have no data (or use the other keywords)
Lambda : `~astropy.coordinates.Angle`, optional, must be keyword
The longitude-like angle corresponding to Sagittarius' orbit.
Beta : `~astropy.coordinates.Angle`, optional, must be keyword
The latitude-like angle corresponding to Sagittarius' orbit.
distance : `~astropy.units.Quantity`, optional, must be keyword
The Distance for this object along the line-of-sight.
pm_Lambda_cosBeta : `~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 : `~astropy.units.Quantity`, optional, must be keyword
The proper motion in Declination for this object (``pm_ra_cosdec`` must
also be given).
radial_velocity : `~astropy.units.Quantity`, optional, keyword-only
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-determined 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)
SGR_MATRIX = (
np.diag([1.,1.,-1.])
@ rotation_matrix(SGR_PSI, "z")
@ rotation_matrix(SGR_THETA, "x")
@ rotation_matrix(SGR_PHI, "z")
)
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 Galactic spherical to heliocentric Sgr transformation matrix."""
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 heliocentric Sgr to spherical Galactic transformation matrix."""
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.SkyCoord(280.161732*u.degree, 11.91934*u.degree, frame='icrs')
sgr = icrs.transform_to(Sagittarius)
print(sgr)
<SkyCoord (Sagittarius): (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 = coord.SkyCoord(Lambda=np.linspace(0, 2*np.pi, 128)*u.radian,
Beta=np.zeros(128)*u.radian, frame='sagittarius')
icrs = sgr.transform_to(coord.ICRS)
print(icrs)
<SkyCoord (ICRS): (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 = coord.SkyCoord(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,
frame='sagittarius')
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(
fr"$\mu_\Lambda \, \cos B$ [{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(
fr"$\mu_\alpha \, \cos\delta$ [{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(
fr"$\mu_\delta$ [{icrs.pm_dec.unit.to_string('latex_inline')}]")
plt.show()
<SkyCoord (ICRS): (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
[( 3.50308914, -6.05114757e-01), (-4.64010066, 6.71435672e-01),
( 4.70295104, -5.47019361e-01), (-3.5845402 , 3.14151501e-01),
(-2.77767763, 1.63196337e-01), (-4.92090208, 1.46254882e-01),
(-1.38650814, 8.55160589e-04), ( 4.72007526, 1.34470773e-01),
(-2.09290672, -1.20395061e-01), ( 4.84246155, 4.18488846e-01),
(-1.07849719, -1.24139985e-01), ( 2.60838807, 3.74321498e-01),
( 1.5865777 , 2.72189898e-01), ( 2.2333072 , 4.44850428e-01),
( 0.62419662, 1.41276204e-01), (-1.97117276, -4.98554429e-01),
(-1.56272236, -4.35833411e-01), ( 1.79822035, 5.46986519e-01),
(-0.5050693 , -1.66029515e-01), ( 1.55547542, 5.48251919e-01),
( 4.35798194, 1.63573755e+00), ( 0.35489681, 1.41001013e-01),
(-4.60346539, -1.92557304e+00), ( 4.26370057, 1.86856840e+00),
(-2.02399546, -9.25250438e-01), (-2.1037853 , -9.99113012e-01),
( 1.79881649, 8.84156730e-01), (-0.83890933, -4.25266484e-01),
(-0.22582383, -1.17675386e-01), ( 2.60402641, 1.39050969e+00),
(-0.93310127, -5.09070620e-01), ( 1.89400732, 1.05272210e+00),
( 3.72172792, 2.10168220e+00), ( 0.56837845, 3.25233425e-01),
( 3.63354306, 2.10134797e+00), ( 0.37031699, 2.15898689e-01),
(-0.07431715, -4.35702853e-02), ( 1.96571421, 1.15604592e+00),
( 2.92380043, 1.72064304e+00), (-0.14660052, -8.61198631e-02),
( 2.8604294 , 1.67323305e+00), ( 0.69069226, 4.01319882e-01),
(-2.70083721, -1.55487077e+00), (-2.55504718, -1.45369459e+00),
( 0.32170572, 1.80415306e-01), (-0.10097542, -5.56671864e-02),
( 1.86508715, 1.00795143e+00), ( 3.27410595, 1.72953199e+00),
(-0.30805591, -1.58577070e-01), (-4.02139125, -2.01081241e+00),
( 2.86670854, 1.38767974e+00), ( 3.3824743 , 1.57934788e+00),
( 3.84818377, 1.72641788e+00), (-2.05838191, -8.83550104e-01),
( 4.33826466, 1.77351582e+00), ( 4.55069302, 1.76280157e+00),
( 3.53519343, 1.29028979e+00), ( 2.21802835, 7.57912116e-01),
( 2.552017 , 8.10495697e-01), ( 2.21869916, 6.49408709e-01),
(-3.17134796, -8.47033385e-01), ( 1.76203899, 4.24346930e-01),
(-0.21541086, -4.60926853e-02), (-3.80597606, -7.10197126e-01),
( 2.92790696, 4.64860045e-01), (-0.55950249, -7.30421464e-02),
(-0.30874709, -3.14948512e-02), (-2.27950626, -1.66903477e-01),
( 3.28836861, 1.45512002e-01), (-1.81111369, -2.74811238e-02),
( 4.45443074, -6.20967098e-02), (-1.05623187, 4.54394791e-02),
(-2.78899479, 2.00793716e-01), ( 2.98214776, -3.00581044e-01),
(-4.93666148, 6.38533884e-01), ( 1.75236382, -2.76138475e-01),
( 3.87311972, -7.18193414e-01), (-2.57997713, 5.49086432e-01),
(-1.66518087, 3.99145748e-01), (-0.01525565, 4.05784329e-03),
( 1.01704884, -2.96599044e-01), ( 1.9838185 , -6.27979772e-01),
(-2.12858572, 7.25209683e-01), ( 2.97583486, -1.08325068e+00),
( 2.93420853, -1.13389670e+00), ( 1.43152097, -5.83945143e-01),
( 3.85960013, -1.65345315e+00), ( 1.83802576, -8.23127650e-01),
( 3.81132712, -1.77672231e+00), (-2.65180953, 1.28179083e+00),
( 3.79221905, -1.89374840e+00), ( 2.66564971, -1.37059204e+00),
(-4.19559824, 2.21401956e+00), (-4.31788858, 2.33140792e+00),
(-4.25453289, 2.34365806e+00), (-1.01136794, 5.66804115e-01),
(-2.6701157 , 1.51831693e+00), ( 0.11824462, -6.80429314e-02),
(-0.7161612 , 4.15976922e-01), (-2.68235362, 1.56869851e+00),
( 3.01374979, -1.77018497e+00), ( 3.10092284, -1.82483144e+00),
(-1.30947865, 7.70170578e-01), ( 2.20497631, -1.29295767e+00),
( 4.24279439, -2.47430352e+00), (-2.8062949 , 1.62357298e+00),
( 4.0386196 , -2.31210982e+00), ( 1.22931085, -6.94624837e-01),
(-1.34244465, 7.46696792e-01), ( 2.63538245, -1.43899767e+00),
( 1.89297207, -1.01179727e+00), ( 2.17061812, -1.13233761e+00),
( 0.75806551, -3.84758720e-01), (-4.45408817, 2.19229656e+00),
( 3.1908974 , -1.51771712e+00), (-0.07042532, 3.22489118e-02),
( 1.62040798, -7.11476998e-01), ( 0.60618196, -2.54083931e-01),
( 2.62317125, -1.04457464e+00), ( 3.4571751 , -1.30090798e+00),
( 0.69424509, -2.45382946e-01), ( 1.26730013, -4.17890535e-01),
(-2.10806129, 6.43459324e-01), ( 1.10109414, -3.08283598e-01),
( 0.50667136, -1.28712774e-01), (-4.80062287, 1.09200267e+00),
( 3.93455391, -7.88285510e-01), ( 1.71457754, -2.96171787e-01)]>
Total running time of the script: (0 minutes 0.806 seconds)