Ejemplo n.º 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);
        }
Ejemplo n.º 2
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));
        }