
/****************************************************************************
 * NCSA HDF                                                                 *
 * Scientific Data Technologies                                             *
 * National Center for Supercomputing Applications                          *
 * University of Illinois at Urbana-Champaign                               *
 * 605 E. Springfield, Champaign IL 61820                                   *
 *                                                                          *
 * For conditions of distribution and use, see the accompanying             *
 * hdf/COPYING f.                                                           *
 *                                                                          *
 ****************************************************************************/


#include "H5TA.h"

#include <stdlib.h>


#define NFIELDS  5
#define NRECORDS 8

int main( void )
{
 
 typedef struct Particle 
 {
  char   name[16];
  int    lati;
  int    longi;
  float  pressure;
  double temperature; 
 } Particle;

 /* Define a subset of Particle, with latitude and longitude fields */
 typedef struct Position 
 {
  int    lati;
  int    longi;
 } Position;

 /* Define a subset of Particle, with name and pressure fields */
 typedef struct NamePressure 
 {
  char   name[16];
  float  pressure;
 } NamePressure;
 
 /* Calculate the type_size and the offsets of our struct members */
 size_t type_size =  sizeof( Particle );
 size_t field_offset[NFIELDS] = { HOFFSET( Particle, name ),
                                  HOFFSET( Particle, lati ),
                                  HOFFSET( Particle, longi ),
                                  HOFFSET( Particle, pressure ),
                                  HOFFSET( Particle, temperature )};

 size_t field_offset_pos[2] = { HOFFSET( Position, lati ),
                                HOFFSET( Position, longi )};
 
 /* Initially no data */
 Particle  *p_data = NULL;

 /* Define field information */
 const char *field_names[NFIELDS]  = 
 { "Name","Latitude", "Longitude", "Pressure", "Temperature" };
 hid_t      field_type[NFIELDS];
 hid_t      string_type;
 hid_t      file_id;
 hsize_t    chunk_size = 10;
 /* Fill value particle */ 
 int        *fill_data = NULL;
 int        compress  = 0;
 hsize_t    nfields;
 hsize_t    start;      /* Record to start reading/writing */
 hsize_t    nrecords;   /* Number of records to read/write */
 herr_t     status; 


 /* Define new values for the field "Pressure"  */
 float      pressure_in  [NRECORDS] = 
 { 0.0f,1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f };

 int        field_index_pre[1]     = { 3 };
 int        field_index_pos[2]     = { 1,2 };


 /* Define new values for the fields "Latitude,Longitude"  */
 Position   position_in[NRECORDS] = { {0,0},
 {10,10},
 {20,20}, 
 {30,30}, 
 {40,40},
 {50,50}, 
 {60,60},    
 {70,70} };

 size_t field_sizes_pos[2]=
 {
  sizeof(position_in[0].longi),
  sizeof(position_in[0].lati)
 };
 
 size_t field_sizes_pre[1]=
 { 
  sizeof(float)
 };


 EXAMPLE("write fields by index");


 /* Initialize the field field_type */
 string_type = H5Tcopy( H5T_C_S1 );
 H5Tset_size( string_type, 16 );
 field_type[0] = string_type;
 field_type[1] = H5T_NATIVE_INT;
 field_type[2] = H5T_NATIVE_INT;
 field_type[3] = H5T_NATIVE_FLOAT;
 field_type[4] = H5T_NATIVE_DOUBLE;
 
 /* Create a new file using default properties. */
 file_id = H5Fcreate( "ex_table_05.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );

 /* Make the table */
 status=H5TBmake_table( "Table Title", file_id, "Table1", (hsize_t)NFIELDS,(hsize_t) NRECORDS, type_size, 
                       field_names, field_offset, field_type, 
                       chunk_size, fill_data, compress, p_data );

 /* Write the pressure field starting at record 2 */
 nfields  = 1;
 start    = 2;      
 nrecords = 3; 
 status=H5TBwrite_fields_index( file_id, "Table1", nfields, field_index_pre, start, nrecords, 
   sizeof( float ), 0, field_sizes_pre, pressure_in  );

 /* Write the new longitude and latitude information starting at record 2  */
 nfields  = 2;
 start    = 2;      
 nrecords = 3; 
 status=H5TBwrite_fields_index( file_id, "Table1", nfields, field_index_pos, start, nrecords, 
   sizeof( Position ), field_offset_pos, field_sizes_pos, position_in  );
 
 /* Close the file. */
 H5Fclose( file_id );

 PASSED();

 return 0;

}

