brownser
brownser

Reputation: 622

Reading vector information as a multidimensional array from VTU files using 'vtkXMLUnstructuredGridReader' in python

I'm trying to read a vector field information from a VTU file in python using vtkXMLUnstructuredGridReader. The vector field to be read is an array of N*3 dimension, where N is the number of cells and 3 the number of components of the vector. The VTU file looks like this (without the XML data),

<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian" header_type="UInt32" compressor="vtkZLibDataCompressor">
  <UnstructuredGrid>
    <FieldData>
      <DataArray type="Float64" Name="timeInPs" NumberOfTuples="1" format="appended" RangeMin="600"                  RangeMax="600"                  offset="0"                   />
    </FieldData>
    <Piece NumberOfPoints="145705"               NumberOfCells="838547"              >
      <PointData Scalars="Material" Vectors="Magnetization">
        <DataArray type="Float64" Name="Magnetization" NumberOfComponents="3" format="appended" RangeMin="1"                    RangeMax="1"                    offset="48"                  />
        <DataArray type="Int32" Name="Material" format="appended" RangeMin="0"                    RangeMax="0"                    offset="4455172"             />
      </PointData>
      <CellData>
      </CellData>
      <Points>
        <DataArray type="Float32" Name="Points" NumberOfComponents="3" format="appended" RangeMin="1.0415804282e-12"     RangeMax="10.00000052"          offset="4456528"             >
          <InformationKey name="L2_NORM_RANGE" location="vtkDataArray" length="2">
            <Value index="0">
              1.0415804282e-12
            </Value>
            <Value index="1">
              10.00000052
            </Value>
          </InformationKey>
        </DataArray>
      </Points>
      <Cells>
        <DataArray type="Int64" Name="connectivity" format="appended" RangeMin=""                     RangeMax=""                     offset="6589768"             />
        <DataArray type="Int64" Name="offsets" format="appended" RangeMin=""                     RangeMax=""                     offset="20080856"            />
        <DataArray type="UInt8" Name="types" format="appended" RangeMin=""                     RangeMax=""                     offset="21531024"            />
      </Cells>
    </Piece>
  </UnstructuredGrid>
  <AppendedData encoding="base64">


For this I did some online search, even though I could not find a proper documentation on this, I found a thread on Stack here (Reading data from a raw VTK (.vtu) file) I tried using the code provided here

import vtk
import numpy
filname = trial.vtu
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName(filename)
reader.Update() 
output = reader.GetOutput()
potential = output.GetPointData().GetArray("Magnetization")
print potential

But as the output, instead of getting the N*3 array I'm getting the following.

  vtkDoubleArray (0x28567a0)
  Debug: Off
  Modified Time: 389
  Reference Count: 2
  Registered Events: (none)
  Name: Magnetization
  Data type: double
  Size: 24519
  MaxId: 24518
  NumberOfComponents: 3
  Information: 0x1fbab50
    Debug: Off
    Modified Time: 388
    Reference Count: 1
    Registered Events: (none)
  Name: Magnetization
  Number Of Components: 3
  Number Of Tuples: 8173
  Size: 24519
  MaxId: 24518
  LookupTable: (none)

This contains all the information about the field, except the vector components as an N*3 array.

I have two questions,

1) What is missing in the code ?

2) Is there any proper documentation regarding this?

Upvotes: 0

Views: 1226

Answers (1)

Julius Loos
Julius Loos

Reputation: 26

You imported the numpy package and forgot the vtk numpy support package. I posted your sample code and added the missing lines.

import vtk
import numpy as np
from vtk.util.numpy_support import vtk_to_numpy #thats what you need 

filname = trial.vtu
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName(filename)
reader.Update() 
output = reader.GetOutput()

# apply the vtk_to_numpy function to your output. Your output should be a N*3 numpy 
# array.
potential = vtk_to_numpy(output.GetPointData().GetArray("Magnetization"))
print potential

Upvotes: 1

Related Questions