读取.vtu文件并将其转换为numpy数组

2024-03-29 15:24:33 发布

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

我收到了一个.vtu文件,我必须对其进行一些后处理。为此,我尝试读入该文件并提取一个变量,但总是出现以下错误:

 File "/home/tont_fe/anaconda2/lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "/home/tont_fe/anaconda2/lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 102, in execfile
    builtins.execfile(filename, *where)

  File "/home/tont_fe/mnt/data/meshvtu.py", line 28, in <module>
    potential = vtk_to_numpy(output.GetPointData().GetArray('density'))

  File "/home/tont_fe/anaconda2/lib/python2.7/site-packages/vtk/util/numpy_support.py", line 215, in vtk_to_numpy
    typ = vtk_array.GetDataType()

AttributeError: 'NoneType' object has no attribute 'GetDataType'

然后我查看了.vtu本身的结构,得到了以下输出:

  Debug: Off

  Modified Time: 97326

  Reference Count: 2

  Registered Events: (none)

  Information: 0x55f0ba1782b0

  Data Released: False

  Global Release Data: Off

  UpdateTime: 97394

  Field Data:

    Debug: Off

    Modified Time: 96775

    Reference Count: 1

    Registered Events: (none)

    Number Of Arrays: 0

    Number Of Components: 0

    Number Of Tuples: 0

  Number Of Points: 7967753

  Number Of Cells: 9254836

  Cell Data:

    Debug: Off

    Modified Time: 97318

    Reference Count: 1

    Registered Events: 

      Registered Observers:

        vtkObserver (0x55f0bb1778c0)

          Event: 33

          EventName: ModifiedEvent

          Command: 0x55f0bac57630

          Priority: 0

          Tag: 1

    Number Of Arrays: 26

    Array 0 name = density

    Array 1 name = pressure

    Array 2 name = H2 mass frac.

    Array 3 name = O2 mass frac.

    Array 4 name = OH mass frac.

    Array 5 name = H2O mass frac.

    Array 6 name = H mass frac.

    Array 7 name = O mass frac.

    Array 8 name = H2O2 mass frac.

    Array 9 name = HO2 mass frac.

    Array 10 name = Temperature

    Array 11 name = Mach number

    Array 12 name = velocity

    Array 13 name = DensityDim

    Array 14 name = PressureDim

    Array 15 name = TemperatureDim

    Array 16 name = VelocityDim

    Array 17 name = SOSDim

    Array 18 name = Cp

    Array 19 name = h

    Array 20 name = s

    Array 21 name = rSRK

    Array 22 name = CpSRK

    Array 23 name = hSRK

    Array 24 name = sSRK

    Array 25 name = hrr

    Number Of Components: 30

    Number Of Tuples: 9254836

    Copy Tuple Flags: ( 1 1 1 1 1 0 1 1 )

    Interpolate Flags: ( 1 1 1 1 1 0 0 1 )

    Pass Through Flags: ( 1 1 1 1 1 1 1 1 )

    Scalars: 

      Debug: Off

      Modified Time: 97254

      Reference Count: 1

      Registered Events: (none)

      Name: SOSDim

      Data type: double

      Size: 9254836

      MaxId: 9254835

      NumberOfComponents: 1

      Information: 0x55f0bac6dab0

        Debug: Off

        Modified Time: 97253

        Reference Count: 1

        Registered Events: (none)

      Name: SOSDim

      Number Of Components: 1

      Number Of Tuples: 9254836

      Size: 9254836

      MaxId: 9254835

      LookupTable: (none)

    Vectors: 

      Debug: Off

      Modified Time: 97246

      Reference Count: 1

      Registered Events: (none)

      Name: VelocityDim

      Data type: double

      Size: 27764508

      MaxId: 27764507

      NumberOfComponents: 3

      Information: 0x55f0baf05df0

        Debug: Off

        Modified Time: 97245

        Reference Count: 1

        Registered Events: (none)

      Name: VelocityDim

      Number Of Components: 3

      Number Of Tuples: 9254836

      Size: 27764508

      MaxId: 27764507

      LookupTable: (none)

    Normals: (none)

    TCoords: (none)

    Tensors: (none)

    GlobalIds: (none)

    PedigreeIds: (none)

    EdgeFlag: (none)

  Point Data:

    Debug: Off

    Modified Time: 96785

    Reference Count: 1

    Registered Events: 

      Registered Observers:

        vtkObserver (0x55f0b99e3b80)

          Event: 33

          EventName: ModifiedEvent

          Command: 0x55f0bac57630

          Priority: 0

          Tag: 1

    Number Of Arrays: 0

    Number Of Components: 0

    Number Of Tuples: 0

    Copy Tuple Flags: ( 1 1 1 1 1 0 1 1 )

    Interpolate Flags: ( 1 1 1 1 1 0 0 1 )

    Pass Through Flags: ( 1 1 1 1 1 1 1 1 )

    Scalars: (none)

    Vectors: (none)

    Normals: (none)

    TCoords: (none)

    Tensors: (none)

    GlobalIds: (none)

    PedigreeIds: (none)

    EdgeFlag: (none)

  Bounds: 

    Xmin,Xmax: (-118, 318.865)

    Ymin,Ymax: (-100, 176.152)

    Zmin,Zmax: (-25, 25)

  Compute Time: 97406

  Number Of Points: 7967753

  Point Coordinates: 0x55f0b9ee7e40

  Locator: 0

  Number Of Pieces: 1

  Piece: 0

  Ghost Level: 0

我可以做些什么来从阵列和网格本身提取数据吗?我还尝试使用vtk_to_numpy工具,但总是出现同样的错误

下面是我尝试使用的代码:

import numpy as np

from vtk import vtk

import pyvista as pv

import matplotlib.pyplot as plt

import vtk.numpy_interface.dataset_adapter as dsa

from vtk.util import numpy_support

from vtk.util.numpy_support import vtk_to_numpy

import csv


file_name = "myfile.vtu"


reader = vtk.vtkXMLUnstructuredGridReader()

reader.SetFileName(file_name)

reader.Update() 

polydata = reader.GetOutput()

points = polydata.GetPoints()

array = points.GetData()

numpy_nodes = vtk_to_numpy(array)

cells = polydata.GetCells()

array2 = cells.GetData()

numpy_arrays = vtk_to_numpy(array2.GetArray('density'))

Tags: ofnamedebugnumpynonenumbertimecount