[Fortran] Get number of groups

There was already a somewhat similar question in the forum from someone else without a clear answer, so I wanted to ask again with a more narrow scope:

In the hdf5 Fortran API, is there a procedure I can use to traverse the content of an hdf5 file and count the number of subgroups of a given specific group that are connected to this group, but not deeper levels (i.e. no sub-subgroups)?

If such a procedure exists, how does the callback function need to look like, and how can I return the number information back to the main calling procedure?
The Fortran API reference isn’t really very helpful on this topic.

For example, let’s assume I have a file with the following group structure:

atlas
|
|-manifold1-|
| |-region1
| |-region2
|
|-manifold2
|
|-resolution

and would like to count the subgroups of ‘atlas’, so the correct result would be number = 3, as region1 and region2 are not counted, but manifold1, manifold2, and resolution are.

Hi @stegmann,

If you are not bound to a specific API, you may want to try HDFql which is a high-level (declarative) programming language that alleviates users from HDF5 low-level details. Your use-case could be solved as follows in Fortran using HDFql:

// get all groups stored in group "atlas" and populate cursor with these
hdfql_execute("SHOW GROUP atlas/")

// print number of groups found in group "atlas"
WRITE(*, *) "Number of groups found: ", hdfql_cursor_get_count()

Hope it helps!

1 Like

Looks interesting but I cannot introduce extra dependencies.

How about this example? Sorry, it is a little messy.

numgrp.F90 (2.8 KB)

1 Like

Hello @brtnfld. Thank you, that looks similar to one of the examples from the repository.
I was able to adapt it by checking the group names to select only specific groups.
I’m assuming to get the information of the entire group tree one would have to pass a pointer to a tree structure.

If you want to transverse each group in the H5Literate callback, you could use H5Lvisit in the callback. On the other hand, if you’re going to do transversal globally, you might want to use H5Lvisit instead of H5Literate.

2 Likes