/* * Use of HDF5 named data types to implement quantities with a unit. * Copyright © 2013 Peter Colberg. * Licensed under the MIT License (Expat). */ #include int main(void) { /* open HDF5 file for writing */ hid_t file = H5Fcreate("h5md_units.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* create units group */ hid_t group = H5Gcreate(file, "units", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* create acceleration data type and store in units group */ hid_t filetype = H5Tcopy(H5T_NATIVE_DOUBLE); H5Tcommit(group, "acceleration", filetype, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group); /* attach unit attribute to acceleration data type */ char *buf[1] = {"meter per square second"}; hid_t space = H5Screate(H5S_SCALAR); hid_t strtype = H5Tcopy(H5T_C_S1); H5Tset_size(strtype, H5T_VARIABLE); H5Tset_cset(strtype, H5T_CSET_UTF8); hid_t attr = H5Acreate(filetype, "unit", strtype, space, H5P_DEFAULT, H5P_DEFAULT); H5Awrite(attr, strtype, buf); H5Aclose(attr); H5Tclose(strtype); /* create attribute using acceleration data type */ double data = 9.81; attr = H5Acreate(file, "data", filetype, space, H5P_DEFAULT, H5P_DEFAULT); H5Awrite(attr, H5T_NATIVE_DOUBLE, &data); H5Aclose(attr); H5Sclose(space); H5Tclose(filetype); H5Fclose(file); return 0; }