/[escript]/trunk/pyvisi/py_src/datacollector.py
ViewVC logotype

Contents of /trunk/pyvisi/py_src/datacollector.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1001 - (show annotations)
Wed Feb 28 01:15:27 2007 UTC (13 years, 6 months ago) by jongui
File MIME type: text/x-python
File size: 10306 byte(s)
Added diffusion_with_pyvisi.py, but the reading of multiple temporary files (created in the background) appears not to work properly. The first file is always displayed.
1 """
2 @author: John NGUI
3 """
4
5 import vtk
6 import tempfile, os, sys
7 from constant import Source
8 try:
9 import esys.escript
10 except ImportError:
11 print "Warning: importing esys.escript failed."
12
13 class DataCollector:
14 """
15 Class that defines a data collector which dealrs with the source
16 of data for the visualisation.
17 """
18
19 def __init__(self, source = Source.XML):
20 """
21 Initialise the data collector.
22
23 @type source: L{Source <constant.Source>} constant
24 @param source: Source data type
25 """
26
27 self.__source=source
28
29 if(source == Source.XML): # Source is an XML file.
30 self.__vtk_xml_reader = vtk.vtkXMLUnstructuredGridReader()
31 # This is a temporary solution using a file:
32 elif (self.__source == Source.ESCRIPT):
33 self.__vtk_xml_reader = vtk.vtkXMLUnstructuredGridReader()
34 self.__tmp_file=tempfile.mkstemp(suffix=".xml")[1]
35 self.__vtk_xml_reader.SetFileName(self.__tmp_file)
36 self.__output = self.__vtk_xml_reader.GetOutput()
37
38 def __del__(self):
39 """
40 Perform some clean up of the temporary file.
41 """
42
43 if (self.__source == Source.ESCRIPT):
44 if os.access(self.__tmp_file,os.F_OK): os.unlink(self.__tmp_file)
45
46 def setFileName(self, file_name):
47 """
48 Set the source file name to read.
49
50 @type file_name: String
51 @param file_name: Name of the file to read
52 """
53
54 if (self.__source == Source.XML):
55 self.__vtk_xml_reader.SetFileName(file_name)
56
57 # NOTE: Update must be called after SetFileName to make the reader
58 # up to date. Otherwise, some output values may be incorrect.
59 self.__vtk_xml_reader.Update()
60 self.__output = self.__vtk_xml_reader.GetOutput()
61 self.__get_attribute_lists()
62 else:
63 raise ValueError("Source type %s does not support 'setFileName'." \
64 % self.__source)
65
66 def setData(self,**args):
67 """
68 sets the data using. <name>=<data> sets the data tagged by <name>
69 to the object <data>. It is expected that the data are given in
70 an appropriate source type.
71 """
72
73 if self.__source == Source.ESCRIPT:
74 esys.escript.saveVTK(self.__tmp_file,**args)
75 self.__vtk_xml_reader.Update()
76 self.__get_attribute_lists()
77 else:
78 raise ValueError("source type %s does not support 'setData'." \
79 % self.__source)
80
81 def setActiveScalar(self, scalar):
82 """
83 Specify the scalar field to load from the source file.
84
85 @type scalar: String
86 @param scalar: Scalar field to load from the file.
87 """
88
89 # Check whether the specified scalar is available in either point
90 # or cell data. If not available, program exits.
91
92 # NOTE: This check is similar to the check used in _getScalarRange
93 # but is used only when a scalar attribute has been specified.
94 if scalar in self.__point_attribute['scalars']:
95 self._getOutput().GetPointData().SetActiveScalars(scalar)
96 elif scalar in self.__cell_attribute['scalars']:
97 self._getOutput().GetCellData().SetActiveScalars(scalar)
98 else:
99 print "\nSorry, no scalar called '%s' is available.\n" % scalar
100 sys.exit(0)
101
102 def setActiveVector(self, vector):
103 """
104 Specify the vector field to load from the source file.
105
106 @type vector: String
107 @param vector: Vector field to load from the file.
108 """
109
110 # Check whether the specified vector is available in either point
111 # or cell data. If not available, program exits.
112
113 # NOTE: This check is similar to the check used in _getVectorRange
114 # but is used only when a vector attribute has been specified.
115 if vector in self.__point_attribute['vectors']:
116 self._getOutput().GetPointData().SetActiveVectors(vector)
117 elif vector in self.__cell_attribute['vectors']:
118 self._getOutput().GetCellData().SetActiveVectors(vector)
119 else:
120 print "\nSorry, no vector called '%s' is available.\n" % vector
121 sys.exit(0)
122
123
124 def setActiveTensor(self, tensor):
125 """
126 Specify the tensor field to load from the source file.
127
128 @type tensor: String
129 @param tensor: Tensor field to load from the file.
130 """
131
132 # Check whether the specified tensor is available in either point
133 # or cell data. If not available, program exits.
134
135 # NOTE: This check is similar to the check used in _getTensorRange
136 # but is used only when a tensor attribute has been specified.
137 if tensor in self.__point_attribute['tensors']:
138 self._getOutput().GetPointData().SetActiveTensors(tensor)
139 elif tensor in self.__cell_attribute['tensors']:
140 self._getOutput().GetCellData().SetActiveTensors(tensor)
141 else:
142 print "\nSorry, no tensor called '%s' is available.\n" % tensor
143 sys.exit(0)
144
145
146 def __get_array_type(self, arr):
147 """
148 Return if the array type is a scalar, vector or tensor by looking
149 at the number of components in the array.
150
151 @type arr: vtkDataArray
152 @param arr: An array from the source.
153 @rtype: String
154 @return: Array type ('scalar', vector', 'tensor')
155 """
156
157 # Number of components in an array.
158 num_components = arr.GetNumberOfComponents()
159
160 if num_components == 1:
161 return 'scalars'
162 elif num_components == 3:
163 return 'vectors'
164 elif num_components == 9:
165 return 'tensors'
166
167 def __get_attribute_list(self, data):
168 """
169 Return the available scalar, vector and tensor attributes
170 (either point or cell data).
171
172 @type data: vtkPointData or vtkCellData
173 @param data: Available point data or cell data from the source
174 @rtype: Dictionary
175 @return: Dictionary containing the available scalar, vector and \
176 tensor attributes
177 """
178
179 attribute = {'scalars':[], 'vectors':[], 'tensors':[]}
180 if data:
181 num_arrays = data.GetNumberOfArrays() # Number of arrays.
182 for i in range(num_arrays):
183 name = data.GetArrayName(i) # Get an array name.
184 type = self.__get_array_type(data.GetArray(i)) # Get array type.
185 attribute[type].extend([name]) # Add array name to dictionary.
186
187 return attribute
188
189 def __get_attribute_lists(self):
190 """
191 Get all the available point and cell data attributes from the source.
192 """
193
194 # Get all the available point data attributes into a list.
195 self.__point_attribute = \
196 self.__get_attribute_list(self._getOutput().GetPointData())
197 # Get all the available cell data attribute into another list.
198 self.__cell_attribute = \
199 self.__get_attribute_list(self._getOutput().GetCellData())
200
201 def _getScalarRange(self):
202 """
203 Return the scalar range.
204
205 @rtype: Two column tuple containing numbers
206 @return: Scalar range
207 """
208
209 # Check whether any tensor is available in either point or cell data.
210 # If not available, program exits.
211
212 # NOTE: This check is similar to the check used in _setActiveScalar
213 # but is used only when no scalar attribute has been specified.
214 if(len(self.__point_attribute['scalars']) != 0):
215 return self._getOutput().GetPointData().GetScalars().GetRange(-1)
216 elif(len(self.__cell_attribute['scalars']) != 0):
217 return self._getOutput().GetCellData().GetScalars().GetRange(-1)
218 else:
219 print "\nSorry, no scalar is available.\n"
220 sys.exit(0)
221
222 def _getVectorRange(self):
223 """
224 Return the vector range.
225
226 @rtype: Two column tuple containing numbers
227 @return: Vector range
228 """
229
230 # Check whether any vector is available in either point or cell data.
231 # If not available, program exits.
232
233 # NOTE: This check is similar to the check used in _setActiveVector
234 # but is used only when no vector attribute has been specified.
235
236 # NOTE: Generally GetRange(-1) returns the correct vector range.
237 # However, there are certain data sets where GetRange(-1) seems
238 # to return incorrect mimimum vector although the maximum vector is
239 # correct. As a result, the mimimum vector has been hard coded to 0.0
240 # to accommodate those incorrect cases.
241 if(len(self.__point_attribute['vectors']) != 0):
242 vector_range = \
243 self._getOutput().GetPointData().GetVectors().GetRange(-1)
244 return (0.0, vector_range[1])
245 elif(len(self.__cell_attribute['vectors']) != 0):
246 vector_range = \
247 self._getOutput().GetCellData().GetVectors().GetRange(-1)
248 return (0.0, vector_range[1])
249 else:
250 print "\nSorry, no vector is available.\n"
251 sys.exit(0)
252
253 def _getTensorRange(self):
254 """
255 Return the tensor range.
256
257 @rtype: Two column table
258 @return: Tensor range
259 """
260
261 # Check whether any tensor is available in either point or cell data.
262 # If not available, program exits.
263
264 # NOTE: This check is similar to the check used in _setActiveTensor
265 # but is used only when no tensor attribute has been specified.
266 if(len(self.__point_attribute['tensors']) != 0):
267 return self._getOutput().GetPointData().GetTensors().GetRange(-1)
268 elif(len(self.__cell_attribute['tensors']) != 0):
269 return self._getOutput().GetCellData().GetTensors().GetRange(-1)
270 else:
271 print "\nSorry, no tensor is available.\n"
272 sys.exit(0)
273
274 def _getOutput(self):
275 """
276 Return the output of the data collector.
277
278 @rtype: vtkUnstructuredGrid
279 @return: Unstructured grid
280 """
281 #cell_to_point_data = vtk.vtkCellDataToPointData()
282 #cell_to_point_data.SetInput(self.__output)
283 #cell_to_point_data.Update()
284 #return (cell_to_point_data.GetOutput())
285 return self.__output
286
287
288 ###############################################################################
289
290
291 from constant import ImageFormat
292
293 class ImageReader:
294 """
295 Class that defines an image reader.
296 """
297
298 def __init__(self, format):
299 """
300 Initialise the image reader.
301
302 @type format: String
303 @param format: Format of the image
304 """
305
306 self.__format = format
307 self.__vtk_image_reader = self.getImageReader()
308
309 def getImageReader(self):
310 """
311 Return the appropriate image reader based on the supplied image
312 format.
313
314 @rtype: vtkImageReader2 (i.e. vtkJPEGReader, etc)
315 @return: Image reader
316 """
317
318 if(self.__format == ImageFormat.JPG):
319 return vtk.vtkJPEGReader()
320 elif(self.__format == ImageFormat.BMP):
321 return vtk.vtkBMPReader()
322 elif(self.__format == ImageFormat.PNM):
323 return vtk.vtkPNMReader()
324 elif(self.__format == ImageFormat.PNG):
325 return vtk.vtkPNGReader()
326 elif(self.__format == ImageFormat.TIF):
327 return vtk.vtkTIFFReader()
328
329 def setFileName(self, file_name):
330 """
331 Set the image file name.
332
333 @type file_name: String
334 @param file_name: Image file name to be read
335 """
336
337 self.__vtk_image_reader.SetFileName(file_name)
338
339 def _getOutput(self):
340 """
341 Return the output of the image reader.
342
343 @rtype: vtkImageData
344 @return Image data
345 """
346
347 return self.__vtk_image_reader.GetOutput()
348

  ViewVC Help
Powered by ViewVC 1.1.26