Package PyFoam :: Package RunDictionary :: Module SampleDirectory
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.RunDictionary.SampleDirectory

  1  #  ICE Revision: $Id:$ 
  2  """Working with a directory of samples""" 
  3   
  4  from os import path,listdir 
  5  from PyFoam.Error import error 
  6   
7 -class SampleDirectory(object):
8 """A directory of sampled times""" 9
10 - def __init__(self,case,dirName="samples"):
11 """@param case: The case directory 12 @param dirName: Name of the directory with the samples""" 13 14 self.dir=path.join(case,dirName) 15 self.times=[] 16 17 for d in listdir(self.dir): 18 if path.isdir(path.join(self.dir,d)): 19 try: 20 v=float(d) 21 self.times.append(d) 22 except ValueError,e: 23 pass 24 25 self.times.sort(self.sorttimes)
26
27 - def __iter__(self):
28 for t in self.times: 29 yield SampleTime(self.dir,t)
30
31 - def __getitem__(self,time):
32 if time in self: 33 return SampleTime(self.dir,time) 34 else: 35 raise KeyError,time
36
37 - def __contains__(self,time):
38 return time in self.times
39
40 - def sorttimes(self,x,y):
41 """Sort function for the solution files""" 42 if(float(x)==float(y)): 43 return 0 44 elif float(x)<float(y): 45 return -1 46 else: 47 return 1
48
49 - def lines(self):
50 """Returns all the found sample lines""" 51 52 lines=[] 53 54 for t in self: 55 for l in t.lines: 56 if l not in lines: 57 lines.append(l) 58 lines.sort() 59 60 return lines
61
62 - def values(self):
63 """Returns all the found sampled values""" 64 65 values=[] 66 67 for t in self: 68 for v in t.values: 69 if v not in values: 70 values.append(v) 71 values.sort() 72 73 return values
74
75 - def getData(self,line=None,value=None,time=None):
76 """Get Sample sets 77 @param line: name of the line. All 78 if unspecified 79 @param value: name of the sampled value. All 80 if unspecified 81 @param times: times for which the samples are to be got. All 82 if unspecified""" 83 84 if line==None: 85 line=self.lines() 86 if value==None: 87 value=self.values() 88 if time==None: 89 time=self.times 90 91 sets=[] 92 93 for t in time: 94 for l in line: 95 for v in value: 96 try: 97 d=self[t][(l,v)] 98 sets.append(d) 99 except KeyError: 100 pass 101 102 return sets
103
104 -class SampleTime(object):
105 """A directory with one sampled time""" 106
107 - def __init__(self,sDir,time):
108 """@param sDir: The sample-dir 109 @param time: the timename""" 110 111 self.dir=path.join(sDir,time) 112 self.lines=[] 113 self.values=[] 114 115 for f in listdir(self.dir): 116 nm=self.extractLine(f) 117 vals=self.extractValues(f) 118 if nm not in self.lines: 119 self.lines.append(nm) 120 for v in vals: 121 if v not in self.values: 122 self.values.append(v) 123 124 self.lines.sort() 125 self.values.sort() 126 127 self.cache={}
128
129 - def extractLine(self,fName):
130 """Extract the name of the line from a filename""" 131 return fName.split("_")[0]
132
133 - def extractValues(self,fName):
134 """Extracts the names of the contained Values from a filename""" 135 tmp=fName.split("_")[1:] 136 tmp[-1]=tmp[-1].split(".")[0] 137 138 return tmp
139
140 - def __getitem__(self,key):
141 """Get the data for a value on a specific line 142 @param key: A tuple with the line-name and the value-name 143 @returns: A SampleData-object""" 144 145 if key in self.cache: 146 return self.cache[key] 147 148 line,val=key 149 if line not in self.lines or val not in self.values: 150 raise KeyError,key 151 152 fName=None 153 154 for f in listdir(self.dir): 155 if line==self.extractLine(f) and val in self.extractValues(f): 156 fName=f 157 break 158 159 if fName==None: 160 error("Can't find a file for the line",line,"and the value",val,"in the directory",self.dir) 161 162 first=True 163 col0=[] 164 data=[] 165 166 for l in open(path.join(self.dir,fName)).readlines(): 167 tmp=l.split() 168 if first: 169 first=False 170 vector,index=self.determineIndex(fName,val,tmp) 171 172 col0.append(float(tmp[0])) 173 if vector: 174 data.append(tuple(map(float,tmp[index:index+3]))) 175 else: 176 data.append(float(tmp[index])) 177 178 self.cache[key]=SampleData(fName=path.join(self.dir,fName), 179 name=val, 180 index=index, 181 col0=col0, 182 data=data) 183 184 return self.cache[key]
185
186 - def determineIndex(self,fName,vName,data):
187 """Determines the index of the data from the filename and a dataset 188 @param fName: name of the file 189 @param vName: Name of the quantity 190 @param data: A list with the data 191 @returns: A tuple of a boolean (whether the data is supposed to be 192 a vector or a scalar) and an integer (the index of the data set - 193 or the first component of the vector""" 194 195 vals=self.extractValues(fName) 196 if len(vals)+1==len(data): 197 vector=False 198 elif len(vals)*3+1==len(data): 199 vector=True 200 else: 201 error("The data in file",fName,"is neither vector nor scalar:",data) 202 203 index=vals.index(vName) 204 if vector: 205 index=index*3+1 206 else: 207 index=index+1 208 209 return vector,index
210
211 -class SampleData(object):
212 """Data from a sample-set""" 213
214 - def __init__(self,fName,name,index,col0,data):
215 """@param fName: Name of the file 216 @param name: Name of the value 217 @param index: Index of the data in the file 218 @param col0: Values that identify the data (the location) 219 @param data: The actual data""" 220 221 self.file=fName 222 self.col0=col0 223 self.data=data 224 self.name=name 225 self.index=index
226
227 - def __repr__(self):
228 return "SampleData of %s on %s at t=%s " % (self.name,self.line(),self.time())
229
230 - def line(self):
231 """Get the line of the sample""" 232 return path.basename(self.file).split("_")[0]
233
234 - def time(self):
235 """Get the time of the sample (as a string)""" 236 return path.basename(path.dirname(self.file))
237
238 - def isVector(self):
239 """Is this vector or scalar data?""" 240 if type(self.data[0]==tuple): 241 return True 242 else: 243 return False
244
245 - def range(self):
246 """Range of the data""" 247 return (min(self.data),max(self.data))
248
249 - def domain(self):
250 """Range of the data domain""" 251 return (min(self.col0),max(self.col0))
252