英文:
Read TimeValue from pvts file with python
问题
我有一个pvts文件,我正试图用Python来读取它。读取点和单元数据没问题,但我也想提取时间值。为此,我已经添加了以下内容到pvts文件的末尾(在<\PStructuredGrid>
之前):
<FieldData>
<DataArray type="Float64" Name="TimeValue" NumberOfTuples="1">49.000000
</DataArray>
</FieldData>
这对Paraview来说没问题,但我无法使用Python的vtk库来提取值。根据我迄今为止尝试的内容,我认为我应该从vtkXMLPStructuredGridReader
对象中提取,而不是基于vtkStructuredGrid
。
reader = vtkXMLPStructuredGridReader()
reader.SetFileName(file)
reader.Update()
print(reader) # 有一行显示“ActiveTimeDataArrayName:TimeValue”
print(reader.GetTimeDataArray(0)) # 打印“TimeValue”
这显示它确实看到了我的pvts中的“TimeValue”条目。
另一方面,它说我没有FieldData。我认为这是因为我没有将TimeValue添加到底层的vts文件中。也许我应该这样做,但Paraview可以使用pvts中的TimeValue,所以我认为它也应该通过Python工作。只为明确:我想从文件中提取49.000000
。
pvts文件的大致结构如下:
<?xml version="1.0"?>
<VTKFile type="PStructuredGrid" version="0.1" byte_order="LittleEndian">
<PStructuredGrid WholeExtent=". . . . . ." GhostLevel=".">
<PPoints>
<PDataArray type="Float64" Name="coordinates" NumberOfComponents="3" format="appended" offset="0">
</PDataArray>
</PPoints>
<PPointData Scalars="scalars">
<PDataArray type="Float64" Name="." NumberOfComponents="1" format="appended" offset="0">
</PDataArray>
<PDataArray type="Float64" Name="." NumberOfComponents="1" format="appended" offset="0">
</PDataArray>
</PPointData>
<Piece Extent=". . . . . ." Source="<n>/field_<n>_0.vts"/>
<Piece Extent=". . . . . ." Source="<n>/field_<n>_1.vts"/>
<FieldData>
<DataArray type="Float64" Name="TimeValue" NumberOfTuples="1">49.000000
</DataArray>
</FieldData>
</PStructuredGrid>
</VTKFile>
我也可以接受使用XML解析器的解决方案。我尝试过研究这个问题,但乍一看似乎也不太容易。
英文:
I have a pvts file that I'm trying to read with Python. Reading the point and cell data works fine, but I also want to extract the time value. For that, I have added
<FieldData>
<DataArray type="Float64" Name="TimeValue" NumberOfTuples="1">49.000000
</DataArray>
</FieldData>
To the end of the pvts file (before <\PStructuredGrid>
)
This works fine for Paraview, but I'm unable to extract the value using the python vtk library. I think I should extract from the vtkXMLPStructuredGridReader
object and not the vtkStructuredGrid
based on what I have tried so far.
reader = vtkXMLPStructuredGridReader()
reader.SetFileName(file)
reader.Update()
print(reader) # Has a line stating "ActiveTimeDataArrayName:TimeValue"
print(reader.GetTimeDataArray(0)) # Prints "TimeValue"
It shows me that it does see the "TimeValue" entry in my pvts.
data = reader.GetOutput()
dim = data.GetDimensions()
print(data)
On the other hand, says that I have no FieldData. I think this is because I haven't added the TimeValue to the underlying vts files. Maybe I should, but paraview can use the one in the pvts, so I think it should also work via python. Just to be clear: I want to extract the 49.000000
from the file.
Rough structure pvts file:
<?xml version="1.0"?>
<VTKFile type="PStructuredGrid" version="0.1" byte_order="LittleEndian">
<PStructuredGrid WholeExtent=". . . . . ." GhostLevel=".">
<PPoints>
<PDataArray type="Float64" Name="coordinates" NumberOfComponents="3" format="appended" offset="0">
</PDataArray>
</PPoints>
<PPointData Scalars="scalars">
<PDataArray type="Float64" Name="." NumberOfComponents="1" format="appended" offset="0">
</PDataArray>
<PDataArray type="Float64" Name="." NumberOfComponents="1" format="appended" offset="0">
</PDataArray>
</PPointData>
<Piece Extent=". . . . . ." Source="<n>/field_<n>_0.vts"/>
<Piece Extent=". . . . . ." Source="<n>/field_<n>_1.vts"/>
<FieldData>
<DataArray type="Float64" Name="TimeValue" NumberOfTuples="1">49.000000
</DataArray>
</FieldData>
</PStructuredGrid>
</VTKFile>
I'm also fine with a solution using an XML parser. I tried to look into that, but that also didn't look too easy at first glance.
答案1
得分: 1
如果您的XML文件格式良好,可以使用iterparse()进行搜索:
import xml.etree.ElementTree as ET
for event, elem in ET.iterparse(file, events=('end',)):
if event == 'end' and elem.tag == 'DataArray':
print(elem.text)
英文:
If you xml file is well-formatted you can search with iterparse():
import xml.etree.ElementTree as ET
for event, elem in ET.iterparse(file, events=('end',)):
if event == 'end' and elem.tag =='DataArray':
print(elem.text)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论