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

Extracting vector data from a VTK(vtu) file using Python for each cell,

我有大量 (~200) 个 VTK(VTU) XML 文件,其中包含一些矢量数据作为沿 X、Y 和 Z 方向的分量。它采用某种 base64 编码。我正在尝试编写一个简单的 python 代码来一个一个地读取这些 VTU(xml) 文件并提取矢量信息,并以某种形式存储它。我是编程新手,我搜索了很多但找不到与此相关的任何教程或文档。任何人都可以通过建议一种从 VTU 文件中提取特定矢量信息的方法来帮助我吗?我的 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>

来自VTKExamples

#!/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()