简体   繁体   English

用python读取.vtk文件

[英]Reading a .vtk file with python

I've been given a legacy format vtk file (I think its an unstructured grid) and I'd like to read it in with python and output a .npy file instead, since I know how to deal with that. 我已经获得了一个遗留格式的vtk文件(我认为它是一个非结构化的网格),我想用python读取它并输出一个.npy文件,因为我知道如何处理它。

The file is a dump from ATHENA and so has density, velocity, magnetic field along with the coordinates. 该文件是来自ATHENA的转储,因此具有密度,速度,磁场以及坐标。

I'm very much a procedural programmer, so all these objects are confusing... 我是一个程序员程序员,因此所有这些对象都令人困惑......

Here is the solution that I came up with, the trick was turning on ReadAllVectorsOn(). 这是我提出的解决方案,诀窍是启用ReadAllVectorsOn()。

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

reader = vtkStructuredPointsReader()
reader.SetFileName(filename)
reader.ReadAllVectorsOn()
reader.ReadAllScalarsOn()
reader.Update()

data = reader.GetOutput()

dim = data.GetDimensions()
vec = list(dim)
vec = [i-1 for i in dim]
vec.append(3)

u = VN.vtk_to_numpy(data.GetCellData().GetArray('velocity'))
b = VN.vtk_to_numpy(data.GetCellData().GetArray('cell_centered_B'))

u = u.reshape(vec,order='F')
b = b.reshape(vec,order='F')

x = zeros(data.GetNumberOfPoints())
y = zeros(data.GetNumberOfPoints())
z = zeros(data.GetNumberOfPoints())

for i in range(data.GetNumberOfPoints()):
        x[i],y[i],z[i] = data.GetPoint(i)

x = x.reshape(dim,order='F')
y = y.reshape(dim,order='F')
z = z.reshape(dim,order='F')

meshio (a project of mine) knows the VTK format, so you could simply meshio (我的一个项目)知道VTK格式,所以你可以简单地

pip install meshio

and then 然后

import meshio
mesh = meshio.read('file.vtk')
# mesh.points, mesh.cells, ...

Here's a script that reads polygon data into numpy arrays from a VTK file using the VTK Python SDK: 这是一个使用VTK Python SDK从VTK文件中将多边形数据读入numpy数组的脚本:

import sys

import numpy
import vtk

reader = vtk.vtkPolyDataReader()
reader.SetFileName(sys.argv[1])
reader.Update()

polydata = reader.GetOutput()

for i in range(polydata.GetNumberOfCells()):
   pts = polydata.GetCell(i).GetPoints()    
   np_pts = numpy.array([pts.GetPoint(i) for i in range(pts.GetNumberOfPoints())])
   print np_pts

Have you tried using paraview? 你尝试过使用paraview吗? (http://www.paraview.org/) It can give you a visual idea of what is going on behind the scenes and can output the file in a number of different ways. (http://www.paraview.org/)它可以让您直观地了解幕后发生的事情,并可以通过多种不同的方式输出文件。 I would suggest this as I don't have a clue what your data is like. 我建议这样做,因为我不知道你的数据是什么样的。 http://www.vtk.org/Wiki/VTK/Examples/Python may also have an example that may fit the bill for you. http://www.vtk.org/Wiki/VTK/Examples/Python也可能有一个可能适合你的账单的例子。 Personally, I'd have a play with paraview and go from there. 就个人而言,我有一个带有paraview的游戏并从那里开始。

应该提到的是,在其最新版本中,yt项目http://yt-project.org/包含对ATHENA的支持,这意味着无论如何这都是使用python分析模拟数据的方法。

Here's a short snippet for reading points from legacy VTK files: 以下是从传统VTK文件中读取点的简短代码段:

import numpy as np
import vtk

filename = 'model.vtk'

reader = vtk.vtkGenericDataObjectReader()
reader.SetFileName(filename)
reader.Update()

points = np.array( reader.GetOutput().GetPoints().GetData() )

The variable points is an (N,2) or (N,3) array, where N is the number of points. 可变points是(N,2)或(N,3)阵列,其中N是点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM