Python Resources

On this site will be a compendium of links and resources for helping you develop your skills in Python, and to apply those to several astronomical projects – identifying asteroids, working with astronomical images, and using ephemeris data to locate asteroids, comets and planets in the sky. There are resources here that include major catalogs of Asteroids, Python notebooks for simulating orbits of solar system objects, and NASA ephemeris databases to enable new solar system research.

I have divided this into sections, and provide links and tips for getting started below.

Getting Started in Python

Maybe you have already been exposed to Python, but if not, there are many helpful introductions to the language, and lots of materials available to help you learn. Since Yale-NUS College had the short course by Marla Geha on Python, I am going to include a lot of her materials, as they provide a very nice entry point into Python with an astronomical slant.

Most computers and all MacOS systems have Python built in. However one system that is highly regarded and complete in all of its packages is known as the Anaconda Python installation. I highly recommend this as it makes for a smooth experience, and has very nice and advanced looking notebooks for visualizing what you are doing. At Caltech the ZTF boot camp uses this distribution too. It is available at https://store.continuum.io/cshop/anaconda/.

Once you have this installed, you can use the guide from Marla Geha from Yale to help you learn more about Python. This includes some image processing exercises, using data both from her short course at Yale-NUS and also from the HST archive. These resources are all available at the Yale-NUS August Short Course site.  Her Pre-course Python guide – marla.geha.python.exercises.Precoursework_1 will help guide you through some of the introductory Python exercises.  She also has a Research Methods site at Yale that has a number of very useful Python exercises – this is available at http://www.astro.yale.edu/mgeha/A255/problemsets.html.

There are two ways to run Python – File Mode: by creating a .py file one can run it in a command line mode with a command like python file.py; Notebook Mode: by creating a Python notebook, one can run the notebook using “ipython” or interactive python. These files will end in the prefix ipynb, can also be run with ipython file.ipynb.  Within a notebook you will see input and output commands. A short example is below. These sections of a notebook are known as “cells” and can be run by highlighting the cell (here labelled In [4]) and punching the “Play button” on the top of the window (the triangle pointing right). The results will show up immediately below in the cell labelled Out (here Out [4] which prints out a table).

screenshot_1392

Sometimes on the site github you will find python notebooks. These are a bit hard to figure out at first, but can be run by punching in the inputs into a new notebook, after visualizing the notebook using something called nbviewer. This program is available at http://nbviewer.ipython.org/ and can be used by pasting in the URL of the github python notebook; nbviewer will render the file into a comprehensible format for you, and enable you a chance to experiment with it.

Additional guides and links to Astronomy and Python are below:

Marla Geha gave a wonderful short course at Yale-NUS on doing image processing in Astronomy with Python. Below are links to Marla’s Yale-NUS Python exercises: http://www.astro.yale.edu/mgeha/Singapore/problemsets.html

Marla recommends this source for learning Python:
http://learnpythonthehardway.org/book/  

Guide from ipython describing the “notebook” – a very useful tool for visualizing code in Python: http://ipython.org/ipython-doc/dev/notebook/index.html

A basic bouncing ball program for Python – a good starting point: http://python4astronomers.github.com/contest/bounce.html

You can learn about basic Python tables and plotting in this guide: http://www.astropy.org/astropy-tutorials/plot-catalog.html

You can learn how to work with Astronomy fits images and make a ‘tricolor’ image in Python out of three monochrome images with this tutorial:http://www.astrobetter.com/making-rgb-images-from-fits-files-with-pythonmatplotlib/

A good setup guide for getting started with the package known as astropy:http://www.astrobetter.com/wiki/tiki-index.php?page=Python+Setup+for+Astronomy

More Advanced Image Processing Notebooks – including SDSS and PTF data access

For a more advanced user, we have several GREAT notebooks that work very well to demonstrate image display, photometric analysis, and integration with large databases such as SDSS and PTF. Within these routines are calls to routines in pyraf and to the image display program known as DS9.  Below are links to the installation guides and software repositories.

American Astronomical Society Python code from workshop entitled “Using Python for Astronomical Data Analysis” – This day-long workshop included a fantastic set of notebooks that led us through all aspects of finding sources in images, extracting photometry catalogs etc. The notebooks and installation instructions are below:

https://github.com/spacetelescope/aas229_workshop

https://github.com/spacetelescope/aas229_workshop/blob/master/Installation_and_Setup.md

Caltech ZTF/PTF Summer School (a.k.a “boot camp”) in Python and Astronomy notebooks include very detailed notebooks which document the techniques of time-domain astronomy and astrophysics. These notebooks include light curve generation, identification of variable sources within databases, and creation of color-color diagrams.

Main Site for 2016 Summer School with notebooks and code – https://sites.google.com/site/pythonbootcamp/

Software Page for 2016 PTF Summer School – https://sites.google.com/site/pythonbootcamp/preparation/software 

Main Site for 2015 Summer School with notebooks and code – http://phares.caltech.edu/iptf/iptf_SummerSchool_2015/agendav5.html

Additional Add-on Python Packages 

The standard Python distribution from Anaconda has numpy (the numerical package) but may lack some of the other packages you will need for working with astronomy data in Python. These packages include astropy – the astronomy python package; pyfits – a package for handling FITS formatted astronomy images; and ephem – an ephemeris package for Python. To install these packages, you will download a big file with an ending like .tar.gz, and move it to the part in your anaconda distribution where packages are stored. This is a directory just below your main directory “anaconda” called anaconda/pkgs. Store the .tar.gz file in this directory anaconda/pkgs, and then unpack it using the commands gunzip and tar. You can “gunzip” the files with a command like gunzip file.tar.gz, and unpack them using a command like tar -xvf file.tar. This will separate all the files you will need. Then change into the directory for the package (for pyfits, I have a directory called pkgs/pyfits-3.3), and once in that directory you can install the package with the command ipython setup.py install.

Main package download sites are at these locations:

Astropy – http://www.astropy.org/

Pyphem – http://rhodesmill.org/pyephem/  (see tutorial at http://rhodesmill.org/pyephem/tutorial)

Pyfits – http://www.stsci.edu/institute/software_hardware/pyfits – note: many features of this package are now in Astropy.

Orbital Elements and Ephemeris Data

For solar system work, one of the first steps is computing the ephemeris data for an object. This can be done using JPL’s Horizons Database, perhaps the best site for this with absolutely accurate data. You can access that database on the web at the site: http://ssd.jpl.nasa.gov/?horizons or http://ssd.jpl.nasa.gov/horizons.cgi. You can also see a very complete guides to orbits at the site http://www.braeunig.us/space/orbmech.htm  and some interesting simulations at http://physics.info/orbital-mechanics-1/practice.shtml .

Many planetarium programs enable asteroids, spacecraft and comets to be simulated in their orbits. A few of the most common ones are listed below:

An astronomical object is specified by its orbital elements. Those can be found at the JPL Horizons site at http://ssd.jpl.nasa.gov/?sb_elem. Each astronomical object can be completely specified by a set of orbital elements. Those are described below in brief. More details can be found on the web site linked above.

VERY COOL Sites with a LOT of interesting stuff.

For anyone who has read this far, and would like to really push the envelope, I have found a number of sites with what look like amazing resources for asteroid, and outer solar system work. I would be very excited to see students work with these sites and help get their various databases and Python notebooks working. They include vast databases of near earth asteroids, amazing solar system orbit simulators and much more!

https://github.com/mwcraig – Matthew Craig’s github repository – highly recommended by experts at the AAS meeting!

https://media.readthedocs.org/pdf/specviz/v0.1.2rc3/specviz.pdf – documentation for a spectral viewing package from STScI known as Specviz

http://alumnus.caltech.edu/~nolan/astorb.html – a cool database of asteroid orbital elements

https://github.com/brandon-rhodes/astronomy-notebooks/blob/master/Asteroids.ipynb – what looks like an amazing resource for asteroid research

http://slittlefair.staff.shef.ac.uk/teaching/notebooks/Orbits.html  – nice notebook for teaching about orbits – could be adapted for other uses.

http://docs.astropy.org/en/v0.3/coordinates/ nice document for working in astropy with coordinates.

https://github.com/brandon-rhodes/astronomy-notebooks  – more amazing astronomy stuff from Brandon Rhodes.

http://www.jzuhone.com/pages/scientific-education.html – an interesting site for an interactive Kepler’s Law demonstration. Again can be modified for some more advanced purposes.

https://fiftyexamples.readthedocs.org/en/latest/gravity.html – a site showing code for simulation of orbits in Python. Very cool!

http://www.inference.phy.cam.ac.uk/mackay/python/ – a compendium of python programs including some simulations for astronomy.

http://www.inference.eng.cam.ac.uk/teaching/comput/C++/python/index.shtml – a nice example of orbit simulation with a very simple program and the package vpython. Works!

http://www.lns.cornell.edu/~seb/celestia/transforming_ephemeris.html – guide for importing ephemeris data into the Celestia program.

Orbital Elements Information — From the NASA horizons site: 

ASCII Files

Orbital elements below are available as fixed-format ASCII tables. Asteroid elements are available as Keplerian elements while comet elements use perihelion distance and time of perihelion passage instead of semimajor axis and mean anomaly. See the table column headings description below. Magnitude parameters, H and G, are included in the asteroid tables. If you need other parameters, you should use the small-body search engine, mentioned above.

(The files above were updated on 2015-Feb-11 02:32 -0800.)

Sample Asteroid File Header for an Orbital Elements File

Num   Name              Epoch      a          e        i         w        Node        M         H    G   Ref

—— —————– —– ———- ———- ——— ——— ——— ———– —– —- ———-

     1 Ceres             55400  2.7653485 0.07913825  10.58682  72.58981  80.39320 113.4104434  3.34 0.12 JPL 30

     2 Pallas            55400  2.7721532 0.23099956  34.84090 310.15094 173.12950  96.1482660  4.13 0.11 JPL 25

     3 Juno              55400  2.6700912 0.25498122  12.98211 248.10807 169.91138  32.0960832  5.33 0.32 JPL 86

Sample Comet File Header for an Orbital Elements File

Num  Name                                     Epoch      q           e        i         w        Node          Tp       Ref

——————————————- ——- ———– ———- ——— ——— ——— ————– ————

  1P/Halley                                   49400  0.58597811 0.96714291 162.26269 111.33249  58.42008 19860205.89532 JPL J863/77

  2P/Encke                                    55460  0.33586872 0.84833156  11.78308 186.54970 334.56582 20100806.50196 JPL K105/1

  3D/Biela                                    -9480  0.87907300 0.75129900  13.21640 221.65880 250.66900 18321126.61520 IAUCAT03

ASCII File Column Headings

screenshot_1391

Num

 

Number assigned to the asteroid or comet. Numbers are only assigned to secure short-period (<200 year) comets.

Name

 

Name given to the numbered asteroid or comet.

Designation

 

Unnumbered asteroid’s preliminary designation.

Epoch

(MJD)

Epoch of the elements represented as the Modified Julian Date (MJD), which is defined as the Julian date – 2400000.5.

a

(AU)

Semimajor axis of the orbit (asteroids only).

q

(AU)

Perihelion distance (comets only).

e

 

Eccentricity of the orbit.

i

(deg.)

Inclination of the orbit with respect to the J2000 ecliptic plane.

w

(deg.)

Argument of perihelion (J2000-Ecliptic).

Node

(deg.)

Longitude of the ascending node (J2000-Ecliptic).

M

(deg.)

Mean anomaly at epoch (asteroids only).

Tp

 

Time of perihelion passage (comets only), formatted as a calendar date (YYYYMMDD.DDD) where “YYYY” is the year, “MM” is the numeric month, and “DD.DDD” is the day and day fraction.

H

(mag.)

Absolute magnitude (asteroids only).

G

 

Magnitude slope parameter (asteroids only).

Ref

 

Orbit solution reference.