/// <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); }
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); } } }