简体   繁体   中英

how to read an HDF5 multi-dimensional array dataset using HDF5DotNet from C# .net?

I need to read a HDF5 dataset that contains a 3 x 3 array, datatype is double.

My Visual Studio 2017 Pro crashes when returning from my code below.

Stepping with debugger, this code reads the 2-dim array from the HDF5; I can read it in the debugger; but function read_double_array crashes when it returns to caller.

2ND CODE ATTEMPT FROM EXAMPLE AT STACKO ….

    public static double[,]  read_double_array( H5FileId fileId, string dataset_name, int dim1, int dim2 )
{
    double [,] return_data = new double[ dim1, dim2 ];
    try
    {
        H5Array<double> h5_array = new H5Array<double>( return_data );
        H5DataSetId double_array_dataset  = H5D.open( fileId, dataset_name );
        H5D.read<double>(   double_array_dataset, 
                            new H5DataTypeId(H5T.H5Type.NATIVE_DOUBLE), 
                            h5_array );
        H5D.close(double_array_dataset);

    }
    catch( HDFException e )
    {
        Console.WriteLine( e.Message );
        int aa=0;
    }
    return return_data;
}

MY CODE 1ST ATTEMPT, WHICH CRASHED ……………………

public static double[,]  read_double_array( H5FileId fileId, string dataset_name, int dim1, int dim2 )
{
        double [,] return_data = new double[ dim1, dim2 ];
    try
    {
        H5DataSetId dataSetId  = H5D.open( fileId, dataset_name );
        H5D.read(   dataSetId, 
                    new H5DataTypeId( H5T.H5Type.NATIVE_DOUBLE ),
                    new H5Array<double>( return_data ) );
    }
    catch( HDFException e )
    {
        Console.WriteLine( e.Message );
        int aa=0;
    }
    return return_data;  <<<<<<<<<<<<<<<<<<   H A N G S   H E R E
}

Save yourself from serious pain (due to HDF5 low-level implementation details) and check out HDFql .

Here goes a solution using HDFql in C# to read a multi-dimensional (size 3x3) dataset named dset of data type double stored in HDF5 file named test.h5 (assume that both the file and the dataset already exist):

// use HDFql namespace (make sure it can be found by the C# compiler)
using AS.HDFql;

public class Test
{
    public static void Main(string []args)
    {
        // declare variables
        double [,]data = new double[3, 3];
        int x;
        int y;

        // use (i.e. open) HDF file "test.h5"
        HDFql.Execute("USE FILE test.h5");

        // register variable "data" for subsequent use (by HDFql)
        HDFql.VariableRegister(data);

        // select (i.e. read) dataset "dset" into variable "data"
        HDFql.Execute("SELECT FROM dset INTO MEMORY " + HDFql.VariableGetNumber(data));

        // unregister variable "data" as it is no longer used/needed (by HDFql)
        HDFql.VariableUnregister(data);

        // display content of variable "data"
        for(x = 0; x < 3; x++)
        {
            for(y = 0; y < 3; y++)
            {
                System.Console.WriteLine(data[x, y]);
            }
        }
    }
}

I got it working. Here is my code...

    // Reads a 2-dim array of double.
// INPUT:  fileId of open HDF5 file
public static bool  read_double_array( H5FileId fileId, string dataset_name, int dim1, int dim2, ref double [,] output_double_array )
{

    try
    {
        output_double_array = new double[ dim1, dim2 ];
        H5Array<double> h5_array = new H5Array<double>( output_double_array );
        H5DataSetId dataset = H5D.open( fileId, dataset_name);
        H5D.read<double>(   dataset, 
                            new H5DataTypeId(H5T.H5Type.NATIVE_DOUBLE), 
                            h5_array);
    }
    catch( HDFException e )
    {
        Console.WriteLine( e.Message );
        return false;
    }
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM