Use of hdf5 library in Python with ctypes
<p>I would like to use <code>hdf5</code> library directly from <code>Python</code> with <code>ctypes</code>. I know that <code>h5py</code> and <code>PyTables</code> do the job perfectly. The reason I want to do this: I need to work with <code>hdf5</code> files with a <code>Python</code> interpreter where I can not install any package.</p>

<p>I am looking for an example that creates a file and write a list of doubles. </p>

<p>So far, I have written</p>

<pre><code>from ctypes import *
hdf5Lib=r'/usr/local/lib/libhdf5.dylib'
lib=cdll.LoadLibrary(hdf5Lib)
major = c_uint()
minor = c_uint()
release = c_uint()
lib.H5get_libversion(byref(major), byref(minor), byref(release))
H5Fopen=lib.H5Fopen
...
</code></pre>

<p>I don't know how to call H5Fopen. Should I use <code>H5Fopen.argtypes</code>? Any advice is welcome is to open the hdf5 file, create a dataset that of doubles, write the data and close the file.</p>

<p>I haven't (yet) done any Hdf5 programming, but looking through <a href="https://www.hdfgroup.org/HDF5/doc/RM/RM_H5F.html#File-Open" rel="nofollow">the documentation</a>, I see a few things to tell you.</p>

<p>What arguments you would pass to H5Fopen: the name is the filename you want to open.  flags will be either H5F_ACC_RDWR (1) or H5F_ACC_RDONLY (0).  These flags are mutually exclusive; don't or them together.  The second int is the identifier for the file access properties list.  You can set it to H5P_DEFAULT (0) for basic cases.</p>

<p>Here's the important thing from the documentation, though:</p>

<blockquote>
  <p><strong>Note that H5Fopen does not create a file if it does not already exist; see H5Fcreate.</strong></p>
</blockquote>

<p>From what you described, you don't want to use H5Fopen.  You want to use H5Fcreate.</p>

<p>For H5Fcreate, here's the signature</p>

<pre><code>hid_t H5Fcreate( const char *name, unsigned flags, hid_t fcpl_id, hid_t fapl_id )
</code></pre>

<p>Name is the filename.  fcpl_id and fapl_id can both be H5P_DEFAULT, i.e. 0.  Your flags in this case are H5F_ACC_TRUNC (2) to overwrite and existing file or H5F_ACC_EXCL (4) to cause an error when you try to create an existing file.</p>

<p>I'm assuming you have access to find out what the constant values I listed are, but if you don't, you can look them up as I did, at <a href="http://stainless-steel.github.io/hdf5/hdf5_sys/constant.H5F_ACC_EXCL.html" rel="nofollow">http://stainless-steel.github.io/hdf5/hdf5_sys/constant.H5F_ACC_EXCL.html</a>.</p>

<p>This information, combined with @101's example of how to invoke the functions should give you enough to make your initial file.</p>

python ctypes hdf5