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
15 #include "hdf5.h"
16 #include "hdf5_hl.h"
17 #include <stdlib.h>
18
19 /*-------------------------------------------------------------------------
20 * Table API example
21 *
22 * H5TBwrite_records
23 *
24 *-------------------------------------------------------------------------
25 */
26
27 #define NFIELDS (hsize_t) 5
28 #define NRECORDS (hsize_t) 8
29 #define NRECORDS_WRITE (hsize_t) 2
30 #define TABLE_NAME "table"
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 Particle p = {"zero",0,0, 0.0f, 0.0};
54 size_t dst_sizes[NFIELDS] = { sizeof( p.name),
55 sizeof( p.lati),
56 sizeof( p.longi),
57 sizeof( p.pressure),
58 sizeof( p.temperature)};
59
60 /* Define field information */
61 const char *field_names[NFIELDS] =
62 { "Name","Latitude", "Longitude", "Pressure", "Temperature" };
63 /* Fill value particle */
64 Particle fill_data[1] =
65 { {"no data",-1,-1, -99.0f, -99.0} };
66 hid_t field_type[NFIELDS];
67 hid_t string_type;
68 hid_t file_id;
69 hsize_t chunk_size = 10;
70 hsize_t start; /* Record to start reading/writing */
71 hsize_t nrecords; /* Number of records to read/write */
72 int i;
73
74 /* Define 2 new particles to write */
75 Particle particle_in[NRECORDS_WRITE] =
76 { {"zero",0,0, 0.0f, 0.0},
77 {"one",10,10, 1.0f, 10.0} };
78
79 /* Initialize the field field_type */
80 string_type = H5Tcopy( H5T_C_S1 );
81 H5Tset_size( string_type, 16 );
82 field_type[0] = string_type;
83 field_type[1] = H5T_NATIVE_INT;
84 field_type[2] = H5T_NATIVE_INT;
85 field_type[3] = H5T_NATIVE_FLOAT;
86 field_type[4] = H5T_NATIVE_DOUBLE;
87
88 /* Create a new file using default properties. */
89 file_id = H5Fcreate( "ex_table_03.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );
90
91 /* Make the table */
92 H5TBmake_table( "Table Title",
93 file_id,
94 TABLE_NAME,
95 NFIELDS,
96 NRECORDS,
97 dst_size,
98 field_names,
99 dst_offset,
100 field_type,
101 chunk_size,
102 fill_data,
103 0, /* no compression */
104 NULL ); /* no data written */
105
106
107 /* Overwrite 2 records starting at record 0 */
108 start = 0;
109 nrecords = NRECORDS_WRITE;
110 H5TBwrite_records( file_id, TABLE_NAME, start, nrecords, dst_size, dst_offset,
111 dst_sizes, particle_in);
112
113 /* read the table */
114 H5TBread_table( file_id, TABLE_NAME, dst_size, dst_offset, dst_sizes, dst_buf );
115
116 /* print it by rows */
117 for (i=0; i<NRECORDS; i++) {
118 printf ("%-5s %-5d %-5d %-5f %-5f",
119 dst_buf[i].name,
120 dst_buf[i].lati,
121 dst_buf[i].longi,
122 dst_buf[i].pressure,
123 dst_buf[i].temperature);
124 printf ("\n");
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
137