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.

Sea surface waves

In this exercise we will read and manipulate ocean data. The data used has been generated by a spectral wave model WaveWatch III, which is a numerical tool for simulating and forecasting sea state.

When it is very windy or storms pass-over large sea areas, surface waves grow from short choppy wind-sea waves into powerful swell waves. The height and energy of the waves is larger in winter time, when there are more storms.

The example file contains a time series of wave data. The numbers representing the significant wave height: the mean wave height (trough to crest) of the highest third of the waves.

These values are monthly averages, over a period of 37 years. The first two columns are related to the timing of the data, years in the first column, then months. The wave height data are in the third column.

To investigate the wave data, we would like to

  1. Calculate an average and maximum
  2. Observe the seasonal cycle
  3. Take averages per month over successive years
  4. Find which months have the smallest and largest waves
  5. Plot the result to discuss and share with colleagues

3-step flowchart shows a photo of the sea moving to the Analysis step
where a heat map of provided data is generated moving to the Conclusion step that asks the
question, What can we conclude from the data?

Data Format

The data sets are stored in comma-separated values (CSV) format:

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

1979,1,3.788
1979,2,3.768
1979,3,4.774
1979,4,2.818
1979,5,2.734
1979,6,2.086
1979,7,2.066

Each data value represents the significant wave height in metres, an average over the month.

For example, value “2.066” at row 7 column 3 of the data set above means that during the seventh month (July) of the first year (1979), the wave height was an average of 2.066 m.

In order to analyze this data and report to our colleagues, 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. Python Fundamentals What basic data types can I work with in Python?
How can I create a new variable in Python?
How do I use a function?
Can I change the value associated with a variable after I create it?
00:30 2. Analyzing some wave-height data How can I process tabular data files in Python?
01:30 3. Visualizing Tabular Data How can I visualize tabular data in Python?
How can I group several plots together?
02:20 4. Storing Multiple Values in Lists How can I store many values together?
03:05 5. Repeating Actions with Loops How can I do the same operations on many different values?
03:35 6. Analyzing Data from Multiple Files How can I do the same operations on many different files?
03:55 7. Making Choices How can my programs do different things based on data values?
04:25 8. 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?
04:55 Finish

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