Header First

This is a debugging block

Header Second

This is a debugging block

Branding

This is a debugging block

User Bar First

This is a debugging block

User Bar Second

This is a debugging block

HDF5 example

Content

This is a debugging block

HDF5 is a data model, library, and file format for storing and managing data. It supports an unlimited variety of datatypes, and is designed for flexible and efficient I/O and for high volume and complex data. HDF5 is portable and is extensible, allowing applications to evolve in their use of HDF5. The HDF5 Technology suite includes tools and applications for managing, manipulating, viewing, and analyzing data in the HDF5 format.

Website: http://www.hdfgroup.org/HDF5/
Reference documation: http://www.hdfgroup.org/HDF5/doc/RM/RM_H5Front.html

A simple HDF5 example in C++. Compile with:

g++ hdf-example.cpp -o hdf-example -lhdf5

It writes an array, a int and a double to a file and then reads them again.

#include <iostream>
#include <cmath>
#include <hdf5.h>

// macro to help check return status of HDF5 functions
#define HDF5_STATUS_CHECK(status) {		    \
    if(status < 0)				    \
    std::cerr << __FILE__ << ":" << __LINE__ <<	    \
    ": Problem with writing to file. Status code="  \
    << status << std::endl;			    \
}

void write(double *array, int arraysize, int &array_attribute, int &somevar, double &parameter);
void read(double *array, int arraysize, int &array_attribute, int &somevar, double &parameter);

/* writes some data to hdf5 file
 * and then read it back again
 */

int main(void)
{
    // data
    int arraysize = 100;
    double *array = new double [arraysize];
    int array_attribute = 10;
    int somevar = 19;
    double parameter = 1.487845;

    // fill array with random data
    for(int i=0;i<arraysize;i++)
	array[i] = i;

    write(array, arraysize, array_attribute, somevar, parameter);

    delete [] array;
    array = new double [arraysize];
    array_attribute = 0;
    somevar = 0;
    parameter = 0;

    read(array, arraysize, array_attribute, somevar, parameter);

    for(int i=0;i<arraysize;i++)
	std::cout << i << "\t" << array[i] << std::endl;

    std::cout << "array attribute: " << array_attribute << std::endl;
    std::cout << "somevar: " << somevar << std::endl;
    std::cout << "parameter: " << parameter << std::endl;

    delete [] array;

    return 0;
}

void write(double *array, int arraysize, int &array_attribute, int &somevar, double &parameter)
{
    // help variables for HDF5
    hid_t       file_id, group_id, dataset_id, dataspace_id, attribute_id;
    herr_t      status;

    // new hdf5 file
    file_id = H5Fcreate("file.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    HDF5_STATUS_CHECK(file_id);

    // make group to put our data in
    group_id = H5Gcreate(file_id, "/mydata", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    HDF5_STATUS_CHECK(group_id);


    /* write array: first make dataspace of needed size and then write */
    hsize_t dimarray = arraysize;

    // make dataspace for a array
    dataspace_id = H5Screate_simple(1, &dimarray, NULL);

    // make dataset under our group
    dataset_id = H5Dcreate(group_id, "array", H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    // fill dataset
    status = H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, array );
    HDF5_STATUS_CHECK(status);

    // Terminate access to the data space.
    status = H5Sclose(dataspace_id);
    HDF5_STATUS_CHECK(status);

    // write an attribute before closing the array
    // First, make the dataspace
    dataspace_id = H5Screate(H5S_SCALAR);

    attribute_id = H5Acreate (dataset_id, "array_attribute", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
    status = H5Awrite (attribute_id, H5T_NATIVE_INT, &array_attribute );
    HDF5_STATUS_CHECK(status);

    // Terminate access to the data space.
    status = H5Sclose(dataspace_id);
    HDF5_STATUS_CHECK(status);

    // Terminate the attribute
    status = H5Aclose(attribute_id);
    HDF5_STATUS_CHECK(status);

    // End access to the dataset and release resources used by it.
    status = H5Dclose(dataset_id);
    HDF5_STATUS_CHECK(status);

    /* write the some_var and parameter to file */

    // make one dimensional dataspace
    dataspace_id = H5Screate(H5S_SCALAR);

    dataset_id = H5Dcreate(group_id, "somevar", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &somevar );
    HDF5_STATUS_CHECK(status);

    // End access to the dataset and release resources used by it.
    status = H5Dclose(dataset_id);
    HDF5_STATUS_CHECK(status);

    dataset_id = H5Dcreate(group_id, "parameter", H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    status = H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &parameter );
    HDF5_STATUS_CHECK(status);

    // End access to the dataset and release resources used by it.
    status = H5Dclose(dataset_id);
    HDF5_STATUS_CHECK(status);

    // Terminate access to the data space.
    status = H5Sclose(dataspace_id);
    HDF5_STATUS_CHECK(status);

    // Close the group.
    status = H5Gclose(group_id);
    HDF5_STATUS_CHECK(status);

    // Terminate access to the file.
    status = H5Fclose(file_id);
    HDF5_STATUS_CHECK(status);
}

void read(double *array, int arraysize, int &array_attribute, int &somevar, double &parameter)
{
    // help variables for HDF5
    hid_t       file_id, group_id, dataset_id, attribute_id;
    herr_t      status;

    // open file
    file_id = H5Fopen("file.h5", H5F_ACC_RDONLY, H5P_DEFAULT);
    HDF5_STATUS_CHECK(file_id);

    // open group
    group_id = H5Gopen(file_id, "/mydata", H5P_DEFAULT);
    HDF5_STATUS_CHECK(group_id);

    // make dataset for array
    dataset_id = H5Dopen(group_id, "array", H5P_DEFAULT);
    HDF5_STATUS_CHECK(dataset_id);

    // read array
    status = H5Dread(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, array);
    HDF5_STATUS_CHECK(status);

    // open attribute of array
    attribute_id = H5Aopen(dataset_id, "array_attribute", H5P_DEFAULT);
    HDF5_STATUS_CHECK(attribute_id);

    // read attribute
    status = H5Aread(attribute_id, H5T_NATIVE_INT, &array_attribute);
    HDF5_STATUS_CHECK(status);

    // close attribute
    status = H5Aclose(attribute_id);
    HDF5_STATUS_CHECK(status);

    // close array
    status = H5Dclose(dataset_id);
    HDF5_STATUS_CHECK(status);


    // make dataset for somevar
    dataset_id = H5Dopen(group_id, "somevar", H5P_DEFAULT);
    HDF5_STATUS_CHECK(dataset_id);

    // read somevar
    status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &somevar);
    HDF5_STATUS_CHECK(status);

    status = H5Dclose(dataset_id);
    HDF5_STATUS_CHECK(status);


    // make dataset for parameter
    dataset_id = H5Dopen(group_id, "parameter", H5P_DEFAULT);
    HDF5_STATUS_CHECK(dataset_id);

    // read somevar
    status = H5Dread(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &parameter);
    HDF5_STATUS_CHECK(status);

    status = H5Dclose(dataset_id);
    HDF5_STATUS_CHECK(status);


    status = H5Gclose(group_id);
    HDF5_STATUS_CHECK(status);

    status = H5Fclose(file_id);
    HDF5_STATUS_CHECK(status);
}

Postscript First

This is a debugging block

Postscript Second

This is a debugging block

Postscript Third

This is a debugging block

Preface First

This is a debugging block

Preface Second

This is a debugging block

Preface Third

This is a debugging block