|
| 1 | +import pylab |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +tide_file = open("../data/2012AVO.txt", "r") |
| 5 | + |
| 6 | +# We know from inspecting the file that the first 11 lines are just |
| 7 | +# header information so lets just skip those lines. |
| 8 | +for i in range(11): |
| 9 | + line = tide_file.readline() |
| 10 | + |
| 11 | +# Initialise an empty list to store the elevation |
| 12 | +elevation = [] |
| 13 | +days = [] |
| 14 | + |
| 15 | +# Now we start reading the interesting data |
| 16 | +n = 0 |
| 17 | +while True: # This will keep looping until we break out. |
| 18 | + # Here we use a try/except block to try to read the data as normal |
| 19 | + # and to break out if unsuccessful - ie when we reach the end of the file. |
| 20 | + try: |
| 21 | + # Read the next line |
| 22 | + line = tide_file.readline() |
| 23 | + |
| 24 | + # Split this line into words. |
| 25 | + words = line.split() |
| 26 | + |
| 27 | + # If we do not have 5 words then it must be blank lines at the end of the file. |
| 28 | + if len(words) != 5: |
| 29 | + break |
| 30 | + except: |
| 31 | + # If we failed to read a line then we must have got to the end. |
| 32 | + break |
| 33 | + |
| 34 | + n += 1 # Count number of data points |
| 35 | + |
| 36 | + try: |
| 37 | + # The elevation data is on the 4th column. However, the BODC |
| 38 | + # appends a "M" when a value is improbable and an "N" when |
| 39 | + # data is missing (maybe a ship dumped into it during rough weather!) |
| 40 | + # Therefore, we put this conversion from a string into a float in a |
| 41 | + # try/except block. |
| 42 | + level = float(words[3]) |
| 43 | + elevation.append(level) |
| 44 | + |
| 45 | + # There is a measurement every quarter hour. |
| 46 | + days.append(n*0.25/24) |
| 47 | + except: |
| 48 | + continue |
| 49 | + |
| 50 | +# For plotting lets convert the list to a NumPy array. |
| 51 | +elevation = np.array(elevation) |
| 52 | +days = np.array(days) |
| 53 | + |
| 54 | +pylab.plot(days, elevation) |
| 55 | +pylab.xlabel("Days") |
| 56 | +pylab.ylabel("Elevation (meters)") |
| 57 | +pylab.show() |
0 commit comments