/[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 994 - (show annotations)
Fri Feb 23 07:00:54 2007 UTC (12 years, 2 months ago) by jongui
File MIME type: text/x-python
File size: 10274 byte(s)
Modified the way setActiveScalar, setActiveVector and setActiveTensor are specified. It appears that the attributes have to be set each time a new file is read.
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 else:
77 raise ValueError("source type %s does not support 'setData'." \
78 % self.__source)
79
80 def setActiveScalar(self, scalar):
81 """
82 Specify the scalar field to load from the source file.
83
84 @type scalar: String
85 @param scalar: Scalar field to load from the file.
86 """
87
88 # Check whether the specified scalar is available in either point
89 # or cell data. If not available, program exits.
90
91 # NOTE: This check is similar to the check used in _getScalarRange
92 # but is used only when a scalar attribute has been specified.
93 if scalar in self.__point_attribute['scalars']:
94 self._getOutput().GetPointData().SetActiveScalars(scalar)
95 elif scalar in self.__cell_attribute['scalars']:
96 self._getOutput().GetCellData().SetActiveScalars(scalar)
97 else:
98 print "\nSorry, no scalar called '%s' is available.\n" % scalar
99 sys.exit(0)
100
101 def setActiveVector(self, vector):
102 """
103 Specify the vector field to load from the source file.
104
105 @type vector: String
106 @param vector: Vector field to load from the file.
107 """
108
109 # Check whether the specified vector is available in either point
110 # or cell data. If not available, program exits.
111
112 # NOTE: This check is similar to the check used in _getVectorRange
113 # but is used only when a vector attribute has been specified.
114 if vector in self.__point_attribute['vectors']:
115 self._getOutput().GetPointData().SetActiveVectors(vector)
116 elif vector in self.__cell_attribute['vectors']:
117 self._getOutput().GetCellData().SetActiveVectors(vector)
118 else:
119 print "\nSorry, no vector called '%s' is available.\n" % vector
120 sys.exit(0)
121
122
123 def setActiveTensor(self, tensor):
124 """
125 Specify the tensor field to load from the source file.
126
127 @type tensor: String
128 @param tensor: Tensor field to load from the file.
129 """
130
131 # Check whether the specified tensor is available in either point
132 # or cell data. If not available, program exits.
133
134 # NOTE: This check is similar to the check used in _getTensorRange
135 # but is used only when a tensor attribute has been specified.
136 if tensor in self.__point_attribute['tensors']:
137 self._getOutput().GetPointData().SetActiveTensors(tensor)
138 elif tensor in self.__cell_attribute['tensors']:
139 self._getOutput().GetCellData().SetActiveTensors(tensor)
140 else:
141 print "\nSorry, no tensor called '%s' is available.\n" % tensor
142 sys.exit(0)
143
144
145 def __get_array_type(self, arr):
146 """
147 Return if the array type is a scalar, vector or tensor by looking
148 at the number of components in the array.
149
150 @type arr: vtkDataArray
151 @param arr: An array from the source.
152 @rtype: String
153 @return: Array type ('scalar', vector', 'tensor')
154 """
155
156 # Number of components in an array.
157 num_components = arr.GetNumberOfComponents()
158
159 if num_components == 1:
160 return 'scalars'
161 elif num_components == 3:
162 return 'vectors'
163 elif num_components == 9:
164 return 'tensors'
165
166 def __get_attribute_list(self, data):
167 """
168 Return the available scalar, vector and tensor attributes
169 (either point or cell data).
170
171 @type data: vtkPointData or vtkCellData
172 @param data: Available point data or cell data from the source
173 @rtype: Dictionary
174 @return: Dictionary containing the available scalar, vector and \
175 tensor attributes
176 """
177
178 attribute = {'scalars':[], 'vectors':[], 'tensors':[]}
179 if data:
180 num_arrays = data.GetNumberOfArrays() # Number of arrays.
181 for i in range(num_arrays):
182 name = data.GetArrayName(i) # Get an array name.
183 type = self.__get_array_type(data.GetArray(i)) # Get array type.
184 attribute[type].extend([name]) # Add array name to dictionary.
185
186 return attribute
187
188 def __get_attribute_lists(self):
189 """
190 Get all the available point and cell data attributes from the source.
191 """
192
193 # Get all the available point data attributes into a list.
194 self.__point_attribute = \
195 self.__get_attribute_list(self._getOutput().GetPointData())
196 # Get all the available cell data attribute into another list.
197 self.__cell_attribute = \
198 self.__get_attribute_list(self._getOutput().GetCellData())
199
200 def _getScalarRange(self):
201 """
202 Return the scalar range.
203
204 @rtype: Two column tuple containing numbers
205 @return: Scalar range
206 """
207
208 # Check whether any tensor is available in either point or cell data.
209 # If not available, program exits.
210
211 # NOTE: This check is similar to the check used in _setActiveScalar
212 # but is used only when no scalar attribute has been specified.
213 if(len(self.__point_attribute['scalars']) != 0):
214 return self._getOutput().GetPointData().GetScalars().GetRange(-1)
215 elif(len(self.__cell_attribute['scalars']) != 0):
216 return self._getOutput().GetCellData().GetScalars().GetRange(-1)
217 else:
218 print "\nSorry, no scalar is available.\n"
219 sys.exit(0)
220
221 def _getVectorRange(self):
222 """
223 Return the vector range.
224
225 @rtype: Two column tuple containing numbers
226 @return: Vector range
227 """
228
229 # Check whether any vector is available in either point or cell data.
230 # If not available, program exits.
231
232 # NOTE: This check is similar to the check used in _setActiveVector
233 # but is used only when no vector attribute has been specified.
234
235 # NOTE: Generally GetRange(-1) returns the correct vector range.
236 # However, there are certain data sets where GetRange(-1) seems
237 # to return incorrect mimimum vector although the maximum vector is
238 # correct. As a result, the mimimum vector has been hard coded to 0.0
239 # to accommodate those incorrect cases.
240 if(len(self.__point_attribute['vectors']) != 0):
241 vector_range = \
242 self._getOutput().GetPointData().GetVectors().GetRange(-1)
243 return (0.0, vector_range[1])
244 elif(len(self.__cell_attribute['vectors']) != 0):
245 vector_range = \
246 self._getOutput().GetCellData().GetVectors().GetRange(-1)
247 return (0.0, vector_range[1])
248 else:
249 print "\nSorry, no vector is available.\n"
250 sys.exit(0)
251
252 def _getTensorRange(self):
253 """
254 Return the tensor range.
255
256 @rtype: Two column table
257 @return: Tensor range
258 """
259
260 # Check whether any tensor is available in either point or cell data.
261 # If not available, program exits.
262
263 # NOTE: This check is similar to the check used in _setActiveTensor
264 # but is used only when no tensor attribute has been specified.
265 if(len(self.__point_attribute['tensors']) != 0):
266 return self._getOutput().GetPointData().GetTensors().GetRange(-1)
267 elif(len(self.__cell_attribute['tensors']) != 0):
268 return self._getOutput().GetCellData().GetTensors().GetRange(-1)
269 else:
270 print "\nSorry, no tensor is available.\n"
271 sys.exit(0)
272
273 def _getOutput(self):
274 """
275 Return the output of the data collector.
276
277 @rtype: vtkUnstructuredGrid
278 @return: Unstructured grid
279 """
280 #cell_to_point_data = vtk.vtkCellDataToPointData()
281 #cell_to_point_data.SetInput(self.__output)
282 #cell_to_point_data.Update()
283 #return (cell_to_point_data.GetOutput())
284 return self.__output
285
286
287 ###############################################################################
288
289
290 from constant import ImageFormat
291
292 class ImageReader:
293 """
294 Class that defines an image reader.
295 """
296
297 def __init__(self, format):
298 """
299 Initialise the image reader.
300
301 @type format: String
302 @param format: Format of the image
303 """
304
305 self.__format = format
306 self.__vtk_image_reader = self.getImageReader()
307
308 def getImageReader(self):
309 """
310 Return the appropriate image reader based on the supplied image
311 format.
312
313 @rtype: vtkImageReader2 (i.e. vtkJPEGReader, etc)
314 @return: Image reader
315 """
316
317 if(self.__format == ImageFormat.JPG):
318 return vtk.vtkJPEGReader()
319 elif(self.__format == ImageFormat.BMP):
320 return vtk.vtkBMPReader()
321 elif(self.__format == ImageFormat.PNM):
322 return vtk.vtkPNMReader()
323 elif(self.__format == ImageFormat.PNG):
324 return vtk.vtkPNGReader()
325 elif(self.__format == ImageFormat.TIF):
326 return vtk.vtkTIFFReader()
327
328 def setFileName(self, file_name):
329 """
330 Set the image file name.
331
332 @type file_name: String
333 @param file_name: Image file name to be read
334 """
335
336 self.__vtk_image_reader.SetFileName(file_name)
337
338 def _getOutput(self):
339 """
340 Return the output of the image reader.
341
342 @rtype: vtkImageData
343 @return Image data
344 """
345
346 return self.__vtk_image_reader.GetOutput()
347

  ViewVC Help
Powered by ViewVC 1.1.26