Python begins!

A good day! Started work on getting up to speed on Python programming for scientific applications. Although I hope to branch into other areas of Python development, my initial focus will be on scientific data processing and visualisation (matplotlib, numpy, scipy) and geospatial processing (postgis, mapnik etc).

The first bit of code developed today is shown below, as are the final graphical outputs.


#!/usr/bin/python
"""
Created on Fri Jul 22 10:10:41 2011

@author: alastairg
"""
# Imports
import os, shutil, csv
import pylab as P
import numpy.numarray as na

# Functions
def printFiles(dirlist, spaceCount, typelist): #formats and prints files ending in ext extension
 for file in dirlist:
 for ext in typelist:
 if file.endswith(ext):
 print "/".rjust(spaceCount+1) + file
 break

def printDirectory(dirEntry, typelist): # formats and prints directories
 print dirEntry[0] + "/"
 printFiles(dirEntry[2], len(dirEntry[0]), typelist)

def plotData(file): # plots timeseries of historical weather data from Oxford
 file.next() # skips header
 for line in csv.reader(file, skipinitialspace = True):
 date.append(line[0] + "_" + line[1]) # joins year and month
 max.append((line[2]).strip("*")) # tidy up data
 min.append((line[3]).strip("*"))
 rain.append((line[4]).strip("*"))

 indexVals = [i * (len(date)/10) for i in range(10)] # creates an evenly spaced index of dates
 myLabels = [date[i] for i in indexVals] # extracts those dates

 P.plot(min, '-b', max, '-r')
 P.title("Oxford: temperatures")
 P.ylabel("Temperature (C)")
 P.legend(('Minimum','Maximum'), loc=2)
 P.xticks(indexVals,myLabels, rotation=60)
 P.show()

 return date, max, min, rain

# Main program
path = "E:/python"
pattern = ".csv"

for directory in os.walk(path):
 printDirectory(directory, pattern)

infile = "E:/python/data/oxford_weather.csv"
outfile = "E:/python/data/copy_oxford_weather.csv"

shutil.copy(infile, outfile) # simple copy method
lineCount = len(open(outfile, 'rU').readlines())
print "File %s has %d lines." % (outfile, lineCount)

date = []
max = []
min = []
rain = []
myTicks = []

f = open(outfile)
date, max, min, rain = plotData(f)
f.close()
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

A WordPress.com Website.

Up ↑

%d bloggers like this: