/***********************************************************************
*  Example for IEEE double 64 bit Big-Endian                        *
*  blw 3/20/03                                                         *
***********************************************************************/

/* HDF5 Library                                                       */ 
#include "hdf5.h"

/* System libraries to include                                        */
#include <stdlib.h>
#include <assert.h>

/* Name of file for database output                                   */
#define DATAFILE       "f64be.h5"

/* Name of dataset to create in datafile                              */
#define DATASETNAME    "TestArray"

/* Dataset dimensions                                                 */
#define ROWS           6
#define COLS           5
#define RANK           2

/* Data type used to create the datafile                              */
#define FILETYPE       H5T_IEEE_F64BE

/* Data type used in memory natively                                  */
#define MEMTYPE        H5T_NATIVE_DOUBLE

int
main (void)
{
    hid_t       file, dataset;         /* File and dataset            */
    hid_t       dataspace;             /* Dataspace handles           */
    hsize_t     dimsf[2];              /* Dataset dimensions          */
    herr_t      status;                /* Error checking              */
    double      data[6][5];                /* Data to fill dataset        */
    
    int         i, j;                  /* Looping control vars        */

    /* Data and output buffer initialization.                         */
    for (j = 0; j < ROWS; j++) {
	for (i = 0; i < COLS; i++){
	    data[j][i] = i + j;
	}
    }     
    /*
     * Example of how the data should look
     * 0, 1, 2, 3, 4,
     * 1, 2, 3, 4, 5,
     * 2, 3, 4, 5, 6,
     * 3, 4, 5, 6, 7,
     * 4, 5, 6, 7, 8,
     * 5, 6, 7, 8, 9
     */

    /* Create the datafile                                            */
    file = H5Fcreate(DATAFILE, H5F_ACC_TRUNC, H5P_DEFAULT,
		    H5P_DEFAULT);
    assert (file >= 0);

    /* Array setting the dimension sizes for the dataspace            */
    dimsf[0] = ROWS;
    dimsf[1] = COLS;

    /* Creating the dataspace                                         */
    dataspace = H5Screate_simple(RANK, dimsf, NULL); 
    assert (dataspace >= 0);

    /* Creating the dataset within the dataspace                      */
    dataset = H5Dcreate(file, DATASETNAME, FILETYPE, dataspace,
			H5P_DEFAULT);
    assert (dataset >= 0);

    /* Writing the data to the dataset                                */
    status = H5Dwrite(dataset, MEMTYPE, H5S_ALL, H5S_ALL,
		      H5P_DEFAULT, data);
    assert (status >= 0);

    /* Close the dataspace                                            */
    status = H5Sclose(dataspace);
    assert (status >= 0);

    /* Close the dataset                                              */
    status = H5Dclose(dataset);
    assert (status >= 0);

    /* Close the datefile                                             */
    status = H5Fclose(file);
    assert (status >= 0);
 
    return 0;
}     
