comparison python/ubc_utils.py @ 110:fae55a4c7a5a

added functions to modify and copy subsets of trajectory files
author Nicolas Saunier <nico@confins.net>
date Fri, 15 Jul 2011 15:49:32 -0400
parents ded58c424783
children 48e3de4acb65
comparison
equal deleted inserted replaced
109:04a874e1f19f 110:fae55a4c7a5a
76 # next object 76 # next object
77 objNum += 1 77 objNum += 1
78 lines = utils.getLines(infile) 78 lines = utils.getLines(infile)
79 79
80 print('read {0} objects'.format(objNum)) 80 print('read {0} objects'.format(objNum))
81
82 def modifyTrajectoryFile(modifyLines, filenameIn, filenameOut):
83 '''Reads filenameIn, replaces the first line with the result of modifyLines and writes the result in filenameOut'''
84 #sortByNum(objects)
85 fileIn = utils.openCheck(filenameIn, 'r', True)
86 fileOut = utils.openCheck(filenameOut, "w", True)
87
88 lines = getLines(fileIn)
89 trajNum = 0
90 while (lines != []):
91 modifiedLines = modifyLines(trajNum, lines)
92 if modifiedLines:
93 for l in modifiedLines:
94 fileOut.write(l+"\n")
95 fileOut.write(utils.delimiterChar+"\n")
96 lines = getLines(fileIn)
97 trajNum += 1
98
99 fileIn.close()
100 fileOut.close()
101
102 def copyTrajectoryFile(keepTrajectory, filenameIn, filenameOut):
103 '''Reads filenameIn, keeps the trajectories for which the function keepTrajectory(trajNum, lines) is True
104 and writes the result in filenameOut'''
105 fileIn = utils.openCheck(filenameIn, 'r', True)
106 fileOut = utils.openCheck(filenameOut, "w", True)
107
108 lines = getLines(fileIn)
109 trajNum = 0
110 while (lines != []):
111 if keepTrajectory(trajNum, lines):
112 for l in modifiedLines:
113 fileOut.write(l+"\n")
114 fileOut.write(utils.delimiterChar+"\n")
115 lines = getLines(fileIn)
116 trajNum += 1
117
118 fileIn.close()
119 fileOut.close()
81 120
82 def loadTrajectories(filename, nObjects = -1): 121 def loadTrajectories(filename, nObjects = -1):
83 '''Loads trajectories''' 122 '''Loads trajectories'''
84 123
85 file = utils.openCheck(filename) 124 file = utils.openCheck(filename)