Skip to content

Commit 9a2b4bc

Browse files
committed
source files
1 parent 552304b commit 9a2b4bc

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

LICENSE

-1
This file was deleted.

src/mean.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Open data file
2+
infile = open("../data/data1.txt", "r") # ".." means "go up one directory"
3+
4+
# Initialise values
5+
mean = 0
6+
n = 0
7+
8+
# Loop to perform sum
9+
for number in infile:
10+
number = float(number)
11+
mean = mean + number
12+
n += 1
13+
14+
# It is good practice to close a file when you are finished.
15+
infile.close()
16+
17+
# Calculate the mean.
18+
mean = mean/n
19+
print mean

src/tides.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)