# HG changeset patch # User Nicolas Saunier # Date 1310759372 14400 # Node ID fae55a4c7a5abeb3960de3261d0b82d65ae21cf4 # Parent 04a874e1f19f1d8d2b2c31b80538b1526cdb0b3b added functions to modify and copy subsets of trajectory files diff -r 04a874e1f19f -r fae55a4c7a5a python/ubc_utils.py --- a/python/ubc_utils.py Fri Jul 15 03:05:00 2011 -0400 +++ b/python/ubc_utils.py Fri Jul 15 15:49:32 2011 -0400 @@ -79,6 +79,45 @@ print('read {0} objects'.format(objNum)) +def modifyTrajectoryFile(modifyLines, filenameIn, filenameOut): + '''Reads filenameIn, replaces the first line with the result of modifyLines and writes the result in filenameOut''' + #sortByNum(objects) + fileIn = utils.openCheck(filenameIn, 'r', True) + fileOut = utils.openCheck(filenameOut, "w", True) + + lines = getLines(fileIn) + trajNum = 0 + while (lines != []): + modifiedLines = modifyLines(trajNum, lines) + if modifiedLines: + for l in modifiedLines: + fileOut.write(l+"\n") + fileOut.write(utils.delimiterChar+"\n") + lines = getLines(fileIn) + trajNum += 1 + + fileIn.close() + fileOut.close() + +def copyTrajectoryFile(keepTrajectory, filenameIn, filenameOut): + '''Reads filenameIn, keeps the trajectories for which the function keepTrajectory(trajNum, lines) is True + and writes the result in filenameOut''' + fileIn = utils.openCheck(filenameIn, 'r', True) + fileOut = utils.openCheck(filenameOut, "w", True) + + lines = getLines(fileIn) + trajNum = 0 + while (lines != []): + if keepTrajectory(trajNum, lines): + for l in modifiedLines: + fileOut.write(l+"\n") + fileOut.write(utils.delimiterChar+"\n") + lines = getLines(fileIn) + trajNum += 1 + + fileIn.close() + fileOut.close() + def loadTrajectories(filename, nObjects = -1): '''Loads trajectories'''