Hello all, 

I tried to be precise in my subject. 
I am completely new to hdf5 and do not know whether I use the correct words. 
What I want to do is maybe best illustrated by a python script that already 
does what I want on the python side; I now want to define 
this same structure on the C++ side for reading and writing access. Simple data 
exchange. 

Short description:
I have three arrays of double: om1, om2, tau. 
These arrays have a runtime-determined length N.
These arrays represent a snapshot of the state of a dynamical system. 
I want to write these three arrays to disk for every time step of my simulation.

Here is the implementation on the python side (writing only one time step):

    import scipy
    from  tables import *

    N = 100
    class Particle(IsDescription):
      om1 = Float64Col(shape=(N+1))
      om2 = Float64Col(shape=(N+1))
      tau = Float64Col(shape=(N+1))
      pass

    fileh = openFile("data.h5", mode = "w")

    root = fileh.root
    group = fileh.createGroup(root, "mygroup")
    gparticles = root.mygroup
    table = fileh.createTable(gparticles, "mytable", Particle, "A Snapshot")
    
   
    om1=scipy.randn(N+1)
    om2=scipy.randn(N+1)
    tau=scipy.randn(N+!)

    particle['om1'] = om1
    particle['om2'] = om2
    particle['tau'] = tau

    # Flush the table buffers
    table.flush()
    fileh.close()
    

This thing works exactly as expected. I am, however, not capable of defining 
the proper compound struct on the C++ side that would be able to read and write 
this thing. I am capable of creating the correct compound struct IF I FIX THE 
SIZE N+1 of the arrays on the C++ side
at compile time. 
I have not found a way to do this, if N is determined at runtime.

 Here is the code that writes a similar file on the C++ side; as I say, with 
compile-time-determined size of the arrays (the group part on the python side 
is not yet implemented):

/***********************************************************************
*  Example for compound structures with multiple datatypes.            *
*  blw 4/16/03                                                         *
***********************************************************************/

/* Include the HDF5 library                                           */
#include "hdf5.h"

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

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

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

/* Dataset dimensions                                                 */
#define LENGTH        15
#define RANK          1
#define ARRAY_RANK    1

int
main(void)
{
  const int ARRAY_DIM = 101;
/* Structure and array for compound types                             */
    typedef struct Array1Struct {
        double             om1[ARRAY_DIM];
        double             om2[ARRAY_DIM];
        double             tau[ARRAY_DIM];
    } Array1Struct;
    Array1Struct       Array1[LENGTH];

    hid_t      Array1Structid;            /* File datatype identifier */
    hid_t      array_tid;                /* Array datatype handle    */
    hid_t      datafile, dataset;
    hid_t      dataspace;
    herr_t     status;
    hsize_t    dim[] = {LENGTH};          /* Dataspace dimensions     */
    hsize_t    array_dim[] = {ARRAY_DIM}; /* Array dimensions         */

    int        m, n;                      /* Array init loop vars     */

    /* Initialize the data in the arrays                              */
    for (m = 0; m< LENGTH; m++) {
        for (n = 0; n < ARRAY_DIM; n++) {
            Array1[m].om1[n] = m+n;
        }
        for (n = 0; n < ARRAY_DIM; n++) {
            Array1[m].om2[n] = m+n;
        }
        for (n = 0; n < ARRAY_DIM; n++) {
            Array1[m].tau[n] = m+n;
        }
    }

    /* Create the dataspace                                           */
    dataspace = H5Screate_simple(RANK, dim, NULL);

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

    /* Create the array data type                                     */
    array_tid = H5Tarray_create(H5T_NATIVE_DOUBLE, ARRAY_RANK,
                    array_dim, NULL);

    /* Create the memory data type                                    */
    Array1Structid = H5Tcreate (H5T_COMPOUND, sizeof(Array1Struct));
    H5Tinsert(Array1Structid, "om1", HOFFSET(Array1Struct, om1),
                    array_tid);
    H5Tinsert(Array1Structid, "om2", HOFFSET(Array1Struct, om2),
                    array_tid);
    H5Tinsert(Array1Structid, "tau", HOFFSET(Array1Struct, tau),
                    array_tid);


    /* Create the dataset                                             */
    dataset = H5Dcreate(datafile, DATASETNAME, Array1Structid,
                    dataspace, H5P_DEFAULT);

    /* Write data to the dataset                                      */
    status = H5Dwrite(dataset, Array1Structid, H5S_ALL, H5S_ALL,
                    H5P_DEFAULT, Array1);

    /* Release resources                                              */
    H5Tclose(Array1Structid);
    H5Tclose(array_tid);
    H5Sclose(dataspace);
    H5Dclose(dataset);
    H5Fclose(datafile);

    /* Return value                                                   */
    return 0;
}



My aim is to be able to read, write,edit files both on the c++ and on the 
python side for data exchange. 
All help appreciated.



_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

Reply via email to