Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the dimensions of a HDF5 dataset

Tags:

c++

c

hdf5

I'm using some HDF5 Files in my C++ program and I have a question regarding the H5Dopen function. Is it possible to get the dimensions of a hdf5 dataset in a given file?

hid_t file, dset;
herr_t status;
file = H5Fopen (filenameField, H5F_ACC_RDONLY, H5P_DEFAULT);
dset = H5Dopen (file, "/xField", H5P_DEFAULT);

before I do the next line I want to get the dimensions of dset.

status = H5Dread (dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,  &readBuf[0]);

I only found H5Dget_storage_size, but that does not fit my case.

Does anyone knows how to do that?

like image 607
raspiede Avatar asked Apr 03 '13 11:04

raspiede


People also ask

Why are HDF5 files so large?

Although these steps are good for small datasets, the hdf5 file size increases rapidly with the number of images. I have experienced situations where the hdf5 file takes 100x times more space than the original dataset. This solely happens because the numpy array takes more storage space than the original image files.

Can Matlab read HDF5?

MATLAB supports reading and writing HDF5 data sets using dynamically loaded filters.

What is HDF5 binary data format?

The Hierarchical Data Format version 5 (HDF5), is an open source file format that supports large, complex, heterogeneous data. HDF5 uses a "file directory" like structure that allows you to organize data within the file in many different structured ways, as you might do with files on your computer.


2 Answers

For this you need to use dataspace functions which are prefixed by H5S.

The HDF5 reference manual is organized using these prefixes so it helps to understand that.

How to get the dimensions of a dataset

First you need to get the data space from your dataset using H5Dget_space:

hid_t dspace = H5Dget_space(dset);

If your data space is simple (i.e. not null or scalar), then you can get the number of dimensions using H5Sget_simple_extent_ndims:

const int ndims = H5Sget_simple_extent_ndims(dspace);

and the size of each dimension using H5Sget_simple_extent_dims:

hsize_t dims[ndims];
H5Sget_simple_extent_dims(dspace, dims, NULL);

The dimensions are now stored in dims.

like image 176
Simon Avatar answered Oct 05 '22 06:10

Simon


Alternatively, this can be done like this (in case of a simple dataspace, see Simons answer and if necessary check with bool H5::DataSpace::isSimple() const):

  #include "H5Cpp.h"
  using namespace H5;
  //[...]
  DataSpace dataspace(RANK, dims);
  //[...]
  /*
   * Get the number of dimensions in the dataspace.
   */
  const int rank = dataspace.getSimpleExtentNdims();

This line might be redundant in most cases, because the whole task can be done in two lines:

  /*
   * Get the dimension size of each dimension in the dataspace and
   * store the dimentionality in ndims.
   */
  hsize_t dims_out[rank];
  const int ndims = dataspace.getSimpleExtentDims( dims_out, NULL);

The function getSimpleExtentNdims() can be called as a member of the H5::DataSpace instance.

These pieces of code are taken from the examples page (readdata.cpp) of the more recent HDF5 C++ reference manual.

Compile everything with h5c++ and it should work.

like image 23
Baedsch Avatar answered Oct 05 '22 05:10

Baedsch