public string[] Column(long n) { using (H5Space file_space = SelectSlice(n, n + 1, axis: 1)) { using (H5Space mem_space = H5Space.Create(1, new long[] { Dims[0] })) { using (H5Type dtype = GetDType()) { int slen = (int)dtype.Size; byte[] buf = new byte[slen * mem_space.Length]; GCHandle pinnedArray = GCHandle.Alloc(buf, GCHandleType.Pinned); int status = H5D.read(ID, dtype.ID, mem_space.ID, file_space.ID, H5P.DEFAULT, pinnedArray.AddrOfPinnedObject()); pinnedArray.Free(); if (status < 0) { throw new H5LibraryException($"H5D.read() returned ({status})"); } string decoded = string1d.Enc.GetString(buf); var rv = new string[mem_space.Length]; for (int i = 0; i < rv.Length; i++) { rv[i] = decoded.Substring(i * slen, slen).TrimEnd('\0'); } return(rv); } } } }
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> /// Change the dimensions of the dataset to `new_dims`. /// </summary> public void Resize(params long[] new_dims) { if (!HasH5Pcreate) { throw new NotImplementedException($"cannot resize using hdf5 v{H5Library.LibVersion}"); } using (H5Space space = GetSpace()) { if (new_dims.Length != space.Rank) { throw new RankException($"{new_dims.Length} != {space.Rank}"); } ulong[] extent = new ulong[space.Rank]; long[] maxdims = space.MaxDims; for (int i = 0; i < new_dims.Length; i++) { if (!(0 <= new_dims[i] && (new_dims[i] <= maxdims[i] || maxdims[i] == H5Space.Unlimited))) { throw new IndexOutOfRangeException($"{new_dims[i]} > {maxdims[i]}"); } extent[i] = (ulong)new_dims[i]; } int status; if ((status = H5D.set_extent(ID, extent)) < 0) { throw new H5LibraryException($"H5D.set_extent() returned ({status})"); } } }
protected H5Space GetSpace() { if (!(ID > 0)) { throw new InvalidOperationException("operation on closed dataset"); } return(H5Space.FromDataset(ID)); }
public string this[long m, long n] { get { using (H5Space file_space = SelectPoint(m, n)) { using (H5Space mem_space = H5Space.Create(1, new long[] { 1 })) { using (H5Type dtype = GetDType()) { long slen = dtype.Size; byte[] buf = new byte[slen]; GCHandle pinnedArray = GCHandle.Alloc(buf, GCHandleType.Pinned); int status = H5D.read(ID, dtype.ID, mem_space.ID, file_space.ID, H5P.DEFAULT, pinnedArray.AddrOfPinnedObject()); pinnedArray.Free(); if (status < 0) { throw new H5LibraryException($"H5D.read() returned ({status})"); } string decoded = string1d.Enc.GetString(buf); return(decoded.TrimEnd('\0')); } } } } set { using (H5Space file_space = SelectPoint(m, n)) { using (H5Space mem_space = H5Space.Create(1, new long[] { 1 })) { using (H5Type dtype = GetDType()) { long slen = dtype.Size; if (value.Length > slen - 1) { throw new IndexOutOfRangeException($"string longer than ({slen})"); } byte[] buf = new byte[slen]; Array.Copy(string1d.Enc.GetBytes(value), buf, value.Length); GCHandle pinnedArray = GCHandle.Alloc(buf, GCHandleType.Pinned); int status = H5D.write(ID, dtype.ID, mem_space.ID, file_space.ID, H5P.DEFAULT, pinnedArray.AddrOfPinnedObject()); pinnedArray.Free(); if (status < 0) { throw new H5LibraryException($"H5D.read() returned ({status})"); } } } } } }
/// <summary> /// Create a new hdf5 `H5DataSet` at `loc_id`. /// </summary> /// <remarks> /// `maxdims` may be `null` in which case it is set to `dims`. /// </remarks> internal static H5DataSet Create(hid_t loc_id, string key, int rank, long[] dims, long[] maxdims, Type primitive_type) { hid_t dcpl; // the 'dataset creation property list' controls chunking.. if (maxdims == null || dims.SequenceEqual(maxdims)) { dcpl = H5P.DEFAULT; } else if (HasH5Pcreate) { // ..which is needed for later resizing: var chunk = new ulong[rank]; // the chunk is of size 1 in each 'unlimited' dimension and of size 'maxdims' // for all other dimensions (just like the 'SPECdata/Intensities' dataset): for (int i = 0; i < rank; i++) { if (maxdims[i] == H5Space.Unlimited) { chunk[i] = 1UL; } else if (maxdims[i] > 0) { checked { chunk[i] = (ulong)maxdims[i]; } } else { throw new ArgumentException($"invalid value in parameter 'maxdims'"); } } dcpl = H5P.create(H5P.DATASET_CREATE); H5P.set_chunk(dcpl, rank, chunk); } else { maxdims = dims; dcpl = H5P.DEFAULT; } hid_t id; using (H5Space space = H5Space.Create(rank, dims, maxdims)) using (H5Type dtype = H5Type.Create(primitive_type)) { if ((id = H5D.create(loc_id, key, dtype.ID, space.ID, H5P.DEFAULT, dcpl, H5P.DEFAULT)) < 0) { throw new H5LibraryException($"H5D.create() returned ({id})"); } } return(FromID(id)); }
/// <summary> /// Set the `value` at `index`. /// </summary> /// <remark> /// The number of indices must match this dataset's rank. /// </remark> public void Set <T>(T value, params long[] index) { using (H5Space file_space = SelectPoint(index)) { using (H5Space mem_space = H5Space.Create(1, new long[] { 1 })) { using (H5Type dtype = GetDType()) { T[] buf = new T[1]; buf[0] = value; GCHandle pinnedArray = GCHandle.Alloc(buf, GCHandleType.Pinned); int status = H5D.write(ID, dtype.ID, mem_space.ID, file_space.ID, H5P.DEFAULT, pinnedArray.AddrOfPinnedObject()); pinnedArray.Free(); if (status < 0) { throw new H5LibraryException($"H5D.write() returned ({status})"); } } } } }
public string[] this[long m] { get { using (H5Space file_space = SelectSlice(m, m + 1)) { using (H5Space mem_space = H5Space.Create(1, new long[] { Dims[1] })) { using (H5Type dtype = GetDType()) { int slen = (int)dtype.Size; byte[] buf = new byte[slen * mem_space.Length]; GCHandle pinnedArray = GCHandle.Alloc(buf, GCHandleType.Pinned); int status = H5D.read(ID, dtype.ID, mem_space.ID, file_space.ID, H5P.DEFAULT, pinnedArray.AddrOfPinnedObject()); pinnedArray.Free(); if (status < 0) { throw new H5LibraryException($"H5D.read() returned ({status})"); } string decoded = string1d.Enc.GetString(buf); var rv = new string[mem_space.Length]; for (int i = 0; i < rv.Length; i++) { rv[i] = decoded.Substring(i * slen, slen).TrimEnd('\0'); } return(rv); } } } } set { using (H5Space file_space = SelectSlice(m, m + 1)) { using (H5Space mem_space = H5Space.Create(1, new long[] { Dims[1] })) { using (H5Type dtype = GetDType()) { if (value.Length > mem_space.Length) { throw new IndexOutOfRangeException($"can't hold array of length ({value.Length})"); } int slen = (int)dtype.Size; if (value.Any(s => s.Length > slen - 1)) { throw new IndexOutOfRangeException($"string longer than ({slen})"); } byte[] buf = new byte[slen * mem_space.Length]; for (int i = 0; i < value.Length; i++) { var bytes = string1d.Enc.GetBytes(value[i]); Array.Copy(bytes, 0, buf, i * slen, bytes.Length); } GCHandle pinnedArray = GCHandle.Alloc(buf, GCHandleType.Pinned); int status = H5D.write(ID, dtype.ID, mem_space.ID, file_space.ID, H5P.DEFAULT, pinnedArray.AddrOfPinnedObject()); pinnedArray.Free(); if (status < 0) { throw new H5LibraryException($"H5D.write() returned ({status})"); } } } } } }
/// <summary> /// Load an existing dataset by `dset_id`. /// </summary> /// <remarks> /// Creates and returns an `H5DataSet` of the appropriate `space` and `dtype`. /// Throws `H5LibraryException` if `dset_id` is invalid. /// </remarks> internal static H5DataSet FromID(hid_t dset_id) { using (H5Space space = H5Space.FromDataset(dset_id)) { using (H5Type dtype = H5Type.FromDataset(dset_id)) { if (space.Rank == 1) { if (dtype.PrimitiveType == typeof(System.Double)) { return(new dset1d <double>(dset_id)); } if (dtype.PrimitiveType == typeof(System.Single)) { return(new dset1d <float>(dset_id)); } if (dtype.PrimitiveType == typeof(System.Byte)) { return(new dset1d <byte>(dset_id)); } if (dtype.PrimitiveType == typeof(System.Int64)) { return(new dset1d <long>(dset_id)); } if (dtype.PrimitiveType == typeof(System.Int32)) { return(new dset1d <int>(dset_id)); } if (dtype.PrimitiveType == typeof(System.String)) { return(new string1d(dset_id)); } } if (space.Rank == 2) { if (dtype.PrimitiveType == typeof(System.Double)) { return(new dset2d <double>(dset_id)); } if (dtype.PrimitiveType == typeof(System.Single)) { return(new dset2d <float>(dset_id)); } if (dtype.PrimitiveType == typeof(System.Byte)) { return(new dset2d <byte>(dset_id)); } if (dtype.PrimitiveType == typeof(System.Int64)) { return(new dset2d <long>(dset_id)); } if (dtype.PrimitiveType == typeof(System.Int32)) { return(new dset2d <int>(dset_id)); } if (dtype.PrimitiveType == typeof(System.String)) { return(new string2d(dset_id)); } } if (space.Rank == 3) { if (dtype.PrimitiveType == typeof(System.Double)) { return(new dset3d <double>(dset_id)); } if (dtype.PrimitiveType == typeof(System.Single)) { return(new dset3d <float>(dset_id)); } if (dtype.PrimitiveType == typeof(System.Byte)) { return(new dset3d <byte>(dset_id)); } if (dtype.PrimitiveType == typeof(System.Int64)) { return(new dset3d <long>(dset_id)); } if (dtype.PrimitiveType == typeof(System.Int32)) { return(new dset3d <int>(dset_id)); } } throw new NotImplementedException($"dataset<{dtype.PrimitiveType}> of rank ({space.Rank})"); } } }