/*********************************************************************** * Example for a more complex compound structure. * * blw 6/16/03 * ***********************************************************************/ /* Include the HDF5 library */ #include "hdf5.h" /* System libraries to include */ #include #include /* Name of file for database output */ #define DATAFILE "compound_complex.h5" /* Name of dataset to create in datafile */ #define DATASETNAME "CompoundComplex" /* Dataset dimensions */ #define LENGTH 6 #define RANK 1 #define ARRAY_RANK 1 #define ARRAY_RANKd 2 #define DIMb 4 #define ARRAY_DIMc 6 #define ARRAY_DIMd 5 #define ARRAY_DIMf 10 int main(void) { /* Structure and array for compound types */ typedef struct Array1Struct { int a; const char *b[DIMb]; char c[ARRAY_DIMc]; short d[5][6]; float e; double f[ARRAY_DIMf]; char g; } Array1Struct; Array1Struct Array1[LENGTH]; /* Define the value of the string array */ const char *quote [DIMb] = { "A fight is a contract that takes two people to honor.", "A combative stance means that you've accepted the contract.", "In which case, you deserve what you get.", " -- Professor Cheng Man-ch'ing" }; /* Define the value of the character array */ char chararray [ARRAY_DIMc] = {'H', 'e', 'l', 'l', 'o', '!'}; hid_t Array1Structid; /* File datatype identifier */ hid_t array_tid; /* Array datatype handle */ hid_t array1_tid; /* Array datatype handle */ hid_t array2_tid; /* Array datatype handle */ hid_t array4_tid; /* Array datatype handle */ hid_t datafile, dataset; /* Datafile/dataset handles */ hid_t dataspace; /* Dataspace handle */ herr_t status; /* Error checking variable */ hsize_t dim[] = {LENGTH}; /* Dataspace dimensions */ hsize_t array_dimb[] = {DIMb}; /* Array dimensions */ hsize_t array_dimd[]={ARRAY_DIMd}; /* Array dimensions */ hsize_t array_dimf[]={ARRAY_DIMf}; /* Array dimensions */ hid_t str_array_id; int m, n, o; /* Array init loop vars */ /* Initialize the data in the arrays/datastructure */ for (m = 0; m< LENGTH; m++) { Array1[m].a = m; for (n = 0; n < DIMb; n++) { Array1[m].b[n] = quote[n]; } for (n = 0; n < ARRAY_DIMc; n++) { Array1[m].c[n] = chararray[n]; } for (n = 0; n < 5; n++) { for (o = 0; o < 6; o++){ Array1[m].d[n][o] = m + n + o; } } Array1[m].e = ( m * .96 ); for (n = 0; n < ARRAY_DIMf; n++) { Array1[m].f[n] = ( m * 1024.9637 ); } Array1[m].g = 'm'; } /* Create the dataspace */ dataspace = H5Screate_simple(RANK, dim, NULL); assert (dataspace >= 0); /* Create the file */ datafile = H5Fcreate(DATAFILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); assert (datafile >= 0); /* Copy the array data type for the string array */ array_tid = H5Tcopy (H5T_C_S1); assert (array_tid >= 0); /* Set the string array size to Variable */ status = H5Tset_size (array_tid,H5T_VARIABLE); assert (status >= 0); /* Create the array data type for the string array */ str_array_id = H5Tarray_create(array_tid, ARRAY_RANK, array_dimb, NULL); assert (str_array_id >= 0); /* Copy the array data type for the character array */ array1_tid = H5Tcopy (H5T_C_S1); assert (array1_tid >= 0); /* Set the character array size */ status = H5Tset_size (array1_tid, ARRAY_DIMc); assert (status >= 0); /* Create the array data type for the character array */ array2_tid = H5Tarray_create(H5T_NATIVE_SHORT, ARRAY_RANKd, array_dimd, NULL); assert (array2_tid >= 0); /* Create the array data type for the character array */ array4_tid = H5Tarray_create(H5T_NATIVE_DOUBLE, ARRAY_RANK, array_dimf, NULL); assert (array4_tid >= 0); /* Create the memory data type */ Array1Structid = H5Tcreate (H5T_COMPOUND, sizeof(Array1Struct)); assert (Array1Structid >= 0); /* Insert the arrays and variables into the structure */ status = H5Tinsert(Array1Structid, "a_name", HOFFSET(Array1Struct, a), H5T_NATIVE_INT); assert (status >= 0); status = H5Tinsert(Array1Structid, "b_name", HOFFSET(Array1Struct, b), str_array_id); assert (status >= 0); status = H5Tinsert(Array1Structid, "c_name", HOFFSET(Array1Struct, c), array1_tid); assert (status >= 0); status = H5Tinsert(Array1Structid, "d_name", HOFFSET(Array1Struct, d), array2_tid); assert (status >= 0); status = H5Tinsert(Array1Structid, "e_name", HOFFSET(Array1Struct, e), H5T_NATIVE_FLOAT); assert (status >= 0); status = H5Tinsert(Array1Structid, "f_name", HOFFSET(Array1Struct, f), array4_tid); assert (status >= 0); status = H5Tinsert(Array1Structid, "g_name", HOFFSET(Array1Struct, g), H5T_NATIVE_CHAR); assert (status >= 0); /* Create the dataset */ dataset = H5Dcreate(datafile, DATASETNAME, Array1Structid, dataspace, H5P_DEFAULT); /* Write data to the dataset */ status = H5Dwrite(dataset, Array1Structid, H5S_ALL, H5S_ALL, H5P_DEFAULT, Array1); assert (status >= 0); /* Release resources */ status = H5Tclose(Array1Structid); assert (status >= 0); status = H5Tclose(array_tid); assert (status >= 0); status = H5Tclose(array1_tid); assert (status >= 0); status = H5Tclose(array2_tid); assert (status >= 0); status = H5Tclose(array4_tid); assert (status >= 0); status = H5Tclose(str_array_id); assert (status >= 0); status = H5Sclose(dataspace); assert (status >= 0); status = H5Dclose(dataset); assert (status >= 0); status = H5Fclose(datafile); assert (status >= 0); /* Return value */ return 0; }