/* * compound.c * * A small test file to demonstrate the data transfer function * with scientific notation. * */ #include #include #include #include "hdf5.h" #define LENGTH 101 typedef struct { char name[64]; char unit[64]; } att_t; int main(int argc, char** argv) { hsize_t dims[] = { LENGTH }; // Approximately 9.2 Gbytes of data. /* Compound datatype */ att_t *atts = malloc(sizeof(att_t)); strcpy(atts[0].name, "Name"); strcpy(atts[0].unit, "Unit"); /* String type */ hid_t str_dtyp_id = H5Tcopy(H5T_C_S1); H5Tset_size(str_dtyp_id, 64); /* Attribute type */ hid_t att_dtyp_id = H5Tcreate(H5T_COMPOUND, sizeof(att_t)); H5Tinsert(att_dtyp_id, "NAME", HOFFSET(att_t, name), str_dtyp_id); H5Tinsert(att_dtyp_id, "UNIT", HOFFSET(att_t, unit), str_dtyp_id); /* Create file. */ hid_t file_id = H5Fcreate("compound.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* Create file dataspace. */ hid_t fspace_id = H5Screate_simple(1, dims, NULL); /* Create dataset. */ hid_t dset_id = H5Dcreate2(file_id, "test_dset", H5T_NATIVE_FLOAT, fspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* Write the attribute (compound) to the dataset */ hsize_t dima[] = { 1 }; hid_t att_dspc_id = H5Screate_simple(1, dima, NULL); hid_t att_attr_id = H5Acreate2(dset_id, "ATTRIBUTES", att_dtyp_id, att_dspc_id, H5P_DEFAULT, H5P_DEFAULT); int status = H5Awrite(att_attr_id, att_dtyp_id, atts); if (status < 0) { fprintf(stderr, "*** ERROR: H5Awrite"); } /* Create dataset transfer property list */ const char *expr = "2*x"; hid_t dxpl_id = H5Pcreate(H5P_DATASET_XFER); status = H5Pset_data_transform(dxpl_id, expr); if (status < 0) { fprintf(stderr, "*** ERROR: H5Pset_data_transform (expression: %s)\n", expr); } float *data = malloc(LENGTH * sizeof(float)); for (unsigned i = 0; i < LENGTH; i++) { data[i] = 10.f; } /* Write the data */ status = H5Dwrite(dset_id, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, dxpl_id, data); if (status < 0) { fprintf(stderr, "*** ERROR: H5Dwrite\n"); } free(data); /* Close all identifiers. */ H5Pclose(dxpl_id); H5Aclose(att_attr_id); H5Sclose(att_dspc_id); H5Dclose(dset_id); H5Sclose(fspace_id); H5Fclose(file_id); H5Tclose(att_dtyp_id); H5Tclose(str_dtyp_id); return 0; }