使用Python为每个单元格从VTK(vtu)文件提取矢量数据,

时间:2018-12-29 17:47:03

标签: python xml vtk paraview

我有大量(〜200个)VTK(VTU)XML文件,其中包含一些矢量数据作为沿X,Y和Z方向的分量。它采用某些 base64 编码。我正在尝试编写一个简单的python代码,以逐一读取这些VTU(xml)文件并提取矢量信息,并将其以某种形式存储。我是编程的新手,我进行了很多搜索,但找不到与此相关的任何教程或文档。谁能通过建议一种从VTU文件中提取特定Vector信息的方式来帮助我?我的VTU文件如下所示。

    <?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="3830100.0073"         RangeMax="3830100.0073"         offset="0"                   />
    </FieldData>
    <Piece NumberOfPoints="611"                  NumberOfCells="2379"                >
      <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="16484"               />
      </PointData>
      <CellData>
      </CellData>
      <Points>
        <DataArray type="Float32" Name="Points" NumberOfComponents="3" format="appended" RangeMin="1.6616296724e-15"     RangeMax="5.000000259"          offset="16544"               >
          <InformationKey name="L2_NORM_RANGE" location="vtkDataArray" length="2">
            <Value index="0">
              1.6616296724e-15
            </Value>
            <Value index="1">
              5.000000259
            </Value>
          </InformationKey>
        </DataArray>
      </Points>
      <Cells>
        <DataArray type="Int64" Name="connectivity" format="appended" RangeMin=""                     RangeMax=""                     offset="23988"               />
        <DataArray type="Int64" Name="offsets" format="appended" RangeMin=""                     RangeMax=""                     offset="46064"               />
        <DataArray type="UInt8" Name="types" format="appended" RangeMin=""                     RangeMax=""                     offset="50312"               />
      </Cells>
    </Piece>
  </UnstructuredGrid>
  <AppendedData encoding="base64">
   _AQAAAACAAAAIAAAAEAAAAA==eJzT2fGWYZWFryMAECkDQg==AQAAAACAAABIOQAAFTAAAA==eJwtm3k81N/D6TM==eJzj4hoFo2AUjIJRMApGwSgYBWQCABzvXO8=
  </AppendedData>
</VTKFile>

1 个答案:

答案 0 :(得分:1)

packaging-trainer

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# by Panos Mavrogiorgos, email: pmav99 <> gmail

import vtk.vtk

# The source file
file_name = "path/to/your/file.vtu"

# Read the source file.
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName(file_name)
reader.Update()  # Needed because of GetScalarRange
output = reader.GetOutput()
scalar_range = output.GetScalarRange()

# Create the mapper that corresponds the objects of the vtk.vtk file
# into graphics elements
mapper = vtk.vtkDataSetMapper()
mapper.SetInputData(output)
mapper.SetScalarRange(scalar_range)

# Create the Actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Create the Renderer
renderer = vtk.vtkRenderer()
renderer.AddActor(actor)
renderer.SetBackground(1, 1, 1)  # Set background to white

# Create the RendererWindow
renderer_window = vtk.vtkRenderWindow()
renderer_window.AddRenderer(renderer)

# Create the RendererWindowInteractor and display the vtk_file
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renderer_window)
interactor.Initialize()
interactor.Start()