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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 987 by jongui, Fri Feb 16 07:02:18 2007 UTC revision 988 by jongui, Tue Feb 20 07:21:12 2007 UTC
# Line 3  Line 3 
3  """  """
4    
5  import vtk  import vtk
6  import tempfile, os  import tempfile, os, sys
7  from constant import Source  from constant import Source
8  try:  try:
9    import esys.escript      import esys.escript
10  except ImportError:  except ImportError:
11    print "Warning: importing esys.escript failed."      print "Warning: importing esys.escript failed."
12    
13  class DataCollector:  class DataCollector:
14      """      """
# Line 23  class DataCollector: Line 23  class DataCollector:
23          @type source: L{Source <constant.Source>} constant          @type source: L{Source <constant.Source>} constant
24          @param source: Source data type          @param source: Source data type
25          """          """
26                    
27                  self.__source=source          self.__source=source
28    
29          if(source == Source.XML): # Source is an XML file.          if(source == Source.XML): # Source is an XML file.
30              self.__vtk_xml_reader = vtk.vtkXMLUnstructuredGridReader()              self.__vtk_xml_reader = vtk.vtkXMLUnstructuredGridReader()
31                  # this is a temporary solution using a file:          # This is a temporary solution using a file:
32                  if (source == Source.ESCRIPT):          elif (self.__source == Source.ESCRIPT):
33                           self.__vtk_xml_reader = vtk.vtkXMLUnstructuredGridReader()              self.__vtk_xml_reader = vtk.vtkXMLUnstructuredGridReader()
34                           self.__tmp_file=tempfile.mkstemp(suffix=".xml")[1]              self.__tmp_file=tempfile.mkstemp(suffix=".xml")[1]
35                           self.__vtk_xml_reader.SetFileName(self.__tmp_file)              self.__vtk_xml_reader.SetFileName(self.__tmp_file)
36                           self.__output = self.__vtk_xml_reader.GetOutput()              self.__output = self.__vtk_xml_reader.GetOutput()
37          def __del__(self):  
38                 """      def __del__(self):
39                 clean up          """
40                 """          Perform some clean up of the temporary file.
41                 if (self.__source == Source.ESCRIPT):          """
                    if os.access(self.__tmp_file,os.F_OK): os.unlink(self.__tmp_file)  
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):      def setFileName(self, file_name):
47          """          """
# Line 48  class DataCollector: Line 50  class DataCollector:
50          @type file_name: String          @type file_name: String
51          @param file_name: Name of the file to read          @param file_name: Name of the file to read
52          """          """
                 if self.__source == Source.XML:  
            self.__vtk_xml_reader.SetFileName(file_name)  
53    
54             # NOTE: Update must be called after SetFileName to make the reader          if (self.__source == Source.XML):
55             # up to date. Otherwise, some output values may be incorrect.              self.__vtk_xml_reader.SetFileName(file_name)
56             self.__vtk_xml_reader.Update()  
57             self.__output = self.__vtk_xml_reader.GetOutput()              # NOTE: Update must be called after SetFileName to make the reader
58                  else:              # up to date. Otherwise, some output values may be incorrect.
59                     raise ValueError("source type %s does not support setFileName"%self.__source)              self.__vtk_xml_reader.Update()
60                self.__output = self.__vtk_xml_reader.GetOutput()
61          def setData(self,**args):              self.__get_attribute_lists()
62                  """          else:
63                  sets the data using. <name>=<data> sets the data tagged by <name> to the object <data>. It is expected              raise ValueError("source type %s does not support setFileName"%self.__source)
64                  that the data are given in an appropriate source type.  
65                  """      def setData(self,**args):
66                  if self.__source == Source.ESCRIPT:          """
67                     esys.escript.saveVTK(self.__tmp_file,**args)          sets the data using. <name>=<data> sets the data tagged by <name>
68                     self.__vtk_xml_reader.Update()          to the object <data>. It is expected that the data are given in
69                  else:          an appropriate source type.
70                     raise ValueError("source type %s does not support setData"%self.__source)          """
71    
72            if self.__source == Source.ESCRIPT:
73                esys.escript.saveVTK(self.__tmp_file,**args)
74                self.__vtk_xml_reader.Update()
75            else:
76                raise ValueError("source type %s does not support setData"%self.__source)
77    
78      def _setActiveScalar(self, scalar):      def _setActiveScalar(self, scalar):
79          """          """
# Line 76  class DataCollector: Line 82  class DataCollector:
82          @type scalar: String          @type scalar: String
83          @param scalar: Scalar field to load from the file.          @param scalar: Scalar field to load from the file.
84          """          """
85            
86          self._getOutput().GetPointData().SetActiveScalars(scalar)          if scalar in self.__point_attribute['scalars']:
87                self._getOutput().GetPointData().SetActiveScalars(scalar)
88            elif scalar in self.__cell_attribute['scalars']:
89                self._getOutput().GetCellData().SetActiveScalars(scalar)
90            else:
91                print "\nSorry, no scalar called '%s' is available.\n" % scalar
92                sys.exit(0)
93                
94    
95      def _setActiveVector(self, vector):      def _setActiveVector(self, vector):
96          """          """
# Line 86  class DataCollector: Line 99  class DataCollector:
99          @type vector: String          @type vector: String
100          @param vector: Vector field to load from the file.          @param vector: Vector field to load from the file.
101          """          """
102            
103          self._getOutput().GetPointData().SetActiveVectors(vector)          if vector in self.__point_attribute['vectors']:
104                self._getOutput().GetPointData().SetActiveVectors(vector)
105            elif vector in self.__cell_attribute['vectors']:
106                self._getOutput().GetCellData().SetActiveVectors(vector)
107            else:
108                print "\nSorry, no vector called '%s' is available.\n" % vector
109                sys.exit(0)
110                
111    
112      def _setActiveTensor(self, tensor):      def _setActiveTensor(self, tensor):
113          """          """
# Line 97  class DataCollector: Line 117  class DataCollector:
117          @param tensor: Tensor field to load from the file.          @param tensor: Tensor field to load from the file.
118          """          """
119    
120          self._getOutput().GetPointData().SetActiveTensors(tensor)          if tensor in self.__point_attribute['tensors']:
121                self._getOutput().GetPointData().SetActiveTensors(tensor)
122            elif tensor in self.__cell_attribute['tensors']:
123                self._getOutput().GetCellData().SetActiveTensors(tensor)
124            else:
125                print "\nSorry, no tensor called '%s' is available.\n" % tensor
126                sys.exit(0)
127    
128    
129        def __get_array_type(self, arr):
130            num_components = arr.GetNumberOfComponents()
131    
132            if num_components == 1:
133                return 'scalars'
134            elif num_components == 3:
135                return 'vectors'
136            elif num_components == 9:
137                return 'tensors'
138    
139        def __get_attribute_list(self, data):
140            attribute = {'scalars':[], 'vectors':[], 'tensors':[]}
141            if data:
142                num_arrays = data.GetNumberOfArrays()
143                for i in range(num_arrays):
144                    name = data.GetArrayName(i)
145                    type = self.__get_array_type(data.GetArray(i))
146                    attribute[type].extend([name])
147    
148            return attribute
149    
150        def __get_attribute_lists(self):
151            self.__point_attribute = \
152                    self.__get_attribute_list(self._getOutput().GetPointData())
153            
154            self.__cell_attribute = \
155                    self.__get_attribute_list(self._getOutput().GetCellData())
156    
157      def _getScalarRange(self):      def _getScalarRange(self):
158          """          """
# Line 107  class DataCollector: Line 162  class DataCollector:
162          @return: Scalar range          @return: Scalar range
163          """          """
164            
165          return self._getOutput().GetPointData().GetScalars().GetRange(-1)          if(len(self.__point_attribute['scalars']) != 0):
166                return self._getOutput().GetPointData().GetScalars().GetRange(-1)
167            elif(len(self.__cell_attribute['scalars']) != 0):
168                return self._getOutput().GetCellData().GetScalars().GetRange(-1)
169            else:
170                print "\nSorry, no scalar is available.\n"  
171                sys.exit(0)
172    
173      def _getVectorRange(self):      def _getVectorRange(self):
174          """          """
# Line 116  class DataCollector: Line 177  class DataCollector:
177          @rtype: Two column tuple containing numbers          @rtype: Two column tuple containing numbers
178          @return: Vector range          @return: Vector range
179          """          """
180            
                 print self._getOutput().GetPointData().GetVectors()  
         vector_range = self._getOutput().GetPointData().GetVectors().GetRange(-1)  
181    
182          # NOTE: Generally GetRange(-1) returns the correct vector range.          # NOTE: Generally GetRange(-1) returns the correct vector range.
183          # However, there are certain data sets where GetRange(-1) seems          # However, there are certain data sets where GetRange(-1) seems
184          # to return incorrect mimimum vector although the maximum vector is          # to return incorrect mimimum vector although the maximum vector is
185          # correct. As a result, the mimimum vector has been hard coded to 0.0          # correct. As a result, the mimimum vector has been hard coded to 0.0
186          # to accommodate those incorrect cases.          # to accommodate those incorrect cases.
187            if(len(self.__point_attribute['vectors']) != 0):
188          return (0.0, vector_range[1])              vector_range = \
189                        self._getOutput().GetPointData().GetVectors().GetRange(-1)
190                return (0.0, vector_range[1])
191            elif(len(self.__cell_attribute['vectors']) != 0):
192                vector_range = \
193                        self._getOutput().GetCellData().GetVectors().GetRange(-1)
194                return (0.0, vector_range[1])
195            else:
196                print "\nSorry, no vector is available.\n"  
197                sys.exit(0)
198    
199      def _getTensorRange(self):      def _getTensorRange(self):
200          """          """
# Line 136  class DataCollector: Line 204  class DataCollector:
204          @return: Tensor range          @return: Tensor range
205          """          """
206    
207          return self._getOutput().GetPointData().GetTensors().GetRange(-1)          if(len(self.__point_attribute['tensors']) != 0):
208                return self._getOutput().GetPointData().GetTensors().GetRange(-1)
209            elif(len(self.__cell_attribute['tensors']) != 0):
210                return self._getOutput().GetCellData().GetTensors().GetRange(-1)
211            else:
212                print "\nSorry, no tensor is available.\n"  
213                sys.exit(0)
214    
215      def _getOutput(self):      def _getOutput(self):
216          """          """
# Line 145  class DataCollector: Line 219  class DataCollector:
219          @rtype: vtkUnstructuredGrid          @rtype: vtkUnstructuredGrid
220          @return: Unstructured grid          @return: Unstructured grid
221          """          """
222            #cell_to_point_data = vtk.vtkCellDataToPointData()
223            #cell_to_point_data.SetInput(self.__output)
224            #cell_to_point_data.Update()
225            #return (cell_to_point_data.GetOutput())
226          return self.__output          return self.__output
227    
228            

Legend:
Removed from v.987  
changed lines
  Added in v.988

  ViewVC Help
Powered by ViewVC 1.1.26