The following post describes how to use Python to download weather observations which can then be used for further processing.

If we visit the following page: http://www.bom.gov.au/products/IDV60901/IDV60901.94868.shtml

we see:

image

showing the weather observations for Melbourne.

Near the bottom of the page are links to download the data in a number of other formats:

image

If you click on the link http://www.bom.gov.au/fwo/IDV60901/IDV60901.94866.jsn, you can download the data in JSON format. JSON is an elegant data exchange format that is described here: http://www.json.org/

In short, it allows data to contain nested data and the data can either be in the form of key-value pairs or arrays. The following is an example of some JSON data:

    { "books": [
            {"title": "Oliver Twist", "author": "Charles Dickens"},
            {"title": "Hamlet", "author": "William Shakespeare"},
            {"title": "Ulysses", "author": "James Joyce"}
        ]
    }

In the above example, books is an array of values, each item of which is a set of key value pairs.

Suppose, in Python, we wanted to print the author of the second book in the collection, it would look like this:

`print a['books'][1]['author']`

Of course, before we can do that, we need to store the contents of the JSON string in a Python structure. Of course Python comes with the batteries to do that. The following program illustrates how we download the latest weather observations and print out the 30 most recent observations.

'''
Read Melbourne temperature data from
http://www.bom.gov.au/fwo/IDV60901/IDV60901.94866.jsn
and print a graph.
'''

import json
import urllib2


# Source of the data...
url = 'http://www.bom.gov.au/fwo/IDV60901/IDV60901.94866.jsn'

# Grab the data...
data = urllib2.urlopen(url)

# Interpret the data as json data
jdat = json.load(data)

for i in range(30):
    print '%s %s' % ((jdat['observations']['data']['local_date_time_full'][i]),
    (str(jdat['observations']['data']['air_temp'][i])))

Running the program gives us:

20120526150000 14.1
20120526143000 14.0
20120526140000 14.6
20120526133000 14.6
...

Which is what we expected.