在CSV fi中导出位置的Paraview脚本

2024-05-19 05:06:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有来自数值模拟的vtk文件,我通常用ParaView可视化。我想在LaTeX文档中绘制一些结果。为此,我喜欢使用CSV文件。好处是Paraview可以将数据导出到CSV文件中。因此,我能够将变量密度的完整时间序列导出到CSV文件序列中。在

但是,我想把这个职位包括在这些CSV文件中。在

我现在可以做的是:

#### import the simple module from the paraview
from paraview.simple import *
import os 
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()


### MY VARIABLES
Folder_output='E:\\My Documents\\VTKfiles'
FileNames_list=[os.path.join(Folder_output, f) for f in os.listdir(Folder_output) if os.path.isfile(os.path.join(Folder_output, f))]
nb_tStep=len(FileNames_list)
Arrays_out_list=[ 'Structured Coordinates:0', 'Structured Coordinates:1' ,' Structured Coordinates:2', 'density',]
CSV_File_Names='E:/My Documents/Results'


for t in range(0,nb_tStep):
    output_LBM_ = LegacyVTKReader(FileNames=FileNames_list[t] )

    ####
    PassArrays1 = PassArrays() 
    PassArrays1.PointDataArrays =  Arrays_out_list
    source = PassArrays1 
    writer = CreateWriter(CSV_File_Names+"{}.csv".format(t), source)
    writer.FieldAssociation = "Points" # or "Cells" 
    writer.UpdatePipeline() 
    del writer 

Tags: 文件csvpathimportoutputosfoldersimple
1条回答
网友
1楼 · 发布于 2024-05-19 05:06:42

有(至少)三个选项可以使用Paraview或/和vtkapi从VTK遗留文件中检索点和变量。在

使用Paraview API

from paraview.simple import *
cylindervtk = LegacyVTKReader(FileNames=['./cylinder.vtk'])
SaveData('./output.csv', proxy=cylindervtk)

并使用pvpython运行它:

^{pr2}$

您将得到一个与此类似的CSV文件:

"density","Points:0","Points:1","Points:2"
1.2,0.5,0.5,0
1.2,0.5,-0.5,0
1.2,0.40451,0.5,-0.29389
1.2,0.40451,-0.5,-0.29389
1.2,0.15451,0.5,-0.47553
1.2,0.15451,-0.5,-0.47553
1.2,-0.15451,0.5,-0.47553
1.2,-0.15451,-0.5,-0.47553
1.2,-0.40451,0.5,-0.29389
1.2,-0.40451,-0.5,-0.29389
1.2,-0.5,0.5,-6.1232e-17
...

稍后可以在脚本(Python或其他任何工具)上使用它来执行一些后处理或绘图操作。在

使用Paraview和vtkapi

虽然第一个选项非常简洁,但您必须将数据写入硬盘,以后可能需要重新加载。如果您只想在一个python脚本中执行后处理或打印操作,那么您将在硬盘上免费写入临时数据。在

下面的代码利用了numpy integration with VTK。它使您能够将坐标和点数据加载到numpy数组中,并在最后将它们导出到CSV文件中(或其他文件):

from paraview.simple import *
from vtk.numpy_interface import dataset_adapter as dsa
import numpy as np

#Paraview reader
pv_reader = LegacyVTKReader(FileNames=['./cylinder.vtk'])

#Fetch the reader data and store them locally into a VTK object
vtk_data = servermanager.Fetch(pv_reader)

#Wrap the vtk_data VTK object to get the coordinates and PointData as numpy arrays
vtk_dataset_adapter = dsa.WrapDataObject(vtk_data)

coords = vtk_dataset_adapter.GetPoints()
density = vtk_dataset_adapter.PointData['density']

data_export = np.column_stack((coords,density))

header = "X Y Z density"
np.savetxt("output2.csv", data_export, header = header)

仅使用VTK API

最后一个与第二个非常相似,但它只使用VTK API:

import vtk
from vtk.numpy_interface import dataset_adapter as dsa
import numpy as np

#PolyDataReader must be modified depending on the type of the Legacy VTK input type
reader = vtk.vtkPolyDataReader()
reader.SetFileName("cylinder.vtk")
reader.ReadAllFieldsOn()
reader.Update()
vtk_data = reader.GetOutput()

vtk_dataset_adapter = dsa.WrapDataObject(vtk_data)

coords = vtk_dataset_adapter.GetPoints()
density = vtk_dataset_adapter.PointData['density']

data_export = np.column_stack((coords,density))

header = "X Y Z density"
np.savetxt("output3.csv", data_export, header = header)

这个解决方案可能很有趣,因为它只将VTK作为依赖项,而且还可以使用Paraview附带的pvpython,因为VTK是Paraview的依赖项。在

相关问题 更多 >

    热门问题