The HDF5 file I’m working with has a dataspace of rank=1 and DIM0=50.000. The datatype is a compound datatype. Let’s say each element has the following fields:
typedef struct element {
long long int SCALAR_FIELD;
double ARRAY_FIELD[ARRAY_SIZE]
} element;
I managed to read the dataset and I stored the data into an array of struct:
// …
element * data = new element[NUMBER_OF_ELEMENTS];
// …
dataset->read(data, e_mtype);
So far so good.
Using the Python HDF5 library I can read the same compound dataset and the output type is a numpy structured array. The numpy structed array allows to read the fields by column:
scalar_field_data = data[“SCALAR_FIELD”] // outputs an array
or
array_filed_data = data[“ARRAY_FIELD”] // output a matrix
How can I achieve the same behaviour using c++? Of course, I can loop through the array of structures and read the field I am interested into for each struct element and put it into a new array, something like:
int * scalar_field_data = new int[NUMBER_OF_ELEMENTS]
for(int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
scalar_field_data[i] = data[i]->SCALAR_FIELD;
}
Is there a way to avoid this loop and read the “SCALAR_FIELD” (for each dataset element) directly into an array?
In addition, I need to put these data inside an xtensor array . I am aware of this library but it seems, it doesn’t support compound datatypes.
Thank your in advance.