Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于在CSV文件中导出位置的Paraview脚本_Python_Csv_Paraview - Fatal编程技术网

Python 用于在CSV文件中导出位置的Paraview脚本

Python 用于在CSV文件中导出位置的Paraview脚本,python,csv,paraview,Python,Csv,Paraview,我有来自数值模拟的vtk文件,我通常使用ParaView可视化这些文件。我想在LaTeX文档中绘制一些结果。为此,我喜欢使用CSV文件。好消息是Paraview可以将数据导出到CSV文件中。因此,我能够将可变密度的完整时间序列导出为CSV文件序列 但是,我想把这个职位包括在这些CSV文件中 以下是我现在可以做的: #### import the simple module from the paraview from paraview.simple import * import os ###

我有来自数值模拟的
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 
使用Paraview或/和vtkapi从VTK遗留文件中检索点和变量(至少)有三个选项

使用Paraview API 并使用
pvpython
运行它:

pvpython vtk_output_csv.py
您将获得一个与此类似的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阵列中,并在最后将其导出到CSV文件(或其他任何文件):

仅使用vtkapi 最后一个与第二个非常相似,但它仅使用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的依赖项。

您当前的代码有什么收获吗?我认为这是不完整的,因为您的数据存储在output_LBM_uu中,并且似乎没有作为源传递给CSV编写器。是的,我得到了一个带有“density”变量的CSV文件。操作
passarray()。因此,它被正确地作为源代码传递给CSV编写器。使用选项1,我得到一个CSV文件,其中包含“密度”列以及一些我不想要的其他变量,并且没有点的坐标!否则,使用选项2和3,当我调用
coords=vtk\u dataset\u adapter.GetPoints()
,我会得到错误消息:
AttributeError:'vtkCommonDataModelPython.vtkImageData'对象没有属性“GetPoints”
。这可能与我的vtk文件使用结构化数据集有关。有没有办法改变所有解决方案的这些不必要的影响(额外变量和无坐标)?选项2和3对我来说很好,只需稍加修改:
coords=np.zero((vtk_数据集_适配器.GetNumberOfPoints(),3))对于范围内的i(vtk_数据集_适配器.GetNumberOfPoints()):coords[i,0],coords[i,1],coords[i,2]=vtk\u数据集\u适配器.GetPoint(i)
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)
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)