HDF5 Reader and Writer for Unstructured Grids

We are taking a quick break from the series of blogs on streaming. Instead, in preparation for a discussion on block-based streaming, I will discuss how you can write multi-block unstructured grid readers and writers in Python using the h5py library. Let’s get right down business. Here is the writer code.

First of all, this writer uses streaming as described in a previous blog. See also this blog for a more detailed description of how streaming works. The meat of the writer is actually just a few lines:

This block of code writes the 3 data arrays specific to vtkUnstructuredGrids: cells, cell types and cell locations. In short, cells describes the connectivity of cells (which points they contain), cell_types describe the type of each cell and cell_locations stores the offset of each cell in the cells array for quick random access. I will not describe these in further detail here. See the VTK Users’ Guide for more information. I also added support for point arrays. Writing out cell arrays is left to you as an exercise.

Note that, in addition to writing these arrays, I wrote the spatial bounds of each block as a meta-data (attribute). Why will become clear in the next blog (hint: think demand-driven pipeline and streaming).

We can exercise this writer with the following code:

This produces a file like this:

Here is a very simple reader for this data:

This is for the most part self-explanatory (you may want to take a quick look at the h5py documentation). It is mostly a matter of mapping HDF5 groups and datasets to VTK data structures:

In the next article, we will discover how we can extend this reader to support block-based streaming. Until then, happy coding.

Questions or comments are always welcome!