1
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
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
32 for t in self.times:
33 yield SurfaceTime(self.dir,t)
34
40
42 return time in self.times
43
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
65
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
109 """Extract the name of the line from a filename"""
110 return fName.split("_")[1].split(".")[0]
111
113 """Extracts the names of the contained Values from a filename"""
114 return fName.split("_")[0]
115
116
118 """A directory with one surfaced time"""
119
142
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