Assignment #03

Unit 03


Until next week, study the material provided on Jupter notebooks and solve the following two exercises.

Use the opportunity of solving the exercises in a notebook to familiarize yourself with the workflow of creating and deleting cells, writing markdown, and running code in a notebook. Download the file 03_unit/tasks.ipynb to find a notebook that I prepared for you.

These exercises are taken from Fabien Maussion’s lecture material to the course Introduction to programming

#03-01: Read weather station data


Below, I have pasted some code that reads the contents of the file 03_unit/Stationsliste_20230101.csv line-by-line. (Note, you have to put both the .csv file and this notebook in the same directory, preferably into 03_unit, for the code to work.)

# Open the csv file:
with open('Stationsliste_20230101.csv', encoding='latin-1') as fhand:
    # Initialize a variable that counts the number of lines
    count = 0
    # Iterate through all lines in the csv file
    for line in fhand:
        count += 1
        line = line.rstrip()

        # <your code will go here>
        
        # once you are starting to add your code, you can comment out 
        # the following three lines that produce the current output:
        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 three lists with data: names, years, elevations. The lists contain the data converted to int and floats for the lists years and elevations. Here is the expected output for the first 5 lines and the list years (note that you will have to extract the years from the complete date):

>>> years
[2021, 2004, 2007, 2007, 1989]

The lists should contain 278 elements each.

Tip: The string methods .split() and .replace(), as well as string slicing, help you achieve that goal.

#02-11: Extracting useful information from the Austrian weather station csv file


Now, use the lists you just computed and the function filter_list you created earlier in exercise #02-09, as well as the python function max() and the list method index() to answer the following questions:

  • How many stations are located above 2000m?
  • what is the highest station elevation, and what is the station’s name?