Abstract : A collection of python resources for astronomy

Category : Recommended   Active   

Homepage : http://oneau.wordpress.com/2010/10/02/python-for-astronomy/

Astropython.org contributor Prasanth has put together a resource list by topic that presents a current view of what is available for astronomers using Python in scientific data analysis.  This posting echoes the original blog post content referenced above.

Table of contents

  • Basic requirements
  • A selection of libraries
  • Other interesting python libraries for astronomy
  • Libraries for interacting with other applications
  • Performing specific tasks
    • Read, manipulate and write tabular data in various formats
    • Plots and annotated images
    • Performing astrometry calculations
    • Mathematical and scientific calculations
    • Calling C, Fortran and IDL code
    • Photometry
    • Spectroscopy
    • Creating graphical and command line user interfaces
      • Command line interfaces
      • Graphical interfaces
    • Accessing VO resources
    • Creating documentation
  • Miscellaneous modules
  • Information sources on using python in scientific data analysis
  • Learning python
  • Links

Basic requirements

When installing non-python packages, make sure that in addition to the package itself, development libraries and header files, if available, are also installed.

There are a few python "distributions" that come with almost all these packages, and their dependencies, and only some of the astronomy specific packages need to be installed separately. Another advantage of of using these distributions is that the python installation that comes with the operating system can be left as it is. But on the other hand they may not be available for all operating systems.

Some python distributions i.e., python + libraries, are:

  • EPD

    Free for academic usage.

  • pythonxy

    Available only for windows. This is the easiest way for windows users to setup a good python environment for scientific computing.

  • scisoft

    Contains a set of python libraries, in addition to a very large collection of non-python astronomy software. The ESO version is available only for Fedora 11. A version for Mac OS X is available at http://web.mac.com/npirzkal/Scisoft/Scisoft.html

The STScI python library is a collection of several astronomy libraries. It includes some of the astronomy related packages mentioned below. Astrolib is another project aimed at developing python modules for astronomy.

PyPi is a repository for python libraries. GitHub, Bitbucket, sourceforge, google code and launchpad are some websites that host python code.

In the following, the words libraries, modules and packages are all used to indicate a collection of related python code.

  • Python

    Any version of python >= 2.5 should be compatible with most of the astronomy packages. But python 3.x series does not have wide support at this time. It is not compatible with python 2.x and it will take a while for most packages to become available for python 3.x. But note that numpy 1.5.0 and asciitable 0.3.1 work with python 3.x.

  • C and Fortran compilers

    Get the latest version of gcc.

  • C and Fortran libraries

    Mathematical libraries such as BLAS, LAPACK and ATLAS are used by numpy for many operations. Use of ATLAS implementation can make many computations much faster.

  • Readline

    Readline is a must for efficient interaction at the terminal.

  • Tcl/Tk

    Python tkinter module provides interface to Tcl/Tk that allows building graphical user interfaces.

  • PIL : Python Imaging Library

    PIL is a python library for performing image manipulations. It can be used to read and write images in various formats, apply various transformations to the image data and other such operations.

  • wxPython

    A library for creating GUIs. This is a python binding for the wxWidgets library. This is used by matplotlib as one of its backends.

  • Numpy

    The main feature provided by numpy is the ability to work with multidimensional arrays in an efficient manner. This forms the basis for carrying out mathematical calculations, such as those related to histograms, correlations, linear algebra etc., . Scipy, for example, builds upon these facilities provided by numpy to create higher level scientific and mathematical routines.

  • IPython

    This is an excellent substitute for the default python shell. Perhaps the most attractive part of this shell is the ability to list the contents of a module or object. For example, after importing the math module, we can type math. and press the tab key, to get a list of members of the module. The command math.sin? will print the docstring for the function sin in the math module. The %cpaste magic command, allows code to be pasted into the shell, which would otherwise result in indentation errors.

  • setuptools and/or pip

    These two tools allow easy installation of python packages.

    Many packages can be installed using the commands:

    easy_install <package-name>


    pip install <package-name>

    These commands will download the required files and install the packages.

    Pip aims to be a replacement for easy_install.

A selection of libraries

  • pyfits

    This library provides facilities for reading, manipulating and writing FITS files.

  • pywcs

    This is a python library for handling World Coordinate System information in FITS headers. It uses the wcslib C library written by Mark Calabretta.

    Requires pyfits and wcslib. The latter comes with the pywcs distribution.

  • coords

    This library can be used for managing astronomical coordinate systems. Ultimately, this library aims to provide facilities needed by astronomers for planning and preparing for observing runs.

    It currently provides facilities for coordinate conversions and is under development.

  • astroasciidata, vo.table, asciitable and ATpy

    Together with pyfits, these libraries provide comprehensive facilities for dealing with the variety of tables used in storing astronomical data. For example, asciitable can read data from tables in the CDS format provided by VizieR and ApJ. ATpy can read daophot data files.

  • pyraf, urwid and pmw

    Pyraf provides an interface to the NOAO IRAF system (also see http://www.iraf.net/ for IRAF user support). Libraries urwid and pmw are dependencies for pyraf.

  • numdisplay

    This library provides an interface to the DS9 image display program. Numdisplay can also interact with the ximtool image display program.

    There is another package, pyds9, from the developers of DS9, that provides the similar functionality.

    RO is another library that has a module for interacting with DS9. The library, pyregion, can parse DS9 region files.

  • matplotlib

    Matplotlib is a 2D plotting library, with some 3D plotting functionality.

  • APLpy

    APLpy builds on matplotlib, and allows creation of production-quality plots of astronomical imaging data in FITS format. Some of the features provided are overlaying coordinate grids, 3-color RGB image display, contours and markers etc., .

  • Kapteyn

    A collection of modules for handling World Coordinate System transformations, coordinate conversions, handling FITS files, plotting and others. It also has a module for parsing and interpreting coordinate information entered as strings.

  • PyEphem, pyslalib and pytpm

    PyEphem can be used to get quick answers to questions such as, Which constellation is Jupiter in tonight?, What is the rising/setting time for Moon, at a particular location?, What is the current Julian Date?. It also has methods for coordinate conversion, finding sidereal time and others. PyEphem has built-in ephemerides for major planets and can read ephemerides in various formats.

    PyEphem uses C routines from the XEphem package and a copy of the C library is included with the source code.

    Pyslalib is a python interface to the SLALIB fortran library. This can be used to perform lower level astrometry calculations such as, direction between points on a sphere, conversion between Cartesian and spherical coordinates and also higher level computations such as calculation of Julian Dates. It also has functions to process sexagesimal string into angles, converting radians into degrees, arcminutes and arcseconds and other.

    This module comes with a version of the SLALIB fortran library.

    Pytpm is an interface to Jeff Percival‘s Telescope Pointing Machine C library. This library is similar to SLALIB in that it provides functions for performing astrometry calculations, but differs from SLALIB in adopting a "state machine" approach to performing these calculations. An end user can perform coordinate conversion calculations without having to deal with the details of the procedure, such as the order of applying individual routines. One call to a function, specifying the starting "state" and end "state" is all that is required for performing the appropriate conversion.

    Pytpm includes a copy of the TPM C library that is part of the coords package.

    Together with Kapteyn, these three modules provide most of the facilities for performing astrometry calculations.

  • Scipy and ScientificPython

    Modules implementing various algorithms for scientific computing.

  • Sphinx

    Sphinx generates formatted documentation from text documents written in the reStructuredText format. This module can read "docstrings" from python code and include them in the final document. It incorporates several features such as running "doctests" in python code, producing plots by running python code, links to source files and others. Most of these functionalities are provided using "sphinx extensions". Source code for matplotlib, matplotlib sampledoc tutorial and the sphinxcontrib collection include many such extensions, in addition to those that come with sphinx.

  • nose

    This is a framework for writing tests for python code. It can find unit tests in python source files and execute them. It provides various other features for performing test on python code.

Libraries pyfits, pywcs, pyraf, pysynphot, and numdisplay are, along with many other libraries, bundled with the stsci_python collection. Sphinx and nose are not astronomy specific packages. Sphinx is used for creating documentation and nose is a framework for carrying out unittests. Both of these, documentation and testing, are integral to any software project and hence they are included here.

Other interesting python libraries for astronomy

  • aipy

    Astronomical Interferometry in PYthon is a collection of tools for radio astronomical interferometry.

  • astropysics

    This is a library aimed at providing a very large set of astronomy related code. Additionally, it wraps SExtractor and Kcorrect.

  • astLib and pyWCSTools

    Another collection of python code for astronomy, including coordinate conversions, WCS manipulations and so on.


    Software for pulsar search and analysis.

  • RO

    RO is a collection of scripts, written by Russel Owen of University of Washington in Seattle, providing Tkinter GUI extensions, Tkinter-compatible networking, astronomical calculations such as coordinate conversion, time related functions and so on. It includes a module for interacting with DS9.

Libraries for interacting with other applications

Performing specific tasks

Some libraries for performing tasks such as reading tabular data, creating plot, calling C libraries and so on.

Read, manipulate and write tabular data in various formats

  • ASCII tables

    Use ATPy, astroasciidata, asciitable and the tabarray module in Kapteyn. A tutorial on using AstroAsciiData is available at this url. Note that ATpy uses asciitable module for reading ASCII tables.

  • SExtractor tables

    Use astroasciidata. The meta-data stored in SExtractor tables are also read in by this module and can be accessed from within python, using the various facilities provided by AstroAsciiData.

  • FITS tables

    Use pyfits.

  • VO tables

    vo.table and ATpy.

  • Tables from SQLite, MySQL and PostgreSQL.

    Use ATpy. For reading MySQL and PostgreSQL tables, ATpy uses the MySQL-python and PyGreSQL libraries, respectively.

  • HDF5 tables

    Use ATPy. ATpy uses the h5py library for reading HDF5 tables. Also see http://www.pytables.org/.

  • CDS format tables, and Vizier and ApJ machine readable tables

    The asciitable module can read ascii files in these formats.

Plots and annotated images

  • Matplotlib

    A library for creating, mainly, 2D plots.

  • APLpy and Kapteyn

    Two libraries for creating plots and annotated images.

  • Mayavi2

    A library for 3D data visualization.

  • Gnuplot.py

    This module can be used for calling Gnuplot from python.

  • rpy and rpy2

    These provide access to the R software environment, including plotting facilities.

  • ppgplot

    This module allows calling functions in the pgplot library from python.

  • pygrace

    Pygrace provides an interface to the GRACE plotting library.

  • Chaco

    A plotting application toolkit that is part of the Enthought Tool Suite.

  • Veusz

    A scientific plotting and graphing package, with a graphical user interface.

Performing astrometry calculations

There are several libraries that provide functions for performing astrometry calculations. These are Kapteyn, pytpm, pyslalib , coords, RO, astLib, astropysics and PyEphem. Only PyEphem provides facilites for handling ephemerides of solar system objects and calculating quantities such as rise and set times of these objects.

Mathematical and scientific calculations

Numpy, Scipy and ScientificPython.

Calling C, Fortran and IDL code


Use pyraf interface to IRAF and pyIDL and pIDLy interfaces to IDL. pysynphot can be used for synthetic photometry and spectrum manipulation. The library tfit is a photometry package, but is currently not maintained.


Use pyraf interface to IRAF and pyIDL and pIDLy interfaces to IDL. pysynphot can be used for synthetic photometry and spectrum manipulation.

Creating graphical and command line user interfaces

Command line interfaces

Graphical interfaces

Creating documentation

Information sources on using python in scientific data analysis

    7           9