Using variables with units

We'll use astropy to unburden us from common calculations (and equally common mistakes when doing them by hand).

astropy has a fair amount of packages, so check of the docs: http://docs.astropy.org/en/latest/


In [1]:
kpc_to_km = 3.086E16
distance = 1. # kpc
distance * kpc_to_km


Out[1]:
3.086e+16

In [2]:
type(distance)


Out[2]:
float

In [3]:
import astropy.units as u
distance_q = 1 * u.kpc
type(distance_q)


Out[3]:
astropy.units.quantity.Quantity

In [4]:
distance_q.to(u.km)


Out[4]:
$3.0856776 \times 10^{16} \; \mathrm{km}$

In [5]:
distance_q.to(u.jupiterRad)


Out[5]:
$4.3161159 \times 10^{11} \; \mathrm{R_{\rm J}}$

In [6]:
distance_M = 1 * u.Mpc
distance_q + distance_M


Out[6]:
$1001 \; \mathrm{kpc}$

Coordinate transformations

Coordinates in astronomy often come in equatorial coordinates, specified by right ascension (RA) and declination (DEC).


In [7]:
import astropy.coordinates as coord

In [8]:
c1 = coord.SkyCoord(ra=150*u.degree, dec=-17*u.degree)
c2 = coord.SkyCoord(ra='21:15:32.141', dec=-17*u.degree, unit=(u.hourangle,u.degree))

If we wanted this coordinate on the celestial sphere to another system (of the celestial sphere), which is tied to our Galaxy, we can do this:


In [9]:
c1.transform_to(coord.Galactic)


Out[9]:
<SkyCoord (Galactic): (l, b) in deg
    ( 254.64626221,  29.37739278)>