File: ViewAll.py

package info (click to toggle)
xdmf 3.0%2Bgit20160803-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 35,384 kB
  • sloc: ansic: 265,382; cpp: 162,889; python: 10,976; f90: 1,378; yacc: 687; fortran: 464; xml: 200; java: 187; lex: 125; makefile: 82; sh: 28
file content (179 lines) | stat: -rwxr-xr-x 5,985 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
#/*******************************************************************/
#/*                               XDMF                              */
#/*                   eXtensible Data Model and Format              */
#/*                                                                 */
#/*  Id : $Id: ViewAll.py,v 1.2 2009-01-23 20:48:54 clarke Exp $  */
#/*  Date : $Date: 2009-01-23 20:48:54 $ */
#/*  Version : $Revision: 1.2 $ */
#/*                                                                 */
#/*  Author:                                                        */
#/*     Jerry A. Clarke                                             */
#/*     clarke@arl.army.mil                                         */
#/*     US Army Research Laboratory                                 */
#/*     Aberdeen Proving Ground, MD                                 */
#/*                                                                 */
#/*     Copyright @ 2002 US Army Research Laboratory                */
#/*     All Rights Reserved                                         */
#/*     See Copyright.txt or http://www.arl.hpc.mil/ice for details */
#/*                                                                 */
#/*     This software is distributed WITHOUT ANY WARRANTY; without  */
#/*     even the implied warranty of MERCHANTABILITY or FITNESS     */
#/*     FOR A PARTICULAR PURPOSE.  See the above copyright notice   */
#/*     for more information.                                       */
#/*                                                                 */
#/*******************************************************************/

from __future__ import print_function
import getopt
import sys
import string

print ('Loading Xdmf')
import Xdmf
print ('Loading vtk')
from vtk import *
from libvtkXdmfPython import *

class ViewAll:

	def __init__( self ):
		self.StartGrid = 0
		self.EndGrid = -1
		self.Attributes = []

	def FindGrids( self, FileName ) :
		DOM = Xdmf.XdmfDOM()
		DOM.SetInputFileName( FileName )
		DOM.Parse()
		self.NumberOfGrids = DOM.FindNumberOfElements( 'Grid' )

	def View ( self, FileName ):
		print ('Parsing ' + FileName)

		if( self.EndGrid <= 0 ) :
			self.FindGrids( FileName )
			self.EndGrid = self.NumberOfGrids - 1
		
		Ren = vtkRenderer()
		for GridIndex in range( self.StartGrid, self.EndGrid + 1 ) :
			Reader = vtkXdmfReader()
			# The XML File is Input
			Reader.SetInputFileName( FileName )
			Reader.SetGridIndex( GridIndex )
			# Parse the XML but don't
			# yet read the Heavy Data (HDF5)
			# This is necessary so that vtk knows
			# the topology of the data since it
			# could be structured or unstructured
			Reader.Initialize()
			# Read XDMF Attributes
			Reader.SetAllAttributeStatusOff()
			Index = GridIndex - self.StartGrid
			if Index < len( self.Attributes ) :
				AttrIndex = self.Attributes[ Index ]
				print ('Setting Attribute %d On' % AttrIndex)
				Reader.SetAttributeStatusOn( AttrIndex )
			
	
			GeometryFilter = vtkGeometryFilter()
			GeometryFilter.SetInput( Reader.GetOutput() )
			# Reader.Update() Triggers the Reading of HDF5
			#  this is triggered by the Viz Pipeline
			GeometryFilter.Update()


			# We can access the underlying XdmfGrid
			# if necessary
			Gridptr = Reader.GetXdmfGridHandle()
			Grid = Xdmf.HandleToXdmfGrid( Gridptr )
			print ('Grid %d has %d ' % (GridIndex, Grid.GetNumberOfElements()) + \
				Grid.GetTopologyTypeAsString())
			Nattr = Grid.GetNumberOfAttributes()
			print ('<XdmfGrid> has %d Attributes' % Nattr)
			for i in range( Nattr ) :
				Attribute = Grid.GetAttribute( i )
				print ('\tAttribute #%d' % i)
				print ('\t\tName: ' + Attribute.GetName())
				print ('\t\tCenter: ' + Attribute.GetAttributeCenterAsString())
				print ('\t\tType: ' + Attribute.GetAttributeTypeAsString())
	
			# List All of the Available Arrays
			PointData = Reader.GetOutput().GetPointData()
			for i in range( PointData.GetNumberOfArrays() ) :
				Array = PointData.GetArray( i )
				print ('\tArray #%d ' % i)
				print ('\t\tName: ' + Array.GetName())
				Min, Max = Array.GetRange()
				print ('\t\tRange: %f -> %f' % (Min, Max))
	
	
	


			# Use the Third Attribute (Z Coordinate) for Color
			# GeometryFilter.GetInput().GetPointData().SetActiveScalars('Z Coordinate')

			Mapper = vtkPolyDataMapper()
			Mapper.SetInput( GeometryFilter.GetOutput() )
			ScalarRange = GeometryFilter.GetOutput().GetScalarRange()
			Mapper.SetScalarRange( ScalarRange )
			# Blue to Red
			Mapper.GetLookupTable().SetHueRange( .667, 0.0 )

			Actor = vtkActor()
			Actor.SetMapper( Mapper )
			Actor.GetProperty().SetRepresentationToWireframe()
			Ren.AddActor( Actor )


		Ren.SetBackground(.2, .2, .2)

		RenWin = vtkRenderWindow()
		RenWin.SetSize(500,500)
		RenWin.AddRenderer(Ren)

		iRen = vtkRenderWindowInteractor()
		iRen.SetRenderWindow(RenWin)

		# Interact with the Mouse and Keyboard
		iRen.Initialize()
		iRen.Start()

	def usage (self) :
		print ('Options : --start=Grid# --end=Grid# --attribute=Index --attribute=Index ... File.xmf')
		sys.exit(0)

	def Options( self, opts ) :
		argc = len( opts )
		print ('%d Args = ' % argc + str( opts ))
		try :
			opts, args = getopt.getopt(opts,
					"aseh:",
					["help", "start=", "end=", "attribute=" ])

		except getopt.GetoptError:
			self.usage()
			sys.exit(2)
		print ('opts = ' + str( opts ))
		print ('args = ' + str( args ))
		output = None
		for o, a in opts:
			if o in ("-h", "--help"):
				self.usage()
				sys.exit()
			if o in ("-s", "--start"):
				self.StartGrid = int(a)
			if o in ("-e", "--end"):
				self.EndGrid = int(a)
			if o in ("-a", "--attribute"):
				print ('Appending ' + a)
				self.Attributes.append( int(a) )
		print ('StartGrid %d' % self.StartGrid)
		print ('EndGrid %d' % self.EndGrid)
		print ('Attributes = ' + str( self.Attributes ))
if __name__ == '__main__' :
	argc = len( sys.argv )
	viewer = ViewAll()
	viewer.Options( sys.argv[1:] )
	viewer.View( sys.argv[ argc - 1 ] )