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

Source Code for Module PyFoam.RunDictionary.SurfaceDirectory

  1  #  ICE Revision: $Id:$ 
  2  """Working with a directory of surface samples 
  3   
  4  Should be able to generalize this with SampleDirectory, but not right now""" 
  5   
  6  from os import path,listdir 
  7  from glob import glob 
  8  from PyFoam.Error import error 
  9  import math 
 10   
11 -class SurfaceDirectory(object):
12 """A directory of sampled times""" 13
14 - def __init__(self,case,dirName="surfaces"):
15 """@param case: The case directory 16 @param dirName: Name of the directory with the surfaces""" 17 18 self.dir=path.join(case,dirName) 19 self.times=[] 20 21 for d in listdir(self.dir): 22 if path.isdir(path.join(self.dir,d)): 23 try: 24 v=float(d) 25 self.times.append(d) 26 except ValueError,e: 27 pass 28 29 self.times.sort(self.sorttimes)
30
31 - def __iter__(self):
32 for t in self.times: 33 yield SurfaceTime(self.dir,t)
34
35 - def __getitem__(self,time):
36 if time in self: 37 return SurfaceTime(self.dir,time) 38 else: 39 raise KeyError,time
40
41 - def __contains__(self,time):
42 return time in self.times
43
44 - def sorttimes(self,x,y):
45 """Sort function for the solution files""" 46 if(float(x)==float(y)): 47 return 0 48 elif float(x)<float(y): 49 return -1 50 else: 51 return 1
52
53 - def surfaces(self):
54 """Returns all the found surfaces""" 55 56 surfaces=[] 57 58 for t in self: 59 for l in t.surfaces: 60 if l not in surfaces: 61 surfaces.append(l) 62 surfaces.sort() 63 64 return surfaces
65
66 - def values(self):
67 """Returns all the found surface values""" 68 69 values=[] 70 71 for t in self: 72 for v in t.values: 73 if v not in values: 74 values.append(v) 75 values.sort() 76 77 return values
78
79 - def getData(self,surface=None,value=None,time=None):
80 """Get Surface sets 81 @param line: name of the line. All 82 if unspecified 83 @param value: name of the surfaced value. All 84 if unspecified 85 @param time: times for which the surfaces are to be got. All 86 if unspecified""" 87 88 if surface==None: 89 surface=self.surfaces() 90 if value==None: 91 value=self.values() 92 if time==None: 93 time=self.times 94 95 sets=[] 96 97 for t in time: 98 for l in surface: 99 for v in value: 100 try: 101 d=self[t][(l,v)] 102 sets.append(d) 103 except KeyError: 104 pass 105 106 return sets
107
108 -def extractSurface(fName):
109 """Extract the name of the line from a filename""" 110 return fName.split("_")[1].split(".")[0]
111
112 -def extractValue(fName):
113 """Extracts the names of the contained Values from a filename""" 114 return fName.split("_")[0]
115 116
117 -class SurfaceTime(object):
118 """A directory with one surfaced time""" 119
120 - def __init__(self,sDir,time):
121 """@param sDir: The surface-dir 122 @param time: the timename""" 123 124 self.dir=path.join(sDir,time) 125 self.surfaces=[] 126 self.values=[] 127 self.time=time 128 129 for pth in glob(path.join(self.dir,"*.vtk")): 130 f=path.basename(pth) 131 nm=extractSurface(f) 132 val=extractValue(f) 133 if nm not in self.surfaces: 134 self.surfaces.append(nm) 135 if val not in self.values: 136 self.values.append(val) 137 138 self.surfaces.sort() 139 self.values.sort() 140 141 self.cache={}
142
143 - def __getitem__(self,key):
144 """Get the data for a value on a specific line 145 @param key: A tuple with the surface-name and the value-name 146 @returns: a path to the VTK-file""" 147 148 if key in self.cache: 149 return self.cache[key] 150 151 surface,val=key 152 if surface not in self.surfaces or val not in self.values: 153 raise KeyError,key 154 155 fName=None 156 157 for pth in glob(path.join(self.dir,"*.vtk")): 158 f=path.basename(pth) 159 if surface==extractSurface(f) and val==extractValue(f): 160 fName=f 161 break 162 163 if fName==None: 164 error("Can't find a file for the surface",line,"and the value",val,"in the directory",self.dir) 165 166 self.cache[key]=(path.join(self.dir,fName),self.time,surface,val) 167 168 return self.cache[key]
169