Charles, how are you? Attached is a snippet of IronPython that does the job.

I leave you the conversion to C# as an exercise. Ok?

 

G.

 

From: Hdf-forum [mailto:[email protected]] On Behalf Of Charles
Henderson
Sent: Wednesday, April 17, 2013 10:44 AM
To: HDF Users Discussion List
Subject: [Hdf-forum] HDF5 and C#

 

I am trying to convert one of the "C" examples into "C#" and not having much
luck.

 

"C" code:

#define FILE            "h5ex_t_cmpdatt.h5"

#define DATASET         "DS1"

#define ATTRIBUTE       "A1"

#define DIM0            4

 

typedef struct {

    int     serial_no;

    char    *location;

    double  temperature;

    double  pressure;

} sensor_t;                                 /* Compound type */

 

int

main (void)

{

    hid_t       file, filetype, memtype, strtype, space, dset, attr;

                                            /* Handles */

    herr_t      status;

    hsize_t     dims[1] = {DIM0};

    sensor_t    wdata[DIM0],                /* Write buffer */

                *rdata;                     /* Read buffer */

    int         ndims,

                i;

    /*     * Initialize data.     */

    wdata[0].serial_no = 1153;

    wdata[0].location = "Exterior (static)";

    wdata[0].temperature = 53.23;

    wdata[0].pressure = 24.57;

    wdata[1].serial_no = 1184;

    wdata[1].location = "Intake";

    wdata[1].temperature = 55.12;

    wdata[1].pressure = 22.95;

    wdata[2].serial_no = 1027;

    wdata[2].location = "Intake manifold";

    wdata[2].temperature = 103.55;

    wdata[2].pressure = 31.23;

    wdata[3].serial_no = 1313;

    wdata[3].location = "Exhaust manifold";

    wdata[3].temperature = 1252.89;

    wdata[3].pressure = 84.11;

    /*     * Create a new file using the default properties.     */

    file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    /*     * Create variable-length string datatype.     */

    strtype = H5Tcopy (H5T_C_S1);

    status = H5Tset_size (strtype, H5T_VARIABLE);

    /*     * Create the compound datatype for memory.     */

    memtype = H5Tcreate (H5T_COMPOUND, sizeof (sensor_t));

    status = H5Tinsert (memtype, "Serial number", HOFFSET (sensor_t,
serial_no), H5T_NATIVE_INT);

 

"C#" Code:

 

            string FILE = @"c:\h5ex_t_cmpdatt.h5";

            string DATASET = "DS1";

            string ATTRIBUTE = "A1";

            int DIM0 = 4;

            int filetype, space, dset, attr, status; /* Handles */

            sensor_t[] wdata = new sensor_t[DIM0];

            int ndims, i;

            /* Initialize data. */

            wdata[0].serial_no = 1153;

            wdata[0].location = "Exterior (static)";

            wdata[0].temperature = 53.23;

            wdata[0].pressure = 24.57;

            wdata[1].serial_no = 1184;

            wdata[1].location = "Intake";

            wdata[1].temperature = 55.12;

            wdata[1].pressure = 22.95;

            wdata[2].serial_no = 1027;

            wdata[2].location = "Intake manifold";

            wdata[2].temperature = 103.55;

            wdata[2].pressure = 31.23;

            wdata[3].serial_no = 1313;

            wdata[3].location = "Exhaust manifold";

            wdata[3].temperature = 1252.89;

            wdata[3].pressure = 84.11;

            /* Create a new file using the default properties. */

            H5FileId FileID = H5F.create(FILE, H5F.CreateMode.ACC_TRUNC);

            /* Create variable-length string datatype. */

            H5DataTypeId strtype = H5T.copy(H5T.H5Type.C_S1);

            status = H5T.setSize(strtype, sizeof(sensor_t));

            /* Create the compound datatype for memory. */

            H5DataTypeId memtype = H5T.create(H5T.CreateClass.COMPOUND,
sizeof(sensor_t)); 

            H5T.insert(memtype, "Serial Number",  offset, (sensor_t,
serial_no), H5T.H5Type.NATIVE_INT);

 

I am having all kinds of problems with the HDF calls.  Anyone want to jump
in and help?

The example I am trying to convert is 'h5ex_t_cmpdatt-1.c'.

 

import clr
clr.AddReferenceToFile('HDF5DotNet.dll')
import HDF5DotNet
from HDF5DotNet import *

import System
from System import Array, Byte, Double, IntPtr, Int32, Int64

import System.IO
from System.IO import BinaryReader, BinaryWriter, MemoryStream

import System.Runtime.InteropServices
from System.Runtime.InteropServices import Marshal

#===============================================================================

H5T_NATIVE_DOUBLE = H5T.H5Type.NATIVE_DOUBLE
H5T_NATIVE_INT = H5T.H5Type.NATIVE_INT
H5T_STD_I64BE = H5T.H5Type.STD_I64BE
H5T_IEEE_F64BE = H5T.H5Type.IEEE_F64BE
STRING = H5T.CreateClass.STRING

#===============================================================================

def createMemType():

    strtype = H5T.create(STRING, -1)
    strsize = H5T.getSize(strtype)

    # component name -> (offset, size, type)

    ht = { 'Serial number': (0, 4, H5T_NATIVE_INT),
           'Location': (4, strsize, strtype),
           'Temperature': (4+strsize, 8, H5T_NATIVE_DOUBLE),
           'Pressure': (4+strsize+8, 8, H5T_NATIVE_DOUBLE) }

    sizeof = 0
    for k in ht.keys():
        sizeof  += ht[k][1]

    dtype = H5T.create(H5T.CreateClass.COMPOUND, sizeof)
    for k in ht.keys():
        H5T.insert(dtype, k, ht[k][0], ht[k][2])

    H5T.close(strtype)

    return dtype

#===============================================================================

def createFileType():

    strtype = H5T.create(STRING, -1)
    strsize = H5T.getSize(strtype)

    # component name -> (offset, size, type)

    ht = { 'Serial number': (0, 8, H5T_STD_I64BE),
           'Location': (8, strsize, strtype),
           'Temperature': (8+strsize, 8, H5T_IEEE_F64BE),
           'Pressure': (16+strsize, 8, H5T_IEEE_F64BE) }

    sizeof = 0
    for k in ht.keys():
        sizeof  += ht[k][1]

    dtype = H5T.create(H5T.CreateClass.COMPOUND, sizeof)
    for k in ht.keys():
        H5T.insert(dtype, k, ht[k][0], ht[k][2])

    H5T.close(strtype)

    return dtype

#===============================================================================

def createDatasetWithCompoundType(h5file):

    mtype = createMemType() 
    ftype = createFileType() 

    npoints = 4 
    shape = Array[Int64]((npoints,))
    dspace = H5S.create_simple(shape.Length, shape)

    dset = H5D.create(h5file, 'DS1', ftype, dspace)

    ms = MemoryStream()
    writer = BinaryWriter(ms)

    writer.Write(Int32(1153))
    s = 'Exterior (static)'
    if IntPtr.Size == 8:
        writer.Write(Marshal.StringToHGlobalAnsi(s).ToInt64())
    else:
        writer.Write(Marshal.StringToHGlobalAnsi(s).ToInt32())
    writer.Write(Double(53.23))
    writer.Write(Double(24.57))

    writer.Write(Int32(1184))
    s = 'Intake'
    if IntPtr.Size == 8:
        writer.Write(Marshal.StringToHGlobalAnsi(s).ToInt64())
    else:
        writer.Write(Marshal.StringToHGlobalAnsi(s).ToInt32())
    writer.Write(Double(55.12))
    writer.Write(Double(22.95))

    writer.Write(Int32(1027))
    s = 'Intake manifold'
    if IntPtr.Size == 8:
        writer.Write(Marshal.StringToHGlobalAnsi(s).ToInt64())
    else:
        writer.Write(Marshal.StringToHGlobalAnsi(s).ToInt32())
    writer.Write(Double(103.55))
    writer.Write(Double(31.23))

    writer.Write(Int32(1313))
    s = 'Exhaust manifold'
    if IntPtr.Size == 8:
        writer.Write(Marshal.StringToHGlobalAnsi(s).ToInt64())
    else:
        writer.Write(Marshal.StringToHGlobalAnsi(s).ToInt32())
    writer.Write(Double(1252.89))
    writer.Write(Double(84.11))

    byteArray = ms.ToArray()

    H5D.write(dset, mtype, H5Array[Byte](byteArray))

    H5S.close(dspace)
    H5T.close(ftype)
    H5T.close(mtype)

    return dset

#===============================================================================

def readFromDatasetWithCompoundType(dset):

    mtype = createMemType() 
    sizeof = H5T.getSize(mtype)
    dspace = H5D.getSpace(dset)
    npoints = H5S.getSimpleExtentNPoints(dspace)
    
    shape = Array[Int64]((npoints*sizeof,))
    byteArray = Array.CreateInstance(Byte, shape)

    H5D.read(dset, mtype, H5Array[Byte](byteArray))

    ms = MemoryStream(byteArray)
    reader = BinaryReader(ms)

    for i in range(npoints):
        if IntPtr.Size == 8:
            print '%d,%s,%.2f,%.2f' % (
                reader.ReadInt32(),
                Marshal.PtrToStringAnsi(IntPtr(reader.ReadInt64())),
                reader.ReadDouble(),
                reader.ReadDouble())
        else:
            print '%d,%s,%.2f,%.2f' % (
                reader.ReadInt32(),
                Marshal.PtrToStringAnsi(IntPtr(reader.ReadInt32())),
                reader.ReadDouble(),
                reader.ReadDouble())

    H5S.close(dspace)
    H5T.close(mtype)

    return None

#===============================================================================

print '\nInitializing HDF5 library\n'
status = H5.Open()

H5P_DEFAULT = H5PropertyListId(H5P.Template.DEFAULT)

name = 'SDScompound.h5'

print "Creating file '%s'" % name

h5file = H5F.create(name, H5F.CreateMode.ACC_TRUNC)

print '\nCreating dataset and writing to file...'

dset = createDatasetWithCompoundType(h5file)

print '\nReading from dataset...\n'

readFromDatasetWithCompoundType(dset)

print '\nDone.'

H5D.close(dset)

H5F.close(h5file)

print '\nShutting down HDF5 library\n'
status = H5.Close()
_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

Reply via email to