HDF5实例code [英] HDF5 Example code

查看:320
本文介绍了HDF5实例code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 HDF5DotNet ,任何人都可以点我在例如code,这将打开一个HDF5文件,解压缩数据集的内容,并打印内容到标准输出?

到目前为止,我有以下几点:

  H5.Open();
        VAR H5 = H5F.open(example.h5,H5F.OpenMode.ACC_RDONLY);
        var数据= H5D.open(H5/时序/ aaPCBTimes);
        VAR空间= H5D.getSpace(数据);
        变种大小= H5S.getSimpleExtentDims(空间);

然后,它变得有点混乱。

其实我想做的数据集的内容,一些处理,但我认为一旦我有转储到标准输出,我可以从那里工作了。

更新:我已经解决这个足以砍死解决我自己的问题。我没有认识到一个数据集是一个多阵列 - 我认为这更像是一个数据库表。万一有人有兴趣,

 双击[,] dataArray中=新的双[尺寸[0],6]。
VAR wrapArray =新H5Array<双>(dataArray中);
VAR的dataType = H5D.getType(D);
H5D.read(数据集中,数据类型wrapArray);
Console.WriteLine(dataArray中[0,0]);


解决方案

试试这个:

 使用系统;
使用HDF5DotNet;命名空间CSharpExample1
{
    类节目
    {
    //功能的使用
    静态INT myFunction的(H5GroupId ID,字符串对象名,对象参数)
    {
        Console.WriteLine(对象名称{0},对象名);
        Console.WriteLine(对象参数是{0},参数);
        返回0;
    }    静态无效的主要(字串[] args)
    {
        尝试
        {
            //我们将写入和读取这个长度的int数组。
            const int的DATA_ARRAY_LENGTH = 12;            //秩是数据阵列的维数。
            const int的RANK = 1;            //创建一个HDF5文件。
            //枚举类型H5F.CreateMode只提供了法律
            //创建模式。提供缺少H5Fcreate参数
            使用默认值//。
            H5FileId FILEID = H5F.create(myCSharp.h5
                                         H5F.CreateMode.ACC_TRUNC);            //创建一个HDF5组。
            H5GroupId的groupId = H5G.create(FILEID,/ cSharpGroup,0);
            H5GroupId亚= H5G.create(的groupIdmySubGroup,0);            //展示getObjectInfo
            ObjectInfo信息= H5G.getObjectInfo(FILEID,/ cSharpGroup,真正的);
            Console.WriteLine(cSharpGroup头的大小为{0},info.headerSize);
            Console.WriteLine(cSharpGroup nlinks是{0},info.nHardLinks);
            Console.WriteLine(cSharpGroup的fileno是{0} {1},
                 info.fileNumber [0],info.fileNumber [1]);
            Console.WriteLine(cSharpGroup objno是{0} {1},
                 info.objectNumber [0],info.objectNumber [1]);
            Console.WriteLine(cSharpGroup类型是{0},info.objectType);
            H5G.close(亚组);            // prepare创建数据空间写一维
            //符号整数数组中。
            ULONG [] =变暗ULONG新[RANK]
            变暗[0] = DATA_ARRAY_LENGTH;            //把下坡的数据在阵列中,使我们能
            //它写入文件。
            INT [] = dset_data新INT [DATA_ARRAY_LENGTH]
            的for(int i = 0; I< D​​ATA_ARRAY_LENGTH;我++)
                dset_data [i] = DATA_ARRAY_LENGTH - 我;            //创建一个数据空间,以适应我们1维阵列。
            //所得H5DataSpaceId将用于创建所述
            // 数据集。
            H5DataSpaceId spaceId = H5S.create_simple(RANK,变暗);            //创建一个标准数据类型的副本。我们将使用
            //导致H5DataTypeId创建数据集。我们可以
            //已经直接在调用中使用的HST.H5Type数据
            // H5D.create,但这说明了如何使用H5T.copy的
            //并利用H5D.create一个H5DataTypeId的。
            H5DataTypeId TYPEID = H5T.copy(H5T.H5Type.NATIVE_INT);            //查找类型的大小
            UINT typeSize = H5T.getSize(typeid的);
            Console.WriteLine(typeSize是{0},typeSize);            //设置为大端
            H5T.setOrder(typeid的,H5T.Order.BE);            //设置以小尾数
            H5T.setOrder(typeid的,H5T.Order.LE);            //创建数据集。
            H5DataSetId资料集= H5D.create(FILEID,/ csharpExample
                                               typeid的,spaceId);            //整数数据写入到数据集。            H5D.write(资料集,新H5DataTypeId(H5T.H5Type.NATIVE_INT)
                              新H5Array< INT>(dset_data));            //如果我们正在写一个值就可能是这样。
            // INT singleValue = 100;
            // H5D.writeScalar(资料集,新H5DataTypeId(H5T.H5Type.NATIVE_INT)
            // REF singleValue);            //创建一个整型数组来接收读取数据。
            INT [] = readDataBack新INT [DATA_ARRAY_LENGTH]            //从数据集中读取整数数据备份
            H5D.read(资料集,新H5DataTypeId(H5T.H5Type.NATIVE_INT)
                新H5Array< INT>(readDataBack));            //回显数据
            的for(int i = 0; I< D​​ATA_ARRAY_LENGTH;我++)
            {
               Console.WriteLine(readDataBack [I]);
            }            //关闭所有打开的资源。
            H5D.close(资料集);            //重新打开并关闭数据集,以表明我们可以。
            资料集= H5D.open(FILEID,/ csharpExample);
            H5D.close(资料集);
            资料集= H5D.open(的groupId,/ csharpExample);
            H5D.close(资料集);            H5S.close(spaceId);
            H5T.close(typeid的);
            H5G.close(的groupId);            // INT X = 10;
            //H5T.enumInsert<int>(typeId的myString,楼盘X);
            //H5G.close(groupId);
            H5GIterateDelegate myDelegate;
            myDelegate = myFunction的;
            INT X = 9;
            INT指数= H5G.iterate(FILEID,/ cSharpGroup
                myDelegate中,x,0);            //重新打开组ID,表明我们可以。
            的groupId = H5G.open(FILEID,/ cSharpGroup);
            H5G.close(的groupId);            H5F.close(FILEID);            //重新打开和重新关闭文件。
            H5FileId的OpenID = H5F.open(myCSharp.h5
                                       H5F.OpenMode.ACC_RDONLY);
            H5F.close(OpenID的);
        }
        //这捕获所有的HDF异常类。因为每次调用
        //生成唯一的例外,不同的异常可以处理
        分别//。例如,要赶上我们可以使用打开错误
        //赶上(H5FopenException openException)。
        赶上(HDFException E)
        {
            Console.WriteLine(e.Message);
        }        Console.WriteLine(处理完毕!);
        到Console.ReadLine();
    }
}
}

Using HDF5DotNet, can anyone point me at example code, which will open an hdf5 file, extract the contents of a dataset, and print the contents to standard output?

So far I have the following:

        H5.Open();
        var h5 = H5F.open("example.h5", H5F.OpenMode.ACC_RDONLY);
        var dataset = H5D.open(h5, "/Timings/aaPCBTimes");
        var space = H5D.getSpace(dataset);
        var size = H5S.getSimpleExtentDims(space);

Then it gets a bit confusing.

I actually want to do some processing on the contents of the dataset but I think once I have dump to standard output I can work it out from there.

UPDATE: I've hacked around this sufficient to solve my own problem. I failed to realise a dataset was a multi-array - I thought it was more like a db table. In the unlikely event anyone is interested,

double[,] dataArray = new double[size[0], 6];
var wrapArray = new H5Array<double>(dataArray);
var dataType = H5D.getType(d);
H5D.read(dataset, dataType, wrapArray);
Console.WriteLine(dataArray[0, 0]);

解决方案

Try this:

using System;
using HDF5DotNet;

namespace CSharpExample1
{
    class Program
    {
    // Function used with 
    static int myFunction(H5GroupId id, string objectName, Object param)
    {
        Console.WriteLine("The object name is {0}", objectName);
        Console.WriteLine("The object parameter is {0}", param);
        return 0;
    }

    static void Main(string[] args)
    {
        try
        {
            // We will write and read an int array of this length.
            const int DATA_ARRAY_LENGTH = 12;

            // Rank is the number of dimensions of the data array.
            const int RANK = 1;

            // Create an HDF5 file.
            // The enumeration type H5F.CreateMode provides only the legal 
            // creation modes.  Missing H5Fcreate parameters are provided
            // with default values.
            H5FileId fileId = H5F.create("myCSharp.h5", 
                                         H5F.CreateMode.ACC_TRUNC);

            // Create a HDF5 group.  
            H5GroupId groupId = H5G.create(fileId, "/cSharpGroup", 0);
            H5GroupId subGroup = H5G.create(groupId, "mySubGroup", 0);   

            // Demonstrate getObjectInfo
            ObjectInfo info = H5G.getObjectInfo(fileId, "/cSharpGroup", true);
            Console.WriteLine("cSharpGroup header size is {0}", info.headerSize);
            Console.WriteLine("cSharpGroup nlinks is {0}", info.nHardLinks);
            Console.WriteLine("cSharpGroup fileno is {0} {1}", 
                 info.fileNumber[0], info.fileNumber[1]);
            Console.WriteLine("cSharpGroup objno is {0} {1}", 
                 info.objectNumber[0], info.objectNumber[1]);
            Console.WriteLine("cSharpGroup type is {0}", info.objectType);


            H5G.close(subGroup);

            // Prepare to create a data space for writing a 1-dimensional 
            // signed integer array.
            ulong[] dims = new ulong[RANK];
            dims[0] = DATA_ARRAY_LENGTH;

            // Put descending ramp data in an array so that we can
            // write it to the file.
            int[] dset_data = new int[DATA_ARRAY_LENGTH];
            for (int i = 0; i < DATA_ARRAY_LENGTH; i++)
                dset_data[i] = DATA_ARRAY_LENGTH - i;

            // Create a data space to accommodate our 1-dimensional array.
            // The resulting H5DataSpaceId will be used to create the 
            // data set.
            H5DataSpaceId spaceId = H5S.create_simple(RANK, dims);

            // Create a copy of a standard data type.  We will use the 
            // resulting H5DataTypeId to create the data set.  We could
            // have  used the HST.H5Type data directly in the call to 
            // H5D.create, but this demonstrates the use of H5T.copy 
            // and the use of a H5DataTypeId in H5D.create.
            H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_INT);

            // Find the size of the type
            uint typeSize = H5T.getSize(typeId);
            Console.WriteLine("typeSize is {0}", typeSize);

            // Set the order to big endian
            H5T.setOrder(typeId, H5T.Order.BE);

            // Set the order to little endian
            H5T.setOrder(typeId, H5T.Order.LE);

            // Create the data set.
            H5DataSetId dataSetId = H5D.create(fileId, "/csharpExample", 
                                               typeId, spaceId);

            // Write the integer data to the data set.

            H5D.write(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_INT),
                              new H5Array<int>(dset_data));

            // If we were writing a single value it might look like this.
            //  int singleValue = 100;
            //  H5D.writeScalar(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_INT),
            //     ref singleValue);

            // Create an integer array to receive the read data.
            int[] readDataBack = new int[DATA_ARRAY_LENGTH];

            // Read the integer data back from the data set
            H5D.read(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_INT), 
                new H5Array<int>(readDataBack));

            // Echo the data
            for(int i=0;i<DATA_ARRAY_LENGTH;i++)
            {
               Console.WriteLine(readDataBack[i]);
            }  

            // Close all the open resources.
            H5D.close(dataSetId);

            // Reopen and close the data sets to show that we can.
            dataSetId = H5D.open(fileId, "/csharpExample");
            H5D.close(dataSetId);
            dataSetId = H5D.open(groupId, "/csharpExample");
            H5D.close(dataSetId);

            H5S.close(spaceId);
            H5T.close(typeId);
            H5G.close(groupId);

            //int x = 10;
            //H5T.enumInsert<int>(typeId, "myString", ref x);
            //H5G.close(groupId);
            H5GIterateDelegate myDelegate;
            myDelegate = myFunction;
            int x = 9;
            int index = H5G.iterate(fileId, "/cSharpGroup",
                myDelegate, x, 0);

            // Reopen the group id to show that we can.
            groupId = H5G.open(fileId, "/cSharpGroup");
            H5G.close(groupId);

            H5F.close(fileId);

            // Reopen and reclose the file.
            H5FileId openId = H5F.open("myCSharp.h5", 
                                       H5F.OpenMode.ACC_RDONLY);
            H5F.close(openId);
        }
        // This catches all the HDF exception classes.  Because each call
        // generates unique exception, different exception can be handled
        // separately.  For example, to catch open errors we could have used
        // catch (H5FopenException openException).
        catch (HDFException e)
        {
            Console.WriteLine(e.Message);
        }

        Console.WriteLine("Processing complete!");
        Console.ReadLine();
    }
}
}

这篇关于HDF5实例code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆