public void UpdateStringAttributeOnDataset() { string fileName = GetFilename("updatestringattributeondataset.h5"); Hdf5File file = Hdf5File.Create(fileName); Hdf5Group group = file.Groups.Add("group"); List <Hdf5DimensionProperty> dimensionProps = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty prop = new Hdf5DimensionProperty { CurrentSize = 1 }; dimensionProps.Add(prop); Hdf5Dataset dataset = group.Datasets.Add("dataset1", Hdf5DataTypes.Int32, dimensionProps); Hdf5Attribute attribute = dataset.Attributes.Add("attribute1", "test"); attribute.Value = "test2"; dataset.Attributes.Update(attribute); file.Close(); file = new Hdf5File(fileName); group = file.Groups[0]; dataset = group.Datasets[0]; attribute = dataset.Attributes[0]; Assert.AreEqual("test2", attribute.Value); }
/// <summary> /// /// </summary> /// <param name="_objectId"></param> /// <param name="_attribute"></param> public static void UpdateAttribute(Hdf5Identifier _objectId, Hdf5Attribute _attribute) { Hdf5Identifier attributeId = H5A.open(_objectId.Value, _attribute.Name).ToId(); if (attributeId.Value > 0) { Hdf5DataType type = TypeHelper.GetDataTypeFromAttribute(attributeId); Hdf5DataTypes enumType = TypeHelper.GetDataTypesEnum(_attribute.Value.GetType()); if (type.Type == enumType) { if (enumType == Hdf5DataTypes.String) { H5A.close(attributeId.Value); DeleteAttribute(_objectId, _attribute.Name); Hdf5Attribute attribute = CreateAttribute(_objectId, _attribute.Name, _attribute.Value); if (attribute != null) { _attribute.Id = attribute.Id; _attribute.Name = attribute.Name; _attribute.Value = attribute.Value; } } else { WriteObjectValue(type, attributeId, _attribute.Value); } } else { throw new Hdf5TypeMismatchException(); } } }
public void UpdateAttributeWithMismatchOnDataset() { string fileName = GetFilename("updateattributewithmistmachondataset.h5"); Hdf5File file = Hdf5File.Create(fileName); Hdf5Group group = file.Groups.Add("group"); List <Hdf5DimensionProperty> dimensionProps = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty prop = new Hdf5DimensionProperty { CurrentSize = 1 }; dimensionProps.Add(prop); Hdf5Dataset dataset = group.Datasets.Add("dataset1", Hdf5DataTypes.Int32, dimensionProps); Hdf5Attribute attribute = dataset.Attributes.Add("attribute1", "test"); try { attribute.Value = 5; dataset.Attributes.Update(attribute); Assert.Fail("Should have thrown an exception"); } catch (Exception ex) { file.Close(); Assert.IsInstanceOf <Hdf5TypeMismatchException>(ex); } }
/// <summary> /// Reads a non-string attribute /// Assumes the parent object is already open /// </summary> /// <param name="_attributeId"></param> /// <param name="_title"></param> /// <param name="_type"></param> /// <returns></returns> public static Hdf5Attribute GetAttribute(Hdf5Identifier _attributeId, string _title, Hdf5DataType _type) { Object value = ReadValue(_type, _attributeId); var attribute = new Hdf5Attribute { Id = _attributeId, Name = _title, Value = value }; return(attribute); }
/// <summary> /// Creates the attribute then adds to the supplied attribute list. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="_objectId"></param> /// <param name="_attributes"></param> /// <param name="_title"></param> /// <param name="_value"></param> /// <returns></returns> public static Hdf5Attribute CreateAttributeAddToList <T>( Hdf5Identifier _objectId, ReadonlyNamedItemList <Hdf5Attribute> _attributes, string _title, T _value) { Hdf5Attribute attribute = CreateAttribute(_objectId, _title, _value); if (attribute != null) { _attributes.Add(attribute); } return(attribute); }
public void UpdateStringAttributeOnFile() { string fileName = GetFilename("updatestringattributeonfile.h5"); Hdf5File file = Hdf5File.Create(fileName); Hdf5Attribute attribute = file.Attributes.Add("attribute1", "test"); attribute.Value = "test2"; file.Attributes.Update(attribute); file.Close(); file = new Hdf5File(fileName); attribute = file.Attributes[0]; Assert.AreEqual("test2", attribute.Value); }
/// <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()); }
public void UpdateAttributeWithMismatchOnFile() { string fileName = GetFilename("updateattributewithmistmachonfile.h5"); Hdf5File file = Hdf5File.Create(fileName); Hdf5Attribute attribute = file.Attributes.Add("attribute1", "test"); try { attribute.Value = 5; file.Attributes.Update(attribute); Assert.Fail("Should have thrown an exception"); } catch (Exception ex) { file.Close(); Assert.IsInstanceOf <Hdf5TypeMismatchException>(ex); } }
/// <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); }
/// <summary> /// Currently does not support arrays /// </summary> /// <typeparam name="T"></typeparam> /// <param name="_objectId"></param> /// <param name="_title"></param> /// <param name="_value"></param> /// <returns></returns> public static Hdf5Attribute CreateAttribute <T>(Hdf5Identifier _objectId, string _title, T _value) { ulong[] sizes = new ulong[1] { 1 }; Hdf5Identifier dataspaceId; Hdf5Identifier attributeId; Hdf5Identifier typeId; Hdf5Attribute attribute = null; Type type = _value.GetType(); var datatype = TypeHelper.GetDataTypesEnum(type); if (datatype != Hdf5DataTypes.String) { var tempType = TypeHelper.GetNativeType(datatype); typeId = H5T.copy(tempType.Value).ToId(); var dataTypeObject = TypeHelper.GetDataTypeByType(typeId); var status = H5T.set_order(typeId.Value, H5T.order_t.LE); dataspaceId = H5S.create_simple(1, sizes, null).ToId(); attributeId = H5A.create(_objectId.Value, _title, typeId.Value, dataspaceId.Value).ToId(); if (attributeId.Value > 0) { WriteValue(dataTypeObject, attributeId, _value); } } else { string tempValue = Convert.ToString(_value); dataspaceId = H5S.create(H5S.class_t.SCALAR).ToId(); typeId = H5T.copy(H5T.C_S1).ToId(); int length = tempValue.Length + 1; var result = H5T.set_size(typeId.Value, new IntPtr(length)); attributeId = H5A.create(_objectId.Value, _title, typeId.Value, dataspaceId.Value).ToId(); IntPtr valueArray = Marshal.StringToHGlobalAnsi(tempValue); result = H5A.write(attributeId.Value, typeId.Value, valueArray); Marshal.FreeHGlobal(valueArray); } H5S.close(dataspaceId.Value); H5T.close(typeId.Value); H5A.close(attributeId.Value); if (attributeId.Value > 0) { attribute = new Hdf5Attribute { Value = _value, Name = _title, Id = attributeId }; } return(attribute); }