コード例 #1
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);
        }
コード例 #2
0
ファイル: H5DataSet.cs プロジェクト: lefi7z/h5ohm
        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);
                    }
                }
            }
        }
コード例 #3
0
ファイル: H5DataSet.cs プロジェクト: lefi7z/h5ohm
        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})");
                            }
                        }
                    }
                }
            }
        }
コード例 #4
0
ファイル: H5DataSet.cs プロジェクト: lefi7z/h5ohm
        /// <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));
        }
コード例 #5
0
ファイル: H5DataSet.cs プロジェクト: lefi7z/h5ohm
 /// <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})");
                 }
             }
         }
     }
 }
コード例 #6
0
ファイル: H5DataSet.cs プロジェクト: lefi7z/h5ohm
        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})");
                            }
                        }
                    }
                }
            }
        }