comparison python/ubc_utils.py @ 13:30559b2cf7a9

minimal code to load UBC data
author Nicolas Saunier <nico@confins.net>
date Fri, 13 Nov 2009 19:29:01 -0500
parents
children 9d6831cfe675
comparison
equal deleted inserted replaced
12:ff5403319cec 13:30559b2cf7a9
1 #! /usr/bin/env python
2 '''Various utilities to load data saved by the UBC tool(s)'''
3
4 import utils
5 from moving import MovingObject, TimeInterval, Trajectory
6
7 __metaclass__ = type
8
9 delimiterChar = '%';
10
11 def getLines(f):
12 '''Gets a complete entry (all the lines) in between delimiterChar.'''
13 dataStrings = []
14 s = utils.readline(f)
15 while (len(s) > 0) and (not s.startswith(delimiterChar)):
16 dataStrings += [s.strip()]
17 s = utils.readline(f)
18 return dataStrings
19
20 def loadTrajectories(filename, nObjects = -1):
21 '''Loads trajectories'''
22
23 file = utils.openCheck(filename)
24 if (not file):
25 return []
26
27 objects = []
28 objNum = 0
29 lines = getLines(file)
30 while (lines != []) and ((nObjects<0) or (objNum<nObjects)):
31 l = lines[0].split(' ')
32 parsedLine = [int(n) for n in l[:4]]
33 obj = MovingObject(num = objNum, timeInterval = TimeInterval(parsedLine[1],parsedLine[2]))
34 add = True
35 if (len(lines) >= 3):
36 obj.positions = Trajectory([[float(n) for n in lines[1].split(' ')],
37 [float(n) for n in lines[2].split(' ')]])
38 if (len(lines) >= 5):
39 obj.velocity = Trajectory([[float(n) for n in lines[3].split(' ')],
40 [float(n) for n in lines[4].split(' ')]])
41
42 if len(lines) != 2:
43 objects.append(obj)
44 objNum+=1
45 else:
46 print("Error two lines of data for feature %d"%(f.num))
47
48 lines = getLines(file)
49
50 file.close()
51 return objects
52