#include "hdf5.h" #include #include #define FAIL -1 #define VERBOSE_HI 0 #define CHECK(ret, val, where) do { \ if (VERBOSE_HI) { \ fprintf(stderr, " Call to routine: %15s at line %4d " \ where, (int)__LINE__ ); \ } \ if ((ret) == (val)) { \ fprintf(stderr, "*** UNEXPECTED RETURN from %s is %ld at line %4d " \ where, (long)(ret), (int)__LINE__); \ } \ } while(0) #define FILE "h5ex_t_cmpd-swmr.h5" #define DATASET "DS1" #define DIM0 4 typedef struct { int serial_no; char *location; double temperature; double pressure; } sensor_t; /* Compound type */ int main (void) { hid_t file, filetype, memtype, strtype, space, dset; /* Handles */ herr_t status; hsize_t dims[1] = {DIM0}; sensor_t wdata[DIM0], /* Write buffer */ *rdata; /* Read buffer */ int ndims, i; hid_t fapid; /* * Initialize data. */ wdata[0].serial_no = 1153; wdata[0].location = "Exterior (static)"; wdata[0].temperature = 53.23; wdata[0].pressure = 24.57; wdata[1].serial_no = 1184; wdata[1].location = "Intake"; wdata[1].temperature = 55.12; wdata[1].pressure = 22.95; wdata[2].serial_no = 1027; wdata[2].location = "Intake manifold"; wdata[2].temperature = 103.55; wdata[2].pressure = 31.23; wdata[3].serial_no = 1313; wdata[3].location = "Exhaust manifold"; wdata[3].temperature = 1252.89; wdata[3].pressure = 84.11; fapid = H5Pcreate (H5P_FILE_ACCESS); CHECK(fapid, FAIL, "H5Pcreate"); /* status = H5Pset_libver_bounds (fapid, H5F_LIBVER_LATEST,H5F_LIBVER_LATEST); */ status = H5Pset_libver_bounds(fapid, H5F_LIBVER_V110, H5F_LIBVER_LATEST); CHECK(status, FAIL, "H5Pset_libver_bounds"); /* * Create a new file using the default properties. */ /* file = H5Fcreate (FILE, H5F_ACC_TRUNC|H5F_ACC_SWMR_WRITE, H5P_DEFAULT, fapid); */ file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, fapid); CHECK(file, FAIL, "H5Fcreate"); status = H5Pclose (fapid); CHECK(status, FAIL, "H5Pclose"); status = H5Fstart_swmr_write(file); CHECK(status, FAIL, "H5Fstart_swmr_write"); /* * Create variable-length string datatype. */ strtype = H5Tcopy (H5T_C_S1); CHECK(strtype, FAIL, "H5Tcopy"); /* status = H5Tset_size (strtype, 20); */ status = H5Tset_size (strtype, H5T_VARIABLE); /* * Create the compound datatype for memory. */ memtype = H5Tcreate (H5T_COMPOUND, sizeof (sensor_t)); status = H5Tinsert (memtype, "Serial number", HOFFSET (sensor_t, serial_no), H5T_NATIVE_INT); status = H5Tinsert (memtype, "Location", HOFFSET (sensor_t, location), strtype); status = H5Tinsert (memtype, "Temperature (F)", HOFFSET (sensor_t, temperature), H5T_NATIVE_DOUBLE); status = H5Tinsert (memtype, "Pressure (inHg)", HOFFSET (sensor_t, pressure), H5T_NATIVE_DOUBLE); /* * Create the compound datatype for the file. Because the standard * types we are using for the file may have different sizes than * the corresponding native types, we must manually calculate the * offset of each member. */ filetype = H5Tcreate (H5T_COMPOUND, 8 + sizeof (hvl_t) + 8 + 8); status = H5Tinsert (filetype, "Serial number", 0, H5T_STD_I64BE); status = H5Tinsert (filetype, "Location", 8, strtype); status = H5Tinsert (filetype, "Temperature (F)", 8 + sizeof (hvl_t), H5T_IEEE_F64BE); status = H5Tinsert (filetype, "Pressure (inHg)", 8 + sizeof (hvl_t) + 8, H5T_IEEE_F64BE); /* * Create dataspace. Setting maximum size to NULL sets the maximum * size to be the current size. */ space = H5Screate_simple (1, dims, NULL); /* * Create the dataset and write the compound data to it. */ dset = H5Dcreate (file, DATASET, filetype, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(dset, FAIL, "H5Dcreate"); status = H5Dwrite (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata); CHECK(status, FAIL, "H5Dwrite"); /* * Close and release resources. */ status = H5Dclose (dset); CHECK(status, FAIL, "H5Dclose"); status = H5Sclose (space); CHECK(status, FAIL, "H5Sclose"); status = H5Tclose (filetype); CHECK(status, FAIL, "H5Tclose"); status = H5Fclose (file); CHECK(status, FAIL, "H5Fclose"); /* * Now we begin the read section of this example. Here we assume * the dataset has the same name and rank, but can have any size. * Therefore we must allocate a new array to read in data using * malloc(). For simplicity, we do not rebuild memtype. */ fprintf(stderr, "Reopen file to read\n"); /* * Open file and dataset. */ file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT); CHECK(file, FAIL, "H5Fopen"); dset = H5Dopen (file, DATASET, H5P_DEFAULT); /* * Get dataspace and allocate memory for read buffer. */ space = H5Dget_space (dset); ndims = H5Sget_simple_extent_dims (space, dims, NULL); rdata = (sensor_t *) malloc (dims[0] * sizeof (sensor_t)); /* * Read the data. */ status = H5Dread (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata); /* * Output the data to the screen. */ for (i=0; i