AstroPy is one of the most powerful and huge packages for astronomers in Python. It contains a wide range of tools for astronomical computations: from basic units to cosmological models. AstroPy also helps to convert values using standard astronomic units, compute distances between stars, and even create cosmologies. AstroPy can interact with most of the astronomical and astrophysical packages to make an environment for astronomical research, which makes it a very effective tool. In this topic, we'll learn how to use AstroPy for astronomical computations.
Units, quantities, and constants
AstroPy allows the conversion of numerical data into units using the astropy.units package. It works with length, wavelength, and frequency units, such as kilometers, Hz, hours, and so on. You can convert int and float types, sequences, and NumPy arrays to Quantity objects.
import numpy as np
import astropy.units as u
lengths = np.array([49, 58, 947])
lengths * u.kilometer # [49, 58, 947]km
We have the to() method to convert the values. You can convert only comparable values.
time = 14 * u.hour
time.to(u.second) # 50400s
AstroPy contains many standard values used in astronomical calculations such as the gravitational constant, Avogadro's number, and so on. These values are stored in the astropy.constants package. When you call a constant value, you may learn the main information about it in full or choose a parameter.
from astropy.constants import mu0
print(mu0.value) # 1.25663706212e-06
print(mu0)
# Name = Vacuum magnetic permeability
# Value = 1.25663706212e-06
# Uncertainty = 1.9e-16
# Unit = N / A2
# Reference = CODATA 2018
AstroPy allows calculation using constants and quantity objects.
F = const.G * 488 * u.kg # 3.2570584 × 10−8 m3 / s2
Sometimes, you may use the coordinates of celestial bodies for calculations. In the next section, you'll learn how to read and write the coordinates.
Astronomical Coordinate Systems
AstroPy has astropy.coordinates, a package that allows you to read and write the coordinates of celestial bodies. It may be useful for the computations and the visualization of stars, planets, and other celestial objects. You can define the coordinates manually using the SkyCoord() class.
from astropy.coordinates import SkyCoord
polaris = SkyCoord(ra=37.95456067*u.degree, dec=89.26410897*u.degree, frame='icrs')
You can get the coordinates of celestial objects using from_name() method.
aldebaran = SkyCoord.from_name('aldebaran') # <SkyCoord (ICRS): (ra, dec) in deg (68.98016279, 16.50930235)>
One of the most popular operations is a computation of the great-circle distance between two celestial objects. It can be done using the separation() method:
aldebaran = SkyCoord.from_name('aldebaran')
polaris = SkyCoord.from_name('polaris')
polaris.separation(aldebaran) # 72∘51′37.66786948′′
SkyCoord() objects allow many operations, such as converting to another coordinate system, working with velocities, visualizing observations from Earth, etc.
Cosmologies
Cosmology, a science at the intersection of physics, mathematics, and astronomy, studies the laws, properties, and evolution of the Universe. Cosmological models are used for research in this area. AstroPy has the subpackage astropy.cosmology that allows us to read, write, import cosmologies, and make the necessary calculations. This subpackage has several classes that correspond to different types of cosmology representation, such as FlatLambdaCDM, FlatwCDM, and so on.
Some built-in cosmologies have already defined parameters, like WMAP3, WMAP5, WMAP7, Planck13, Planck15, and many others. When importing you may know more about cosmology, and its parameters, or make some calculations.
from astropy.cosmology import WMAP3
print(WMAP3.__doc__) # WMAP3 instance of FlatLambdaCDM cosmology
#(from Spergel et al. 2007, ApJS, 170, 377, doi: 10.1086/513700. Table 6 (WMAP + SNGold) obtained from: https://lambda.gsfc.nasa.gov/product/map/dr2/params/lcdm_wmap_sngold.cfm\nPending WMAP team approval and subject to change.)
It is also possible to create your cosmological model using the specification Hubble parameter and Omega matter.
from astropy.cosmology import FlatLambdaCDM
my_cosmo = FlatLambdaCDM(H0=67.7, Om0=0.272)
Cosmology() class interacts well with astropy.units the package that is useful for calculations. Cosmologies provide in-depth research, including dark energy research.
Conclusion
In this topic, you have learned:
- How to make astronomical calculations using the
AstroPylibrary; - How to read and write the coordinates of celestial bodies and find the great-circle distance between them;
- How to import built-in cosmologies and create your cosmological models.