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  * H5TBwrite_fields_name
22  *
23  *-------------------------------------------------------------------------
24  */
25 
26 #define NFIELDS       (hsize_t)   5
27 #define NRECORDS      (hsize_t)   8
28 #define NRECORDS_ADD  (hsize_t)   3
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  /* Define a subset of Particle, with latitude and longitude fields */
43  typedef struct Position
44  {
45   int    lati;
46   int    longi;
47  } Position;
48 
49  /* Define a subset of Particle, with name and pressure fields */
50  typedef struct NamePressure
51  {
52   char   name[16];
53   float  pressure;
54  } NamePressure;
55 
56  Particle  dst_buf[NRECORDS];
57  /* Calculate the size and the offsets of our struct members in memory */
58  size_t dst_size =  sizeof( Particle );
59  size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ),
60                                 HOFFSET( Particle, lati ),
61                                 HOFFSET( Particle, longi ),
62                                 HOFFSET( Particle, pressure ),
63                                 HOFFSET( Particle, temperature )};
64  size_t dst_sizes[NFIELDS] = { sizeof( dst_buf[0].name),
65                                sizeof( dst_buf[0].lati),
66                                sizeof( dst_buf[0].longi),
67                                sizeof( dst_buf[0].pressure),
68                                sizeof( dst_buf[0].temperature)};
69  size_t field_offset_pos[2] = { HOFFSET( Position, lati ),
70                                 HOFFSET( Position, longi )};
71  const char *field_names[NFIELDS]  =     /* Define field information */
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  Particle   fill_data[1] =
78  { {"no data",-1,-1, -99.0f, -99.0} };   /* Fill value particle */
79  hsize_t    start;                       /* Record to start reading/writing */
80  hsize_t    nrecords;                    /* Number of records to read/write */
81  int        compress  = 0;
82  int        i;
83  Particle  *p_data = NULL;               /* Initially no data */
84  float      pressure_in [NRECORDS_ADD] = /* Define new values for the field "Pressure"  */
85  { 0.0f,1.0f,2.0f};
86  Position   position_in[NRECORDS_ADD] = {/* Define new values for "Latitude,Longitude"  */
87  {0,0},
88  {10,10},
89  {20,20}};
90  NamePressure   namepre_in[NRECORDS_ADD] =/* Define new values for "Name,Pressure"  */
91  { {"zero",0.0f},
92  {"one",   1.0f},
93  {"two",   2.0f},
94  };
95  size_t field_sizes_pos[2]=
96  {
97   sizeof(position_in[0].longi),
98   sizeof(position_in[0].lati)
99  };
100  size_t field_sizes_pre[1]=
101  {
102   sizeof(namepre_in[0].pressure)
103  };
104 
105  /* Initialize the field field_type */
106  string_type = H5Tcopy( H5T_C_S1 );
107  H5Tset_size( string_type, 16 );
108  field_type[0] = string_type;
109  field_type[1] = H5T_NATIVE_INT;
110  field_type[2] = H5T_NATIVE_INT;
111  field_type[3] = H5T_NATIVE_FLOAT;
112  field_type[4] = H5T_NATIVE_DOUBLE;
113 
114  /* Create a new file using default properties. */
115  file_id = H5Fcreate( "ex_table_04.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );
116 
117  /* Make the table */
118  H5TBmake_table( "Table Title",file_id,TABLE_NAME,NFIELDS,NRECORDS,
119                          dst_size,field_names, dst_offset, field_type,
120                          chunk_size, fill_data, compress, p_data  );
121 
122  /* Write the pressure field starting at record 2 */
123  start    = 2;
124  nrecords = NRECORDS_ADD;
125  H5TBwrite_fields_name( file_id, TABLE_NAME, "Pressure", start, nrecords,
126    sizeof( float ), 0, field_sizes_pre, pressure_in  );
127 
128  /* Write the new longitude and latitude information starting at record 2 */
129  start    = 2;
130  nrecords = NRECORDS_ADD;
131  H5TBwrite_fields_name( file_id, TABLE_NAME, "Latitude,Longitude", start, nrecords,
132    sizeof( Position ), field_offset_pos, field_sizes_pos,  position_in  );
133 
134  /* read the table */
135  H5TBread_table( file_id, TABLE_NAME, dst_size, dst_offset, dst_sizes, dst_buf );
136 
137  /* print it by rows */
138  for (i=0; i<NRECORDS; i++) {
139   printf ("%-5s %-5d %-5d %-5f %-5f",
140    dst_buf[i].name,
141    dst_buf[i].lati,
142    dst_buf[i].longi,
143    dst_buf[i].pressure,
144    dst_buf[i].temperature);
145   printf ("\n");
146  }
147 
148 /*-------------------------------------------------------------------------
149  * end
150  *-------------------------------------------------------------------------
151  */
152 
153   /* close type */
154  H5Tclose( string_type );
155 
156  /* close the file */
157  H5Fclose( file_id );
158 
159  return 0;
160 
161 
162 }
163 
164