public static T ReadEnumAttribute <T>(hid_t hid, string key) where T : struct, IConvertible
        {
            var attribute = H5A.open(hid, key);

            if (attribute < 0)
            {
                throw new ArgumentException(string.Format("Attribute {0} not found.", key));
            }

            var type = H5A.get_type(attribute);

            if (type < 0)
            {
                H5A.close(attribute);
                throw new Exception("H5A.get_type failed.");
            }

            var size = H5T.get_size(type).ToInt32();

            if (size == 0)
            {
                H5T.close(type);
                H5A.close(attribute);
                throw new Exception("H5T.get_size failed.");
            }

            var unmanagedBuffer = new UnmanagedBuffer(size);

            H5A.read(attribute, type, unmanagedBuffer);

            H5T.close(type);
            H5A.close(attribute);

            return(unmanagedBuffer.ReadEnum <T>());
        }
Beispiel #2
0
        public object this[string key]
        {
            get
            {
                return(Host.With <object>((objId) =>
                {
                    long attrId = 0;
                    long dtypeId = 0;
                    try
                    {
                        attrId = H5A.open(objId, key);
                        if (attrId < 0)
                        {
                            throw new ArgumentException($"Unknown Attribute: {key}", nameof(key));
                        }
                        dtypeId = H5A.get_type(attrId);
                        int size = H5T.get_size(dtypeId).ToInt32();

                        IntPtr iPtr = Marshal.AllocHGlobal(size);
                        int result = H5A.read(attrId, dtypeId, iPtr);
                        if (result < 0)
                        {
                            throw new IOException("Failed to read attribute");
                        }

                        if (H5T.equal(dtypeId, H5T.NATIVE_INT64) > 0)
                        {
                            var dest = new long[1];
                            Marshal.Copy(iPtr, dest, 0, 1);
                            Marshal.FreeHGlobal(iPtr);
                            return dest[0];
                        }
                        else // Must be a string...
                        {
                            var dest = new byte[size];
                            Marshal.Copy(iPtr, dest, 0, size);
                            Marshal.FreeHGlobal(iPtr);
                            return Encoding.ASCII.GetString(dest).TrimEnd((Char)0);
                        }

//                        return null;
                    }
                    finally
                    {
                        if (attrId > 0)
                        {
                            H5A.close(attrId);
                        }
                        if (dtypeId > 0)
                        {
                            H5T.close(dtypeId);
                        }
                    }
                }));
            }
        }
Beispiel #3
0
        public static bool WriteStringAttribute(hid_t hid, string key, string value, bool utf8 = true, bool variable = true)
        {
            if (H5A.exists(hid, key) == 0)
            {
                // Attribute doesn't exist.
                return(variable ? CreateOrOverwriteVariableStringAttribute(hid, key, new string[] { value }, utf8)
                    : CreateOrOverwriteFixedStringAttribute(hid, key, value, utf8));
            }
            else
            {
                var attribute = H5A.open(hid, key);
                if (attribute < 0)
                {
                    return(false);
                }

                var type = H5A.get_type(attribute);
                if (type < 0)
                {
                    H5A.close(attribute);
                    return(false);
                }

                var typeClass = H5T.get_class(type);
                if (typeClass == H5T.class_t.NO_CLASS)
                {
                    H5T.close(type);
                    H5T.close(attribute);
                    return(false);
                }

                if (typeClass != H5T.class_t.STRING)
                {
                    H5T.close(type);
                    H5T.close(attribute);
                    throw new ArgumentException("H5T.get_class(type) != H5T.class_t.STRING");
                }

                utf8 = H5T.get_cset(type) == H5T.cset_t.UTF8;

                bool ok;
                if (H5T.is_variable_str(type) > 0)
                {
                    ok = CreateOrOverwriteVariableStringAttribute(hid, key, new string[] { value }, utf8);
                }
                else
                {
                    ok = CreateOrOverwriteFixedStringAttribute(hid, key, value, utf8);
                }

                H5T.close(type);
                H5T.close(attribute);

                return(ok);
            }
        }
Beispiel #4
0
        public static Hdf5DataType GetDataTypeFromAttribute(Hdf5Identifier _objectId)
        {
            var typeId = H5A.get_type(_objectId.Value).ToId();

            if (typeId.Value > 0)
            {
                return(GetDataTypeByType(typeId));
            }

            return(null);
        }
Beispiel #5
0
        public static (bool success, string[] result) ReadStrings(long groupId, string name, string alternativeName)
        {
            var datasetId = H5D.open(groupId, Hdf5Utils.NormalizedName(name));

            if (datasetId < 0) //does not exist?
            {
                datasetId = H5D.open(groupId, Hdf5Utils.NormalizedName(alternativeName));
            }

            if (datasetId <= 0)
            {
                Hdf5Utils.LogError?.Invoke($"Error reading {groupId}. Name:{name}. AlternativeName:{alternativeName}");
                return(false, null);
            }

            long typeId  = H5A.get_type(datasetId);
            long spaceId = H5A.get_space(datasetId);
            long count   = H5S.get_simple_extent_npoints(spaceId);

            H5S.close(spaceId);

            var strs = new List <string>();

            if (count >= 0)
            {
                IntPtr[] rdata = new IntPtr[count];
                GCHandle hnd   = GCHandle.Alloc(rdata, GCHandleType.Pinned);
                H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL,
                         H5P.DEFAULT, hnd.AddrOfPinnedObject());

                for (int i = 0; i < rdata.Length; ++i)
                {
                    int len = 0;
                    while (Marshal.ReadByte(rdata[i], len) != 0)
                    {
                        ++len;
                    }
                    byte[] buffer = new byte[len];
                    Marshal.Copy(rdata[i], buffer, 0, buffer.Length);
                    string s = Encoding.UTF8.GetString(buffer);

                    strs.Add(s);

                    // H5.free_memory(rdata[i]);
                }
                hnd.Free();
            }
            H5T.close(typeId);
            H5D.close(datasetId);
            return(true, strs.ToArray());
        }
Beispiel #6
0
        public static T ReadScalarNumericAttribute <T>(hid_t attribute) where T : struct
        {
            var space = H5A.get_space(attribute);

            if (space < 0)
            {
                throw new Exception("Failed to get space");
            }

            if (H5S.get_simple_extent_type(space) != H5S.class_t.SCALAR)
            {
                H5S.close(space);
                throw new Exception("Not a scalar data space");
            }

            H5S.close(space);

            var type = H5A.get_type(attribute);

            if (type < 0)
            {
                throw new Exception("Failed to get type");
            }

            var typeClass = H5T.get_class(type);

            H5T.close(type);
            if (typeClass != H5T.class_t.INTEGER && typeClass != H5T.class_t.FLOAT)
            {
                throw new Exception("Not an integral or floating point data type");
            }

            // TODO:
            // Check if value can be cast to type of this attribute.
            //
            // TODO:
            // T[] value = new T[1];
            // H5A.read(attribute, NumericTypeToHDF5Type<T>(), new PinnedObject(value));
            object boxedValue = new T();

            if (H5A.read(attribute, NumericTypeToHDF5Type <T>(), new PinnedObject(boxedValue)) < 0)
            {
                throw new Exception("Failed to read attribute");
            }

            return((T)boxedValue);
        }
Beispiel #7
0
        /// <summary>
        /// Loads all attributes on an object into the supplied attributes collection.
        /// Assumes that the object is already open.
        /// </summary>
        /// <param name="_attributes"></param>
        public static void LoadAttributes(
            Hdf5Attributes _attributes)
        {
            ulong n = 0;

            AbstractHdf5Object obj = _attributes.ParentObject;

            int id = H5A.iterate(obj.Id.Value, H5.index_t.NAME, H5.iter_order_t.NATIVE, ref n,
                                 delegate(int _id, IntPtr _namePtr, ref H5A.info_t _ainfo, IntPtr _data)
            {
                string attributeName = Marshal.PtrToStringAnsi(_namePtr);

                var attributeId = H5A.open(_id, attributeName).ToId();
                if (attributeId.Value > 0)
                {
                    var attributeTypeId = H5A.get_type(attributeId.Value).ToId();
                    var type            = TypeHelper.GetDataTypeByType(attributeTypeId);

                    if (attributeTypeId.Value > 0)
                    {
                        Hdf5Attribute attribute = null;
                        if (type.NativeType.Value == H5T.C_S1)
                        {
                            attribute = GetStringAttribute(obj.Id, attributeName);
                        }
                        else
                        {
                            attribute = GetAttribute(attributeId, attributeName, type);
                        }

                        if (attribute != null)
                        {
                            _attributes.Add(attribute);
                        }

                        H5T.close(attributeTypeId.Value);
                    }

                    H5A.close(attributeId.Value);
                }

                return(0);
            }, new IntPtr());
        }
Beispiel #8
0
        public static (long AttributeId, bool IsNew) OpenOrCreateAttribute(long locationId, string name, long attributeTypeId, Func <long> createAttributeCallback)
        {
            long attributeId            = -1;
            long attributeTypeId_actual = -1;

            bool isNew;

            try
            {
                if (H5A.exists(locationId, name) > 0)
                {
                    attributeId            = H5A.open(locationId, name);
                    attributeTypeId_actual = H5A.get_type(attributeId);

                    if (H5T.equal(attributeTypeId_actual, attributeTypeId) <= 0)
                    {
                        throw new Exception($"{ ErrorMessage.IOHelper_DataTypeMismatch } Attribute: '{ name }'.");
                    }

                    isNew = false;
                }
                else
                {
                    attributeId = createAttributeCallback.Invoke();

                    isNew = true;
                }

                if (H5I.is_valid(attributeId) <= 0)
                {
                    throw new Exception($"{ ErrorMessage.IOHelper_CouldNotOpenOrCreateAttribute } Attribute: '{ name }'.");
                }
            }
            finally
            {
                if (H5I.is_valid(attributeTypeId_actual) > 0)
                {
                    H5T.close(attributeTypeId_actual);
                }
            }

            return(attributeId, isNew);
        }
Beispiel #9
0
        /// <summary>
        /// Reads a string scalar attribute value.
        /// Assumes that the parent object is already open.
        /// </summary>
        /// <param name="_objectId"></param>
        /// <param name="_title"></param>
        /// <returns></returns>
        public static Hdf5Attribute GetStringAttribute(Hdf5Identifier _objectId, string _title)
        {
            int attributeId = 0;
            int typeId      = 0;

            attributeId = H5A.open(_objectId.Value, _title);
            typeId      = H5A.get_type(attributeId);
            var sizeData = H5T.get_size(typeId);
            var size     = sizeData.ToInt32();

            byte[] strBuffer = new byte[size];

            var      aTypeMem    = H5T.get_native_type(typeId, H5T.direction_t.ASCEND);
            GCHandle pinnedArray = GCHandle.Alloc(strBuffer, GCHandleType.Pinned);

            H5A.read(attributeId, aTypeMem, pinnedArray.AddrOfPinnedObject());
            pinnedArray.Free();
            H5T.close(aTypeMem);

            string value = System.Text.Encoding.ASCII.GetString(strBuffer, 0, strBuffer.Length - 1);

            var attribute = new Hdf5Attribute
            {
                Id    = attributeId.ToId(),
                Name  = _title,
                Value = value
            };

            if (attributeId > 0)
            {
                H5A.close(attributeId);
            }

            if (typeId > 0)
            {
                H5T.close(typeId);
            }

            return(attribute);
        }
Beispiel #10
0
        public static bool WriteScalarNumericAttribute <T>(hid_t attribute, T value, hid_t type) where T : struct
        {
            var space = H5A.get_space(attribute);

            if (space < 0)
            {
                throw new Exception("Failed to get space");
            }

            if (H5S.get_simple_extent_type(space) != H5S.class_t.SCALAR)
            {
                H5S.close(space);
                throw new Exception("Not a scalar data space");
            }

            H5S.close(space);

            var attributeType = H5A.get_type(attribute);

            if (attributeType < 0)
            {
                throw new Exception("Failed to get type");
            }

            var attributeTypeClass = H5T.get_class(attributeType);

            H5T.close(attributeType);
            if (attributeTypeClass != H5T.class_t.INTEGER && attributeTypeClass != H5T.class_t.FLOAT)
            {
                throw new Exception("Not an integral or floating point data type");
            }

            // TODO:
            // Check if value can be cast to type of this attribute.
            object boxedValue = value;

            H5A.write(attribute, type, new PinnedObject(boxedValue));

            return(true);
        }
Beispiel #11
0
        public void H5Aget_typeTest1()
        {
            hid_t att = H5A.create(m_v2_test_file, "A", H5T.IEEE_F64LE,
                                   m_space_scalar);

            Assert.IsTrue(att >= 0);
            hid_t type = H5A.get_type(att);

            Assert.IsTrue(type >= 0);
            Assert.IsTrue(H5T.equal(type, H5T.IEEE_F64LE) > 0);
            Assert.IsTrue(H5T.close(type) >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            att = H5A.create(m_v0_test_file, "A", H5T.IEEE_F64LE,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            type = H5A.get_type(att);
            Assert.IsTrue(type >= 0);
            Assert.IsTrue(H5T.equal(type, H5T.IEEE_F64LE) > 0);
            Assert.IsTrue(H5T.close(type) >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);
        }
Beispiel #12
0
        public static object ReadAttribute(long locationId, string attributeName)
        {
            long attributeId = -1;
            long typeId      = -1;

            object result;

            try
            {
                attributeId = H5A.open(locationId, attributeName);

                if (H5I.is_valid(attributeId) <= 0)
                {
                    throw new Exception(ErrorMessage.IOHelper_CouldNotOpenAttribute);
                }

                typeId = H5A.get_type(attributeId);

                // invoke HdfHelper.Read
                result = GeneralHelper.InvokeGenericMethod(typeof(IOHelper), null, nameof(IOHelper.Read),
                                                           BindingFlags.Public | BindingFlags.Static,
                                                           TypeConversionHelper.GetTypeFromHdfTypeId(typeId),
                                                           new object[] { attributeId, DataContainerType.Attribute, Type.Missing });
            }
            finally
            {
                if (H5I.is_valid(typeId) > 0)
                {
                    H5T.close(typeId);
                }
                if (H5I.is_valid(attributeId) > 0)
                {
                    H5A.close(attributeId);
                }
            }

            return(result);
        }
Beispiel #13
0
        public static Array ReadPrimitiveAttributes <T>(hid_t groupId, string name) //where T : struct
        {
            Type type     = typeof(T);
            var  datatype = GetDatatype(type);

            var attributeId = H5A.open(groupId, name);
            var spaceId     = H5A.get_space(attributeId);
            int rank        = H5S.get_simple_extent_ndims(spaceId);

            ulong[] maxDims = new ulong[rank];
            ulong[] dims    = new ulong[rank];
            hid_t   memId   = H5S.get_simple_extent_dims(spaceId, dims, maxDims);

            long[] lengths    = dims.Select(d => Convert.ToInt64(d)).ToArray();
            Array  attributes = Array.CreateInstance(type, lengths);

            var typeId   = H5A.get_type(attributeId);
            var mem_type = H5T.copy(datatype);

            if (datatype == H5T.C_S1)
            {
                H5T.set_size(datatype, new IntPtr(2));
            }

            var propId = H5A.get_create_plist(attributeId);

            memId = H5S.create_simple(rank, dims, maxDims);
            GCHandle hnd = GCHandle.Alloc(attributes, GCHandleType.Pinned);

            H5A.read(attributeId, datatype, hnd.AddrOfPinnedObject());
            hnd.Free();
            H5A.close(typeId);
            H5A.close(attributeId);
            H5S.close(spaceId);
            return(attributes);
        }
Beispiel #14
0
        private string GetModelConfig()
        {
            long fileId  = H5F.open(this.fileName, H5F.ACC_RDONLY);
            long attrId  = H5A.open(fileId, @"model_config");
            long typeId  = H5A.get_type(attrId);
            long spaceId = H5A.get_space(attrId);
            long count   = H5S.get_simple_extent_npoints(spaceId);

            H5S.close(spaceId);
            IntPtr[] dest   = new IntPtr[count];
            GCHandle handle = GCHandle.Alloc(dest, GCHandleType.Pinned);

            H5A.read(attrId, typeId, handle.AddrOfPinnedObject());

            var attrStrings = new List <string>();

            for (int i = 0; i < dest.Length; ++i)
            {
                int attrLength = 0;
                while (Marshal.ReadByte(dest[i], attrLength) != 0)
                {
                    ++attrLength;
                }

                byte[] buffer = new byte[attrLength];
                Marshal.Copy(dest[i], buffer, 0, buffer.Length);
                string stringPart = Encoding.UTF8.GetString(buffer);

                attrStrings.Add(stringPart);

                H5.free_memory(dest[i]);
            }

            handle.Free();
            return(attrStrings.ToArray().ToString());
        }
        public static string ReadStringAttribute(hid_t hid, string key)
        {
            var attribute = H5A.open(hid, key);

            if (attribute < 0)
            {
                throw new ArgumentException(string.Format("Attribute {0} not found.", key));
            }

            var type = H5A.get_type(attribute);

            if (type < 0)
            {
                H5A.close(attribute);
                throw new Exception("H5A.get_type failed.");
            }

            var typeClass = H5T.get_class(type);

            if (typeClass != H5T.class_t.STRING)
            {
                H5T.close(type);
                throw new Exception("Not a string attribute");
            }

            var utf8  = H5T.get_cset(type) == H5T.cset_t.UTF8;
            var ascii = H5T.get_cset(type) == H5T.cset_t.ASCII;

            if (!utf8 && !ascii)
            {
                H5T.close(type);
                H5A.close(attribute);
                throw new Exception("Neither ASCII nor UTF8.");
            }

            var isVariableString = H5T.is_variable_str(type);

            if (isVariableString < 0)
            {
                H5T.close(type);
                H5A.close(attribute);
                throw new Exception("H5T.is_variable_str failed");
            }

            if (isVariableString > 0)
            {
                var space = H5A.get_space(attribute);
                if (space < 0)
                {
                    H5T.close(type);
                    H5A.close(attribute);
                    throw new Exception("H5A.get_space failed.");
                }

                hid_t count = H5S.get_simple_extent_npoints(space);
                var   rdata = new IntPtr[count];

                H5A.read(attribute, type, new PinnedObject(rdata));

                var attrStrings = new List <string>();
                for (int i = 0; i < rdata.Length; ++i)
                {
                    int attrLength = 0;
                    while (Marshal.ReadByte(rdata[i], attrLength) != 0)
                    {
                        ++attrLength;
                    }

                    byte[] buffer = new byte[attrLength];
                    Marshal.Copy(rdata[i], buffer, 0, buffer.Length);

                    string part = utf8 ? Encoding.UTF8.GetString(buffer)
                        : Encoding.ASCII.GetString(buffer);

                    attrStrings.Add(part);

                    H5.free_memory(rdata[i]);
                }

                H5S.close(space);
                H5T.close(type);
                H5A.close(attribute);

                return(attrStrings[0]);
            }

            // Must be a non-variable length string.
            var size            = H5T.get_size(type).ToInt32();
            var unmanagedBuffer = new UnmanagedBuffer(size);

            int result = H5A.read(attribute, type, unmanagedBuffer);

            if (result < 0)
            {
                H5T.close(type);
                H5D.close(attribute);
                throw new IOException("Failed to read attribute.");
            }

            var bytes = new byte[size];

            unmanagedBuffer.CopyTo(bytes, 0, bytes.Length);

            H5T.close(type);
            H5A.close(attribute);

            var value = utf8 ? Encoding.UTF8.GetString(bytes)
                : Encoding.ASCII.GetString(bytes);

            return(value.TrimEnd('\0'));
        }
        public static bool WriteVariableStringAttribute(hid_t hid, string key, IEnumerable <string> values, bool utf8)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            var exists = H5A.exists(hid, key);

            if (exists < 0)
            {
                throw new Exception("H5A.exists failed");
            }

            if (exists == 0) // Attribute doesn't exist
            {
#if true
                var type = H5T.create(H5T.class_t.STRING, H5T.VARIABLE);
                if (type < 0)
                {
                    throw new Exception("Failed to create string type");
                }
#else
                var type = H5T.copy(H5T.C_S1);
                if (type < 0)
                {
                    throw new Exception("Failed to create string type");
                }

                if (H5T.set_size(type, H5T.VARIABLE) < 0)
                {
                    H5T.close(type);
                    throw new Exception("Failed to set type size");
                }
#endif

                if (utf8)
                {
                    if (H5T.set_cset(type, H5T.cset_t.UTF8) < 0)
                    {
                        H5T.close(type);
                        throw new Exception("Failed to set cset");
                    }
                }

                if (H5T.set_strpad(type, H5T.str_t.NULLTERM) < 0)
                {
                    H5T.close(type);
                    throw new Exception("Failed to set strpad");
                }

                var space = values.Count() == 1 ? H5S.create(H5S.class_t.SCALAR)
                    : H5S.create_simple(1, new ulong[1] {
                    (ulong)values.Count()
                }, null);
                if (space < 0)
                {
                    H5T.close(type);
                    throw new Exception("Failed to create data space");
                }

                var attribute = H5A.create(hid, key, type, space);
                H5S.close(space);
                if (attribute < 0)
                {
                    H5T.close(type);
                    throw new Exception(string.Format("Failed to create attribute \"{0}\"", key));
                }

                var pinnedObjects = new PinnedObject[values.Count()];
                var data          = new IntPtr[values.Count()];
                int count         = 0;
                foreach (string str in values)
                {
                    var bytes = str.ToBytes(utf8);
                    pinnedObjects[count] = new PinnedObject(bytes);
                    data[count]          = pinnedObjects[count];
                    count += 1;
                }

                H5A.write(attribute, type, new PinnedObject(data));

                H5T.close(type);
                H5A.close(attribute);
            }
            else
            {
                // Attribute exists.
                var attribute = H5A.open(hid, key);
                if (attribute < 0)
                {
                    throw new Exception(string.Format("Failed to open attribute \"{0}\"", key));
                }

                var type = H5A.get_type(attribute);
                if (type < 0)
                {
                    H5A.close(attribute);
                    throw new Exception("Failed to get data type");
                }

                var pinnedObjects = new PinnedObject[values.Count()];
                var data          = new IntPtr[values.Count()];
                int count         = 0;
                foreach (string str in values)
                {
                    var bytes = str.ToBytes(utf8);
                    pinnedObjects[count] = new PinnedObject(bytes);
                    data[count]          = pinnedObjects[count];
                    count += 1;
                }

                H5A.write(attribute, type, new PinnedObject(data));

                H5T.close(type);
                H5A.close(attribute);
            }

            return(true);
        }
        public static bool WriteFixedStringAttribute(hid_t attribute, string value, bool utf8)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            var space = H5A.get_space(attribute);

            if (space < 0)
            {
                throw new Exception("Failed to get data space");
            }

            if (H5S.get_simple_extent_type(space) != H5S.class_t.SCALAR)
            {
                H5S.close(space);
                throw new Exception("Not a scalar data space");
            }

            H5S.close(space);

            var type = H5A.get_type(attribute);

            if (type < 0)
            {
                throw new Exception("Failed to get type");
            }

            var typeClass = H5T.get_class(type);

            if (typeClass != H5T.class_t.STRING)
            {
                H5T.close(type);
                throw new Exception("Not a string attribute");
            }

            var isVariableString = H5T.is_variable_str(type);

            H5T.close(type);
            if (isVariableString < 0)
            {
                throw new Exception("H5T.is_variable_str failed");
            }

            if (isVariableString > 0)
            {
                throw new Exception("Not a fixed string attribute");
            }

            // TODO: Do we really need to create a new memory type here?
            var bytes = value.ToBytes(utf8);

            var memType = CreateFixedStringType(bytes, utf8);

            // TODO: type or memType here?
            H5A.write(attribute, memType, new PinnedObject(bytes));

            H5T.close(memType);

            return(true);
        }
Beispiel #18
0
        private static bool CreateOrOverwriteVariableStringAttribute(hid_t hid, string key, IEnumerable <string> values, bool utf8)
        {
            if (H5A.exists(hid, key) == 0)
            {
                // Attribute doesn't exist.
#if true
                var type = H5T.create(H5T.class_t.STRING, H5T.VARIABLE);
                if (type < 0)
                {
                    return(false);
                }
#else
                var type = H5T.copy(H5T.C_S1);
                if (type < 0)
                {
                    return(false);
                }

                H5T.set_size(type, H5T.VARIABLE);
#endif

                if (utf8)
                {
                    H5T.set_cset(type, H5T.cset_t.UTF8);
                }
                H5T.set_strpad(type, H5T.str_t.NULLTERM);

                var space = values.Count() == 1 ? H5S.create(H5S.class_t.SCALAR)
                    : H5S.create_simple(1, new ulong[1] {
                    (ulong)values.Count()
                }, null);
                if (space < 0)
                {
                    H5T.close(type);
                    return(false);
                }

                var attribute = H5A.create(hid, key, type, space);
                if (attribute < 0)
                {
                    H5S.close(space);
                    H5T.close(type);
                    return(false);
                }

                H5S.close(space);

                var pinnedObjects = new PinnedObject[values.Count()];
                var data          = new IntPtr[values.Count()];
                int count         = 0;
                foreach (string str in values)
                {
                    var bytes = str.ToBytes(utf8);
                    pinnedObjects[count] = new PinnedObject(bytes);
                    data[count]          = pinnedObjects[count];
                    count += 1;
                }

                H5A.write(attribute, type, new PinnedObject(data));

                H5T.close(type);
                H5A.close(attribute);
            }
            else
            {
                // Attribute exists.
                var attribute = H5A.open(hid, key);
                if (attribute < 0)
                {
                    return(false);
                }

                var type = H5A.get_type(attribute);
                if (type < 0)
                {
                    H5A.close(attribute);
                    return(false);
                }

                var pinnedObjects = new PinnedObject[values.Count()];
                var data          = new IntPtr[values.Count()];
                int count         = 0;
                foreach (string str in values)
                {
                    var bytes = str.ToBytes(utf8);
                    pinnedObjects[count] = new PinnedObject(bytes);
                    data[count]          = pinnedObjects[count];
                    count += 1;
                }

                H5A.write(attribute, type, new PinnedObject(data));

                H5T.close(type);
                H5A.close(attribute);
            }

            return(true);
        }
Beispiel #19
0
        private object GetAttributeValue(int _h5FileId, string attributeName)
        {
            H5AttributeId attId = H5A.open(_h5FileId, attributeName);

            if (attId == 0)
            {
                return(null);
            }
            H5DataTypeId typeId = 0;
            H5DataTypeId dtId   = 0;

            H5A.info_t    attInfo   = new H5A.info_t();
            H5DataSpaceId spaceId   = 0;
            H5DataTypeId  oldTypeId = 0;
            object        retObject = null;

            try
            {
                typeId = H5A.get_type(attId);
                H5A.get_info(attId, ref attInfo);
                dtId    = H5A.get_type(attId);
                spaceId = H5A.get_space(attId);
                IntPtr dataSize = H5T.get_size(dtId);
                //
                oldTypeId = typeId;
                typeId    = H5T.get_native_type(typeId, H5T.direction_t.DEFAULT);
                H5T.class_t typeClass = H5T.get_class(typeId);
                int         ndims     = H5S.get_simple_extent_ndims(spaceId);
                ulong[]     dims      = new ulong[ndims];
                H5S.get_simple_extent_dims(spaceId, dims, null);
                ulong dimSize = 1;
                if (dims.Length == 0)
                {
                    dimSize = 1;
                }
                else
                {
                    foreach (ulong dim in dims)
                    {
                        dimSize *= dim;
                    }
                }
                switch (typeClass)
                {
                case H5T.class_t.NO_CLASS:
                    break;

                case H5T.class_t.INTEGER:
                    // H5T.Sign.TWOS_COMPLEMENT;
                    H5T.sign_t sign = H5T.get_sign(oldTypeId);
                    switch (dataSize.ToInt32())
                    {
                    case 1:
                        retObject = ReadArray <byte>(dimSize, attId, typeId);
                        break;

                    case 2:
                        switch (sign)
                        {
                        case H5T.sign_t.SGN_2:
                            retObject = ReadArray <Int16>(dimSize, attId, typeId);
                            break;

                        case H5T.sign_t.NONE:
                            retObject = ReadArray <UInt16>(dimSize, attId, typeId);
                            break;
                        }
                        break;

                    case 4:
                        switch (sign)
                        {
                        case H5T.sign_t.SGN_2:
                            retObject = ReadArray <Int32>(dimSize, attId, typeId);
                            break;

                        case H5T.sign_t.NONE:
                            retObject = ReadArray <UInt32>(dimSize, attId, typeId);
                            break;
                        }
                        break;

                    case 8:
                        switch (sign)
                        {
                        case H5T.sign_t.SGN_2:
                            retObject = ReadArray <Int64>(dimSize, attId, typeId);
                            break;

                        case H5T.sign_t.NONE:
                            retObject = ReadArray <UInt64>(dimSize, attId, typeId);
                            break;
                        }
                        break;
                    }
                    break;

                case H5T.class_t.FLOAT:
                    switch (dataSize.ToInt32())
                    {
                    case 4:
                        retObject = ReadArray <float>(dimSize, attId, typeId);
                        break;

                    case 8:
                        retObject = ReadArray <double>(dimSize, attId, typeId);
                        break;
                    }
                    break;

                case H5T.class_t.STRING:
                    ulong  size  = attInfo.data_size;
                    byte[] chars = ReadArray <byte>(size, attId, typeId);
                    retObject = Encoding.ASCII.GetString(chars);
                    break;

                default:
                    break;
                }
                return(retObject);
            }
            finally
            {
                if (spaceId != 0)
                {
                    H5S.close(spaceId);
                }
                if (attId != 0)
                {
                    H5A.close(attId);
                }
                if (oldTypeId != 0)
                {
                    H5T.close(oldTypeId);
                }
                if (typeId != 0)
                {
                    H5T.close(typeId);
                }
                if (dtId != 0)
                {
                    H5T.close(dtId);
                }
            }
        }
Beispiel #20
0
        private static bool CreateOrOverwriteFixedStringAttribute(hid_t hid, string key, string value, bool utf8)
        {
            if (H5A.exists(hid, key) == 0)
            {
                // Attribute doesn't exist.
                var bytes = value.ToBytes(utf8);

#if true
                var type = H5T.create(H5T.class_t.STRING, new IntPtr(bytes.Length));
                if (type < 0)
                {
                    return(false);
                }
#else
                var type = H5T.copy(H5T.C_S1);
                if (type < 0)
                {
                    return(false);
                }

                H5T.set_size(type, new IntPtr(bytes.Length));
#endif

                if (utf8)
                {
                    H5T.set_cset(type, H5T.cset_t.UTF8);
                }
                H5T.set_strpad(type, H5T.str_t.NULLTERM);

                var space = H5S.create(H5S.class_t.SCALAR);
                if (space < 0)
                {
                    H5T.close(type);
                    return(false);
                }

                var attribute = H5A.create(hid, key, type, space);
                if (attribute < 0)
                {
                    H5S.close(space);
                    H5T.close(type);
                    return(false);
                }

                H5A.write(attribute, type, new PinnedObject(bytes));

                H5A.close(attribute);
            }
            else
            {
                // Attribute exists.
                var attribute = H5A.open(hid, key);
                if (attribute < 0)
                {
                    return(false);
                }

                var type = H5A.get_type(attribute);
                H5A.write(attribute, type, new PinnedObject(value.ToBytes(utf8)));
                H5T.close(type);

                H5A.close(attribute);
            }

            return(true);
        }
Beispiel #21
0
 /// <summary>
 /// Read the appropriate H5Type from an *attribute* with ID `attr_id`.
 /// </summary>
 public static H5Type FromAttribute(hid_t attr_id)
 {
     return(new H5Type(H5A.get_type(attr_id)));
 }
        public static bool WriteEnumAttribute <T>(hid_t hid, string key, T value) where T : struct, IConvertible
        {
            var enumSize = EnumHelper.GetUnderlyingTypeSize <T>();

            if (!Enum.IsDefined(typeof(T), value))
            {
                throw new ArgumentException("value");
            }

            if (H5A.exists(hid, key) == 0)
            {
                // Attribute doesn't exist.
                var type = CreateEnumType <T>();
                if (type < 0)
                {
                    return(false);
                }

                var space = H5S.create(H5S.class_t.SCALAR);
                if (space < 0)
                {
                    H5T.close(type);
                    return(false);
                }

                var attribute = H5A.create(hid, key, type, space);
                if (attribute < 0)
                {
                    H5S.close(space);
                    H5T.close(type);
                    return(false);
                }

                var unmanagedBuffer = new UnmanagedBuffer(enumSize);
                unmanagedBuffer.WriteEnum(value);
                H5A.write(attribute, type, unmanagedBuffer);

                H5A.close(attribute);
                H5S.close(space);
                H5T.close(type);
            }
            else
            {
                var attribute = H5A.open(hid, key);
                if (attribute < 0)
                {
                    return(false);
                }

                var type = H5A.get_type(attribute);
                if (type < 0)
                {
                    H5A.close(attribute);
                    return(false);
                }

                var unmanagedBuffer = new UnmanagedBuffer(enumSize);
                unmanagedBuffer.WriteEnum(value);

                H5A.write(attribute, type, unmanagedBuffer);

                H5T.close(type);
                H5A.close(attribute);
            }

            return(true);
        }
Beispiel #23
0
 public void H5Aget_typeTest2()
 {
     Assert.IsFalse(
         H5A.get_type(Utilities.RandomInvalidHandle()) >= 0);
 }