Lab Five
The first thing I did was set up different virtual environments for Python 2 and Python 3 by following the instructions set out on the worksheet. I have got the file to read in the data. But now I am stuck.
##update()
I DID IT. Here's what I've done.
#!/usr/bin/env python3
# MJ22 Lab 5, Exercise 2
# A program to smooth a series of numeric values
import sys
from tools import moving_average
try:
  input_filename = sys.argv[1]
  output_filename = sys.argv[2]
  width = int(sys.argv[3])
except:
  sys.exit('Usage: ./smooth.py <infile> <outfile> <width>')
data = [ ]
#Read data from input file into a list
input_file = open(input_filename, 'rt')
for line in input_file:
  if not line.startswith('#'):
    data.append(float(line))
# Compute a moving average
smoothed_data = moving_average(data, width)
# Write smoothed data to output file
with open(output_filename, 'wt') as output_file:
  for value in data:
     output = format( value, '.3f' )
# Â Â Â Â print (output)
     output_file.write(output + '\n')
output_file.close()
Fantastic. Then I just opened up gnuplot and ran
plot 'elevation.data' with points, 'out.txt' with lines
elevation.data is the provided file, and out.txt is the output file from my program, and this is what we get.
WOW AMAZING. Thanks gnuplot.
The next bit
I'm not going to go into loads of detail here. I wrote 3 different programs, each a bit more complicated than the last. The first was to find the max and min longitude and latitude..:
import csv
longitude = []
lat = []
with open('gpsdata.csv') as input_file:
    input_file.readline()
    for record in csv.reader(input_file):
        longitude.append(float(record[2]))
        lat.append(float(record[1]))
minlong = min(longitude)
maxlong = max(longitude)
minlat = min(lat)
maxlat = max(lat)
print ('The minimum longitude is', minlong,'. The maximum longitude is', maxlong)
print ('The minimum latitude is', minlat,'. The maximum latitude is', maxlat)
Nice.
The next one was very similar, instead of finding the max and min, it calculated the mean (centroid)..:
import csv
longitude = []
lat = []
with open('gpsdata.csv') as input_file:
    input_file.readline()
    for record in csv.reader(input_file):
        longitude.append(float(record[2]))
        lat.append(float(record[1]))
centlong = sum(longitude)/len(longitude)
centlat = sum(lat)/len(lat)
print ('The centroid longitude is', centlong)
print ('The centroid latitude is', centlat)
The last one was a bit more complex. Instead of printing the answers, it had to write them to a file. I also incorporated a bit of code from 'smooth.py' to include command line arguments in the program:
import sys
import csv
try:
  input_filename = sys.argv[1]
  output_filename = sys.argv[2]
except:
  sys.exit('Usage: ./points <infile> <outfile>')
points = []
with open( input_filename, 'rt' ) as input_file:
    input_file.readline()
    with open( output_filename, 'wt' ) as output_file:
        for record in csv.reader(input_file):
           print ( float(record[2]), float(record[1]), file = output_file )
output_file.close()
input_file.close()
I took the output file, and plotted it on gnuplot, here is the result:
WOWEEEEE. We can see the gps data. Where the gps went. Super.











