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); }
/// <summary> /// Add a hdf5 *attribute* to this dataset. The `Type` argument is mandatory. /// If a `default_` value is given, it is written to the hdf5 file immediately. /// Note, that an existing *attribute* will not be overwritten. /// </summary> public H5Attribute SetAttribute(string name, Type primitive, object default_ = null) { if (H5Attribute.Exists(ID, name)) { throw new InvalidOperationException($"attribute exists ({name})"); } return(H5Attribute.Create(ID, name, primitive, default_)); }
/// <summary> /// Return the hdf5 *attribute* by `name` of this dataset. /// </summary> public H5Attribute GetAttribute(string name) { if (!H5Attribute.Exists(ID, name)) { throw new KeyNotFoundException(name); } return(H5Attribute.FromID(H5Attribute.Open(ID, name))); }