1

I have a HDF file which contains a simple array of compound types. To read all elements in the array i do

hid_t hDataSet = H5Dopen(hSpecies,AGENT_DATASET_NAME, H5P_DEFAULT);
herr_t status = H5Dread(hDataSet, agent_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, *ppAgentData);

Now i want to read only a selection of those elements. I place this before the call to H5Dread:

hsize_t coords[3][1];
coords[0][0] = 1;
coords[1][0] = 3;
coords[2][0] = 6;
hid_t hDataSpace = H5Dget_space(hDataSet);
int iRes = H5Sselect_elements(hDataSpace, H5S_SELECT_SET, 3, (const hsize_t *)&coords);

I expected that i would get the first, the third and the sixth element, but actually i get the same result as without the call to H5Sselect_elements. Do i misunderstand something about the use of H5Sselect_elements? The problem is, all examples i found use this function only in combination with H5Dwrite()...

user1479670
  • 1,145
  • 3
  • 10
  • 22

1 Answers1

1

The problem was that i used 'H5S_ALL' for the 'mem_space_id' and 'file_space_id' parameters of H5Dread. The working code now looks like this:

hid_t hDataSetAgents = H5Dopen(hSpecies,AGENT_DATASET_NAME, H5P_DEFAULT);
hid_t hDataSpaceAgents = H5Dget_space(hDataSetAgents);
hsize_t coords[3];
coords[0] = 1;
coords[1] = 3;
coords[2] = 6;
int iRes = H5Sselect_elements(hDataSpaceAgents, H5S_SELECT_SET, 3, (const hsize_t *)&coords);
hsize_t d = 3;
agsub *pAgentData = new agsub[d];
hid_t hMemSpace = H5Screate_simple(1, &d, NULL);
herr_t status = H5Dread(hDataSetAgents, agsubid, hMemSpace, hDataSpaceAgents, H5P_DEFAULT, pAgentData);

The 'file_space_id' parameter tells H5Dread "where to read from" (in this case my dataspace with a selection of three elements applied to it), and the 'mem_space_id' parameter tells it "where to write to" (in this case a simple array of three elements). If you specify 'H5S_ALL' for both, the entire dataspace is read.

user1479670
  • 1,145
  • 3
  • 10
  • 22