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:
27 pass
28
29 self.times.sort(key=float)
30
32 for t in self.times:
33 yield SurfaceTime(self.dir,t)
34
40
42 return time in self.times
43
56
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
100 """Extract the name of the line from a filename"""
101 return fName.split("_")[1].split(".")[0]
102
104 """Extracts the names of the contained Values from a filename"""
105 return fName.split("_")[0]
106
107
109 """A directory with one surfaced time"""
110
133
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
162