v.tralala
v.tralala

Reputation: 1715

How many datasets in one hdf5 file

I am using MATLAB's function h5write to write a double variable foo to a hdf5 file called saved_foos.h5. I have a loop which changes foo in each iteration and I save it each time in the same hdf5 file but in another dataset, which is named according to the current number of iterations.

Afterwards I read out the data of each dataset (eaach iteration) in a C++ program with the H5Cpp library as follows:

#include "H5Cpp.h"
using namespace H5;

double readDouble(std::string dir, std::string file, std::string s_dataset) {
    if (!fexists((dir + file + std::string(".h5")).c_str())) {
        throw std::runtime_error((std::string("File ") + dir + file + std::string(".h5 does not exist.")).c_str());
    }
    H5File file_h(dir + file + std::string(".h5"), H5F_ACC_RDONLY);
    DataSet dataset = file_h.openDataSet(s_dataset);
    DataSpace dataspace = dataset.getSpace();
    int rank = dataspace.getSimpleExtentNdims();
    hsize_t *dims_out = new hsize_t[rank];
    dataspace.getSimpleExtentDims(dims_out, NULL);
    if (rank>=2 && (dims_out[0] * dims_out[1] != 1)) {
        throw std::runtime_error("Requested dataset is not a scalar double value.");
    }
    double data;
    dataset.read(&data, PredType::NATIVE_DOUBLE);
    delete dims_out;
    return data;
}

But how can I determine how many datasets are stored in the given hdf5 file?

Upvotes: 0

Views: 2383

Answers (1)

gdlmx
gdlmx

Reputation: 6789

Traverse the file

Seems that you want to list the datasets in the file. Here is a pretty complete example which is overkill for your problem. To help understanding it, I'll explain the relevant code sessions:

The C-API function H5Literate is used to iterate through all objects in the group.

/*
 * Use iterator to see the names of the objects in the file
 * root directory.
 */
cout << endl << "Iterating over elements in the file" << endl;
herr_t idx = H5Literate(file->getId(), H5_INDEX_NAME, H5_ITER_INC, NULL,    file_info, NULL);
cout << endl;

where file_info is a callback function:

/*
 * Operator function.
 */
herr_t
file_info(hid_t loc_id, const char *name, const H5L_info_t *linfo, void *opdata)
{
    hid_t group;
    group = H5Gopen2(loc_id, name, H5P_DEFAULT);
    cout << "Name : " << name << endl; // Display the group name.
    H5Gclose(group);
    return 0;
}

In your case, other iterating function instead of H5Literate may be more suitable. Please find it here. A pure C-API example that traverse a file can be found here.

Only get the number

If all datasets are stored under root and the format of their names are known. There is a simpler solution to just get the number of datasets:

    hsize_t  num_obj;
    H5Gget_num_objs(file->getId(), &num_obj); // if success, num_obj will be assigned the number of objects in the group

Upvotes: 2

Related Questions