如何使用 C# .net 中的 HDF5DotNet 读取 HDF5 多维数组数据集?

我需要读取一个包含 3 x 3 数组的 HDF5 数据集,数据类型为双精度。


从下面的代码返回时,我的 Visual Studio 2017 Pro 崩溃。


使用调试器,此代码从 HDF5 读取 2-dim 数组;我可以在调试器中读取它;但是函数 read_double_array 在返回给调用者时会崩溃。


来自 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;

}

我的代码第一次尝试,崩溃了……………………


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

}


慕标5832272
浏览 505回答 2
2回答

开满天机

我让它工作了。这是我的代码...&nbsp; &nbsp; // Reads a 2-dim array of double.// INPUT:&nbsp; fileId of open HDF5 filepublic static bool&nbsp; read_double_array( H5FileId fileId, string dataset_name, int dim1, int dim2, ref double [,] output_double_array ){&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; output_double_array = new double[ dim1, dim2 ];&nbsp; &nbsp; &nbsp; &nbsp; H5Array<double> h5_array = new H5Array<double>( output_double_array );&nbsp; &nbsp; &nbsp; &nbsp; H5DataSetId dataset = H5D.open( fileId, dataset_name);&nbsp; &nbsp; &nbsp; &nbsp; H5D.read<double>(&nbsp; &nbsp;dataset,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new H5DataTypeId(H5T.H5Type.NATIVE_DOUBLE),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; h5_array);&nbsp; &nbsp; }&nbsp; &nbsp; catch( HDFException e )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine( e.Message );&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; return true;}

qq_笑_17

避免严重的痛苦(由于 HDF5 低级实现细节)并查看HDFql。这是一个使用 C# 中的 HDFql 来读取名为 double 的数据类型的多维(大小 3x3)数据集的解决方案,该dset数据类型存储在名为的 HDF5 文件中test.h5(假设文件和数据集都已经存在):// use HDFql namespace (make sure it can be found by the C# compiler)using AS.HDFql;public class Test{&nbsp; &nbsp; public static void Main(string []args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // declare variables&nbsp; &nbsp; &nbsp; &nbsp; double [,]data = new double[3, 3];&nbsp; &nbsp; &nbsp; &nbsp; int x;&nbsp; &nbsp; &nbsp; &nbsp; int y;&nbsp; &nbsp; &nbsp; &nbsp; // select (i.e. read) dataset "dset" from HDF5 file "test.h5" into variable "data"&nbsp; &nbsp; &nbsp; &nbsp; HDFql.Execute("SELECT FROM test.h5 dset INTO MEMORY " + HDFql.VariableTransientRegister(data));&nbsp; &nbsp; &nbsp; &nbsp; // display content of variable "data"&nbsp; &nbsp; &nbsp; &nbsp; for(x = 0; x < 3; x++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(y = 0; y < 3; y++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine(data[x, y]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP