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()); }
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); }
public static ulong[] PrepareAttributeValueSet <T>(long attributeId, ref T[] valueSet, bool isReference) { long dataspaceId = -1; ulong[] dimensionSet; ulong[] dimensionLimitSet; dimensionSet = new ulong[] { 0 }; dimensionLimitSet = new ulong[] { 0 }; try { dataspaceId = H5A.get_space(attributeId); H5S.get_simple_extent_dims(dataspaceId, null, dimensionLimitSet); // merge data if (dimensionLimitSet[0] == H5S.UNLIMITED) { T[] valueSet_File = IOHelper.Read <T>(attributeId, DataContainerType.Attribute); if (isReference) { if (valueSet_File.Count() == 0 || !Enumerable.SequenceEqual(valueSet_File, valueSet.Skip(Math.Max(0, valueSet.Count() - valueSet_File.Count())))) { valueSet = valueSet.Concat(valueSet_File).ToArray(); } } else { if (valueSet.Count() == 0 || !Enumerable.SequenceEqual(valueSet, valueSet_File.Skip(Math.Max(0, valueSet_File.Count() - valueSet.Count())))) { valueSet = valueSet_File.Concat(valueSet).ToArray(); } } } } finally { if (H5I.is_valid(dataspaceId) > 0) { H5S.close(dataspaceId); } } return(dimensionLimitSet); }
public static IEnumerable <string> ReadStringAttributes(hid_t groupId, string name) { hid_t datatype = H5T.create(H5T.class_t.STRING, H5T.VARIABLE); H5T.set_cset(datatype, H5T.cset_t.UTF8); H5T.set_strpad(datatype, H5T.str_t.NULLTERM); //name = ToHdf5Name(name); var datasetId = H5A.open(groupId, name); hid_t spaceId = H5A.get_space(datasetId); long count = H5S.get_simple_extent_npoints(spaceId); H5S.close(spaceId); IntPtr[] rdata = new IntPtr[count]; GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned); H5A.read(datasetId, datatype, hnd.AddrOfPinnedObject()); var strs = new List <string>(); 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(datatype); H5A.close(datasetId); return(strs); }
public void H5Aget_spaceTest1() { hid_t att = H5A.create(m_v2_test_file, "A", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); hid_t space = H5A.get_space(att); Assert.IsTrue(space >= 0); Assert.IsTrue(H5S.close(space) >= 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); space = H5A.get_space(att); Assert.IsTrue(space >= 0); Assert.IsTrue(H5S.close(space) >= 0); Assert.IsTrue(H5A.close(att) >= 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); }
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); }
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 void H5Aget_spaceTest2() { Assert.IsFalse( H5A.get_space(Utilities.RandomInvalidHandle()) >= 0); }
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 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); }
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); } } }
public static T[] Read <T>(long dataPortId, DataContainerType dataContainerType, long dataspaceId = -1) { long dataspaceId_file = -1; long dataspaceId_memory = -1; long typeId = -1; long elementCount; int elementTypeSize; int byteLength; IntPtr bufferPtr; Type elementType; T[] returnValue; elementTypeSize = 0; byteLength = 0; bufferPtr = IntPtr.Zero; elementType = typeof(T); returnValue = null; try { if (dataspaceId > -1) { dataspaceId = H5S.copy(dataspaceId); } switch (dataContainerType) { case DataContainerType.Attribute: if (dataspaceId == -1) { dataspaceId = H5A.get_space(dataPortId); } break; case DataContainerType.Dataset: if (dataspaceId == -1) { dataspaceId = H5D.get_space(dataPortId); } dataspaceId_file = dataspaceId; break; default: throw new NotSupportedException(); } if (elementType == typeof(string)) { elementTypeSize = Marshal.SizeOf <IntPtr>(); } else if (elementType == typeof(bool)) { elementTypeSize = Marshal.SizeOf <byte>(); } else { elementTypeSize = Marshal.SizeOf(elementType); } elementCount = H5S.get_select_npoints(dataspaceId); byteLength = (int)elementCount * elementTypeSize; bufferPtr = Marshal.AllocHGlobal(byteLength); typeId = TypeConversionHelper.GetHdfTypeIdFromType(elementType); switch (dataContainerType) { case DataContainerType.Attribute: if (H5A.read(dataPortId, typeId, bufferPtr) < 0) { throw new Exception(ErrorMessage.IOHelper_CouldNotReadAttribute); } break; case DataContainerType.Dataset: dataspaceId_memory = H5S.create_simple(1, new ulong[] { (ulong)elementCount }, new ulong[] { (ulong)elementCount }); if (H5D.read(dataPortId, typeId, dataspaceId_memory, dataspaceId_file, H5P.DEFAULT, bufferPtr) < 0) { throw new Exception(ErrorMessage.IOHelper_CouldNotReadDataset); } break; default: throw new NotSupportedException(); } if (elementType.IsPrimitive) { T[] genericSet; GCHandle gcHandle; byte[] byteSet; genericSet = new T[(int)elementCount]; gcHandle = GCHandle.Alloc(genericSet, GCHandleType.Pinned); byteSet = new byte[byteLength]; Marshal.Copy(bufferPtr, byteSet, 0, byteLength); Marshal.Copy(byteSet, 0, gcHandle.AddrOfPinnedObject(), byteLength); returnValue = genericSet; gcHandle.Free(); } else if (elementType == typeof(string)) { IntPtr[] intPtrSet; intPtrSet = new IntPtr[(int)elementCount]; Marshal.Copy(bufferPtr, intPtrSet, 0, (int)elementCount); returnValue = intPtrSet.Select(x => { string result = Marshal.PtrToStringAnsi(x); H5.free_memory(x); return(result); }).Cast <T>().ToArray(); } else if (elementType.IsValueType && !elementType.IsPrimitive && !elementType.IsEnum) { T[] structSet; int offset; structSet = new T[(int)elementCount]; offset = 0; Enumerable.Range(0, (int)elementCount).ToList().ForEach(x => { structSet[x] = Marshal.PtrToStructure <T>(IntPtr.Add(bufferPtr, offset)); offset += elementTypeSize; }); returnValue = structSet; } else { throw new NotSupportedException(); } } finally { Marshal.FreeHGlobal(bufferPtr); if (H5I.is_valid(typeId) > 0) { H5T.close(typeId); } if (H5I.is_valid(dataspaceId_memory) > 0) { H5S.close(dataspaceId_memory); } if (H5I.is_valid(dataspaceId) > 0) { H5S.close(dataspaceId); } } return(returnValue); }