Package PyFoam :: Package Basics :: Module OutFileCollection
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.OutFileCollection

 1  #  ICE Revision: $Id: OutFileCollection.py 8292 2007-12-12 15:22:00Z bgschaid $  
 2  """Collections of output files""" 
 3   
 4  from os import path 
 5   
 6  from OutputFile import OutputFile 
 7   
8 -class OutFileCollection(object):
9 """Collection of output files 10 11 The files are stored in a common directory and are created on 12 first access 13 14 Each file can be identified by a unique name. If a file is 15 accessed a second time at the same simulation-time a file with the 16 ending _2 is created (incrementing with each access)""" 17
18 - def __init__(self,basename,titles=[]):
19 """ 20 @param basename: name of the base directory 21 @param titles: names of the data columns 22 """ 23 self.files={} 24 self.lastTime="" 25 self.called={} 26 self.basename=basename 27 self.setTitles(titles)
28 29 # def __del__(self): 30 # print "\n Deleting this OutputFile\n" 31
32 - def setTitles(self,titles):
33 """ 34 Sets the titles anew 35 36 @param titles: the new titles 37 """ 38 self.titles=titles 39 for f in self.files.items(): 40 f.setTitles(titles)
41
42 - def checkTime(self,time):
43 """check whether the time has changed""" 44 if time!=self.lastTime: 45 self.lastTime=time 46 self.called={}
47
48 - def getFile(self,name):
49 """get a OutputFile-object""" 50 if not self.files.has_key(name): 51 fullname=path.join(self.basename,name) 52 self.files[name]=OutputFile(fullname,titles=self.titles) 53 54 return self.files[name]
55
56 - def prevCalls(self,name):
57 """checks whether the name was used previously at that time-step""" 58 if self.called.has_key(name): 59 return self.called[name] 60 else: 61 return 0
62
63 - def incrementCalls(self,name):
64 """increments the access counter for name""" 65 self.called[name]=1+self.prevCalls(name)
66
67 - def write(self,name,time,data):
68 """writes data to file 69 70 name - name of the file 71 time - simulation time 72 data - tuple with the data""" 73 self.checkTime(time) 74 75 fname=name 76 self.incrementCalls(name) 77 78 if self.prevCalls(name)>1: 79 fname+="_"+str(self.prevCalls(name)) 80 81 f=self.getFile(fname) 82 83 f.write(time,data)
84
85 - def close(self):
86 """Force all files to be closed""" 87 88 for f in self.files: 89 self.files[f].close()
90