Assignment 04#

Due date: 19.04.2023

This week’s assignment has to be returned in the form of one python script OR one jupyter notebook. Don’t forget the instructions!

Exercise 1: write a list filtering function#

Write a function corresponding to the following signature. After writing the function, running the tests with doctest.testmod() should work!

def filter_list(values, min_value=None, max_value=None):
    """Filters a list of numbers to keep only the ones between one or two thresholds.

    You need to set at least one of min_value or max_value!

    Parameters
    ----------
    values : list
        a list of numbers (floats or ints)
    min_value : float or int, optional
        the minimum threshold to filter the data (inclusive)
    max_value : float or int, optional
        the maximum threshold to filter the data (inclusive)

    Examples
    --------
    >>> a = [1, 3, 4, 2.2]
    >>> filter_list(a, min_value=2)
    [3, 4, 2.2]
    >>> a  # a is unchanged
    [1, 3, 4, 2.2]
    >>> filter_list(a, max_value=3)
    [1, 3, 2.2]
    >>> filter_list(a, min_value=2, max_value=3)
    [3, 2.2]
    >>> filter_list([1, 2, 3, 4])
    Traceback (most recent call last):
     ...
    ValueError: Need to set at least one of min_value or max_value!
    """

    if min_value is None and max_value is None:
        raise ValueError('Need to set at least one of min_value or max_value!')

    output = []
    
    <your own code here>
    
    return output

Exercise 2: read Austrian weather station locations#

Download a text file containing a list of all Austrian weather stations here (Stationsliste_20230101.csv). Put the file in the same folder as your script. If you want, you can see a map of all stations on this website from the ZAMG.

Here is a bit of code showing the first lines of the file:

with open('Stationsliste_20230101.csv', encoding='latin-1') as fhand:
    count = 0
    for line in fhand:
        count += 1
        line = line.rstrip()
        print(line)
        if count > 5:
            break
SYNNR;NAME;BUNDESLAND;LÄNGE;BREITE;STATIONSHÖHE;BEGINNDATUM;ORDNUNG;LÄNGE DEZI;BREITE DEZI
11395;ANDAU ;BGL;170208;474612;117;20210616;TAWES/VAMES;17,0355554;47,77
11193;BAD TATZMANNSDORF ;BGL;161330;472017;347;20040917;Ö3;16,22499847;47,33805466
11197;BERNSTEIN;BGL;161541;472430;631;20070701;TAWES;16,26138878;47,40833282
11394;BRUCKNEUDORF;BGL;165042;480046;166;20071127;TAWES;16,84499931;48,01277924
11190;EISENSTADT-NORDOST;BGL;163218;475115;184;19890101;TAWES/VAMES;16,53833389;47,85416794

Your task is to extend this code to ignore the first line, and then populate five lists with data: names, years, longitudes, latitudes, elevations. The lists contain the data converted to int and floats where appropriate. Here is the expected output for the first 5 lines:

>>> years
[2021, 2004, 2007, 2007, 1989]
>>> latitudes
[47.77, 47.33805466, 47.40833282, 48.01277924, 47.85416794]
>>> names
['ANDAU',  'BAD TATZMANNSDORF', 'BERNSTEIN', 'BRUCKNEUDORF', 'EISENSTADT-NORDOST']

You should have lists of length 278.

Tip

I used the string methods .split() and .replace() in my code, as well as string slicing like in the book.

Exercise 3: Austrian weather station location statistics#

Now, use these lists and the function filter_list you created above as well as the python functions max() and list.index() to answer the following questions:

  • how many stations are located above 2000m?

  • Tirol is located (very) roughly between 10.2°E and 12.6°E. How many stations are there in Tirol? (this is a rough estimate of course)

  • what it the longitude, latitude and elevation of the station 'INNSBRUCK-UNIV.'?

  • what it the highest station elevation, and what is the station’s name?