Access time dependent PointData in vtkStructuredGrid

Dear vtk-users,

I am using the vtk library in python to importe a netcdf file.
The netcdf file contains data of two variables for different time steps.
It will be converted into a vtkStructuredGrid
The vtkStructuredGrid then should have PointData for different time steps.

reader_nc = vtk.vtkNetCDFCFReader()
reader_nc.SetFileName(scr_netcdf_path)
reader_nc.UpdateMetaData()
reader_nc.SetSphericalCoordinates(0)
reader_nc.SetOutputTypeToStructured()
reader_nc.Update()
source_nc = reader_nc.GetOutput()

How do I access the PointData of the two variabls at the different time steps?

numpy_support.vtk_to_numpy(src.GetPointData().GetArray(0))

Gives me the data for the 1st variable at the very first time step.
How can I access to remaining time steps?

I would really appreciate some hint.

Cheers,

You’ll need to set the time step when updating the reader and loop through a pipeline calling update for each timestamp with the method .UpdateTimeStep() method on the reader_nc: https://vtk.org/doc/nightly/html/classvtkAlgorithm.html#af8c35754df1cb02cf74aa3ea0cd224ac

This might get really verbose and slow in Python, so I’d recomend just using the
netCDF4 Python package (or another library like xarray that can handle your data format) to load your netCDF files into NumPy arrays.

Then if you want to create a structured grid for visualization, use the vtkStructuredGrid from the vtk.vtkNetCDFCFReader like you have above and simply update the array using the numpy arrays loaded directly using netCDF4.

FYI: PyVista makes managing numpy arrays and VTK datasets super streamlined

1 Like