Apollo3zehn / PureHDF

A pure .NET library that makes reading and writing of HDF5 files (groups, datasets, attributes, ...) very easy.
MIT License
47 stars 16 forks source link

Unable to read variable-length attribute #51

Closed melmar99 closed 6 months ago

melmar99 commented 6 months ago

Hello, reading the variable-length attribute is causing the following exception: Exception: Variable-length sequence data can only be decoded as array (incompatible type: System.String). The exception says that type System.String is incompatible even though a type string[] has been used. The code below successfully reads the variable-length type dataset, but reading the variable-length type attribute fails.

            var dataset_NUTS_keys = nativeFile.Dataset("/NUTS_keys");

            foreach (var attribute in dataset_NUTS_keys.Attributes())
            {
                var typeClass = attribute.Type.Class; // attribute.Type.Class is VariableLength

                //This attribute has the name DIMENSION_LIST and should contain the text 'NUTS'
                string[] attributeArray = attribute.Read<string[]>(); // Exception
            }

            var typeClass_NUTS_keys = dataset_NUTS_keys.Type.Class; // Type.Class is VariableLength

            string[] NUTS_keys = dataset_NUTS_keys.Read<string[]>(); // Successful

A copy of the HDF5 file used is here

If attribute.Read<string[][]>() is used the exception is: Bitfield data can only be decoded as NativeObjectReference1 (incompatible type: System.String) If NativeObjectReference1 is used the exception is: Unable to decode a reference type as value type.

Any help would be greatly appreciated.

Apollo3zehn commented 6 months ago

Thanks for reporting that issue, I will check it now!

Apollo3zehn commented 6 months ago

It works if I try to read the attribute like this:

using PureHDF;
using PureHDF.VOL.Native;

var file = H5File
    .OpenRead("/home/vincent/Downloads/invest/NUTS_0_sp_historical.h5.nc");

var attribute = file
    .Dataset("/NUTS_keys")
    .Attribute("DIMENSION_LIST");

var objectReferences = attribute.Read<NativeObjectReference1[][]>();
var firstObjectReference = objectReferences[0][0];
var dataset = (NativeDataset)file.Get(firstObjectReference);
var isNuts = dataset.Name == "NUTS";
melmar99 commented 6 months ago

Yes it works. Thank you very much for your help.

Apollo3zehn commented 6 months ago

Perfect :-)