Exemplo n.º 1
0
 /// <summary>
 /// Store a `value` in this *attribute*. The `object` is unboxed to this attribute's
 /// `.PrimitiveType`. This may throw an `InvalidCastException`.
 /// </summary>
 public void Write(object value)
 {
     using (H5Type dtype = GetDType())
     {
         if (dtype.PrimitiveType == typeof(System.String))
         {
             Writes((string)value);
         }
         else if (dtype.PrimitiveType == typeof(System.Double))
         {
             Write((double)value);
         }
         else if (dtype.PrimitiveType == typeof(System.Single))
         {
             Write((float)value);
         }
         else if (dtype.PrimitiveType == typeof(System.Byte))
         {
             Write((byte)value);
         }
         else if (dtype.PrimitiveType == typeof(System.Int64))
         {
             Write((long)value);
         }
         else if (dtype.PrimitiveType == typeof(System.Int32))
         {
             Write((int)value);
         }
         else
         {
             throw new NotImplementedException(dtype.PrimitiveType.ToString());
         }
     }
 }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        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);
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Read the value stored in this *attribute* and box it as an `object`.
        /// </summary>
        public object Read()
        {
            using (H5Type dtype = GetDType())
            {
                if (dtype.PrimitiveType == typeof(System.String))
                {
                    return(Reads());
                }
                if (dtype.PrimitiveType == typeof(System.Double))
                {
                    return(Read <double>());
                }
                if (dtype.PrimitiveType == typeof(System.Single))
                {
                    return(Read <float>());
                }
                if (dtype.PrimitiveType == typeof(System.Byte))
                {
                    return(Read <byte>());
                }
                if (dtype.PrimitiveType == typeof(System.Int64))
                {
                    return(Read <long>());
                }
                if (dtype.PrimitiveType == typeof(System.Int32))
                {
                    return(Read <int>());
                }

                throw new NotImplementedException(dtype.PrimitiveType.ToString());
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Store a string in this *attribute*.
        /// </summary>
        public void Writes(string value)
        {
            using (H5Type dtype = GetDType())
            {
                if (typeof(System.String) != dtype.PrimitiveType)
                {
                    throw new InvalidCastException(dtype.PrimitiveType.ToString());
                }

                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      = H5A.write(ID, dtype.ID, pinnedArray.AddrOfPinnedObject());
                pinnedArray.Free();
                if (status < 0)
                {
                    throw new H5LibraryException($"H5A.write() returned ({status})");
                }
            }
        }
Exemplo n.º 6
0
        protected H5Type GetDType()
        {
            if (!(ID > 0))
            {
                throw new InvalidOperationException("operation on closed dataset");
            }

            return(H5Type.FromDataset(ID));
        }
Exemplo n.º 7
0
        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})");
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
        /// <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));
        }
Exemplo n.º 9
0
 /// <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})");
                 }
             }
         }
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// Store a numeric value in this *attribute*.
        /// </summary>
        /// <remarks>
        /// For writing a `string`, use `Writes()` instead!
        /// </remarks>
        public void Write <T>(T value)
        {
            using (H5Type dtype = GetDType())
            {
                if (typeof(T) != dtype.PrimitiveType)
                {
                    throw new InvalidCastException(dtype.PrimitiveType.ToString());
                }

                T[] buf = new T[1] {
                    value
                };
                GCHandle pinnedArray = GCHandle.Alloc(buf, GCHandleType.Pinned);
                int      status      = H5A.write(ID, dtype.ID, pinnedArray.AddrOfPinnedObject());
                pinnedArray.Free();
                if (status < 0)
                {
                    throw new H5LibraryException($"H5A.write() returned ({status})");
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Read the string value stored in this *attribute*.
        /// </summary>
        public string Reads()
        {
            using (H5Type dtype = GetDType())
            {
                if (typeof(System.String) != dtype.PrimitiveType)
                {
                    throw new InvalidCastException(dtype.PrimitiveType.ToString());
                }

                long     slen        = dtype.Size;
                byte[]   buf         = new byte[slen];
                GCHandle pinnedArray = GCHandle.Alloc(buf, GCHandleType.Pinned);
                int      status      = H5A.read(ID, dtype.ID, pinnedArray.AddrOfPinnedObject());
                pinnedArray.Free();
                if (status < 0)
                {
                    throw new H5LibraryException($"H5A.read() returned ({status})");
                }

                string decoded = System.Text.Encoding.ASCII.GetString(buf);

                return(decoded.TrimEnd('\0'));
            }
        }
Exemplo n.º 12
0
 H5Type GetDType()
 {
     return(H5Type.FromAttribute(ID));
 }
Exemplo n.º 13
0
        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})");
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 14
0
 /// <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})");
         }
     }
 }