public static void PrepareAttribute <T>(long locationId, string name, T[] valueSet, ulong[] dimensionLimitSet, bool initializeAttribute, [CallerMemberName()] string callerMemberName = "") { Contract.Requires(valueSet != null, nameof(valueSet)); long fileId = -1; long typeId = -1; long attributeId = -1; ulong[] dimensionSet; bool isNew; Type elementType; dimensionSet = new ulong[] { 0 }; elementType = typeof(T); try { fileId = H5I.get_file_id(locationId); // create attribute typeId = TypeConversionHelper.GetHdfTypeIdFromType(fileId, elementType); (attributeId, isNew) = IOHelper.OpenOrCreateAttribute(locationId, name, typeId, (ulong)valueSet.LongLength, dimensionLimitSet); // write attribute data (regenerate attribute if necessary) if (initializeAttribute) { ulong[] oldValueSetCount; if (!isNew && callerMemberName != nameof(IOHelper.PrepareAttribute)) { oldValueSetCount = IOHelper.PrepareAttributeValueSet(attributeId, ref valueSet, false); } else { oldValueSetCount = new ulong[1] { (ulong)valueSet.Count() } }; if (valueSet.Count() == (int)oldValueSetCount[0]) { IOHelper.Write(attributeId, valueSet, DataContainerType.Attribute); } else { H5A.close(attributeId); H5A.delete(locationId, name); IOHelper.PrepareAttribute(locationId, name, valueSet, dimensionLimitSet, true); } } } finally { if (H5I.is_valid(attributeId) > 0) { H5A.close(attributeId); } if (H5I.is_valid(typeId) > 0) { H5T.close(typeId); } if (H5I.is_valid(fileId) > 0) { H5F.close(fileId); } } }
public static unsafe T[] Read <T>(long dataPortId, DataContainerType dataContainerType, long dataspaceId = -1) { long dataspaceId_file = -1; long dataspaceId_buffer = -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_buffer = H5S.create_simple(1, new ulong[] { (ulong)elementCount }, new ulong[] { (ulong)elementCount }); if (H5D.read(dataPortId, typeId, dataspaceId_buffer, 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 => { // keep this, otherwise °C gets read-in wrongly on Linux // (https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.ptrtostringansi?view=net-5.0) string result = IOHelper.PtrToStringAnsiWithEncoding(x, Encoding.GetEncoding(1252)); 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_buffer) > 0) { H5S.close(dataspaceId_buffer); } if (H5I.is_valid(dataspaceId) > 0) { H5S.close(dataspaceId); } } return(returnValue); }