从原始 VTK (.vtu) 文件中读取数据 [英] Reading data from a raw VTK (.vtu) file

查看:90
本文介绍了从原始 VTK (.vtu) 文件中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Python VTK 模块从 .vtu 文件中提取数据数组.文件看起来是这样的(文件末尾的原始数据被省略了):

I want to extract data arrays from a .vtu file using the Python VTK module. The file looks like this (the raw data at the end of the file is ommitted):

<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">
  <UnstructuredGrid>
    <Piece NumberOfPoints="10471" NumberOfCells="64892">
      <PointData>
        <DataArray type="Float64" Name="potential" NumberOfComponents="1" format="appended" offset="0"/>
        <DataArray type="Float64" Name="electric field" NumberOfComponents="3" format="appended" offset="83772"/>
      </PointData>
      <CellData>
        <DataArray type="Int32" Name="GeometryIds" format="appended" offset="335080"/>
      </CellData>
      <Points>
        <DataArray type="Float64" NumberOfComponents="3" format="appended" offset="594652"/>
      </Points>
      <Cells>
        <DataArray type="Int32" Name="connectivity" format="appended" offset="845960"/>
        <DataArray type="Int32" Name="offsets" format="appended" offset="1865068"/>
        <DataArray type="Int32" Name="types" format="appended" offset="2124640"/>
      </Cells>
    </Piece>
  </UnstructuredGrid>
<AppendedData encoding="raw">

我尝试使用以下 python 代码提取数据:

I try to extract the data using the following python code:

import numpy
from vtk import vtkUnstructuredGridReader
from vtk.util import numpy_support as VN

reader = vtkUnstructuredGridReader()
reader.SetFileName("charged_wire.vtu")
reader.ReadAllVectorsOn()
reader.ReadAllScalarsOn()
reader.Update()

data = reader.GetOutput()
potential = data.GetPointData().GetScalars("potential")

print(type(potential))

不幸的是,该程序将 NoneType 打印为输出,我不确定我需要更改什么才能提取 potential 数组中的数据存储?

Unfortunately, this program prints NoneType as output and I'm not really sure what I need to change in order to extract the data stores in the potential array?

推荐答案

您没有使用正确的阅读器,这是一个 .vtu 文件,您必须使用 vtkXMLUnstructuredGridReader.

You are not using the right reader, this is a .vtu file, you have to use the vtkXMLUnstructuredGridReader.

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()
potential = output.GetPointData().GetArray("potential")

这篇关于从原始 VTK (.vtu) 文件中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆