Programming with Python

The best way to learn how to program is to do something useful, so this introduction to Python is built around a common scientific task: data analysis.

Supernovae Lighcurves

A supernova is a tremendous stellar explosion. A supernova is a transient event which occurs at the end of life of a massive star or when a white dwarf is triggered into a runaway nuclear reaction. The light curve of a supernova is constructed by plotting its magnitude as a function of time. At the peak of its brightness a supernova can outshine its host galaxy but its light quickly fades away. More detailed information about light curves can be found [here] (http://astronomy.swin.edu.au/cosmos/T/Type+Ia+Supernova+Light+Curves).

We are studying supernova lighcurves measured in a four bandpasses - g, r, i and z. The data in this tutorial comes from Guy et al., (2010). The observations of 231 high-redshift supernovae are split into separate files in the comma-separated values (CSV) format:

The first three rows of our first file look like this:

MJD,Flux_g,e_Flux_g,Flux_r,e_Flux_r,Flux_i,e_Flux_i,Flux_z,e_Flux_z
52880.5,13.035,5.4246,NaN,NaN,NaN,NaN,NaN,NaN
52881.5,NaN,NaN,7.0282,9.9105,16.157,18.629,22.086,71.951
52886.6,NaN,NaN,NaN,NaN,189.44,19.855,NaN,NaN

So, we want to:

  1. Calculate the maximum brightness in each band.
  2. Plot the light curves to discuss and share with colleagues.

To do all that, we’ll have to learn a little bit about programming.

Prerequisites

You need to understand the concepts of files and directories and how to start a Python interpreter before tackling this lesson. This lesson sometimes references Jupyter Notebook although you can use any Python interpreter mentioned in the Setup.

The commands in this lesson pertain to Python 3.

Getting Started

To get started, follow the directions on the “Setup” page to download data and install a Python interpreter.

Schedule

Setup Download files required for the lesson
00:00 1. Analyzing Supernova Data How can I process tabular data files in Python?
00:40 2. Visualizing Data How can I create simple plots in Python?
How can I create multi-panel plots in Pyhton?
01:20 3. Repeating Actions with Loops How can I do the same operations on many different values?
01:50 4. Storing Multiple Values in Lists How can I store many values together?
02:20 5. Analyzing Data from Multiple Files How can I do the same operations on many different files?
02:40 6. Making Choices How can my programs do different things based on data values?
03:10 7. Creating Functions How can I define new functions?
What’s the difference between defining and calling a function?
What happens when I call a function?
03:40 8. Errors and Exceptions How does Python report errors?
How can I handle errors in Python programs?
04:10 9. Command-Line Programs How can I write Python programs that will work like Unix command-line tools?
04:40 10. Command-Line Programs How can I write Python programs that will work like Unix command-line tools?
05:10 11. Defensive Programming How can I make my programs more reliable?
05:40 12. Automating Tests for Your Code How do you test your code?
06:10 13. Debugging How can I debug my program?
06:40 14. Astropy and Jupyter Notebooks What is Astropy?
07:10 Finish

The actual schedule may vary slightly depending on the topics and exercises chosen by the instructor.