public void H5Aget_info_by_idxTest1() { H5A.info_t info = new H5A.info_t(); hid_t att = H5A.create(m_v2_test_file, "A", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.create(m_v2_test_file, "B", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); Assert.IsTrue(H5A.get_info_by_idx(m_v2_test_file, ".", H5.index_t.NAME, H5.iter_order_t.NATIVE, 0, ref info) >= 0); Assert.IsTrue(H5A.get_info_by_idx(m_v2_test_file, ".", H5.index_t.NAME, H5.iter_order_t.NATIVE, 1, ref info) >= 0); att = H5A.create(m_v0_test_file, "A", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); Assert.IsTrue(H5A.get_info_by_idx(m_v0_test_file, ".", H5.index_t.NAME, H5.iter_order_t.NATIVE, 0, ref info) >= 0); Assert.IsFalse(H5A.get_info_by_idx(m_v0_test_file, ".", H5.index_t.NAME, H5.iter_order_t.NATIVE, 1, ref info) >= 0); }
internal static H5Attribute Create(hid_t loc_id, string name, Type primitive, object default_ = null) { if (primitive == null) { primitive = default_.GetType(); // may throw NullReferenceException, which is informational enough } int rank = 1; long[] dims = new long[1] { 1 }; hid_t id; using (H5Space space = H5Space.Create(rank, dims)) using (H5Type dtype = H5Type.Create(primitive)) { if ((id = H5A.create(loc_id, name, dtype.ID, space.ID, H5P.DEFAULT, H5P.DEFAULT)) < 0) { throw new H5LibraryException($"H5A.create() returned ({id})"); } } H5Attribute rv = FromID(id); if (default_ != null) { rv.Write(default_); } return(rv); }
private bool setOMXFileAttributes() { // write OMX attributes H5DataSpaceId dspace; H5DataTypeId dtype; H5AttributeId attr; // OMX version dspace = H5S.create(H5S.H5SClass.SCALAR); dtype = H5T.copy(H5T.H5Type.C_S1); // string datatype H5T.setSize(dtype, dllVersion[0].Length); attr = H5A.create(fileId, omxVersionName, dtype, dspace); ASCIIEncoding ascii = new ASCIIEncoding(); H5A.write(attr, dtype, new H5Array <System.Byte>(ascii.GetBytes(dllVersion[0]))); H5A.close(attr); // OMX shape - only 2D tables dspace = H5S.create_simple(1, new long[] { 2 }); dtype = H5T.copy(H5T.H5Type.NATIVE_INT); attr = H5A.create(fileId, omxShapeAttr, dtype, dspace); int[] shape = new int[2]; shape[0] = (int)Shape[0]; shape[1] = (int)Shape[1]; H5A.write <int>(attr, dtype, new H5Array <int>(shape)); H5S.close(dspace); H5A.close(attr); return(true); }
public void H5AreadTest1() { double[] x = { Math.PI }; IntPtr buf = Marshal.AllocHGlobal(8); hid_t att = H5A.create(m_v2_test_file, "A", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.read(att, H5T.IEEE_F64BE, buf) >= 0); Assert.IsTrue(H5A.read(att, H5T.NATIVE_DOUBLE, buf) >= 0); Marshal.Copy(buf, x, 0, 1); Assert.IsTrue(x[0] == 0.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); Assert.IsTrue(H5A.read(att, H5T.IEEE_F64BE, buf) >= 0); Assert.IsTrue(H5A.read(att, H5T.NATIVE_DOUBLE, buf) >= 0); Marshal.Copy(buf, x, 0, 1); Assert.IsTrue(x[0] == 0.0); Assert.IsTrue(H5A.close(att) >= 0); Marshal.FreeHGlobal(buf); }
private static long GetId(long parentId, string name, long dataType, long spaceId, Hdf5ElementType type) { string normalizedName = NormalizedName(name); bool exists = ItemExists(parentId, normalizedName, type); if (exists) { LogMessage($"{normalizedName} already exists", Hdf5LogLevel.Debug); if (!Hdf5.Settings.OverrideExistingData) { if (Hdf5.Settings.ThrowOnError) { throw new Hdf5Exception($"{normalizedName} already exists"); } return(-1); } } var datasetId = -1L; switch (type) { case Hdf5ElementType.Unknown: break; case Hdf5ElementType.Group: case Hdf5ElementType.Dataset: if (exists) { H5L.delete(parentId, normalizedName); // datasetId = H5D.open(parentId, normalizedName); } datasetId = H5D.create(parentId, normalizedName, dataType, spaceId); break; case Hdf5ElementType.Attribute: if (exists) { H5A.delete(parentId, normalizedName); } datasetId = H5A.create(parentId, normalizedName, dataType, spaceId); break; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } if (datasetId == -1L) { string error = $"Unable to create dataset for {normalizedName}"; LogMessage($"{normalizedName} already exists", Hdf5LogLevel.Error); if (Hdf5.Settings.ThrowOnError) { throw new Hdf5Exception(error); } } return(datasetId); }
private static void WriteAttribute(H5ObjectWithAttributes target, string name, string value) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Name must not be empty (or null)", "name"); } if (string.IsNullOrEmpty(value)) { throw new ArgumentException("Value must not be empty or null", "value"); } H5DataTypeId dtype; byte[] strdata = EncodeStringData(value, out dtype); H5DataSpaceId spaceId = H5S.create(H5S.H5SClass.SCALAR); H5AttributeId attributeId = H5A.create(target, name, dtype, spaceId); H5A.write(attributeId, dtype, new H5Array <byte>(strdata)); H5A.close(attributeId); H5T.close(dtype); H5S.close(spaceId); }
public static (long AttributeId, bool IsNew) OpenOrCreateAttribute(long locationId, string name, long attributeTypeId, ulong length, ulong[] dimensionLimitSet) { return(IOHelper.OpenOrCreateAttribute(locationId, name, attributeTypeId, () => { long dataspaceId = -1; long attributeId = -1; try { dataspaceId = H5S.create_simple(1, new ulong[] { length }, dimensionLimitSet); attributeId = H5A.create(locationId, name, attributeTypeId, dataspaceId); if (H5I.is_valid(attributeId) <= 0) { throw new Exception(ErrorMessage.IOHelper_CouldNotOpenOrCreateAttribute); } } finally { if (H5I.is_valid(dataspaceId) > 0) { H5S.close(dataspaceId); } } return attributeId; })); }
/// <summary> /// 写数据集属性 /// </summary> public void WriteDatasetAttribute(string datasetName, string attrName, string value) { H5DataSetId datasetId = H5D.open(_fileId, datasetName); H5DataTypeId typeId = H5T.copy(H5T.H5Type.C_S1); H5DataSpaceId spaceId = H5S.create(H5S.H5SClass.SCALAR); H5T.setSize(typeId, value.Length); H5AttributeId attrId = H5A.create(datasetId, attrName, typeId, spaceId); if (value != "") { H5Array <byte> buffer = new H5Array <byte>(Encoding.Default.GetBytes(value)); H5A.write(attrId, typeId, buffer); } if (typeId != null) { H5T.close(typeId); } if (spaceId != null) { H5S.close(spaceId); } if (attrId != null) { H5A.close(attrId); } if (datasetId != null) { H5D.close(datasetId); } }
public void H5AdeleteTest1() { hid_t att = H5A.create(m_v0_class_file, "DNA", H5T.IEEE_F32BE, m_space_null); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.create(m_v2_class_file, "DNA", H5T.IEEE_F32BE, m_space_null); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.create(m_v0_class_file, "DSA", H5T.IEEE_F32BE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.create(m_v2_class_file, "DSA", H5T.IEEE_F32BE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); Assert.IsTrue(H5A.delete(m_v0_class_file, "DNA") >= 0); Assert.IsTrue(H5A.delete(m_v0_class_file, "DSA") >= 0); Assert.IsTrue(H5A.delete(m_v2_class_file, "DNA") >= 0); Assert.IsTrue(H5A.delete(m_v2_class_file, "DSA") >= 0); }
public void H5Aget_nameTest1() { size_t buf_size = IntPtr.Zero; ssize_t size = IntPtr.Zero; hid_t att = H5A.create(m_v2_test_file, "H5Aget_name", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); // pretend we don't know the size size = H5A.get_name(att, buf_size, (StringBuilder)null); Assert.IsTrue(size.ToInt32() == 11); buf_size = new IntPtr(size.ToInt32() + 1); StringBuilder nameBuilder = new StringBuilder(buf_size.ToInt32()); size = H5A.get_name(att, buf_size, nameBuilder); Assert.IsTrue(size.ToInt32() == 11); string name = nameBuilder.ToString(); // names should match Assert.AreEqual("H5Aget_name", name); // read a truncated version buf_size = new IntPtr(3); nameBuilder = new StringBuilder(3); size = H5A.get_name(att, buf_size, nameBuilder); Assert.IsTrue(size.ToInt32() == 11); name = nameBuilder.ToString(); // names won't match Assert.AreNotEqual("H5Aget_name", name); Assert.AreEqual("H5", name); Assert.IsTrue(H5A.close(att) >= 0); }
public void H5Aget_info_by_nameTest1() { H5A.info_t info = new H5A.info_t(); hid_t att = H5A.create(m_v2_test_file, "A", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.create(m_v2_test_file, "B", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); Assert.IsTrue(H5A.get_info_by_name(m_v2_test_file, ".", "A", ref info) >= 0); Assert.IsTrue(H5A.get_info_by_name(m_v2_test_file, ".", "B", ref info) >= 0); att = H5A.create(m_v0_test_file, "A", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); Assert.IsTrue(H5A.get_info_by_name(m_v0_test_file, ".", "A", ref info) >= 0); Assert.IsFalse(H5A.get_info_by_name(m_v0_test_file, ".", "B", ref info) >= 0); }
public void H5Aopen_by_idxTest1() { hid_t att = H5A.create(m_v2_test_file, "A", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.create(m_v2_test_file, "AA", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.open_by_idx(m_v2_test_file, ".", H5.index_t.NAME, H5.iter_order_t.NATIVE, 0); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.open_by_idx(m_v2_test_file, ".", H5.index_t.NAME, H5.iter_order_t.NATIVE, 1); Assert.IsTrue(att >= 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); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.open_by_idx(m_v0_test_file, ".", H5.index_t.NAME, H5.iter_order_t.NATIVE, 0); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); }
public static int WritePrimitiveAttribute <T>(hid_t groupId, string name, Array attributes, string datasetName = null) //where T : struct { var tmpId = groupId; if (!string.IsNullOrWhiteSpace(datasetName)) { var datasetId = H5D.open(groupId, datasetName); if (datasetId > 0) { groupId = datasetId; } } int rank = attributes.Rank; ulong[] dims = Enumerable.Range(0, rank).Select(i => { return((ulong)attributes.GetLength(i)); }).ToArray(); ulong[] maxDims = null; var spaceId = H5S.create_simple(rank, dims, maxDims); var datatype = GetDatatype(typeof(T)); var typeId = H5T.copy(datatype); var attributeId = H5A.create(groupId, name, datatype, spaceId); GCHandle hnd = GCHandle.Alloc(attributes, GCHandleType.Pinned); var result = H5A.write(attributeId, datatype, hnd.AddrOfPinnedObject()); hnd.Free(); H5A.close(attributeId); H5S.close(spaceId); H5T.close(typeId); if (tmpId != groupId) { H5D.close(groupId); } return(result); }
public static (int success, hid_t CreatedgroupId) 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(); } H5A.close(attributeId); H5S.close(spaceId); H5T.close(datatype); if (tmpId != groupId) { H5D.close(groupId); } return(result, attributeId); }
private static void WriteFile(string filePath) { var file = H5F.create(filePath, H5F.CreateMode.ACC_TRUNC); var group = H5G.create(file, "/group"); H5G.close(group); const int RANK = 2; const int DIM0 = 3; const int DIM1 = 4; var dims = new long[RANK] { DIM0, DIM1 }; var dataSpace = H5S.create_simple(RANK, dims); var dataSet = H5D.create(file, "/group/dataset", H5T.H5Type.NATIVE_INT, dataSpace); H5S.close(dataSpace); var data = new int[DIM0, DIM1] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; H5D.write(dataSet, new H5DataTypeId(H5T.H5Type.NATIVE_INT), new H5Array <int>(data)); var dataType = new H5DataTypeId(H5T.H5Type.NATIVE_INT); dataSpace = H5S.create(H5S.H5SClass.SCALAR); var integerAttribute = H5A.create(dataSet, "int", dataType, dataSpace); H5A.write(integerAttribute, dataType, new H5Array <int>(new int[1] { 42 })); H5A.close(integerAttribute); H5S.close(dataSpace); //H5T.close(dataType); // Read-only. var str = "Hello, world!"; var strBytes = Encoding.ASCII.GetBytes(str); // There is a H5T.get_cset, but there does not seem to be a way of setting the character encoding, i.e. set_cset. dataType = H5T.copy(H5T.H5Type.C_S1); H5T.setSize(dataType, strBytes.Length); dataSpace = H5S.create(H5S.H5SClass.SCALAR); var stringAttribute = H5A.create(dataSet, "string", dataType, dataSpace); H5A.write(stringAttribute, dataType, new H5Array <byte>(strBytes)); H5A.close(stringAttribute); H5S.close(dataSpace); H5T.close(dataType); H5D.close(dataSet); H5F.close(file); }
public static bool WriteScalarNumericAttribute <T>(hid_t hid, string key, T value, hid_t type) where T : struct { if (key == null) { throw new ArgumentNullException("key"); } var exists = H5A.exists(hid, key); if (exists < 0) { throw new Exception("H5A.exists failed"); } if (exists == 0) { var space = H5S.create(H5S.class_t.SCALAR); if (space < 0) { throw new Exception("Failed to create scalar space"); } var attribute = H5A.create(hid, key, type, space); if (attribute < 0) { H5S.close(space); throw new Exception(string.Format("Failed to create attribute \"{0}\"", key)); } H5S.close(space); object boxedValue = value; H5A.write(attribute, type, new PinnedObject(boxedValue)); H5A.close(attribute); 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(WriteScalarNumericAttribute(attribute, value, type)); } finally { H5A.close(attribute); } } }
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); } } }
// 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); }
public void H5AiterateTest1() { hid_t att = H5A.create(m_v2_test_file, "IEEE_F32BE", H5T.IEEE_F32BE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.create(m_v2_test_file, "IEEE_F64BE", H5T.IEEE_F64BE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.create(m_v2_test_file, "NATIVE_B8", H5T.NATIVE_B8, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); ArrayList al = new ArrayList(); GCHandle hnd = GCHandle.Alloc(al); IntPtr op_data = (IntPtr)hnd; hsize_t n = 0; // the callback is defined in H5ATest.cs H5A.operator_t cb = DelegateMethod; Assert.IsTrue(H5A.iterate(m_v2_test_file, H5.index_t.NAME, H5.iter_order_t.NATIVE, ref n, cb, op_data) >= 0); // we should have 3 elements in the array list Assert.IsTrue(al.Count == 3); att = H5A.create(m_v0_test_file, "IEEE_F32BE", H5T.IEEE_F32BE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.create(m_v0_test_file, "IEEE_F64BE", H5T.IEEE_F64BE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.create(m_v0_test_file, "NATIVE_B8", H5T.NATIVE_B8, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); al.Clear(); n = 0; Assert.IsTrue(H5A.iterate(m_v0_test_file, H5.index_t.NAME, H5.iter_order_t.NATIVE, ref n, cb, op_data) >= 0); // we should have 3 elements in the array list Assert.IsTrue(al.Count == 3); hnd.Free(); }
public void H5Acreate_by_nameTest2() { Assert.IsFalse( H5A.create(Utilities.RandomInvalidHandle(), "A", H5T.IEEE_F32BE, m_space_null) >= 0); Assert.IsFalse( H5A.create(m_v0_test_file, "A", Utilities.RandomInvalidHandle(), m_space_null) >= 0); Assert.IsFalse( H5A.create(m_v2_test_file, "A", H5T.IEEE_F32BE, Utilities.RandomInvalidHandle()) >= 0); }
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); }
// 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); }
// 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); }
static void test_attr_plist() { try { Console.Write("Testing attribute property lists"); hssize_t[] dims = { 256, 512 }; const string PLST_FILE_NAME = ("tattr_plist.h5"); hssize_t[] dims1 = { SPACE1_DIM1, SPACE1_DIM2, SPACE1_DIM3 }; hssize_t[] dims2 = { ATTR1_DIM }; // Create file. H5FileId fileId = H5F.create(PLST_FILE_NAME, H5F.CreateMode.ACC_TRUNC); // Create dataspace for dataset. H5DataSpaceId space1_Id = H5S.create_simple(SPACE1_RANK, dims1); // Create a dataset. H5DataSetId dsetId = H5D.create(fileId, DSET1_NAME, H5T.H5Type.NATIVE_UCHAR, space1_Id); // Create dataspace for attribute. H5DataSpaceId space2_Id = H5S.create_simple(ATTR1_RANK, dims2); // Create default property list for attribute. H5PropertyListId plist = H5P.create(H5P.PropertyListClass.ATTRIBUTE_CREATE); // Create an attribute for the dataset using the property list. H5AttributeId attrId = H5A.create(dsetId, ATTR1_NAME, new H5DataTypeId(H5T.H5Type.NATIVE_INT), space2_Id, plist); // Close all objects. H5S.close(space1_Id); H5S.close(space2_Id); H5P.close(plist); H5A.close(attrId); H5D.close(dsetId); H5F.close(fileId); Console.WriteLine("\t\t\tPASSED"); } catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } } // test_attr_plist
private void AttributeCreateProcedure() { string name = Thread.CurrentThread.Name; hid_t space = H5S.create(H5S.class_t.NULL); Assert.IsTrue(space >= 0); Assert.IsTrue( H5A.close(H5A.create(m_shared_file_id, name, H5T.STD_I32BE, space)) >= 0); Assert.IsTrue(H5S.close(space) >= 0); }
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 unsafe void Add(ContainerType container, long fileId, string groupName, string elementName, long typeId, void *dataPtr, long spaceId, long cpl = 0, long apl = 0) { long res; long groupId; if (H5L.exists(fileId, groupName) > 0) { groupId = H5G.open(fileId, groupName); } else { groupId = H5G.create(fileId, groupName); } long id; if (container == ContainerType.Dataset) { id = H5D.create(groupId, Encoding.UTF8.GetBytes(elementName), typeId, spaceId, dcpl_id: cpl, dapl_id: apl); if (id == -1) { throw new Exception("Could not create dataset."); } if ((int)dataPtr != 0) { res = H5D.write(id, typeId, spaceId, H5S.ALL, 0, new IntPtr(dataPtr)); } res = H5D.close(id); } else { id = H5A.create(groupId, Encoding.UTF8.GetBytes(elementName), typeId, spaceId, acpl_id: cpl); if (id == -1) { throw new Exception("Could not create attribute."); } if ((int)dataPtr != 0) { res = H5A.write(id, typeId, new IntPtr(dataPtr)); } res = H5A.close(id); } res = H5G.close(groupId); }
private static void WriteAttribute(H5ObjectWithAttributes target, string name, double[] value) { H5DataTypeId dtype = H5T.copy(H5T.H5Type.NATIVE_DOUBLE); H5DataSpaceId spaceId = H5S.create_simple(1, new[] { value.LongCount() }); H5AttributeId attributeId = H5A.create(target, name, dtype, spaceId); H5A.write(attributeId, dtype, new H5Array <double>(value)); H5A.close(attributeId); H5T.close(dtype); H5S.close(spaceId); }
public void H5ArenameTest1() { hid_t att = H5A.create(m_v2_test_file, "A", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); Assert.IsTrue(H5A.rename(m_v2_test_file, "A", "new A") >= 0); Assert.IsFalse(H5A.rename(m_v2_test_file, "A", "new A") >= 0); att = H5A.create(m_v0_test_file, "A", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); Assert.IsTrue(H5A.rename(m_v0_test_file, "A", "new A") >= 0); }
public void H5Aget_name_by_idxTest1() { hid_t att = H5A.create(m_v2_test_file, "H5Aget_name", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); att = H5A.create(m_v2_test_file, "H5Aget_name_by_idx", H5T.STD_I16LE, m_space_scalar); Assert.IsTrue(att >= 0); Assert.IsTrue(H5A.close(att) >= 0); size_t buf_size = IntPtr.Zero; ssize_t size = IntPtr.Zero; StringBuilder nameBuilder = new StringBuilder(19); buf_size = new IntPtr(19); size = H5A.get_name_by_idx(m_v2_test_file, ".", H5.index_t.NAME, H5.iter_order_t.NATIVE, 0, nameBuilder, buf_size); Assert.IsTrue(size.ToInt32() == 11); string name = nameBuilder.ToString(); // names should match Assert.AreEqual("H5Aget_name", name); nameBuilder.Clear(); size = H5A.get_name_by_idx(m_v2_test_file, ".", H5.index_t.NAME, H5.iter_order_t.NATIVE, 1, nameBuilder, buf_size); Assert.IsTrue(size.ToInt32() == 18); name = nameBuilder.ToString(); // names should match Assert.AreEqual("H5Aget_name_by_idx", name); // read a truncated version buf_size = new IntPtr(3); nameBuilder = new StringBuilder(3); size = H5A.get_name_by_idx(m_v2_test_file, ".", H5.index_t.NAME, H5.iter_order_t.NATIVE, 1, nameBuilder, buf_size); Assert.IsTrue(size.ToInt32() == 18); name = nameBuilder.ToString(); // names won't match Assert.AreNotEqual("H5Aget_name_by_idx", name); Assert.AreEqual("H5", name); }