2012-09-28 3 views
2

UNSTRUCTURED POINTS 데이터 세트가 포함 된 vtk 파일이 있습니다. 내부에 여러 데이터 세트 (필드, 전류, 밀도)가 있습니다.ascii vtk 파일을 파이썬으로 읽고 numpy 배열로 변환

이 파일을 파이썬으로로드하고 모든 데이터 세트를 numpy 배열로 변환하여 matplotlib로 플롯 팅하려고합니다. 이 작업을 수행하는 방법?

답변

3

파일의 예가 없어도 정확한 대답을 내리기는 어렵습니다. 하지만 vtk 파일에 관해서는 4 줄 헤더 뒤에 ASCII 또는 이진 데이터를 포함 할 수 있습니다. VTK의 데이터가 다음 ASCII 인 경우

np.loadtxt(filename, skiplines=4) 

작동합니다. 다시 말하지만, 파일의 구조에 따라 여러 필드가 있으면이 작업이 까다로울 수 있습니다. 데이터가 바이너리 인 경우

, 당신은

filename.read() 
struct.unpack() 

또는

np.fromfile() 
0

이 솔루션은 VTK 패키지에서 vtk_to_numpy 기능에 의해 주어진다 같은 것을 사용해야합니다. 그리드 포맷에 따라 Vtk 그리드 리더 (구조화 또는 비 구조화)에 따라 사용됩니다 : vtkXMLUnstructuredGridReader이 좋은 선택입니다.

샘플 코드가 보일 것 같은 :

matplotib 음모를 꾸미고있는 이상 버전이 스레드에서 찾을 수 있습니다
from vtk import * 
from vtk.util.numpy_support import vtk_to_numpy 

# load a vtk file as input 
reader = vtk.vtkXMLUnstructuredGridReader() 
reader.SetFileName("my_input_data.vtk") 
reader.Update() 

#The "Temperature" field is the third scalar in my vtk file 
temperature_vtk_array = reader.GetOutput().GetPointData().GetArray(3) 

#Get the coordinates of the nodes and their temperatures 
nodes_nummpy_array = vtk_to_numpy(nodes_vtk_array) 
temperature_numpy_array = vtk_to_numpy(temperature_vtk_array) 

x,y,z= nodes_nummpy_array[:,0] , 
     nodes_nummpy_array[:,1] , 
     nodes_nummpy_array[:,2] 


(...continue with matplotlib) 

: VTK to Maplotlib using Numpy