Hi all! I’m having trouble extending a 1D dataset using the C++ API. I have some code based on the demo here, but it is producing unexpected results. Here is my code:
#include <H5Cpp.h>
#include <hdf5.h>
using namespace H5;
int main(int argc, char* argv[]) {
const int DIM0 = 4;
const int EDIM0 = 6;
const int CHUNK0 = 1;
const std::string FILE = "/tmp/test.h5";
const std::string DATASET = "DS1";
hid_t file, space, dset, dcpl; /* Handles */
herr_t status;
hsize_t dims[1] = {DIM0};
hsize_t extdims[1] = {EDIM0 + DIM0};
hsize_t maxdims[1];
hsize_t chunk[1] = {CHUNK0};
hsize_t start[1];
hsize_t count[1];
int wdata[DIM0];
int wdata2[EDIM0];
/*
* Fill Dataset
*/
for (int i = 0; i < DIM0; i++)
wdata[i] = i + 1;
file = H5Fcreate(FILE.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
maxdims[0] = H5S_UNLIMITED;
space = H5Screate_simple(1, dims, maxdims);
dcpl = H5Pcreate(H5P_DATASET_CREATE);
status = H5Pset_chunk(dcpl, 1, chunk);
dset = H5Dcreate(file, DATASET.c_str(), H5T_STD_I32LE, space, H5P_DEFAULT, dcpl, H5P_DEFAULT);
status = H5Dwrite(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &wdata[0]);
/*
* Extend the dataset.
*/
status = H5Dset_extent(dset, extdims);
space = H5Dget_space(dset);
for (int i = 0; i < EDIM0; i++)
wdata2[i] = 100 - (i + 1);
status = H5Sselect_all(space);
/*
* Subtract a hyperslab reflecting the original dimensions from the
* selection. The selection now contains only the newly extended
* portions of the dataset.
*/
start[0] = DIM0;
count[0] = EDIM0;
status = H5Sselect_hyperslab(space, H5S_SELECT_SET, start, NULL, count, NULL);
status = H5Dwrite(dset, H5T_NATIVE_INT, H5S_ALL, space, H5P_DEFAULT, &wdata2[0]);
status = H5Dclose(dset);
status = H5Sclose(space);
status = H5Fclose(file);
}
Now what I’m expecting is a dataset called “DS1” with the following data:
[1,2,3,4,99,98,97,96,95,94]
where the [1,2,3,4] is the ‘original’ data and the [99,98,97,96,95,94] is the “extension”. But what I’m getting is:
[1,2,3,4,95,94,79149008,0,4,0].
It’s as if when I extend the dataset, it starts in position 0, but only starts writing once it gets to the empty part of the array and then continues to write garbage memory from beyond the array bounds until it gets to the end of the dataset extension. I’m pretty sure I’m making a mistake with H5Sselect_hyperslab, but I’m not sure what.
Thanks!