How do I add multiple column headers in HDF5 Table so that its rendered by HDFView
Fox example, I want to have field names as well as field units.
I can add attributes using H5LTset_attribute_string
. But is there a way to add such that it is reflected in the column header in HDFView
#include "hdf5.h"
#include "hdf5_hl.h"
#include "mpi.h"
#include <cassert>
#include <string>
int main()
{
herr_t err;
hid_t file_id = H5Fcreate("foo.out",H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);
constexpr int COLUMN_NAME_WIDTH = 10;
typedef struct{
double time;
char foo1[COLUMN_NAME_WIDTH];
int foo2;
}Data;
constexpr int NROWS = 5;
constexpr int NCOLS = 3;
size_t dataset_size = sizeof(Data);
size_t dataset_offset[NROWS] = {
HOFFSET(Data,time),
HOFFSET(Data,foo1),
HOFFSET(Data,foo2)
};
const char *field_names[NCOLS] = {"Time","foo1","foo2"};
const char *field_units[NCOLS] = {"(s)","(none)","(K)"};
hid_t H5T_CUSTOM_STRING = H5Tcopy(H5T_C_S1);
err = H5Tset_size(H5T_CUSTOM_STRING,COLUMN_NAME_WIDTH);
hid_t field_types[NCOLS] = {H5T_NATIVE_DOUBLE,H5T_CUSTOM_STRING,H5T_NATIVE_INT};
Data data[NROWS] = {
{0.0,"Haha0",0},
{0.1,"Haha1",1},
{0.2,"Haha2",2},
{0.3,"Haha3",3},
{0.4,"Haha4",4}
};
size_t chunk_size =NROWS;//Number of rows to be written to memory at once
Data fill_data[1] = {{1.,"HahaN",__INT_MAX__}};//optional input
int compress = 0;//Enable/Disable compression
//Create the table
err = H5TBmake_table(
"Table Title",//Optional attribute
file_id,
"Table Name",
NCOLS,
NROWS,
dataset_size,
field_names,
dataset_offset,
field_types,
chunk_size,
fill_data,//Optional attribute
compress,
data
);
//Note : "Table_Title"(optional),field_names and fill_data(optional) are attributes
assert(err==0 && "Table creation failed");
for(size_t i = 0; i < NCOLS; ++i) {
std::string attr_name = std::string("UNIT_") + field_names[i];
err = H5LTset_attribute_string(file_id, "Table Name", attr_name.c_str(), field_units[i]);
assert(err == 0 && "Unable to set units attribute");
}
//Close File
err = H5Fclose(file_id);
assert(err==0 && "Unable to close file");
return 0;
}