1
2 """
3 Application class that implements pyFoamSamplePlot.py
4 """
5
6 import sys,string
7
8 from PyFoamApplication import PyFoamApplication
9 from PyFoam.RunDictionary.SampleDirectory import SampleDirectory
10
11 from PyFoam.Error import error
12
26
27 modeChoices=["separate","timesInOne","fieldsInOne","complete"]
28
30 self.parser.add_option("--line",
31 action="append",
32 default=None,
33 dest="line",
34 help="Thesample line from which data is plotted (can be used more than once)")
35 self.parser.add_option("--field",
36 action="append",
37 default=None,
38 dest="field",
39 help="The fields that are plotted (can be used more than once). If none are specified all found fields are used")
40 self.parser.add_option("--time",
41 action="append",
42 default=None,
43 dest="time",
44 help="The times that are plotted (can be used more than once). If none are specified all found times are used")
45 self.parser.add_option("--min-time",
46 action="store",
47 type="float",
48 default=None,
49 dest="minTime",
50 help="The smallest time that should be used")
51 self.parser.add_option("--max-time",
52 action="store",
53 type="float",
54 default=None,
55 dest="maxTime",
56 help="The biggest time that should be used")
57 self.parser.add_option("--mode",
58 type="choice",
59 default="separate",
60 dest="mode",
61 action="store",
62 choices=self.modeChoices,
63 help="What kind of plots are generated: a) separate for every time and field b) all times of a field in one plot c) all fields of a time in one plot d) all lines in one plot. (Names: "+string.join(self.modeChoices,", ")+") Default: %default")
64 self.parser.add_option("--directory-name",
65 action="store",
66 default="samples",
67 dest="dirName",
68 help="Alternate name for the directory with the samples (Default: %default)")
69 self.parser.add_option("--unscaled",
70 action="store_false",
71 dest="scaled",
72 default=True,
73 help="Don't scale a value to the same range for all plots")
74 self.parser.add_option("--info",
75 action="store_true",
76 dest="info",
77 default=False,
78 help="Print info about the sampled data and exit")
79 self.parser.add_option("--style",
80 action="store",
81 default="lines",
82 dest="style",
83 help="Gnuplot-style for the data (Default: %default)")
84
86 samples=SampleDirectory(self.parser.getArgs()[0],dirName=self.opts.dirName)
87
88 lines=samples.lines()
89 times=samples.times
90 values=samples.values()
91
92 if self.opts.info:
93 print "Times : ",samples.times
94 print "Lines : ",samples.lines()
95 print "Values: ",samples.values()
96
97 return 0
98
99 if self.opts.line==None:
100 error("At least one line has to be specified. Found were",samples.lines())
101 else:
102
103 for l in self.opts.line:
104 if l not in lines:
105 error("The line",l,"does not exist in",lines)
106
107 if self.opts.maxTime or self.opts.minTime:
108 if self.opts.time:
109 error("Times",self.opts.time,"and range [",self.opts.minTime,",",self.opts.maxTime,"] set: contradiction")
110 self.opts.time=[]
111 if self.opts.maxTime==None:
112 self.opts.maxTime= 1e20
113 if self.opts.minTime==None:
114 self.opts.minTime=-1e20
115
116 for t in times:
117 if float(t)<=self.opts.maxTime and float(t)>=self.opts.minTime:
118 self.opts.time.append(t)
119
120 if len(self.opts.time)==0:
121 error("No times in range [",self.opts.minTime,",",self.opts.maxTime,"] found: ",times)
122
123 plots=[]
124
125 if self.opts.mode=="separate":
126 if self.opts.time==None:
127 self.opts.time=samples.times
128 if self.opts.field==None:
129 self.opts.field=samples.values()
130 for t in self.opts.time:
131 for f in self.opts.field:
132 plots.append(samples.getData(line=self.opts.line,
133 value=[f],
134 time=[t]))
135 elif self.opts.mode=="timesInOne":
136 if self.opts.field==None:
137 self.opts.field=samples.values()
138 for f in self.opts.field:
139 plots.append(samples.getData(line=self.opts.line,
140 value=[f],
141 time=self.opts.time))
142 elif self.opts.mode=="fieldsInOne":
143 if self.opts.time==None:
144 self.opts.time=samples.times
145 for t in self.opts.time:
146 plots.append(samples.getData(line=self.opts.line,
147 value=self.opts.field,
148 time=[t]))
149 elif self.opts.mode=="complete":
150 plots.append(samples.getData(line=self.opts.line,
151 value=self.opts.field,
152 time=self.opts.time))
153
154 if self.opts.scaled:
155 vRange=None
156 for p in plots:
157 for d in p:
158 mi,ma=d.range()
159 if vRange==None:
160 vRange=mi,ma
161 else:
162 vRange=min(vRange[0],mi),max(vRange[1],ma)
163
164 result="set term png\n"
165
166 for p in plots:
167 if len(p)<1:
168 continue
169
170 name=self.opts.dirName
171 title=None
172 tIndex=times.index(p[0].time())
173
174 name+="_"+string.join(self.opts.line,"_")
175
176 if self.opts.mode=="separate":
177 name+="_%s_%04d" % (p[0].name,tIndex)
178 title="%s at t=%f" % (p[0].name,float(p[0].time()))
179 elif self.opts.mode=="timesInOne":
180 if self.opts.time!=None:
181 name+="_"+string.join(self.opts.time,"_")
182 name+="_%s" % p[0].name
183 title="%s" % p[0].name
184 elif self.opts.mode=="fieldsInOne":
185 if self.opts.field!=None:
186 name+="_"+string.join(self.opts.field,"_")
187 name+="_%04d" % tIndex
188 title="t=%f" % float(p[0].time())
189 elif self.opts.mode=="complete":
190 pass
191
192 name+=".png"
193 result+='set output "%s"\n' % name
194 if title!=None:
195 result+='set title "%s"\n' % title
196
197 result+="plot "
198 if self.opts.scaled:
199 result+="[][%f:%f] " % vRange
200
201 first=True
202
203 for d in p:
204 if first:
205 first=False
206 else:
207 result+=", "
208
209 result+='"%s" using 1:%d ' % (d.file,d.index+1)
210
211 title=None
212 if self.opts.mode=="separate":
213 title=""
214 elif self.opts.mode=="timesInOne":
215 title="t=%f" % float(d.time())
216 elif self.opts.mode=="fieldsInOne":
217 title="%s" % d.name
218 elif self.opts.mode=="complete":
219 title="%s at t=%f" % (d.name,float(d.time()))
220
221 if len(self.opts.line)>1:
222 title+=" on %s" % d.line()
223
224 if title=="":
225 result+="notitle "
226 else:
227 result+='title "%s" ' % title
228
229 result+="with %s " % self.opts.style
230
231 result+="\n"
232
233 print result
234