annotate python/ubc_utils.py @ 42:1a2ac2d4f53a

added loading of the rest of the data for objects
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 01 Jul 2010 10:44:32 -0400
parents 28e546861263
children 6d11d9e7ad4e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
1 #! /usr/bin/env python
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
2 '''Various utilities to load data saved by the UBC tool(s)'''
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
3
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
4 import utils
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
5 from moving import MovingObject, TimeInterval, Trajectory
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
6
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
7 __metaclass__ = type
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
8
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
9 delimiterChar = '%';
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
10
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
11 def getLines(f):
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
12 '''Gets a complete entry (all the lines) in between delimiterChar.'''
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
13 dataStrings = []
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
14 s = utils.readline(f)
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
15 while (len(s) > 0) and (not s.startswith(delimiterChar)):
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
16 dataStrings += [s.strip()]
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
17 s = utils.readline(f)
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
18 return dataStrings
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
19
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
20 def loadTrajectories(filename, nObjects = -1):
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
21 '''Loads trajectories'''
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
22
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
23 file = utils.openCheck(filename)
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
24 if (not file):
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
25 return []
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
26
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
27 objects = []
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
28 objNum = 0
42
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
29 isObjectFile = filename.endswith('objects.txt');
13
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
30 lines = getLines(file)
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
31 while (lines != []) and ((nObjects<0) or (objNum<nObjects)):
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
32 l = lines[0].split(' ')
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
33 parsedLine = [int(n) for n in l[:4]]
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
34 obj = MovingObject(num = objNum, timeInterval = TimeInterval(parsedLine[1],parsedLine[2]))
42
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
35 #add = True
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
36 if len(lines) >= 3:
25
28e546861263 added Point class and modified trajectory accordingly
Nicolas Saunier <nico@confins.net>
parents: 16
diff changeset
37 obj.positions = Trajectory(lines[1], lines[2])
42
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
38 if len(lines) >= 5:
25
28e546861263 added Point class and modified trajectory accordingly
Nicolas Saunier <nico@confins.net>
parents: 16
diff changeset
39 obj.velocities = Trajectory(lines[3], lines[4])
42
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
40 if (isObjectFile):
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
41 obj.userType = parsedLine[3]
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
42 obj.nObjects = float(l[4])
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
43 obj.featureNumbers = [int(n) for n in l[5:]]
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
44
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
45 # load contour data if available
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
46 if len(lines) >= 6:
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
47 obj.contourType = utils.line2Floats(lines[6])
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
48 obj.contourOrigins = Trajectory(lines[7], lines[8])
1a2ac2d4f53a added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 25
diff changeset
49 obj.contourSizes = Trajectory(lines[9], lines[10])
13
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
50
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
51 if len(lines) != 2:
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
52 objects.append(obj)
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
53 objNum+=1
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
54 else:
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
55 print("Error two lines of data for feature %d"%(f.num))
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
56
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
57 lines = getLines(file)
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
58
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
59 file.close()
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
60 return objects
30559b2cf7a9 minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
61