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: 27 pass 28 29 self.times.sort(key=float)
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 surfaces(self):
45 """Returns all the found surfaces""" 46 47 surfaces=[] 48 49 for t in self: 50 for l in t.surfaces: 51 if l not in surfaces: 52 surfaces.append(l) 53 surfaces.sort() 54 55 return surfaces
56
57 - def values(self):
58 """Returns all the found surface values""" 59 60 values=[] 61 62 for t in self: 63 for v in t.values: 64 if v not in values: 65 values.append(v) 66 values.sort() 67 68 return values
69
70 - def getData(self,surface=None,value=None,time=None):
71 """Get Surface sets 72 @param line: name of the line. All 73 if unspecified 74 @param value: name of the surfaced value. All 75 if unspecified 76 @param time: times for which the surfaces are to be got. All 77 if unspecified""" 78 79 if surface==None: 80 surface=self.surfaces() 81 if value==None: 82 value=list(self.values()) 83 if time==None: 84 time=self.times 85 86 sets=[] 87 88 for t in time: 89 for l in surface: 90 for v in value: 91 try: 92 d=self[t][(l,v)] 93 sets.append(d) 94 except KeyError: 95 pass 96 97 return sets
98
99 -def extractSurface(fName):
100 """Extract the name of the line from a filename""" 101 return fName.split("_")[1].split(".")[0]
102
103 -def extractValue(fName):
104 """Extracts the names of the contained Values from a filename""" 105 return fName.split("_")[0]
106 107
108 -class SurfaceTime(object):
109 """A directory with one surfaced time""" 110
111 - def __init__(self,sDir,time):
112 """@param sDir: The surface-dir 113 @param time: the timename""" 114 115 self.dir=path.join(sDir,time) 116 self.surfaces=[] 117 self.values=[] 118 self.time=time 119 120 for pth in glob(path.join(self.dir,"*.vtk")): 121 f=path.basename(pth) 122 nm=extractSurface(f) 123 val=extractValue(f) 124 if nm not in self.surfaces: 125 self.surfaces.append(nm) 126 if val not in self.values: 127 self.values.append(val) 128 129 self.surfaces.sort() 130 self.values.sort() 131 132 self.cache={}
133
134 - def __getitem__(self,key):
135 """Get the data for a value on a specific line 136 @param key: A tuple with the surface-name and the value-name 137 @returns: a path to the VTK-file""" 138 139 if key in self.cache: 140 return self.cache[key] 141 142 surface,val=key 143 if surface not in self.surfaces or val not in self.values: 144 raise KeyError(key) 145 146 fName=None 147 148 for pth in glob(path.join(self.dir,"*.vtk")): 149 f=path.basename(pth) 150 if surface==extractSurface(f) and val==extractValue(f): 151 fName=f 152 break 153 154 if fName==None: 155 error("Can't find a file for the surface",line,"and the value",val,"in the directory",self.dir) 156 157 self.cache[key]=(path.join(self.dir,fName),self.time,surface,val) 158 159 return self.cache[key]
160 161 # Should work with Python3 and Python2 162