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  * H5TBcombine_tables
21  *
22  *-------------------------------------------------------------------------
23  */
24 
25 #define NFIELDS  (hsize_t)  5
26 #define NRECORDS (hsize_t)  8
27 #define TABLE1_NAME         "table1"
28 #define TABLE2_NAME         "table2"
29 #define TABLE3_NAME         "table3"
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  /* Define an array of Particles */
43  Particle  p_data[NRECORDS] = {
44  {"zero",0,0, 0.0f, 0.0},
45  {"one",10,10, 1.0f, 10.0},
46  {"two",  20,20, 2.0f, 20.0},
47  {"three",30,30, 3.0f, 30.0},
48  {"four", 40,40, 4.0f, 40.0},
49  {"five", 50,50, 5.0f, 50.0},
50  {"six",  60,60, 6.0f, 60.0},
51  {"seven",70,70, 7.0f, 70.0}
52   };
53 
54  Particle  dst_buf[ 2 * NRECORDS ];
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( dst_buf[0].name),
63                                sizeof( dst_buf[0].lati),
64                                sizeof( dst_buf[0].longi),
65                                sizeof( dst_buf[0].pressure),
66                                sizeof( dst_buf[0].temperature)};
67 
68 
69  /* Define field information */
70  const char *field_names[NFIELDS]  =
71  { "Name","Latitude", "Longitude", "Pressure", "Temperature" };
72  hid_t      field_type[NFIELDS];
73  hid_t      string_type;
74  hid_t      file_id;
75  hsize_t    chunk_size = 10;
76  int        compress  = 0;
77  int        *fill_data = NULL;
78  hsize_t    nfields_out;
79  hsize_t    nrecords_out;
80  int        i;
81 
82  /* Initialize the field field_type */
83  string_type = H5Tcopy( H5T_C_S1 );
84  H5Tset_size( string_type, 16 );
85  field_type[0] = string_type;
86  field_type[1] = H5T_NATIVE_INT;
87  field_type[2] = H5T_NATIVE_INT;
88  field_type[3] = H5T_NATIVE_FLOAT;
89  field_type[4] = H5T_NATIVE_DOUBLE;
90 
91  /* Create a new file using default properties. */
92  file_id = H5Fcreate( "ex_table_10.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );
93 
94  /* Make two tables */
95  H5TBmake_table( "Table Title",file_id,TABLE1_NAME,NFIELDS,NRECORDS,
96                          dst_size,field_names, dst_offset, field_type,
97                          chunk_size, fill_data, compress, p_data  );
98 
99  H5TBmake_table( "Table Title",file_id,TABLE2_NAME,NFIELDS,NRECORDS,
100                          dst_size,field_names, dst_offset, field_type,
101                          chunk_size, fill_data, compress, p_data  );
102 
103  /* Combine the two tables into a third in the same file  */
104  H5TBcombine_tables( file_id, TABLE1_NAME, file_id, TABLE2_NAME, TABLE3_NAME );
105 
106  /* read the combined table */
107  H5TBread_table( file_id, TABLE3_NAME, dst_size, dst_offset, dst_sizes, dst_buf );
108 
109  /* Get table info  */
110  H5TBget_table_info (file_id,TABLE3_NAME, &nfields_out, &nrecords_out );
111 
112  /* print */
113  printf ("Table has %d fields and %d records\n",(int)nfields_out,(int)nrecords_out);
114 
115  /* print it by rows */
116  for (i=0; i<nrecords_out; i++) {
117   printf ("%-5s %-5d %-5d %-5f %-5f",
118    dst_buf[i].name,
119    dst_buf[i].lati,
120    dst_buf[i].longi,
121    dst_buf[i].pressure,
122    dst_buf[i].temperature);
123   printf ("\n");
124  }
125 
126   /* close type */
127  H5Tclose( string_type );
128 
129  /* close the file */
130  H5Fclose( file_id );
131 
132  return 0;
133 
134 }
135 
136