在我的HDF5文件的属性上需要一个字符串值

时间:2018-06-05 15:56:59

标签: c# string encoding attributes hdf5

我正在尝试在我的数据集上添加一些属性并且它工作正常,但是在编写属性的值时我有一个问题,我需要它们是一个字符串,而不是INT也不是浮点数等等,每个教程我发现在互联网上使用这些属性的int值.. 我尝试了很多不同的东西,但没有一个能很好用,而且代码变得非常难看,这是我的代码:

    foreach (BasicVariable bv in pf.basicVariableList.bvList)
    {               
        H5DataSetId dataSetId = H5D.create(bvGroupId, bv.bvTag, typeId, spaceId);
        bv.atribList.Add(new Atribbs("Eng. Unit", "Temperatura"));
        bv.atribList.Add(new Atribbs("Atrib2", "Teste"));
        foreach(Atribbs a in bv.atribList )
        {


            H5DataTypeId auxDT = H5T.copy(H5T.H5Type.C_S1);
            H5T.setSize(auxDT, H5T.getSize(H5T.H5Type.C_S1));
            const int auxData = 2;
            long[] auxDim = new long[1];
            auxDim[0] = 1;
            H5DataSpaceId auxDS = H5S.create_simple(1, auxDim);


            var attr_id = H5A.create(dataSetId, a.attName, auxDT, auxDS);
            byte[] bytes = Encoding.UTF8.GetBytes(a.valueStr);
            var atrData = new string[] { Encoding.Default.GetString(bytes)};
            H5A.write<string>(attr_id, auxDT, new H5Array<string>(atrData));

            H5A.close(attr_id);
        }
        H5D.close(dataSetId);
    }
    H5F.close(fileId);

我创建的属性是&#34; Eng。单元&#34;和&#34; Atrib2&#34;这工作正常,问题是&#34; Eng。单元&#34;应该有&#34; Temperatura&#34;价值和&#34; Atrib2&#34;应该有&#34; Teste&#34;因为它的价值,这就是我在hdfl查看器上获得的 enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

也许你想看看HDFql,这大大简化了HDF5文件的处理方式。下面是在C#中使用HDFql的解决方案(假设HDF5文件名为test.h5,数据集名为dset):

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

HDFql.Execute("CREATE ATTRIBUTE \"dset/Eng. Unit\" AS VARCHAR VALUES(Temperatura)");   // create attribute "Eng. Unit" in dataset "dset" with an initial value of "Temperatura"

HDFql.Execute("CREATE ATTRIBUTE dset/Atrib2 AS VARCHAR VALUES(Teste)");   // create attribute "Atrib2" in dataset "dset" with an initial value of "Teste"