Copyright&Credit

A Shallow Dive Into Astropy

Beyza Ulasti
2 min readFeb 11, 2023

--

Astropy is a community-developed Python library, the primary use of which is to assist astronomers and astrophysicists. Additionally, Astropy provides a framework for building other, more specific, libraries as Astropy too was built on several different libraries including NumPy, SciPy, Mathplotlib, Pandas, Pytest, and more.

Some functions of Astropy include:

  • Unit Conversions & Constants: The field of astronomy deals with different units, and Astropy helps automate conversions as it has built-in conversion factors. Astropy also handles physical units and constants in a way that can be easily used in calculations.
  • File I/O: Astropy supports reading and writing data in different formats by providing a unified interface
  • Coordinate Transformations & Time: Time and space are, without a doubt, staples in astronomy, astrophysics, and cosmology. Astropy provides functions to handle time zones, time scales, time transformations, and converting between different coordinate systems.
  • Flexible Image Transport System & Image Analysis: FITS-Flexible Image Transport System is the international standard for storing astronomical data. Therefore, it is supported by many packages used in astronomy and astrophysics, including Astropy. This community-developed library also makes it easy to analyze images.
  • Tables: Like Pandas, Astropy supports working with tabular data in ASCII and FITS format

Most of these functions can be performed using different libraries and packages. For example, PyFITS can read and write FITS files in Python. However, most of the competitors of Astropy are packages with limited functionality. Astropy aims to be a comprehensive library with multi-functionality.

If you want to see how effective Astropy looks, I asked ChapGPT to perform a coordinate transformation once using Astropy and once with NumPy.

Here’s the result for Astropy:

from astropy import units as u
from astropy.coordinates import SkyCoord

# Define the initial coordinates in the ICRS (International Celestial Reference System) frame
coord_icrs = SkyCoord(ra=10.68458*u.degree, dec=41.26917*u.degree, frame='icrs')

# Transform the coordinates to the Galactic frame
coord_galactic = coord_icrs.galactic

# Print the transformed coordinates in Galactic longitude and latitude
print("Galactic longitude:", coord_galactic.l.degree)
print("Galactic latitude:", coord_galactic.b.degree)

Here’s the result for Numpy:

import numpy as np

# Define the initial coordinates in the ICRS (International Celestial Reference System) frame
ra = 10.68458 # in degrees
dec = 41.26917 # in degrees

# Convert the right ascension and declination to radians
ra_rad = np.radians(ra)
dec_rad = np.radians(dec)

# Convert the ICRS coordinates to Galactic coordinates using the conversion matrix
sin_b = np.sin(dec_rad) * np.sin(np.radians(27.4)) + np.cos(dec_rad) * np.cos(np.radians(27.4)) * np.cos(ra_rad)
b = np.arcsin(sin_b)
l = np.arctan2(np.sin(ra_rad) * np.cos(dec_rad), np.cos(ra_rad) * np.sin(dec_rad) * np.sin(np.radians(27.4)) - np.sin(dec_rad) * np.cos(np.radians(27.4)))

# Print the transformed Galactic longitude and latitude in degrees
print("Galactic longitude:", np.degrees(l))
print("Galactic latitude:", np.degrees(b))

--

--