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 * H5TBappend_records
22 *
23 *-------------------------------------------------------------------------
24 */
25
26 #define NFIELDS (hsize_t) 5
27 #define NRECORDS (hsize_t) 8
28 #define NRECORDS_ADD (hsize_t) 2
29 #define TABLE_NAME "table"
30
main(void)31 int main( void )
32 {
33 typedef struct Particle
34 {
35 char name[16];
36 int lati;
37 int longi;
38 float pressure;
39 double temperature;
40 } Particle;
41
42 Particle dst_buf[NRECORDS+NRECORDS_ADD];
43
44 /* Define an array of Particles */
45 Particle p_data[NRECORDS] = {
46 {"zero",0,0, 0.0f, 0.0},
47 {"one",10,10, 1.0f, 10.0},
48 {"two", 20,20, 2.0f, 20.0},
49 {"three",30,30, 3.0f, 30.0},
50 {"four", 40,40, 4.0f, 40.0},
51 {"five", 50,50, 5.0f, 50.0},
52 {"six", 60,60, 6.0f, 60.0},
53 {"seven",70,70, 7.0f, 70.0}
54 };
55
56 /* Calculate the size and the offsets of our struct members in memory */
57 size_t dst_size = sizeof( Particle );
58 size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ),
59 HOFFSET( Particle, lati ),
60 HOFFSET( Particle, longi ),
61 HOFFSET( Particle, pressure ),
62 HOFFSET( Particle, temperature )};
63
64 size_t dst_sizes[NFIELDS] = { sizeof( p_data[0].name),
65 sizeof( p_data[0].lati),
66 sizeof( p_data[0].longi),
67 sizeof( p_data[0].pressure),
68 sizeof( p_data[0].temperature)};
69
70 /* Define field information */
71 const char *field_names[NFIELDS] =
72 { "Name","Latitude", "Longitude", "Pressure", "Temperature" };
73 hid_t field_type[NFIELDS];
74 hid_t string_type;
75 hid_t file_id;
76 hsize_t chunk_size = 10;
77 int *fill_data = NULL;
78 int compress = 0;
79 int i;
80
81 /* Append particles */
82 Particle particle_in[ NRECORDS_ADD ] =
83 {{ "eight",80,80, 8.0f, 80.0},
84 {"nine",90,90, 9.0f, 90.0} };
85
86 /* Initialize the field field_type */
87 string_type = H5Tcopy( H5T_C_S1 );
88 H5Tset_size( string_type, 16 );
89 field_type[0] = string_type;
90 field_type[1] = H5T_NATIVE_INT;
91 field_type[2] = H5T_NATIVE_INT;
92 field_type[3] = H5T_NATIVE_FLOAT;
93 field_type[4] = H5T_NATIVE_DOUBLE;
94
95 /* Create a new file using default properties. */
96 file_id = H5Fcreate( "ex_table_02.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );
97
98 /* make a table */
99 H5TBmake_table( "Table Title",file_id,TABLE_NAME,NFIELDS,NRECORDS,
100 dst_size, field_names, dst_offset, field_type,
101 chunk_size, fill_data, compress, p_data );
102
103 /* append two records */
104 H5TBappend_records(file_id, TABLE_NAME,NRECORDS_ADD, dst_size, dst_offset, dst_sizes,
105 &particle_in );
106
107 /* read the table */
108 H5TBread_table( file_id, TABLE_NAME, dst_size, dst_offset, dst_sizes, dst_buf );
109
110 /* print it by rows */
111 for (i=0; i<NRECORDS+NRECORDS_ADD; i++) {
112 printf ("%-5s %-5d %-5d %-5f %-5f",
113 dst_buf[i].name,
114 dst_buf[i].lati,
115 dst_buf[i].longi,
116 dst_buf[i].pressure,
117 dst_buf[i].temperature);
118 printf ("\n");
119 }
120
121 /* close type */
122 H5Tclose( string_type );
123
124 /* close the file */
125 H5Fclose( file_id );
126
127 return 0;
128 }
129
130