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); }
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); }
/// <summary> /// Deletes the attribute from the file. /// Assumes that parent object is already open /// </summary> /// <param name="_objectId"></param> /// <param name="_title"></param> public static void DeleteAttribute(Hdf5Identifier _objectId, string _title) { int result = H5A.delete(_objectId.Value, _title); }
/// <summary> /// Prepares an attribute. The following use cases exist: /// InitializeAttribute = False: no data is written /// InitializeAttribute = True and DimensionLimitSet < H5S.Unlimited: overwrite attribute data /// InitializeAttribute = True and DimensionLimitSet = H5S.Unlimited: merge attribute data /// </summary> /// <typeparam name="T">The data type of the attribute to be created.</typeparam> /// <param name="locationId">The location ID.</param> /// <param name="name">The name of the attribute.</param> /// <param name="valueSet">The values to be written to the attribute.</param> /// <param name="dimensionLimitSet">The maximum dimensions of the attribute.</param> /// <param name="initializeAttribute">A boolean which indicates if the attribute data shall be untouched, overwritten or merged.</param> /// <param name="callerMemberName">The name of the calling function.</param> 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 void H5AdeleteTest2() { Assert.IsFalse( H5A.delete(Utilities.RandomInvalidHandle(), "A") >= 0); Assert.IsFalse(H5A.delete(m_v0_test_file, ".") >= 0); }