Assignment #03: exceptions and testing

Since last week you have learned two important new concepts: unit testing and exceptions handling. In today's assignment we will revisit last week's code in order to properly test it and make it more robust.

In exercise #03-02 we will learn the basics of numpy and matplotlib in order to display live meteorological data.

Note: for these exercises you are allowed to use the numpy and matplotlib libraries only! No external module is allowed (with the exception of the standard library of course).

Exercise #03-01: refactor and test last week's code

  • Write a test suite for both exercises from last week (one test_*.py file per module). The test suite must contain unit tests for each function you programmed (I myself wrote 4 functions for exercise #02-01 and 3 for exercise #02-02).
  • Apply the exception handling methods learned in lesson 09 to the download function(s) of exercise #02-02. Add a test to your test suite to verify that exceptions are raised when an invalid tile is requested for download.

Tutorials: getting started with numpy and matplotlib

numpy and matplotlib are two fundamental pillars of the scientific python stack. You will find numerous tutorials for both libraries online. I am asking you to learn the basics of both tools by yourself, at the pace that suits you. Personally I recommend to follow these two tutorials from the Scipy Lecture Notes for a start:

Exercise #03-02: ACINN meteorological data

The institute website provides live visualization of meteorological data: http://acinn.uibk.ac.at/current-weather/innsbruck-university . The raw data used for the plots are available in a live feed at the following addresses:

The data for the other stations are available, per analogy:

The data is shared by ACINN under a Creative Commons Attribution-ShareAlike 4.0 International License.

The data is provided in the json format, often used for web applications. Fortunately, this is very easy to read in python:

In [1]:
from urllib.request import Request, urlopen
import json

url = 'http://acinn.uibk.ac.at/innsbruck/3'
# Parse the given url
req = urlopen(Request(url)).read()
# Read the data
data = json.loads(req.decode('utf-8'))

Now I will help you to parse the timestamp of the data:

In [2]:
from datetime import datetime, timedelta
data['time'] = [datetime(1970, 1, 1) + timedelta(milliseconds=ds) for ds in data['datumsec']]

And make a first plot to get you started:

In [3]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(data['time'], data['tl']);

Your main task is to write a script that accepts three arguments: station name, variable name, and a number of days (3 or 7, defaults to 3). The variable name might be of: ['temperature', 'dewpoint_temperature', 'relative_humidity', 'windspeed', 'winddirection', 'pressure', 'precipitation', 'sunshine_duration']. The script then displays a plot of the desired variable. The plot must contain the following information: variable name, variable description, unit. If the variable is not available, the script must print a warning and exit.

Here are some hints to get you started:

  • write several functions!
  • preprocess the data: rename the variables using a conversion table between the original variable names and the more human readable name of above. Add the metadata to the data, with at least two attributes: 'description' and 'units'. A possible way to organize your data would be with the help of nested dictionaries.
  • write a function to convert dewpoint and temperature to relative humidity. It must be called only if necessary.
  • write a function to convert temperature and relative humidity to dewpoint temperature. It must be called only if necessary.
  • write tests!

Exercise #03-03: wind

Based on the data from exercise #03-02, write a function that prints the following information in the terminal:

At station XXX, over the last 3 days, the dominant wind direction was XX (xx% of the time). The second most dominant wind direction was XX (xx% of the time), the least dominant wind direction was XX (xx% of the time). The maximum wind speed was XX m/s, while the strongest wind speed averaged over an hour was XX m/s.

With the wind directions being of 8 classes: N, NW, W, SW, S, SE, E, NE.

Back to the table of contents