First of all, forgive my very basic question: I'm going through the tutorials and I've successfully written my first HDF5 dataset. Now, I'm moving to slabs, but I'm finding difficulties in doing this.
As far as I understand, I need to obtain a valid memory space, select a slab, and then write my data. But obviously I'm doing something wrong, since I get errors:
HDF5-DIAG: Error detected in HDF5 (1.8.14) thread 0:
#000: H5Dio.c line 271 in H5Dwrite(): can't prepare for writing data
major: Dataset
minor: Write failed
#001: H5Dio.c line 352 in H5D__pre_write(): can't write data
major: Dataset
minor: Write failed
#002: H5Dio.c line 690 in H5D__write(): src and dest data spaces have different sizes
major: Invalid arguments to routine
minor: Bad value
Obviously I'm trying to write data with wrong dimensions, but I don't know how to correct that. My goal is to create a 20x3 matrix, and set the second row to { 10, 20, 30 }
:
0 0 0
10 20 30
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
Can you help me in understanding this issue? My buggy code is below!
Thanks!
#include "hdf5.h"
#define FILE "dset.h5"
#define DSET "/dset"
int main() {
hid_t file_id, dataset_id, dataspace_id, filespace, memspace;
hsize_t dims[2], offset[2], count[2];
herr_t status;
unsigned int dset_data[3] = { 10, 20, 30 };
/* Create a new file using default properties. */
file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Create the data space for the dataset. */
dims[0] = 20; /* ROWS */
dims[1] = 3; /* COLS */
dataspace_id = H5Screate_simple(2, dims, NULL);
/* Create the dataset. */
dataset_id = H5Dcreate2(file_id, DSET, H5T_STD_I32BE, dataspace_id,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
/* Get the memory space */
memspace = H5Dget_space (dataset_id);
offset[0] = 1; /* ROWS */
offset[1] = 0; /* COLS */
count[0] = 1; /* ROWS */
count[1] = 3; /* COLS */
/* Select the slab */
status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET,
offset, NULL, count, NULL);
/* Write the dataset. */
status = H5Dwrite(dataset_id, H5T_NATIVE_INT,
memspace, H5S_ALL, H5P_DEFAULT,
dset_data);
/* End access to the dataset and release resources used by it. */
status = H5Dclose(dataset_id);
/* Terminate access to the data space. */
status = H5Sclose(dataspace_id);
/* Close the file. */
status = H5Fclose(file_id);
}
Your data is contiguous in memory (memspace
) but it's an hyperslab in the file's space (dataspace_id
). So you need to do your selection on dataset_id
, not memspace
.
And you need to size memspace
to contain just enough space for your row. Here I would just create a simple 1D dataspace of 3 elements. Like this the size of the hyperslab (3 elements) and the size in memory will match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With