/*  
 *  This example writes data to the HDF5 file.
 *  Data conversion is performed during write operation.  
 */
 
#include<stdio.h>
#include <hdf5.h>

#define DATASETNAME "Coordinates" 
#define NX     20                      /* dataset dimensions */
#define NY     3 
#define RANK   2

int
main (void)
{
    hid_t       file, dataset;         /* file and dataset handles */
    hid_t       datatype, dataspace;   /* handles */
    hsize_t     dimsf[2];              /* dataset dimensions */
    herr_t      status;                             
    double       data[NX][NY];          /* data to write */
    double       data1[NX][NY];         /* data to read */
    int         i, j, num;
    double         val;
    FILE *fp;

    fp = fopen ("coordinates.txt", "r");

    for (i=0; i<NX; i++)
        for (j=0; j<NY; j++)
        {
           num = fscanf (fp, "%le ", &val);
           data[i][j] = val;
        } 

    /*
     * Create a new file using H5F_ACC_TRUNC access,
     * default file creation properties, and default file
     * access properties.
     */

    file = H5Fcreate("coord.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);


    /*
     * Describe the size of the array and create the data space for fixed
     * size dataset. 
     */
    dimsf[0] = NX;
    dimsf[1] = NY;
    dataspace = H5Screate_simple(RANK, dimsf, NULL); 

    dataset = H5Dcreate(file, DATASETNAME, H5T_IEEE_F64BE, dataspace,
			H5P_DEFAULT);

    /*
     * Write the data to the dataset using default transfer properties.
     */
    status = H5Dwrite(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
		      H5P_DEFAULT, data);
    status = H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
		      H5P_DEFAULT, data1);

    for (i=0; i<NX; i++)
     {
        for (j=0; j<NY; j++)
            printf ("%2.8e ", data1[i][j]);
        printf ("\n"); 
     }

    /*
     * Close/release resources.
     */
    H5Sclose(dataspace);
    H5Dclose(dataset);
    H5Fclose(file);
 
    return 0;
}     
