Writing Data to Simple HDF5 Files with Mathematica

HDF5 files (https://support.hdfgroup.org) are a convenient format for storing related data and information. HDF5 is the basis of the netCDF format used in Generic Mapping Tools and the native format for the most recent versions of Matlab. Storing your data or computations in HDF5 allows you to import the data into many different tools and into the most common computer languages (Python handles them very well (see http://www.h5py.org) , C and Fortran are more tedious but well supported – see the hdfgroup pages for libraries and documentation).

Mathematica can be used to store data in HDF5 files as long as you keep things simple. To my knowledge, you cannot export HDF5 attributes using Mathematica. But you can easily create and use a relatively simple HDF5 file. Simple is often a good choice. The following lines can get you started. You simply use the Export command and it handles all the details (and there are many details, for example, if you are using Fortran).

hdf5FileName = "~/test.h5";
(* the first call creates the hdf5 file if it doesn't exist *)
Export[hdf5FileName, theModel, {"Datasets", "TheVModel"}]
(* this call appends a new data set to the hdf5 file *)
Export[hdf5FileName, dsprsn, {"Datasets", "/Love/Dispersion"}, "Append" -> True ]
(* writing text to the file *)
Export[hdf5FileName, infoString, {"Datasets", "/00README.txt"}, "Append" -> True ]

The file suffix “.h5” at the end of the filename identifies the HDF5 file format. The variables theModel and dsprsn are Mathematica lists containing numerical values. I tend to think of the lists as tables of values like you would see in a spreadsheet. I include a description of the data by exporting a string (infoString) into the 00README.txt dataset. This is not an ideal approach, but it is easy, simple, and works for my internal research applications.

Once HDF5 files are created, they can be loaded easily and displayed, used in computations, etc. You can also use simple tools to explore (and even edit) the HDF5 file to make sure that the process works. Definitely get the command line tools from https://portal.hdfgroup.org/display/support. One handy, simple interactive tool is HDFView, which I often use to double-check things in the data that I’ve written.