lab3¶
New topics: file reading, string data extraction
Exercises in this lab:
- mean(xs)and- seq_sqrtexercise the processing of lists
- line_averages(filename)is an exercise in reading data files, and processing the data.
- noaa_temperatureis an exercise in extracting data from a web page (string processing).
Relevant Socratica video:
mean(xs)¶
A function mean(xs) that takes a sequence xs of numbers, and
returns the (arithmetic) mean \(m\) (i.e. the average value).
With xs \(= [ x_1, x_2, ..., x_n ]\), the equation for the mean \(m\)  reads
Example:
In [ ]: mean([0, 1, 2])
Out[ ]: 1.0
seq_sqrt(xs)¶
A function seq_sqrt(xs) which takes a list of non-negative numbers
xs with elements \([x_0, x_1, x_2, \ldots, x_n]\), and returns
the list
\([\sqrt{x_0}, \sqrt{x_1}, \sqrt{x_2}, \ldots, \sqrt{x_n}]\). In
other words, the function takes a list of numbers, and returns a list of
the same length that contains the square root for each number in the
list.
Examples:
In [ ]: seq_sqrt([1, 2, 3])
Out[ ]: [1.0, 1.4142135623730951, 1.7320508075688772]
In [ ]: seq_sqrt([1, 4, 9])
Out[ ]: [1.0, 2.0, 3.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 EDDH for
Hamburg—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="EDDH"):
    """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, EDDH for Hamburg is chosen.
    Examples are:
    - "EDDH" for Hamburg airport (Germany)
    - "KJFK" for John F Kennedy airport (US)
    - "EGHI" for Southampton airport (UK)
    """
    # 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 (including the import urllib.request line).
The library urllib.request allows to access a webpage like a file through
its urlopen() function.
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¶
- Call the function - noaa_string()from the Python prompt and inspect the return value.- Consider to send the output of the function to the print function, for example - print(noaa_string()).
- Your task is to write a function - noaa_temperature(s)which should take a string- sas obtained by- s = noaa_string()as the input argument. The function- noaa_temperatureshould extract the temperature in degree Celsius from the string, and return this temperature as a floating point 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.
Testing suggestions¶
You can test your noaa_temperature() function by extracting the temperature
for other airports. 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
You could also invent your own noaa-compatible string, and pass this string to your function for additional testing.
Please submit your file lab3.py for this assignment.
Additional (voluntary) tasks are available in lab3-extra.
End of lab3.