在 Python 中保存来自 VTK 的数据数组 [英] Save an array of data from a VTK in Python

查看:44
本文介绍了在 Python 中保存来自 VTK 的数据数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

如何在 Python 中从 VTK 中提取数据集数组并将其保存在新文件中?例如对于具有位移电流量级数据集的VTK,仅提取位移em> 并将其保存在一个较小的文件中.

How do I extract a dataset array from a VTK in Python and save it in a new file? e.g. for a VTK with data sets for magnitudes force, displacement and current extract only displacement and save it in a smaller file.

问题:

我在远程服务器中有数百个 4GB VTK 文件,我想提取为不同量级生成的多个数据集之一.在这些数据集中,我有标量和向量.

I have hundreds of 4GB VTK files in a remote server and I want to extract one of the several data sets that are generated for different magnitudes. In these data sets I have Scalars and Vectors.

我编写了以下 VTK Python 代码,在其中读取非结构化网格,然后使用vtkArrayWriter"获取第二个数据数组以保存它.

I wrote the following VTK Python code where I read the unstructured grid and I get the second array of data to save it later using a "vtkArrayWriter".

import vtk
Filename = 'file.vtk'
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(Filename)
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
reader.Update()
obj = reader.GetOutput().GetPointData().GetArray(1)
writer = vtk.vtkArrayWriter()
writer.SetInputData(obj)
writer.SetFileName('test.vtk')
writer.Update()

代码给了我以下输出:

TypeError: SetInputData argument 1: method requires a vtkDataObject, a提供了 vtkFloatArray.

TypeError: SetInputData argument 1: method requires a vtkDataObject, a vtkFloatArray was provided.

我没有设法将vtkFloatArray"转换为vtkDataObject",也没有找到支持vtkFloatArray"作为输入的特定方法.我没有找到很多相关的代码,但可能是我在谷歌上搜索了错误的关键字.在这一点上,我被卡住了.

I did not manage to cast "vtkFloatArray" to "vtkDataObject" or to find an specific method that supports "vtkFloatArray" as input. I did not find many related codes but may be I googled the wrong keywords. At this point I got stuck.

注意:

这与通过在 Paraview 中应用过滤器PassArays"然后保存可以实现的过程相同,但鉴于我的问题的大小和特征,这不是一个可行的解决方案.

This is the same procedure that can be achieved by applying the filter "PassArays" in Paraview and then saving but, given the size and characteristics of my problem, this is not a feasible solution.

推荐答案

VTK 有一个 vtkPassArrays 过滤器,其文档为 此处.您需要知道输出文件中所需数组的名称以及该数组是否为点/单元/场数据.

VTK has a vtkPassArrays filter whose documentation is here. You need to know the name of the array that you want in the output file and whether the array is a Point/Cell/Field Data.

import vtk as v
Filename = 'file.vtk'
reader = v.vtkUnstructuredGridReader()
reader.SetFileName(Filename)
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
pa = v.vtkPassArrays()
pa.SetInputConnection(reader.GetOutputPort())
pa.AddArray( 0, 'Array1Name' ) # 0 for PointData, 1 for CellData, 2 for FieldData
writer = v.vtkDataSetWriter()
writer.SetFileName('test.vtk')
writer.SetInputConnection(pa.GetOutputPort())
writer.Update()
writer.Write()

输出文件 test.vtk 将包含所有点和单元格,但仅包含您指定的数组名称.如果您只想要数组而不是点或单元格,则可以使用 VTK NumPy 集成numpy.savetxt.

The output file test.vtk will contain all the points and the cells but only the array name you specify will be included. If instead you only want the array and not the points or the cells then the following solution is possible using VTK NumPy integration and numpy.savetxt.

import numpy as np
import vtk as v
from vtk.numpy_interface import dataset_adapter as dsa
reader = v.vtkUnstructuredGridReader()
reader.SetFileName(Filename)
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
reader.Update()
usg = dsa.WrapDataObject( reader.GetOutput() )
array1 = usg.PointData['Array1Name'] # Assuming you know the name of the array
# array1 is a child class type of numpy.ndarray type
np.savetxt('array1.dat', array1, fmt='%4.5f' )

这篇关于在 Python 中保存来自 VTK 的数据数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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