1 #include "hdf.h"
2 
3 #define  FILE_NAME        "Packed_Vdata.hdf"
4 #define  VDATA_NAME       "Mixed Data Vdata"
5 #define  CLASS_NAME       "General Data Class"
6 #define  FIELD1_NAME      "Temp"
7 #define  FIELD2_NAME      "Height"
8 #define  FIELD3_NAME      "Speed"
9 #define  FIELD4_NAME      "Ident"
10 #define  ORDER            1        /* number of values in the field         */
11 #define  N_RECORDS        20       /* number of records the vdata contains  */
12 #define  N_FIELDS         4        /* number of fields in the vdata         */
13 #define  FIELDNAME_LIST   "Temp,Height,Speed,Ident"  /* No spaces b/w names */
14 
15 /* number of bytes of the data to be written, i.e., the size of all the
16    field values combined times the number of records */
17 #define BUF_SIZE (2*sizeof(float32) + sizeof(int16) + sizeof(char)) * N_RECORDS
18 
main()19 int main( )
20 {
21    /************************* Variable declaration **************************/
22 
23    intn  status_n;      /* returned status for functions returning an intn  */
24    int32 status_32,     /* returned status for functions returning an int32 */
25          file_id, vdata_id,
26          vdata_ref = -1,   /* vdata's reference number, set to -1 to create */
27          num_of_records; /* number of records actually written to the vdata */
28    float32 temp[N_RECORDS];       /* buffer to hold values of first field   */
29    int16   height[N_RECORDS];     /* buffer to hold values of second field  */
30    float32 speed[N_RECORDS];      /* buffer to hold values of third field   */
31    char8   ident[N_RECORDS];      /* buffer to hold values of fourth field  */
32    VOIDP   fldbufptrs[N_FIELDS];/*pointers to be pointing to the field buffers*/
33    uint16  databuf[BUF_SIZE]; /* buffer to hold the data after being packed*/
34    int     i;
35 
36    /********************** End of variable declaration **********************/
37 
38    /*
39    * Create an HDF file.
40    */
41    file_id = Hopen (FILE_NAME, DFACC_CREATE, 0);
42 
43    /*
44    * Initialize the VS interface.
45    */
46    status_n = Vstart (file_id);
47 
48    /*
49    * Create a new vdata.
50    */
51    vdata_id = VSattach (file_id, vdata_ref, "w");
52 
53    /*
54    * Set name and class name of the vdata.
55    */
56    status_32 = VSsetname (vdata_id, VDATA_NAME);
57    status_32 = VSsetclass (vdata_id, CLASS_NAME);
58 
59    /*
60    * Introduce each field's name, data type, and order.  This is the first
61    * part in defining a vdata field.
62    */
63    status_n = VSfdefine (vdata_id, FIELD1_NAME, DFNT_FLOAT32, ORDER);
64    status_n = VSfdefine (vdata_id, FIELD2_NAME, DFNT_INT16, ORDER);
65    status_n = VSfdefine (vdata_id, FIELD3_NAME, DFNT_FLOAT32, ORDER);
66    status_n = VSfdefine (vdata_id, FIELD4_NAME, DFNT_CHAR8, ORDER);
67 
68    /*
69    * Finalize the definition of the fields of the vdata.
70    */
71    status_n = VSsetfields (vdata_id, FIELDNAME_LIST);
72 
73    /*
74    * Enter data values into the field buffers by the records.
75    */
76    for (i = 0; i < N_RECORDS; i++)
77    {
78       temp[i] = 1.11 * (i+1);
79       height[i] = i;
80       speed[i] = 1.11 * (i+1);
81       ident[i] = 'A' + i;
82    }
83 
84    /*
85    * Build an array of pointers each of which points to a field buffer that
86    * holds all values of the field.
87    */
88    fldbufptrs[0] = &temp[0];
89    fldbufptrs[1] = &height[0];
90    fldbufptrs[2] = &speed[0];
91    fldbufptrs[3] = &ident[0];
92 
93    /*
94    * Pack all data in the field buffers that are pointed to by the set of
95    * pointers fldbufptrs, and store the packed data into the buffer
96    * databuf.  Note that the second parameter is _HDF_VSPACK for packing.
97    */
98    status_n = VSfpack (vdata_id,_HDF_VSPACK, NULL, (VOIDP)databuf,
99            BUF_SIZE, N_RECORDS, NULL, (VOIDP)fldbufptrs);
100 
101    /*
102    * Write all records of the packed data to the vdata.
103    */
104    num_of_records = VSwrite (vdata_id, (uint8 *)databuf, N_RECORDS,
105                              FULL_INTERLACE);
106 
107    /*
108    * Terminate access to the vdata and the VS interface, then close
109    * the HDF file.
110    */
111    status_32 = VSdetach (vdata_id);
112    status_n = Vend (file_id);
113    status_32 = Hclose (file_id);
114    return 0;
115 }
116