public void H5Sselect_allTest4() { hid_t space = H5S.create(H5S.class_t.SCALAR); Assert.IsTrue(space > 0); Assert.IsTrue(H5S.select_all(space) >= 0); Assert.IsTrue(H5S.get_select_npoints(space) == 1); Assert.IsTrue(H5S.close(space) >= 0); }
public void H5Sselect_noneTest3() { hid_t space = H5S.create(H5S.class_t.NULL); Assert.IsTrue(space > 0); Assert.IsTrue(H5S.select_none(space) >= 0); Assert.IsTrue(H5S.get_select_npoints(space) == 0); Assert.IsTrue(H5S.close(space) >= 0); }
public void H5Sselect_allTest1() { hsize_t[] dims = { 1, 2, 3 }; hid_t space = H5S.create_simple(dims.Length, dims, null); Assert.IsTrue(space > 0); Assert.IsTrue(H5S.select_all(space) >= 0); Assert.IsTrue(H5S.get_select_npoints(space) == 6); Assert.IsTrue(H5S.close(space) >= 0); }
public Array Get() { Array result = null; WithDataSpace((h5Ref, dsRef) => { var success = H5S.select_none(dsRef); if (success < 0) { throw new H5SSException("Error with dataspace: select_none"); } success = H5S.select_all(dsRef); if (success < 0) { throw new H5SSException("Error with dataspace: select_all"); } int selectElemNpoints = (int)H5S.get_select_npoints(dsRef); var effectiveSize = ElementSize * selectElemNpoints; if (DataType == HDF5DataType.String) { effectiveSize *= _stringLength; } IntPtr iPtr = Marshal.AllocHGlobal(effectiveSize); // TODO Deallocate try { var dtype = H5D.get_type(h5Ref); // Return? success = H5D.read(h5Ref, dtype, H5S.ALL, dsRef, H5P.DEFAULT, iPtr); H5T.close(dtype); if (success < 0) { throw new H5SSException("Error reading dataset"); } var tmp = CreateClrArray(iPtr, selectElemNpoints); var shape = Shape.Select(ul => (long)ul).ToArray(); if (ClrType == typeof(byte)) { shape = shape.Concat(new[] { (long)_stringLength }).ToArray(); } result = Array.CreateInstance(ClrType, shape); Buffer.BlockCopy(tmp, 0, result, 0, effectiveSize); } finally { Marshal.FreeHGlobal(iPtr); } // Convert bytes to characters... if (DataType == HDF5DataType.String) { byte[,] byteArray = (byte[, ])result; result = Enumerable.Range(0, byteArray.GetLength(0)).Select(i => { var slice = Enumerable.Range(0, byteArray.GetLength(1)).Select(j => byteArray[i, j]).ToArray(); //return System.Text.Encoding.Default.GetString(slice); return(Encoding.ASCII.GetString(slice).TrimEnd((Char)0)); }).ToArray(); } H5S.get_simple_extent_dims(dsRef, _shape, _maxDims); // WTF? }); return(result); }
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); }
public void H5Sget_select_npointsTest2() { Assert.IsFalse( H5S.get_select_npoints(Utilities.RandomInvalidHandle()) >= 0); }