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