public void H5DreadTest2() { hid_t mem_type = H5T.create(H5T.class_t.STRING, H5T.VARIABLE); Assert.IsTrue(H5T.set_cset(mem_type, H5T.cset_t.UTF8) >= 0); Assert.IsTrue(H5T.set_strpad(mem_type, H5T.str_t.NULLTERM) >= 0); hid_t fspace = H5D.get_space(m_v0_utf8_dset); Assert.IsTrue(fspace >= 0); hssize_t count = H5S.get_simple_extent_npoints(fspace); Assert.IsTrue(count > 0); Assert.IsTrue(H5S.close(fspace) >= 0); IntPtr[] rdata = new IntPtr[count]; GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned); Assert.IsTrue(H5D.read(m_v0_utf8_dset, mem_type, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0); 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); Assert.IsTrue(s == ((string)m_utf8strings[i])); Assert.IsTrue(H5.free_memory(rdata[i]) >= 0); } Assert.IsTrue(H5D.read(m_v2_utf8_dset, mem_type, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0); 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); Assert.IsTrue(s == ((string)m_utf8strings[i])); Assert.IsTrue(H5.free_memory(rdata[i]) >= 0); } hnd.Free(); }
public void H5Sget_simple_extent_npointsTest3() { hid_t space = H5S.create(H5S.class_t.SCALAR); Assert.IsTrue(space >= 0); Assert.IsTrue(H5S.get_simple_extent_npoints(space) == 1); Assert.IsTrue(H5S.close(space) >= 0); }
public void H5Sget_simple_extent_npointsTest1() { hsize_t[] dims = { 1, 2, 3 }; hid_t space = H5S.create_simple(dims.Length, dims, dims); Assert.IsTrue(space >= 0); Assert.IsTrue(H5S.get_simple_extent_npoints(space) == 6); Assert.IsTrue(H5S.close(space) >= 0); }
public static (bool success, IEnumerable <string> result) ReadStrings(long groupId, string name, string alternativeName) { long 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 = 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, Array.Empty <string>()); } long spaceId = H5D.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, datatype, 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(datatype); H5D.close(datasetId); return(true, strs); }
/// <summary> /// Reads an n-dimensional dataset. /// </summary> /// <typeparam name="T">Generic parameter strings or primitive type</typeparam> /// <param name="groupId">id of the group. Can also be a file Id</param> /// <param name="name">name of the dataset</param> /// <returns>The n-dimensional dataset</returns> public static Array ReadDatasetToArray <T>(hid_t groupId, string name) //where T : struct { var datatype = GetDatatype(typeof(T)); var datasetId = H5D.open(groupId, name); var spaceId = H5D.get_space(datasetId); int rank = H5S.get_simple_extent_ndims(spaceId); long count = H5S.get_simple_extent_npoints(spaceId); Array dset; Type type = typeof(T); if (rank >= 0 && count >= 0) { int rankChunk; ulong[] maxDims = new ulong[rank]; ulong[] dims = new ulong[rank]; ulong[] chunkDims = new ulong[rank]; hid_t memId = H5S.get_simple_extent_dims(spaceId, dims, maxDims); long[] lengths = dims.Select(d => Convert.ToInt64(d)).ToArray(); dset = Array.CreateInstance(type, lengths); var typeId = H5D.get_type(datasetId); var mem_type = H5T.copy(datatype); if (datatype == H5T.C_S1) { H5T.set_size(datatype, new IntPtr(2)); } var propId = H5D.get_create_plist(datasetId); if (H5D.layout_t.CHUNKED == H5P.get_layout(propId)) { rankChunk = H5P.get_chunk(propId, rank, chunkDims); } memId = H5S.create_simple(rank, dims, maxDims); GCHandle hnd = GCHandle.Alloc(dset, GCHandleType.Pinned); H5D.read(datasetId, datatype, memId, spaceId, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); } else { dset = Array.CreateInstance(type, new long[1] { 0 }); } H5D.close(datasetId); H5S.close(spaceId); return(dset); }
public static IEnumerable <string> ReadStrings(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 = H5D.open(groupId, name); hid_t spaceId = H5D.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, datatype, 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(datatype); H5D.close(datasetId); return(strs); }
public static T[,] ReadDataset <T>(int groupId, string name) where T : struct { var datatype = GetDatatype(typeof(T)); name = ToHdf5Name(name); var datasetId = H5D.open(groupId, name); var spaceId = H5D.get_space(datasetId); int rank = H5S.get_simple_extent_ndims(spaceId); long count = H5S.get_simple_extent_npoints(spaceId); int rankChunk; ulong[] maxDims = new ulong[rank]; ulong[] dims = new ulong[rank]; ulong[] chunkDims = new ulong[rank]; var memId = H5S.get_simple_extent_dims(spaceId, dims, maxDims); T[,] dset = new T[dims[0], dims[1]]; var typeId = H5D.get_type(datasetId); var mem_type = H5T.copy(datatype); if (datatype == H5T.C_S1) { H5T.set_size(datatype, new IntPtr(2)); } var propId = H5D.get_create_plist(datasetId); if (H5D.layout_t.CHUNKED == H5P.get_layout(propId)) { rankChunk = H5P.get_chunk(propId, rank, chunkDims); } memId = H5S.create_simple(rank, dims, maxDims); GCHandle hnd = GCHandle.Alloc(dset, GCHandleType.Pinned); H5D.read(datasetId, datatype, memId, spaceId, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); H5D.close(typeId); H5D.close(datasetId); H5S.close(spaceId); return(dset); }
public static string ReadUnicodeString(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); var datasetId = H5D.open(groupId, name); var typeId = H5D.get_type(datasetId); var classId = H5T.get_class(typeId); var order = H5T.get_order(typeId); IntPtr size = H5T.get_size(typeId); int strLen = (int)size; var spaceId = H5D.get_space(datasetId); hid_t count = H5S.get_simple_extent_npoints(spaceId); IntPtr[] rdata = new IntPtr[count]; byte[] wdata = new byte[strLen]; GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned); H5D.read(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); for (int i = 0; i < strLen; i++) { Marshal.ReadByte(rdata[0], i); } Marshal.Copy(rdata[0], wdata, 0, strLen); string s = Encoding.UTF8.GetString(wdata); hnd.Free(); H5S.close(spaceId); H5T.close(datatype); H5D.close(datasetId); return(s); }
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 static string ReadUnicodeString(hid_t groupId, string name) { var datasetId = H5D.open(groupId, name); var typeId = H5D.get_type(datasetId); if (H5T.is_variable_str(typeId) > 0) { var spaceId = H5D.get_space(datasetId); hid_t count = H5S.get_simple_extent_npoints(spaceId); IntPtr[] rdata = new IntPtr[count]; GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned); H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); 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 stringPart = Encoding.UTF8.GetString(buffer); attrStrings.Add(stringPart); H5.free_memory(rdata[i]); } hnd.Free(); H5S.close(spaceId); H5D.close(datasetId); return(attrStrings[0]); } // Must be a non-variable length string. int size = H5T.get_size(typeId).ToInt32(); IntPtr iPtr = Marshal.AllocHGlobal(size); int result = H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL, H5P.DEFAULT, iPtr); if (result < 0) { throw new IOException("Failed to read dataset"); } var strDest = new byte[size]; Marshal.Copy(iPtr, strDest, 0, size); Marshal.FreeHGlobal(iPtr); H5D.close(datasetId); return(Encoding.UTF8.GetString(strDest).TrimEnd((Char)0)); }
static void Main_Read(string[] args) { int DATA_ARRAY_LENGTH = 5; //var h5 = H5F.open(@"E:\HDF5\HDF5DotNet-src\examples\CSharpExample\CSharpExample1\table.h5", H5F.ACC_RDONLY); //var h5 = H5F.open(@"D:\test.h5", H5F.ACC_RDONLY); //var h5 = H5F.open(@"E:\HDF5\Hdf5DotnetTools-master\ToolTest\bin\Debug\table.h5", H5F.ACC_RDONLY); var h5 = H5F.open(@"E:\HDF5\test_gzip.h5", H5F.ACC_RDONLY); var dataset = H5D.open(h5, "trans_detail/20160929"); var spaceid = H5D.get_space(dataset); var npoints = H5S.get_simple_extent_npoints(spaceid); //var dims = H5S.get_simple_extent_dims(spaceid); int rank = H5S.get_simple_extent_ndims(spaceid); // 是不是不能用自己的type var dtype = H5D.get_type(dataset); var dtcls = H5T.get_class(dtype); var size = H5T.get_size(dtype); var sz = Marshal.SizeOf(typeof(ComType)); var dtype_n = H5T.get_nmembers(dtype); for (uint i = 0; i < dtype_n; ++i) { var x = H5T.get_member_name(dtype, i); var x4 = Marshal.PtrToStringAnsi(x); var y = H5T.get_member_type(dtype, i); var z = H5T.get_class(y); var x1 = H5T.get_member_offset(dtype, i); var x3 = H5T.get_size(y); Console.WriteLine(x4); Console.WriteLine(z); Console.WriteLine(x1); //var x2 = Marshal.OffsetOf(typeof(ComType), x4).ToInt32(); //Console.WriteLine(x2); Console.WriteLine(x3); } int ss1 = Marshal.SizeOf(typeof(ComType)); IntPtr p = Marshal.AllocHGlobal(ss1 * 11); H5D.read(dataset, dtype, H5S.ALL, H5S.ALL, H5P.DEFAULT, p); var s = Marshal.PtrToStructure(p, typeof(ComType)); Console.WriteLine(s); var s2 = Marshal.PtrToStructure(p + ss1, typeof(ComType)); Console.WriteLine(s2); var s3 = Marshal.PtrToStructure(p + ss1 * 4, typeof(ComType)); Console.WriteLine(s3); var s4 = Marshal.PtrToStructure(p + ss1 * 5, typeof(ComType)); Console.WriteLine(s4); var s6 = Marshal.PtrToStructure(p + ss1 * 10, typeof(ComType)); Console.WriteLine(s6); }
public void H5Sget_simple_extent_npointsTest4() { Assert.IsFalse( H5S.get_simple_extent_npoints(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')); }