lab4

New topics: file reading, string data extraction

Exercises in this lab:

  • mean(xs) is a revision of list processing.

  • line_averages(filename) is an exercise in reading data files, and processing the data.

  • noaa_temperature is an exercise in extracting data from a web page (string processing).

Relevant supplementary videos :

Relevant Socratica video:

mean(xs)

A function mean(xs) that takes a sequence xs of numbers, and returns the (arithmetic) mean (i.e. the average value).

Example:

In [ ]: mean([0, 1, 2])
Out[ ]: 1.0

line_averages(filename)

A function line_averages(filename) that takes a string filename which contains the name of a file to be processed. The function should open and read that file. The file is expected to contain numbers that are separated by commas (the format is known as a comma-separated-value file, short csv). The function should compute the average value for every line, and return the average values in a list. For example, if we call line_averages("data.csv") and file data.csv reads:

1,2
1,1,1,1
-1,0,1
42,17

then the function should return the list [1.5, 1.0, 0.0, 29.5].


noaa_temperature(s)

Introduction

The U.S. National Oceanic and Atmospheric Administration (NOAA) provides observations of current weather conditions for airports around the globe. Using the special 4-letter “International Civil Aviation Organization (ICAO) airport code —which is EGHI for Southampton—we can find quantitative information on current weather conditions for Southampton by pointing a web browser to: http://tgftp.nws.noaa.gov/data/observations/metar/decoded/EGHI.TXT

noaa_string()

We provide the following noaa_string() function which downloads the data from http://tgftp.nws.noaa.gov/data/observations/metar/decoded/EGHI.TXT and returns it as a string:

import urllib.request


def noaa_string(location="EGHI"):
    """Fetch from the Internet and return the current NOAA METAR
    weather observation data for `location` as a string.

    `location` needs to be the 4-letter ICAO airport code.

    If no `location` is provided, EGHI for Southampton is chosen.

    Examples are:
    - "EGHI" for Southampton airport (UK, default)
    - "KJFK" for John F Kennedy airport (US)
    - "EDDH" for Hamburg airport (Germany)
    """

    # compose correct URL
    url = (
        "http://tgftp.nws.noaa.gov/data/observations/metar/decoded/"
        + location.upper()
        + ".TXT"
    )

    # read web page from URL as one long string
    noaa_data_string = urllib.request.urlopen(url).read().decode("utf-8")

    # and return
    return noaa_data_string

You should copy the code snippet above into the file in which you implement the noaa_temperature() function (i.e. lab4.py).

The library urllib.request allows to access a webpage like a file through its urlopen() function, and you should include the line import urllib.request at the beginning of your lab4.py file (as shown in the example above).

You do not need to understand the python code in noaa_string(): for now it is sufficient to use the function noaa_string() for this exercise to get the weather data string.

Exercise

  1. Call the function noaa_string() from the Python prompt and inspect the return value.

  2. Your task is to write a function noaa_temperature(s) which should take a string s as obtained by s = noaa_string() as the input argument. The function noaa_temperature should extract the temperature in degree Celsius from the string, and return this temperature as an integer number.

Example

In [ ]: noaa_temperature(noaa_string())
Out[ ]: 10

NOAA may at times change the number of total lines and order of lines in the data, but you can assume that the format of the line containing the temperature data does not change.

Suggestion

You can test your noaa_temperature() function by extracting the temperature for airports other than Southampton. For example, to extract the temperature for the JFK airport use the ICAO airport code KJFK as an argument to noaa_string:

In [ ]: noaa_temperature(noaa_string('KJFK'))
Out[ ]: 7

Please submit your file lab4.py for this assignment.

Additional (voluntary) tasks are available in lab4-extra.

End of lab4.