Note that there are some explanatory texts on larger screens.

plurals
  1. POHDF5 C++ interface: writing dynamic 2D arrays
    text
    copied!<p>I am using the <a href="http://www.hdfgroup.org/HDF5/doc/cpplus_RM/index.html" rel="nofollow noreferrer">HDF5 C++ API</a> to write 2D array dataset files. The HDF Group has <a href="ftp://ftp.hdfgroup.org/HDF5/current/src/unpacked/c++/examples/create.cpp" rel="nofollow noreferrer">an example to create</a> a HDF5 file from a statically defined array size, which I've modified to suite my needs below. However, I require a dynamic array, where both <code>NX</code> and <code>NY</code> are determined at runtime. I've found <a href="https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-using-new">another solution to create 2D arrays using the "new" keyword</a> to help create a dynamic array. Here is what I have:</p> <pre><code>#include "StdAfx.h" #include "H5Cpp.h" using namespace H5; const H5std_string FILE_NAME("C:\\SDS.h5"); const H5std_string DATASET_NAME("FloatArray"); const int NX = 5; // dataset dimensions const int NY = 6; int main (void) { // Create a 2D array using "new" method double **data = new double*[NX]; for (int j = 0; j &lt; NX; j++) // 0 1 2 3 4 5 { // 1 2 3 4 5 6 data[j] = new double[NY]; // 2 3 4 5 6 7 for (int i = 0; i &lt; NY; i++) // 3 4 5 6 7 8 data[j][i] = (float)(i + j); // 4 5 6 7 8 9 } // Create HDF5 file and dataset H5File file(FILE_NAME, H5F_ACC_TRUNC); hsize_t dimsf[2] = {NX, NY}; DataSpace dataspace(2, dimsf); DataSet dataset = file.createDataSet(DATASET_NAME, PredType::NATIVE_DOUBLE, dataspace); // Attempt to write data to HDF5 file dataset.write(data, PredType::NATIVE_DOUBLE); // Clean up for(int j = 0; j &lt; NX; j++) delete [] data[j]; delete [] data; return 0; } </code></pre> <p>The resulting file, however, is not as expected (output from <a href="http://www.hdfgroup.org/HDF5/doc/RM/Tools.html#Tools-Dump" rel="nofollow noreferrer"><code>hdf5dump</code></a>):</p> <pre><code>HDF5 "SDS.h5" { GROUP "/" { DATASET "FloatArray" { DATATYPE H5T_IEEE_F64LE DATASPACE SIMPLE { ( 5, 6 ) / ( 5, 6 ) } DATA { (0,0): 4.76465e-307, 4.76541e-307, -7.84591e+298, -2.53017e-098, 0, (0,5): 3.8981e-308, (1,0): 4.76454e-307, 0, 2.122e-314, -7.84591e+298, 0, 1, (2,0): 2, 3, 4, 5, -2.53017e-098, -2.65698e+303, (3,0): 0, 3.89814e-308, 4.76492e-307, 0, 2.122e-314, -7.84591e+298, (4,0): 1, 2, 3, 4, 5, 6 } } } } </code></pre> <p>The problem stems back to how the 2D array was created (since this example works fine with a static array method). As I understand from <a href="http://mail.hdfgroup.org/pipermail/hdf-forum_hdfgroup.org/2008-June/001118.html" rel="nofollow noreferrer">this email thread</a>:</p> <blockquote> <p>The HDF5 library expects to a contiguous array of elements, not pointers to elements in lower dimensions</p> </blockquote> <p>As I am rather new to C++/HDF5, I'm not sure how to create a dynamically sized array at runtime that is a contiguous array of elements. I do not want to do the more complicated "hyperslab" method described in the email thread, as this looks overly complicated. Any help is appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload