1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Copyright by The HDF Group. *
3 * Copyright by the Board of Trustees of the University of Illinois. *
4 * All rights reserved. *
5 * *
6 * This file is part of HDF5. The full HDF5 copyright notice, including *
7 * terms governing use, modification, and redistribution, is contained in *
8 * the COPYING file, which can be found at the root of the source code *
9 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. *
10 * If you do not have access to either file, you may request a copy from *
11 * help@hdfgroup.org. *
12 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13
14 #include "hdf5.h"
15 #include "hdf5_hl.h"
16 #include <stdlib.h>
17
18 /*-------------------------------------------------------------------------
19 * Table API example
20 *
21 * H5TBmake_table
22 * H5TBread_table
23 *
24 *-------------------------------------------------------------------------
25 */
26
27 #define NFIELDS (hsize_t) 5
28 #define NRECORDS (hsize_t) 8
29 #define TABLE_NAME "table"
30
31
main(void)32 int main( void )
33 {
34 typedef struct Particle
35 {
36 char name[16];
37 int lati;
38 int longi;
39 float pressure;
40 double temperature;
41 } Particle;
42
43 Particle dst_buf[NRECORDS];
44
45 /* Calculate the size and the offsets of our struct members in memory */
46 size_t dst_size = sizeof( Particle );
47 size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ),
48 HOFFSET( Particle, lati ),
49 HOFFSET( Particle, longi ),
50 HOFFSET( Particle, pressure ),
51 HOFFSET( Particle, temperature )};
52
53 size_t dst_sizes[NFIELDS] = { sizeof( dst_buf[0].name),
54 sizeof( dst_buf[0].lati),
55 sizeof( dst_buf[0].longi),
56 sizeof( dst_buf[0].pressure),
57 sizeof( dst_buf[0].temperature)};
58
59
60 /* Define an array of Particles */
61 Particle p_data[NRECORDS] = {
62 {"zero",0,0, 0.0f, 0.0},
63 {"one",10,10, 1.0f, 10.0},
64 {"two", 20,20, 2.0f, 20.0},
65 {"three",30,30, 3.0f, 30.0},
66 {"four", 40,40, 4.0f, 40.0},
67 {"five", 50,50, 5.0f, 50.0},
68 {"six", 60,60, 6.0f, 60.0},
69 {"seven",70,70, 7.0f, 70.0}
70 };
71
72 /* Define field information */
73 const char *field_names[NFIELDS] =
74 { "Name","Latitude", "Longitude", "Pressure", "Temperature" };
75 hid_t field_type[NFIELDS];
76 hid_t string_type;
77 hid_t file_id;
78 hsize_t chunk_size = 10;
79 int *fill_data = NULL;
80 int compress = 0;
81 int i;
82
83 /* Initialize field_type */
84 string_type = H5Tcopy( H5T_C_S1 );
85 H5Tset_size( string_type, 16 );
86 field_type[0] = string_type;
87 field_type[1] = H5T_NATIVE_INT;
88 field_type[2] = H5T_NATIVE_INT;
89 field_type[3] = H5T_NATIVE_FLOAT;
90 field_type[4] = H5T_NATIVE_DOUBLE;
91
92 /* Create a new file using default properties. */
93 file_id = H5Fcreate( "ex_table_01.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );
94
95 /*-------------------------------------------------------------------------
96 * H5TBmake_table
97 *-------------------------------------------------------------------------
98 */
99
100 H5TBmake_table( "Table Title", file_id, TABLE_NAME,NFIELDS,NRECORDS,
101 dst_size,field_names, dst_offset, field_type,
102 chunk_size, fill_data, compress, p_data );
103
104 /*-------------------------------------------------------------------------
105 * H5TBread_table
106 *-------------------------------------------------------------------------
107 */
108
109 H5TBread_table( file_id, TABLE_NAME, dst_size, dst_offset, dst_sizes, dst_buf );
110
111 /* print it by rows */
112 for (i=0; i<NRECORDS; i++) {
113 printf ("%-5s %-5d %-5d %-5f %-5f",
114 dst_buf[i].name,
115 dst_buf[i].lati,
116 dst_buf[i].longi,
117 dst_buf[i].pressure,
118 dst_buf[i].temperature);
119 printf ("\n");
120 }
121
122 /*-------------------------------------------------------------------------
123 * end
124 *-------------------------------------------------------------------------
125 */
126
127 /* close type */
128 H5Tclose( string_type );
129
130 /* close the file */
131 H5Fclose( file_id );
132
133 return 0;
134 }
135
136