changeset 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 04a874e1f19f
children 48e3de4acb65
files python/ubc_utils.py
diffstat 1 files changed, 39 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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'''