The Python Programming Language

Introduction

Python is a dynamic programming language that offers a readable syntax, integration with other languages and tools, and is relatively easy to learn. We will use Python throughout this course. I use Python for running numerical models, generating dynamic Web pages, manipulating data and files, and for many, many other practical and research applications. See Python Advocacy and references therein for an explanation of how great Python really is.

There are many tutorials where you can learn the basics of programming with Python. A good place to start is the official site Python, which offers, among other things, The Python Tutorial. Even better, perhaps, is an introduction to Python specifically for use in the atmospheric and oceanic sciences by Johnny Lin. A very important reference manual details The Python Standard Library, which describes built-in data types, functions, and exceptions, along with specific modules that you must import at the top of your program. These powerful modules dramatically enhance Python's capabilities. Note that Blizzard has Python version 2.7.13 installed (if you are using the Enthought distribution, which should be the case). Version 3 is incompatible with version 2 in a few ways. Here are the important differences between Python 2.x and Python 3.x. Python 2.x reached end-of-life status in January 2020, so the recommendation for new programmers is to start by learning Python 3.x. However, since I use Python 2.x and that is what is available on Blizzard, I guess you're stuck with it for now. I may create new tutorials that use Python 3.x. For now, just be aware of the differences. The most consequential difference that you may notice is with the syntax of print statements. Lastly, A Byte of Python provides a great introduction to Python for newbies. Learn Python the Hard Way offers some great tutorials with a free trial period. Here is a huge list of Python Tutorials for lots of applications.

While reading tutorials may give a nice overview, my experience is that learning a programming language is best accomplished by jumping right in and modifying an existing program. With a little practice, you'll be writing your own programs from scratch. The following steps will get you started with using Python on Blizzard.

Reading, Analyzing, and Writing Files

Many tasks in programming often involve reading and writing files. Here, you will retrieve, read, and analyze the latest time series of unadjusted monthly average temperature data from the Global Historical Climatology Network-Monthly dataset available from the National Centers for Environmental Information (NCEI) Center for Weather and Climate (formerly NCDC). Read all about the GHCN-M version 3 dataset here and, on that page, click on the link in the "Download" box to visit the FTP directory containing the ASCII text files. I strongly recommend that you read the README file before doing anything else.

You could download the latest time series of unadjusted monthly average temperature data (i.e., ghcnm.tavg.latest.qcu.tar.gz) by copying the link and using wget in your Blizzard terminal, but I have given you some code within a Python script, ghcnm.py, that will do this for you. It will also unzip and untar it and then locate the data. See the lines at the top of the code to see how it works.

Use wget to grab ghcnm.py and then open it with gvim or another suitable text editor and examine the code. (To colorize your code in gvim, and thus make it much easier to read, visit the Customizing GVIM tutorial. It is very likely that I have already customized the colors for you.) Notice that this Python program uses tabs for indentation, unlike other programming languages with which you may have some familiarity. This makes it rather easy to read your programs, especially if you have several nested blocks, and actually forces good programming practice. For some interesting opinions, search Google for discussions of Python's indentation rules.

You can execute your Python programs on Blizzard after changing the file permissions to make them executable: chmod u+x *.py Execute your program with ./ghcnm.py

snow If your Python program executes without a problem, then make some modifications to ghcnm.py. Before doing so, you may want to make a copy of your file: cp ghcnm.py ghcnm_backup.py Carefully read through the comments within ghcnm.py and follow the directions. After setting the variable 'grabit' equal to 1 and providing the FTP address and file name, execution of ghcnm.py should grab a zipped tar file from NCDC and inflate and untar it, producing a new directory. Inside this new directory, you should have two files. One is a list of station names (.inv) and the other contains the data (.dat). Your job is to calculate the following statistics and create the following plots with your modified Python script:

For the Asheville Regional Airport

If you are not sure what a particular statement does, Google it. If that doesn't work out for you, please ask for help. Uncomment some of the print statements to see what you are working with. You may find useful examples online that show you Python's basic operators and how to work with NumPy arrays, draw a variety of plots using matplotlib, or add a legend with multiple axes. Your completed program should produce an HTML report with embedded plots showing the requested information. It will look much like this one, except that I created my report for Gardiner, Maine. Please post your ghcnm.py and ghcnm.html files to your password-protected Web page and send me a link to the page when you are done.

For reference, here is the old assignment from 2009–11.

North Carolina's Public Liberal Arts University