Hi all,
I’m trying to write a time series of vtkUnstructuredGrid data to a single VTKHDF (.vtkhdf) file using Python. Specifically, the grid topology is constant, but a point data array named “foo” changes over 100 timesteps.
I’ve found several resources that explain how to construct the VTKHDF file format from scratch using h5py, such as:
- Trying to make a temporal vtkhdf polydata using Python
- https://gitlab.kitware.com/keu-public/vtkhdf/vtkhdf-scripts/-/blob/main/scripts/generate_poly_data.py
- https://www.kitware.com/how-to-write-time-dependent-data-in-vtkhdf-files/
However, I’m wondering if there’s a more direct way to do this using vtkHDFWriter. It seems like vtkHDFWriter should be able to handle this, which would be much simpler than manually creating the HDF5 structure.
My question is: How can I use vtkHDFWriter to save an UnstructuredGrid with 100 timesteps of a “foo” point data array into a single .vtkhdf file?
Here’s a conceptual code snippet of what I’m trying to do:
import numpy as np
from vtkmodules.util import numpy_support
from vtkmodules.vtkCommonDataModel import vtkUnstructuredGrid
from vtkmodules.vtkIOHDF import vtkHDFWriter
# 1. Create a sample UnstructuredGrid
grid = vtkUnstructuredGrid()
# make some structure
# 2. Prepare for time series data
num_timesteps = 100
# Dummy data: 3 points, so we need 3 values at each timestep
foo_data_over_time = [
np.random.rand(grid.number_of_cells) for _ in range(num_timesteps)
]
# 3. This is where I'm unsure how to proceed
writer = vtkHDFWriter()
writer.SetFileName("temporal_ug.vtkhdf")
writer.SetInputData(grid)
# How do I add the time-varying data?
# Do I add all arrays to the grid and give them special names?
# Or do I call writer.Write() in a loop?
# The writer has Write() and AddInputDataObject(), but it's not clear how to use them for time series.
# I would expect something like this, but this is just a guess:
# writer.Start() # is there a method like this?
# for i in range(num_timesteps):
# foo_array = numpy_support.numpy_to_vtk(foo_data_over_time[i])
# foo_array.SetName("foo")
# grid.GetPointData().SetArrays(None) # Clear previous array
# grid.GetPointData().AddArray(foo_array)
# writer.SetTimeValue(float(i)) # Is there a method like this?
# writer.Write() # Append this timestep
# writer.Stop() # is there a method like this?
How can I achieve this correctly so that ParaView or other readers recognize it as a time series?
Any help or examples would be greatly appreciated!
Thanks!