Astropy 5.X: astronomical tools for everybody
What is Astropy¶
- The Astropy Project (https://www.astropy.org/) aims to enable the community to develop a robust ecosystem for:
- Astronomical research.
- Data processing.
- Data analysis.
- Over 50 coordinated and affiliated packages
- The
astropy
core package contains key functionality and common tools needed for performing astronomy and astrophysics with Python. - Over 34k commits and 3400 stars on Github.
- Latest stable version: 5.1.1.
- Useful for other purposes too.
Units¶
Provide units to scalars and Numpy arrays:
In [1]:
from astropy import units as u
42.0 * u.meter
Out[1]:
$42 \; \mathrm{m}$
In [2]:
import numpy as np
np.array([1., 2., 3.]) * u.m
Out[2]:
$[1,~2,~3] \; \mathrm{m}$
Units¶
Conversions made easy:
In [3]:
from astropy.units import imperial
kms = u.km / u.s
mph = imperial.mile / u.hour
q = 10.0 * kms
q.to(mph)
Out[3]:
$22369.363 \; \mathrm{\frac{mi}{h}}$
In [4]:
ly = 1 * u.lightyear
ly.to(u.km)
Out[4]:
$9.4607305 \times 10^{12} \; \mathrm{km}$
In [5]:
radio_fm = 100 * u.MHz
radio_fm.to(u.meter, equivalencies=u.spectral())
Out[5]:
$2.9979246 \; \mathrm{m}$
Dates and times¶
Robust date and time operation:
In [7]:
from astropy.time import Time
# Beware of leap seconds!
Time("2016-12-31 23:59:60")
Out[7]:
<Time object: scale='utc' format='iso' value=2016-12-31 23:59:60.000>
In [8]:
Time("2017-12-31 23:59:60")
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/erfa/core.py:155: ErfaWarning: ERFA function "dtf2d" yielded 1 of "time is after end of day (Note 5)" ErfaWarning)
Out[8]:
<Time object: scale='utc' format='iso' value=2018-01-01 00:00:00.000>
Dates and times¶
Fully compatible with datetime
:
In [9]:
from datetime import datetime
from_datetime = Time(datetime(2022, 11, 26, 15, 30))
to_datetime = from_datetime.to_datetime()
print(from_datetime)
print(to_datetime)
2022-11-26 15:30:00 2022-11-26 15:30:00
Many other tools¶
- Physical constants with units.
- Tabular operations and manipulation, fully compatible with Pandas.
- Time series.
- Coordinates: geocentric, baricentric, heliocentric, ...
- Model fitting: box fitting, gaussian fitting, ...
- Read/write formats: CSV, HTML, LaTeX tables, FITS, HDF5, YAML, ASDF, ...
- Simple progress bar.
- Image (Numpy array) visualization.