public void H5Tarray_createTest1() { hsize_t[] dims = new hsize_t[] { 3, 3 }; hid_t dtype = H5T.array_create(H5T.IEEE_F64LE, (uint)dims.Length, dims); Assert.IsTrue(dtype >= 0); Assert.IsTrue(H5T.close(dtype) >= 0); }
public override void Dispose() { base.Dispose(); if (ID > 0) { ID = H5T.close(ID); } }
private IntPtr CreateNativeArray(Array clrArray, long dtypeID) { var arraySize = clrArray.Length * ElementSize; Array oneD; if (clrArray.Rank > 1) { oneD = Array.CreateInstance(clrArray.ElementType(), clrArray.Length); Buffer.BlockCopy(clrArray, 0, oneD, 0, arraySize); } else { oneD = clrArray; } var nativeSize = arraySize; if (ClrType == typeof(byte)) { nativeSize *= H5T.get_size(dtypeID).ToInt32(); } var result = Marshal.AllocHGlobal(nativeSize); if (ClrType == typeof(byte)) { var maxLength = H5T.get_size(dtypeID).ToInt32(); string[] allStrings = (string[])oneD; byte[] actualArray = new byte[maxLength * oneD.Length]; for (var i = 0; i < allStrings.Length; i++) { //return System.Text.Encoding.Default.GetString(slice); byte[] bytes = Encoding.ASCII.GetBytes(allStrings[i] + '\0'); Buffer.BlockCopy(bytes, 0, actualArray, i * maxLength, bytes.Length); } Marshal.Copy(actualArray, 0, result, actualArray.Length); } else if (ClrType == typeof(double)) { Marshal.Copy((double[])oneD, 0, result, oneD.Length); } else if (ClrType == typeof(float)) { Marshal.Copy((float[])oneD, 0, result, oneD.Length); } else if (ClrType == typeof(long)) { Marshal.Copy((long[])oneD, 0, result, oneD.Length); } else { throw new ArgumentException("Unknown ClrType"); } return(result); }
/// <summary> /// Constructor to create a chuncked dataset object /// </summary> /// <param name="name"></param> /// <param name="groupId"></param> /// <param name="chunckSize"></param> public ChunkedDataset(string name, hid_t groupId, ulong[] chunckSize) { //Datasetname = Hdf5.ToHdf5Name(name); Datasetname = name; GroupId = groupId; datatype = Hdf5.GetDatatype(typeof(T)); typeId = H5T.copy(datatype); chunkDims = chunckSize; }
public static int WriteStringAttributes(hid_t groupId, string name, IEnumerable <string> strs, string datasetName = null) { hid_t tmpId = groupId; if (!string.IsNullOrWhiteSpace(datasetName)) { hid_t datasetId = H5D.open(groupId, datasetName); if (datasetId > 0) { groupId = datasetId; } } // create UTF-8 encoded attributes 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.SPACEPAD); int strSz = strs.Count(); hid_t spaceId = H5S.create_simple(1, new ulong[] { (ulong)strSz }, null); var attributeId = H5A.create(groupId, name, datatype, spaceId); GCHandle[] hnds = new GCHandle[strSz]; IntPtr[] wdata = new IntPtr[strSz]; int cntr = 0; foreach (string str in strs) { hnds[cntr] = GCHandle.Alloc( Encoding.UTF8.GetBytes(str), GCHandleType.Pinned); wdata[cntr] = hnds[cntr].AddrOfPinnedObject(); cntr++; } var hnd = GCHandle.Alloc(wdata, GCHandleType.Pinned); var result = H5A.write(attributeId, datatype, hnd.AddrOfPinnedObject()); hnd.Free(); for (int i = 0; i < strSz; ++i) { hnds[i].Free(); } H5S.close(spaceId); H5T.close(datatype); { H5D.close(groupId); } return(result); }
public void H5Tcommit_anonTest3() { Assert.IsFalse( H5T.commit_anon(m_v0_test_file, Utilities.RandomInvalidHandle()) >= 0); Assert.IsFalse( H5T.commit_anon(Utilities.RandomInvalidHandle(), Utilities.RandomInvalidHandle()) >= 0); }
public static Double AttributeAsDouble(H5AttributeId _attributeId) { H5DataTypeId attributeType = H5T.copy(H5T.H5Type.NATIVE_DOUBLE); double[] value = new double[1]; H5A.read <double>(_attributeId, attributeType, new H5Array <double>(value)); return(value[0]); }
public static int AttributeAsInt32(H5AttributeId _attributeId) { H5DataTypeId attributeType = H5T.copy(H5T.H5Type.NATIVE_INT); int[] value = new int[1]; H5A.read <int>(_attributeId, attributeType, new H5Array <int>(value)); return(value[0]); }
public static unsafe void AddString(long fileId, ContainerType container) { long res; var dims = new ulong[] { 2, 2, 3 }; /* "extendible contiguous non-external dataset not allowed" */ // fixed length string attribute (ASCII) var typeIdFixed = H5T.copy(H5T.C_S1); res = H5T.set_size(typeIdFixed, new IntPtr(2)); res = H5T.set_cset(typeIdFixed, H5T.cset_t.ASCII); var dataFixed = new string[] { "00", "11", "22", "33", "44", "55", "66", "77", " ", "AA", "ZZ", "!!" }; var dataFixedChar = dataFixed .SelectMany(value => Encoding.ASCII.GetBytes(value)) .ToArray(); TestUtils.Add(container, fileId, "string", "fixed", typeIdFixed, dataFixedChar.AsSpan(), dims); res = H5T.close(typeIdFixed); // variable length string attribute (ASCII) var typeIdVar = H5T.copy(H5T.C_S1); res = H5T.set_size(typeIdVar, H5T.VARIABLE); res = H5T.set_cset(typeIdVar, H5T.cset_t.ASCII); var dataVar = new string[] { "00", "11", "22", "33", "44", "55", "66", "77", " ", "AA", "ZZ", "!!" }; var dataVarIntPtr = dataVar.Select(x => Marshal.StringToCoTaskMemUTF8(x)).ToArray(); TestUtils.Add(container, fileId, "string", "variable", typeIdVar, dataVarIntPtr.AsSpan(), dims); foreach (var ptr in dataVarIntPtr) { Marshal.FreeCoTaskMem(ptr); } res = H5T.close(typeIdVar); // variable length string attribute (UTF8) var typeIdVarUTF8 = H5T.copy(H5T.C_S1); res = H5T.set_size(typeIdVarUTF8, H5T.VARIABLE); res = H5T.set_cset(typeIdVarUTF8, H5T.cset_t.UTF8); var dataVarUTF8 = new string[] { "00", "11", "22", "33", "44", "55", "66", "77", " ", "ÄÄ", "的的", "!!" }; var dataVarUTF8IntPtr = dataVarUTF8.Select(x => Marshal.StringToCoTaskMemUTF8(x)).ToArray(); TestUtils.Add(container, fileId, "string", "variableUTF8", typeIdVarUTF8, dataVarUTF8IntPtr.AsSpan(), dims); foreach (var ptr in dataVarUTF8IntPtr) { Marshal.FreeCoTaskMem(ptr); } res = H5T.close(typeIdVarUTF8); }
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); }
public static bool WriteFixedStringAttribute(hid_t hid, string key, string value, bool utf8) { var exists = H5A.exists(hid, key); if (exists < 0) { throw new Exception("H5A.exists failed"); } if (exists == 0) // Attribute doesn't exist { var bytes = value.ToBytes(utf8); var type = CreateFixedStringType(bytes, utf8); var space = H5S.create(H5S.class_t.SCALAR); if (space < 0) { H5T.close(type); throw new Exception("Failed to create scalar space"); } var attribute = H5A.create(hid, key, type, space); if (attribute < 0) { H5S.close(space); H5T.close(type); throw new Exception(string.Format("Failed to create attribute \"{0}\"", key)); } H5A.write(attribute, type, new PinnedObject(bytes)); H5A.close(attribute); H5S.close(space); H5T.close(type); return(true); } else { var attribute = H5A.open(hid, key); if (attribute < 0) { throw new Exception(string.Format("Failed to open attribute \"{0}\"", key)); } try { return(WriteFixedStringAttribute(attribute, value, utf8)); } finally { H5A.close(attribute); } } }
public object this[string key] { get { return(Host.With <object>((objId) => { long attrId = 0; long dtypeId = 0; try { attrId = H5A.open(objId, key); if (attrId < 0) { throw new ArgumentException($"Unknown Attribute: {key}", nameof(key)); } dtypeId = H5A.get_type(attrId); int size = H5T.get_size(dtypeId).ToInt32(); IntPtr iPtr = Marshal.AllocHGlobal(size); int result = H5A.read(attrId, dtypeId, iPtr); if (result < 0) { throw new IOException("Failed to read attribute"); } if (H5T.equal(dtypeId, H5T.NATIVE_INT64) > 0) { var dest = new long[1]; Marshal.Copy(iPtr, dest, 0, 1); Marshal.FreeHGlobal(iPtr); return dest[0]; } else // Must be a string... { var dest = new byte[size]; Marshal.Copy(iPtr, dest, 0, size); Marshal.FreeHGlobal(iPtr); return Encoding.ASCII.GetString(dest).TrimEnd((Char)0); } // return null; } finally { if (attrId > 0) { H5A.close(attrId); } if (dtypeId > 0) { H5T.close(dtypeId); } } })); } }
public static bool WriteStringAttribute(hid_t hid, string key, string value, bool utf8 = true, bool variable = true) { if (H5A.exists(hid, key) == 0) { // Attribute doesn't exist. return(variable ? CreateOrOverwriteVariableStringAttribute(hid, key, new string[] { value }, utf8) : CreateOrOverwriteFixedStringAttribute(hid, key, value, utf8)); } else { var attribute = H5A.open(hid, key); if (attribute < 0) { return(false); } var type = H5A.get_type(attribute); if (type < 0) { H5A.close(attribute); return(false); } var typeClass = H5T.get_class(type); if (typeClass == H5T.class_t.NO_CLASS) { H5T.close(type); H5T.close(attribute); return(false); } if (typeClass != H5T.class_t.STRING) { H5T.close(type); H5T.close(attribute); throw new ArgumentException("H5T.get_class(type) != H5T.class_t.STRING"); } utf8 = H5T.get_cset(type) == H5T.cset_t.UTF8; bool ok; if (H5T.is_variable_str(type) > 0) { ok = CreateOrOverwriteVariableStringAttribute(hid, key, new string[] { value }, utf8); } else { ok = CreateOrOverwriteFixedStringAttribute(hid, key, value, utf8); } H5T.close(type); H5T.close(attribute); return(ok); } }
// Generate string attribute // GroupName: target group for the new attribute // AttName: attribute name // AttContent: content for the attribute, has to be a string public static void StringAttributeGenerator(H5GroupId GroupName, string AttName, string AttContent) { char[] AttContentChar = AttContent.ToCharArray(); byte[] asciiStr = ASCIIEncoding.ASCII.GetBytes(AttContentChar); int length = asciiStr.Length; H5AttributeId attributeId = H5A.create(GroupName, AttName, H5T.create(H5T.CreateClass.STRING, length), H5S.create(H5S.H5SClass.SCALAR)); H5A.write(attributeId, H5T.create(H5T.CreateClass.STRING, length), new H5Array <byte>(asciiStr)); H5A.close(attributeId); }
protected T[] getAttribute <T>(H5AttributeId aid) { H5DataTypeId sv = H5A.getType(aid); int size = H5T.getSize(sv); var attValue = new T[size]; H5A.read <T>(aid, sv, new H5Array <T>(attValue)); return(attValue); }
private static byte[] EncodeStringData(string str, out H5DataTypeId dtype) { byte[] strdata = System.Text.Encoding.UTF8.GetBytes(str); dtype = H5T.copy(H5T.H5Type.C_S1); H5T.setSize(dtype, strdata.Length); return(strdata); }
private static bool IsHDF5String(hid_t fileLoc, string name) { hid_t dset = H5D.open(fileLoc, name); hid_t type = H5D.get_type(dset); H5T.class_t cl = H5T.get_class(type); return(cl == H5T.class_t.STRING); }
void LoadRowData() { var dtype = H5D.getType(Id); var size = H5T.getSize(dtype); _row_data = new byte[FindNumberOfRows(), size]; H5D.read(Id, dtype, new H5Array <byte>(_row_data)); //TODO: Does this work with more than one row? Dunno. Probs not. }
// Test that we can read an OMX file with attributes and tables public static void ReadTest(string file) { OmxReadStream rs = OmxFile.OpenReadOnly(file); Console.WriteLine("OMX version is {0}", rs.OmxVersion); Console.WriteLine("mat shape is {0},{1}", rs.Shape[0], rs.Shape[1]); Console.WriteLine("first mat names is {0}", rs.MatrixNames[0]); Console.WriteLine("first mat data type is {0}", H5T.getClass(rs.GetMatrixDataType(rs.MatrixNames[0]))); rs.Close(); }
private static H5AttributeId WriteStringAttribute(H5ObjectWithAttributes fileOrdatasetId, HDFAttributeDef hDFAttributeDef, H5DataSpaceId dataSpaceId, H5DataTypeId dataTypeId) { string attValue = Convert.ToString(hDFAttributeDef.Value); int stringLength = attValue.Length; H5T.setSize(dataTypeId, stringLength); H5AttributeId attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); byte[] bs = Encoding.Default.GetBytes(attValue); H5A.write(attributeId, dataTypeId, new H5Array <byte>(bs)); return(attributeId); }
public void TestCreateEnumType() { var type = CreateEnumType(new string[] { "FALSE", "TRUE" }, new int[] { 0, 1 }); Assert.IsFalse(type < 0); H5T.close(type); var type2 = CreateEnumType(new string[] { "FALSE", "TRUE" }, new Boolean[] { Boolean.False, Boolean.True }); Assert.IsFalse(type2 < 0); H5T.close(type2); }
public void H5TcommitTest1() { hid_t dtype = H5T.copy(H5T.IEEE_F64LE); Assert.IsTrue(dtype >= 0); Assert.IsTrue(H5T.commit(m_v0_test_file, "foo", dtype) >= 0); // can't commit twice Assert.IsFalse(H5T.commit(m_v0_test_file, "bar", dtype) >= 0); // can't commit to different files Assert.IsFalse(H5T.commit(m_v2_test_file, "bar", dtype) >= 0); Assert.IsTrue(H5T.close(dtype) >= 0); }
// Generate floating number attributes // GroupName: target group for the new attribute // AttName: attribute name // AttContent: content for the attribute, has to be a single floating number, here 32bit floating number is used, consider using 64bit double if necessary public static void NumberAttributeGenerator(H5GroupId GroupName, string AttName, float AttContent) { float[] AttArray = new float[1] { AttContent }; long[] dims = new long[1]; dims[0] = AttArray.Length; H5AttributeId attributeId = H5A.create(GroupName, AttName, H5T.copy(H5T.H5Type.NATIVE_FLOAT), H5S.create_simple(1, dims)); H5A.write(attributeId, H5T.copy(H5T.H5Type.NATIVE_FLOAT), new H5Array <float>(AttArray)); H5A.close(attributeId); }
// Generate double attributes // GroupName: target group for the new attribute // AttName: attribute name // AttContent: content for the attribute, has to be a single floating number, here 64bit double is used, to generate subgroups in Data public static void DoubleAttributeGenerator(H5GroupId GroupName, string AttName, double AttContent) { double[] AttArray = new double[1] { AttContent }; long[] dims = new long[1]; dims[0] = AttArray.Length; H5AttributeId attributeId = H5A.create(GroupName, AttName, H5T.copy(H5T.H5Type.NATIVE_DOUBLE), H5S.create_simple(1, dims)); H5A.write(attributeId, H5T.copy(H5T.H5Type.NATIVE_FLOAT), new H5Array <double>(AttArray)); H5A.close(attributeId); }
static void Main2222(string[] args) { var h5 = H5F.create(@"D:\test.h5", H5F.ACC_TRUNC); var typeId = H5T.create(H5T.class_t.COMPOUND, new IntPtr(40)); var strtype = H5T.copy(H5T.C_S1); H5T.set_size(strtype, new IntPtr(16)); H5T.insert(typeId, "Name", new IntPtr(0), strtype); H5T.insert(typeId, "x_pos", new IntPtr(16), H5T.NATIVE_INT32); H5T.insert(typeId, "y_pos", new IntPtr(20), H5T.NATIVE_INT32); H5T.insert(typeId, "Mass", new IntPtr(24), H5T.NATIVE_FLOAT); H5T.insert(typeId, "Temperature", new IntPtr(32), H5T.NATIVE_DOUBLE); ulong[] dims = new ulong[] { 10000 }; ulong[] chunk_size = new ulong[] { 1000 }; var spaceid = H5S.create_simple(dims.Length, dims, null); var dcpl = H5P.create(H5P.DATASET_CREATE); H5P.set_layout(dcpl, H5D.layout_t.COMPACT); H5P.set_deflate(dcpl, 6); H5P.set_chunk(dcpl, chunk_size.Length, chunk_size); var datasetid = H5D.create(h5, "Table1", typeId, spaceid, H5P.DEFAULT, dcpl); ComType ct = new ComType() { Name = "aabb", x_pos = 2, y_pos = 1, Mass = 1.24F, Temperature = 45.7, }; IntPtr p = Marshal.AllocHGlobal(40 * (int)dims[0]); Marshal.StructureToPtr(ct, p, false); H5D.write(datasetid, typeId, spaceid, H5S.ALL, H5P.DEFAULT, p); H5F.close(h5); }
public HDF5DataSet CreateDataset(string name, ulong[] shape, Type dType, long maxSize = 1, bool[] unlimited = null, ulong[] chunkShape = null, bool compress = false) { HDF5DataSet result = null; With((id) => { int nDims = shape.Length; if (unlimited == null) { unlimited = Enumerable.Range(0, nDims).Select(d => false).ToArray(); } ulong[] maxShape = Enumerable.Range(0, nDims).Select(d => unlimited[d] ? H5S.UNLIMITED : shape[d]).ToArray(); var dataspaceID = H5S.create_simple(nDims, shape, maxShape); long dataTypeID = HDF5DataSet.OpenHDFDataType(dType, maxSize); long creationPropertyList = 0L; if (compress) { if (chunkShape == null) { chunkShape = shape; } creationPropertyList = H5P.create(H5P.DATASET_CREATE); H5P.set_layout(creationPropertyList, H5D.layout_t.CHUNKED); H5P.set_deflate(creationPropertyList, 9); H5P.set_chunk(creationPropertyList, shape.Length, chunkShape); } var newID = H5D.create(id, name, dataTypeID, dataspaceID, 0L, creationPropertyList, 0L); if (creationPropertyList > 0) { H5P.close(creationPropertyList); } H5T.close(dataTypeID); H5S.close(dataspaceID); if (newID <= 0) { throw new H5SSException("Couldn't create DataSet"); } // write! H5D.close(newID); result = new HDF5DataSet(name, this); }); return(result); }
private ISimpleDataStorage LoadDataset(long sourceFileId, string datasetPath, ulong start, ulong stride, ulong block, ulong count) { long datasetId = -1; long typeId = -1; Array dataset; Array dataset_status; Type genericType; ExtendedDataStorageBase extendedDataStorage; ISimpleDataStorage simpleDataStorage; dataset = IOHelper.ReadDataset(sourceFileId, datasetPath, start, stride, block, count); // apply status (only if native dataset) if (H5L.exists(sourceFileId, datasetPath + "_status") > 0) { try { datasetId = H5D.open(sourceFileId, datasetPath); typeId = H5D.get_type(datasetId); dataset_status = IOHelper.ReadDataset(sourceFileId, datasetPath + "_status", start, stride, block, count).Cast <byte>().ToArray(); genericType = typeof(ExtendedDataStorage <>).MakeGenericType(TypeConversionHelper.GetTypeFromHdfTypeId(typeId)); extendedDataStorage = (ExtendedDataStorageBase)Activator.CreateInstance(genericType, dataset, dataset_status); dataset_status = null; } finally { if (H5I.is_valid(datasetId) > 0) { H5D.close(datasetId); } if (H5I.is_valid(typeId) > 0) { H5T.close(typeId); } } simpleDataStorage = extendedDataStorage.ToSimpleDataStorage(); extendedDataStorage.Dispose(); return(simpleDataStorage); } else { return(new SimpleDataStorage(dataset.Cast <double>().ToArray())); } }
public static unsafe void AddOpaque(long fileId, ContainerType container) { long res; var length = (ulong)TestData.SmallData.Length * 2; var typeId = H5T.create(H5T.class_t.OPAQUE, new IntPtr(2)); res = H5T.set_tag(typeId, "Opaque Test Tag"); TestUtils.Add(container, fileId, "opaque", "opaque", typeId, TestData.SmallData.AsSpan(), length); res = H5T.close(typeId); }
private static void WriteAttribute(H5ObjectWithAttributes target, string name, long value) { H5DataTypeId dtype = H5T.copy(H5T.H5Type.NATIVE_LLONG); H5DataSpaceId spaceId = H5S.create(H5S.H5SClass.SCALAR); H5AttributeId attributeId = H5A.create(target, name, dtype, spaceId); H5A.write(attributeId, dtype, new H5Array <long>(new[] { value })); H5A.close(attributeId); H5T.close(dtype); H5S.close(spaceId); }
public static (int success, long CreatedgroupId) WriteStrings(long groupId, string name, IEnumerable <string> strs) { // create UTF-8 encoded test datasets long datatype = H5T.create(H5T.class_t.STRING, H5T.VARIABLE); H5T.set_cset(datatype, H5T.cset_t.ASCII); H5T.set_strpad(datatype, H5T.str_t.NULLTERM); int strSz = strs.Count(); long spaceId = H5S.create_simple(1, new[] { (ulong)strSz }, null); string normalizedName = Hdf5Utils.NormalizedName(name); var datasetId = Hdf5Utils.GetDatasetId(groupId, normalizedName, datatype, spaceId); if (datasetId == -1L) { return(-1, -1L); } GCHandle[] hnds = new GCHandle[strSz]; IntPtr[] wdata = new IntPtr[strSz]; int cntr = 0; foreach (string str in strs) { hnds[cntr] = GCHandle.Alloc( Encoding.UTF8.GetBytes(str), GCHandleType.Pinned); wdata[cntr] = hnds[cntr].AddrOfPinnedObject(); cntr++; } var hnd = GCHandle.Alloc(wdata, GCHandleType.Pinned); var result = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); for (int i = 0; i < strSz; ++i) { hnds[i].Free(); } H5D.close(datasetId); H5S.close(spaceId); H5T.close(datatype); return(result, datasetId); }
public H5Datatype(H5T.H5Type nativeTypeId) : base(null, null) { _nativeTypeId = nativeTypeId; }