我已经获得了一个遗留格式的vtk文件(我认为它是一个非结构化的网格),我想用python读取它并输出一个.npy文件,因为我知道如何处理它.
该文件是来自ATHENA的转储,因此具有密度,速度,磁场以及坐标.
我是一个程序员程序员,因此所有这些对象都令人困惑......
Ben*_*kel 18
这是我提出的解决方案,诀窍是启用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')
Run Code Online (Sandbox Code Playgroud)
meshio(我的一个项目)知道VTK格式,所以你可以简单
pip install meshio
Run Code Online (Sandbox Code Playgroud)
然后
import meshio
mesh = meshio.read('file.vtk')
# mesh.points, mesh.cells, ...
Run Code Online (Sandbox Code Playgroud)
这是一个使用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
Run Code Online (Sandbox Code Playgroud)
以下是从旧版 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() )
Run Code Online (Sandbox Code Playgroud)
该变量points是 (N,2) 或 (N,3) 数组,其中 N 是点数。
您尝试过使用paraview吗?(http://www.paraview.org/) 它可以让您直观地了解幕后发生的情况,并可以通过多种不同的方式输出文件。我建议这样做,因为我不知道你的数据是什么样的。http://www.vtk.org/Wiki/VTK/Examples/Python也可能有一个适合您的示例。就我个人而言,我会尝试一下paraview,然后从那里开始。
| 归档时间: |
|
| 查看次数: |
24864 次 |
| 最近记录: |