Python Astronomy Homework

Please note that all the SIEpedia's articles address specific issues or questions raised by IAC users, so they do not attempt to be rigorous or exhaustive, and may or may not be useful or applicable in different or more general contexts.

Python for astronomers

Python is becoming one the most used interpreted languages for data analysis, competing directly with well-stablish commercial solutions as Matlab or IDL. Apart from its simple syntax and very smooth learning curve, the main advantage of Python is that you can use it virtually for everything, having modules for database interaction, web development, parallel computing and a long etcetera. Of course, there mature scientific and mathematical libraries.

Beeing Python so extensive, its easy to get lost with all the possible modules and its documentation, because there are many options to do the same. Differing from monolithic-packages as IDL or Matlab, where you have one plotting library, one GUI library, a standard IDE (integrated Development Environment) and so on, all with a common documentation, in Python, as with any general use programming language, you have to choose the modules you need and install them, refering to the documentation of each module separately.

In this guide you will find which are the most common packages and documentation for astronomy and where to obtain them.

The basics

Obviously first you need Python itself. If you are using GNU/Linux or a Mac, its already installed by default on this systems. If you have Windows you can install the official package or WinPython (recommended) or Python(x,y) project which apart from Python includes the most common scientific packages and tools.

Scientific packages

The main scientific package is Scipy, which includes ODE solvers, linear algebra libraries, interpolation, optimization, etc. Scipy requires Numpy, a powerful arrays library.

  • Scipy - The main scientific library
  • Numpy - Arrays manipulation library
  • Matplotlib - 2D (and basic 3D) plotting library
  • IPython - An advanced interactive console

From GNU/Linux, you can try to obtain this packages using the package installer of your distribution (apt-get in Ubuntu/Debian, yum in Fedora or YAST in SUSE). The Python(x,y) package (Windows only) includes all this.

Other scientific modules


  • NLopt - Non-Linear optimization library
  • lmfit - It is a high-level interface to Scipy's optimize.leastsq and others.
  • OpenOPT - Numerical optimization framework

Statistics and other

  • Statsmodels - Data analysis and statistics
  • Pandas - Python Data Analysis Library
  • Sympy - Symbolic Mathematics Library

Astronomy packages

There are several modules to do the same thing, below are the most popular. Also take a look to Astropython, a knowledge base for research in astronomy using Python.

  • Astropy - A community effort to develop a single core package for Astronomy
  • Pyfits - FITS images and tables manipulation (already included in astropy)
  • PyRAF - A command language for IRAF written in Python. It also enable to call IRAF tasks from Python. Requires IRAF.
  • APLpy - Astronomical Plotting Library with matplotlib
  • Kapteyn Package - A very complete and well documented collection of libraries from the Kapteyn Astronomical Institute. It includes coordinate and WCS manipulation, tables and plotting utilities.
  • pywcsgrid2 - Astronomical plotting with matplotlib.
  • SAMPy - A SAMP implementation for Python.
  • pyregion - A ds9 region files parser.
  • CosmoPy - A cosmology package.
  • idlsave - An IDL's "save" file format reader (to numpy types). Do not requires IDL.
  • A Python Connection to DS9 via XPA
  • - L.A.Cosmic algorithm implementation in Python (for cosmic ray detection)
  • Chantipy - A Python interface to the CHIANTI atomic database for astrophysical spectroscopy
  • python-montage - A Python wrapper for montage
  • SunPy: An effort to create an open-source software library for solar physics
  • AsPyLib: A python library for FITS image processing, including photometry and astrometry.
  • pySpecKit: A splot-like spectroscopic analysis toolkit for astronomy
  • - Python and C library for source extraction and photometry
  • Optical simulation toolkit poppy and Simulated PDF for JWT webbpsf
  • pyAOS - Monte-Carlo Adaptive Optics Simulation toolkit
  • SpecViz - 1D astronomical spectral visualization and analysis suite

Other useful packages

  • Sympy - Impressive symbolic calculus module
  • Mayavi - A powerful 3D data visualizer (Matplotlib only make simple 3D plots)
  • f2py - Fortran to Python interface creator
  • imageio - Python library for reading and writing image data. Supports a lot of image and video formats.
  • AstroML: A Python module for machine learning and data mining


If you don't know anything about Python, just begin with official tutorial, its brief and clear. Then, the main reference for astronomers is the Perry Greenfield and Robert Jedrzejewski's Using Python for interactive data analysis in astronomy tutorial, which covers the Python's basics and all the important astronomical packages with many examples (many thanks Perry and Robert!). Remember that the reference website for scientific packages and documentation is

Here are documentation of the main scientific packages:

Courses and tutorials

Video tutorials and demonstrations

    Class handouts and assignments

  • Course Information and syllabus (handout at first class; updated November 16)
  • Our textbook, Statistics, Data Mining, and Machine Learning in Astronomy is available online (this may require using a Princeton computer).
  • Homework 1, due Monday, September 26, in class.
  • Solutions to Homework 1.
  • Homework 2, due Wednesday, October 5.
  • The quasar spectrum needed for Homework 2. This is a spectrum from the SDSS; the columns are wavelength, flux density (in units of 10-17erg/s/cm2/A), flux density error (same units), and a mask that indicates possible problems with each pixel. For this homework, you need only use the first two columns of data.
  • Solutions to Homework 2, together with the Python code needed to do Problems 1 and 2.
  • Homework 3, due Monday, October 17.
  • The A star spectrum needed for Homework 3. First two columns are wavelength in Angstroms and flux density (in units of 10-17erg/s/cm2/A).
  • Solutions to Homework 3, together with the Python code needed to do Problem 3.
  • Homework 4, due Monday, November 7.
  • Solutions to Homework 4, together with the Python code needed to do Problem 4.
  • Homework 5, due Thursday, November 13. The data file needed for Problem 1.
  • Solutions to Homework 5, together with the Python code needed to do Problem 1.
  • Homework 6, due Wednesday, November 30.
  • Solutions to Homework 6
  • Homework 7, due Tuesday, January 17 (Dean's Date; hand into Michael's office), together with the sky spectrum needed for Problem 3.
  • Resources for the final JWST proposal project (updated December 1).

    Computers and Python resources

  • A description of how to get started using computers in Peyton Hall, including information on python.
  • A brief introduction to Unix at Princeton, by Robert Lupton and Jill Knapp.
  • An introduction to X windows (the window-manager system that many of the computers in the building use), by Robert Lupton.
  • An alternative introduction to Unix; the bare minimum is contained in the first five tutorials.
  • A General Introduction to Python, including numpy and SciPy.
  • Programming in Python, for astronomers.
  • Python for Data Analysis, a 470-page textbook available online.
  • An introduction to SciPy.
  • Astropy, a project for building useful utilities for astronomers in Python.
  • There is a blog associated with the book A Student's Guide to Python for Physical Modeling.
  • AstroML is a website accompanying the book Statistics, Data Mining, and Machine Learning in Astronomy.
  • Useful ipython notebooks from Jake Vander Plas.
  • Unix and Python reference page from Physics 209.
  • The NIST Digital Library of Mathematical Functions, an update of the classic handbook by Abramowitz and Stegun.
  • The Second Edition of Numerical Recipes (i.e., not the latest version) is available for free on the web in C, Fortran 77, and Fortran 90.

    General Astronomy Resources

  • Useful astronomical links. These are from AST 203, so tend to the elementary, and are a bit dated...
  • Science White Papers for the James Webb Space Telescope.
  • The weekly calendar of astrophysics-related talks in the Princeton area.
  • ArXiv, the repository of the daily preprints of the astrophysics community, often referred to as "astro-ph". There is a page describing how to sign up to receive a daily update of astrophysics papers, and a website organizing the Peyton Hall daily discussion of these papers.
  • The Astrophysics Data System, a portal to essentially the complete journal literature of astronomy.
  • AstroBetter, a blog where professional astronomers share tips and tricks forbeing successful in the astronomy world.
  • Astrobites, a website run by grad students for undergrads, where they summarize interesting astro-ph articles and provide general tips.
  • The full, downloadable report of ASTRO2010, The Astronomy and Astrophysics Decadal Survey, and the 2016 Midterm Report.

    Professors: Michael Strauss and Jenny Greene.

0 Replies to “Python Astronomy Homework”

Lascia un Commento

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *