public void H5Sget_select_hyper_blocklistTest1()
        {
            hsize_t[] dims  = { 1, 2, 3 };
            hid_t     space = H5S.create_simple(dims.Length, dims, dims);

            Assert.IsTrue(space > 0);
            hsize_t[] start = { 0, 0, 0 };
            hsize_t[] count = { 1, 1, 1 };
            hsize_t[] block = { 1, 2, 3 };
            Assert.IsTrue(
                H5S.select_hyperslab(space, H5S.seloper_t.SET, start, null,
                                     count, block) >= 0);
            Assert.IsTrue(H5S.get_select_hyper_nblocks(space) == 1);

            hsize_t[] buf = new hsize_t [2 * dims.Length];

            Assert.IsTrue(
                H5S.get_select_hyper_blocklist(space, 0, 1, buf) >= 0);

            for (int i = 0; i < dims.Length; ++i)
            {
                buf[i]     = 0;
                buf[i + 3] = dims[i] - 1;
            }

            Assert.IsTrue(H5S.close(space) >= 0);
        }
Exemplo n.º 2
0
        public void Put(Array data, ulong[] location = null)
        {
            ulong[] shape = data.Shape();

            WithDataSpace((h5Ref, dsRef) =>
            {
                long memDataSpace = H5S.ALL;

                if (location != null)
                {
                    int selection = H5S.select_none(dsRef);
                    if (selection < 0)
                    {
                        throw new H5SSException("Couldn't clear dataspace selection");
                    }
                    ulong[] stride = Ones(shape.Length);
                    selection      = H5S.select_hyperslab(dsRef,
                                                          H5S.seloper_t.SET,
                                                          location,
                                                          stride,
                                                          stride,
                                                          shape
                                                          );
                    if (selection < 0)
                    {
                        throw new H5SSException("Couldn't select hyperslab");
                    }

                    memDataSpace = H5S.create_simple(shape.Length, shape, shape);
                }

                IntPtr iPtr;
                var effectiveSize = data.Length * ElementSize;
                //if (DataType == HDF5DataType.String)
                //{
                //    // Convert to byte array...

                //}
                //else
                //{
                //}
                var dtype = H5D.get_type(h5Ref); // Return?

                iPtr = CreateNativeArray(data, dtype);
                // copy to unmanaged array?
                var success = H5D.write(h5Ref, dtype, memDataSpace, dsRef, H5P.DEFAULT, iPtr);
                H5T.close(dtype);

                if (location != null)
                {
                    H5S.close(memDataSpace);
                }

                Marshal.FreeHGlobal(iPtr);
                if (success < 0)
                {
                    throw new H5SSException(string.Format("Couldn't write to dataset: {0}", this.Path));
                }
            });
        }
Exemplo n.º 3
0
 public void SelectHyperslab(ulong[] start, ulong[] stride, ulong[] count, ulong[] block)
 {
     if (H5S.select_hyperslab(ID, H5S.seloper_t.SET, start, stride, count, block) < 0)
     {
         throw new H5LibraryException($"H5S.select_hyperslab() returned an ERROR");
     }
 }
Exemplo n.º 4
0
        private static void AppendData <T>(hid_t dataSet, T[] data, ref int rows)
        {
            var fileSpace = H5D.get_space(dataSet);

            if (fileSpace < 0)
            {
                throw new Exception("Failed to get data space of data set.");
            }

            var offset = new ulong[] { (ulong)rows };
            var count  = new ulong[] { (ulong)data.Length };

            if (H5S.select_hyperslab(fileSpace, H5S.seloper_t.SET, offset, null, count, null) < 0)
            {
                throw new Exception("H5S.select_hyperslab failed.");
            }

            var memSpace = H5S.create_simple(1, count, null);

            if (memSpace < 0)
            {
                throw new Exception("H5S.create_simple failed.");
            }

            if (H5D.write(dataSet, NumericTypeToHDF5Type <T>(), memSpace, fileSpace, H5P.DEFAULT, new PinnedObject(data)) < 0)
            {
                throw new Exception("H5D.write failed.");
            }

            H5S.close(memSpace);
            H5S.close(fileSpace);

            rows += data.Length;
        }
Exemplo n.º 5
0
        private unsafe void WriteData(ulong fileOffset, ulong bufferOffset, ulong length, ChannelContext channelContext)
        {
            Contract.Requires(channelContext != null, nameof(channelContext));

            long groupId            = -1;
            long datasetId          = -1;
            long dataspaceId        = -1;
            long dataspaceId_Buffer = -1;

            try
            {
                groupId = H5G.open(_fileId, $"/{ channelContext.ChannelDescription.ChannelName }");

                var datasetName = $"dataset_{ channelContext.ChannelDescription.DatasetName.Replace(" ", "_") }";
                datasetId          = H5D.open(groupId, datasetName);
                dataspaceId        = H5D.get_space(datasetId);
                dataspaceId_Buffer = H5S.create_simple(1, new ulong[] { length }, null);

                var simpleBuffers = channelContext.Buffer.ToSimpleBuffer();

                // dataset
                H5S.select_hyperslab(dataspaceId,
                                     H5S.seloper_t.SET,
                                     new ulong[] { fileOffset },
                                     new ulong[] { 1 },
                                     new ulong[] { 1 },
                                     new ulong[] { length });

                var offset = (int)bufferOffset * simpleBuffers.ElementSize;
                var buffer = simpleBuffers.RawBuffer[offset..];
Exemplo n.º 6
0
        public void H5RcreateTest4()
        {
            byte[] path =
                   Encoding.UTF8.GetBytes(String.Join("/", m_utf8strings));
            // make room for the trailling \0
            byte[] name = new byte[path.Length + 1];
            Array.Copy(path, name, path.Length);

            hsize_t[] dims = new hsize_t[] { 10, 20 };
            hid_t space = H5S.create_simple(2, dims, null);
            Assert.IsTrue(space >= 0);
            hid_t dset = H5D.create(m_v2_test_file, name, H5T.STD_I32LE, space,
                m_lcpl_utf8);
            Assert.IsTrue(dset >= 0);
            hsize_t[] start = { 5, 10 };
            hsize_t[] count = { 1, 1 };
            hsize_t[] block = { 2, 4 };
            Assert.IsTrue(
                H5S.select_hyperslab(space, H5S.seloper_t.SET, start, null,
                count, block) >= 0);

            byte[] refer = new byte[H5R.DSET_REG_REF_BUF_SIZE];
            GCHandle hnd = GCHandle.Alloc(refer, GCHandleType.Pinned);

            Assert.IsTrue(
                H5R.create(hnd.AddrOfPinnedObject(), m_v2_test_file, name,
                H5R.type_t.DATASET_REGION, space) >= 0);

            ssize_t size = H5R.get_name(m_v2_test_file,
                H5R.type_t.DATASET_REGION, hnd.AddrOfPinnedObject(),
                (byte[])null, IntPtr.Zero);
            Assert.IsTrue(size.ToInt32() == name.Length);

            byte[] buf = new byte[size.ToInt32() + 1];
            size = H5R.get_name(m_v2_test_file, H5R.type_t.DATASET_REGION,
                hnd.AddrOfPinnedObject(), buf, new IntPtr(buf.Length));
            Assert.IsTrue(size.ToInt32() == name.Length);

            // we need to account for the leading "/", which was not included
            // in path
            for (int i = 0; i < name.Length; ++i)
            {
                Assert.IsTrue(name[i] == buf[i + 1]);
            }

            hid_t sel = H5R.get_region(dset, H5R.type_t.DATASET_REGION,
                hnd.AddrOfPinnedObject());
            Assert.IsTrue(sel >= 0);

            hnd.Free();

            Assert.IsTrue(H5S.extent_equal(space, sel) > 0);
            Assert.IsTrue(H5S.get_select_hyper_nblocks(space)
                == H5S.get_select_hyper_nblocks(sel));

            Assert.IsTrue(H5S.close(sel) >= 0);
            Assert.IsTrue(H5D.close(dset) >= 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
Exemplo n.º 7
0
        /// <summary>
        /// WARNING: ADVANCED USE ONLY!! Loads a 2D generic dataset from an H5 file.
        /// The generic loaders only loads data in non-Unity friendly types, such as bytes, uints, longs etc...
        /// You'll have to know the correct cast to retrieve usable data.
        ///
        /// Created With help from https://github.com/LiorBanai/HDF5-CSharp/blob/master/HDF5-CSharp/Hdf5Dataset.cs
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="datasetName"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        /// <exception cref="FileNotFoundException"></exception>
        static T[,] Load2DDataset <T>(string filePath, string datasetName)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException($"Loading dataset {datasetName} from file that doesn't exist {filePath}");
            }
            long fileId = H5F.open(filePath, H5F.ACC_RDONLY);

            T[,] resultArray = new T[2, 2];
            try {
                ulong[] start = { 0, 0 };
                ulong[] count = { 0, 0 };

                long    datasetId = H5D.open(fileId, datasetName);
                var     datatype  = H5D.get_type(datasetId);
                var     spaceId   = H5D.get_space(datasetId);
                int     rank      = H5S.get_simple_extent_ndims(spaceId);
                ulong[] maxDims   = new ulong[rank];
                ulong[] dims      = new ulong[rank];
                H5S.get_simple_extent_dims(spaceId, dims, maxDims);

                count[0] = dims[0];
                count[1] = dims[1];

                // Define file hyperslab.
                long status = H5S.select_hyperslab(spaceId, H5S.seloper_t.SET, start, null, count, null);

                // Define the memory dataspace.
                resultArray = new T[dims[0], dims[1]];
                var memId = H5S.create_simple(rank, dims, null);

                // Define memory hyperslab.
                status = H5S.select_hyperslab(memId, H5S.seloper_t.SET, start, null,
                                              count, null);

                // Read data from hyperslab in the file into the hyperslab in
                // memory and display.
                GCHandle handle = GCHandle.Alloc(resultArray, GCHandleType.Pinned);

                try {
                    H5D.read(datasetId, datatype, memId, spaceId,
                             H5P.DEFAULT, handle.AddrOfPinnedObject());
                }
                finally {
                    handle.Free();
                    H5S.close(status);
                    H5S.close(memId);
                    H5S.close(spaceId);
                    H5D.close(datatype);
                    H5D.close(datasetId);
                }
            }
            finally {
                H5F.close(fileId);
            }
            return(resultArray);
        }
Exemplo n.º 8
0
        public void H5RdereferenceTest4()
        {
            byte[] path =
                Encoding.UTF8.GetBytes(String.Join("/", m_utf8strings));
            // make room for the trailling \0
            byte[] name = new byte[path.Length + 1];
            Array.Copy(path, name, path.Length);

            hsize_t[] dims  = new hsize_t[] { 10, 20 };
            hid_t     space = H5S.create_simple(2, dims, null);

            Assert.IsTrue(space >= 0);
            hid_t dset = H5D.create(m_v2_test_file, name, H5T.STD_I32LE, space,
                                    m_lcpl_utf8);

            H5O.info_t info = new H5O.info_t();
            Assert.IsTrue(H5O.get_info(dset, ref info) >= 0);
            haddr_t address = info.addr;

            Assert.IsTrue(H5D.close(dset) >= 0);

            Assert.IsTrue(dset >= 0);
            hsize_t[] start = { 5, 10 };
            hsize_t[] count = { 1, 1 };
            hsize_t[] block = { 2, 4 };
            Assert.IsTrue(
                H5S.select_hyperslab(space, H5S.seloper_t.SET, start, null,
                                     count, block) >= 0);

            byte[]   refer = new byte[H5R.DSET_REG_REF_BUF_SIZE];
            GCHandle hnd   = GCHandle.Alloc(refer, GCHandleType.Pinned);

            Assert.IsTrue(
                H5R.create(hnd.AddrOfPinnedObject(), m_v2_test_file, name,
                           H5R.type_t.DATASET_REGION, space) >= 0);

            #if HDF5_VER1_10
            dset = H5R.dereference(m_v2_test_file, H5P.DEFAULT,
                                   H5R.type_t.DATASET_REGION, hnd.AddrOfPinnedObject());
            #else
            dset = H5R.dereference(m_v2_test_file, H5R.type_t.DATASET_REGION,
                                   hnd.AddrOfPinnedObject());
            #endif
            Assert.IsTrue(dset >= 0);

            hnd.Free();

            Assert.IsTrue(H5O.get_info(dset, ref info) >= 0);
            Assert.IsTrue(address == info.addr);
            Assert.IsTrue(H5D.close(dset) >= 0);

            Assert.IsTrue(H5S.close(space) >= 0);
        }
Exemplo n.º 9
0
        public void H5Sget_select_hyper_nblocksTest1()
        {
            hsize_t[] dims  = { 1, 2, 3 };
            hid_t     space = H5S.create_simple(dims.Length, dims, dims);

            Assert.IsTrue(space > 0);
            hsize_t[] start = { 0, 0, 0 };
            hsize_t[] count = { 1, 1, 1 };
            hsize_t[] block = { 1, 2, 3 };
            Assert.IsTrue(
                H5S.select_hyperslab(space, H5S.seloper_t.SET, start, null,
                                     count, block) >= 0);
            Assert.IsTrue(H5S.get_select_hyper_nblocks(space) == 1);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
Exemplo n.º 10
0
        public static T[] ReadDataset <T>(long locationId, string datasetPath, ulong start = 0, ulong stride = 0, ulong block = 0, ulong count = 0)
        {
            long datasetId   = -1;
            long dataspaceId = -1;

            T[] result;

            try
            {
                datasetId = H5D.open(locationId, datasetPath);

                if (H5I.is_valid(datasetId) <= 0)
                {
                    throw new Exception(ErrorMessage.IOHelper_CouldNotOpenDataset);
                }

                if (start == 0 && stride == 0 && block == 0 && count == 0)
                {
                    result = IOHelper.Read <T>(datasetId, DataContainerType.Dataset);
                }
                else
                {
                    dataspaceId = H5D.get_space(datasetId);

                    if (H5S.select_hyperslab(dataspaceId, H5S.seloper_t.SET, new ulong[] { start }, new ulong[] { stride }, new ulong[] { block }, new ulong[] { count }) < 0)
                    {
                        throw new Exception(ErrorMessage.IOHelper_CouldNotSelectHyperslab);
                    }

                    result = IOHelper.Read <T>(datasetId, DataContainerType.Dataset, dataspaceId);
                }
            }
            finally
            {
                if (H5I.is_valid(dataspaceId) > 0)
                {
                    H5S.close(dataspaceId);
                }
                if (H5I.is_valid(datasetId) > 0)
                {
                    H5D.close(datasetId);
                }
            }

            return(result);
        }
Exemplo n.º 11
0
        public static T[,] ReadDataset <T>(int groupId, string name, ulong beginIndex, ulong endIndex) where T : struct
        {
            ulong[]  start = { 0, 0 }, stride = null, count = { 0, 0 },
            block = null, offsetOut = new ulong[] { 0, 0 };
            var      datatype = GetDatatype(typeof(T));

            name = ToHdf5Name(name);

            var      datasetId = H5D.open(groupId, name);
            var      spaceId   = H5D.get_space(datasetId);
            int      rank      = H5S.get_simple_extent_ndims(spaceId);
            ulong[]  maxDims   = new ulong[rank];
            ulong[]  dims      = new ulong[rank];
            ulong[]  chunkDims = new ulong[rank];
            var      memId_n   = H5S.get_simple_extent_dims(spaceId, dims, maxDims);

            start[0] = beginIndex;
            start[1] = 0;
            count[0] = endIndex - beginIndex;
            count[1] = dims[1];

            var      status = H5S.select_hyperslab(spaceId, H5S.seloper_t.SET, start, stride, count, block);


            // Define the memory dataspace.

            T[,] dset = new T[count[0], count[1]];
            var      memId = H5S.create_simple(rank, count, null);

            // Define memory hyperslab.
            status = H5S.select_hyperslab(memId, H5S.seloper_t.SET, offsetOut, null,
                                          count, null);

            /*
             * Read data from hyperslab in the file into the hyperslab in
             * memory and display.
             */
            GCHandle hnd = GCHandle.Alloc(dset, GCHandleType.Pinned);
            H5D.read(datasetId, datatype, memId, spaceId,
                     H5P.DEFAULT, hnd.AddrOfPinnedObject());
            hnd.Free();
            H5D.close(datasetId);
            H5S.close(spaceId);
            H5S.close(memId);
            return(dset);
        }
Exemplo n.º 12
0
        public void H5Soffset_simpleTest1()
        {
            hsize_t[] dims  = { 10, 10 };
            hid_t     space = H5S.create_simple(dims.Length, dims, null);

            Assert.IsTrue(space > 0);

            hsize_t[] start = { 0, 0 };
            hsize_t[] count = { 1, 1 };
            hsize_t[] block = { 2, 3 };
            Assert.IsTrue(
                H5S.select_hyperslab(space, H5S.seloper_t.SET, start, null,
                                     count, block) >= 0);

            hssize_t[] offset = { 0, 0 };
            Assert.IsTrue(H5S.offset_simple(space, offset) >= 0);

            Assert.IsTrue(H5S.close(space) >= 0);
        }
Exemplo n.º 13
0
        public void H5Rget_obj_typeTest3()
        {
            byte[] path =
                Encoding.UTF8.GetBytes(String.Join("/", m_utf8strings));
            // make room for the trailling \0
            byte[] name = new byte[path.Length + 1];
            Array.Copy(path, name, path.Length);

            hsize_t[] dims  = new hsize_t[] { 10, 20 };
            hid_t     space = H5S.create_simple(2, dims, null);

            Assert.IsTrue(space >= 0);
            hid_t dset = H5D.create(m_v0_test_file, name, H5T.STD_I32LE, space,
                                    m_lcpl_utf8);

            Assert.IsTrue(dset >= 0);
            hsize_t[] start = { 5, 10 };
            hsize_t[] count = { 1, 1 };
            hsize_t[] block = { 2, 4 };
            Assert.IsTrue(
                H5S.select_hyperslab(space, H5S.seloper_t.SET, start, null,
                                     count, block) >= 0);

            byte[]   refer = new byte[H5R.DSET_REG_REF_BUF_SIZE];
            GCHandle hnd   = GCHandle.Alloc(refer, GCHandleType.Pinned);

            Assert.IsTrue(
                H5R.create(hnd.AddrOfPinnedObject(), m_v0_test_file, name,
                           H5R.type_t.DATASET_REGION, space) >= 0);

            H5O.type_t obj_type = H5O.type_t.UNKNOWN;
            Assert.IsTrue(
                H5R.get_obj_type(m_v0_test_file, H5R.type_t.DATASET_REGION,
                                 hnd.AddrOfPinnedObject(), ref obj_type) >= 0);

            hnd.Free();

            Assert.IsTrue(obj_type == H5O.type_t.DATASET);

            Assert.IsTrue(H5D.close(dset) >= 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
Exemplo n.º 14
0
        public void AppendDataset(Array dataset)
        {
            if (!Hdf5Utils.GetRealName(GroupId, Datasetname, string.Empty).valid)
            {
                string msg = "call constructor or FirstDataset first before appending.";
                Hdf5Utils.LogError?.Invoke(msg);
                throw new Hdf5Exception(msg);
            }
            _oldDims     = _currentDims;
            _currentDims = GetDims(dataset);
            int rank = dataset.Rank;

            ulong[] zeros = Enumerable.Range(0, rank).Select(z => (ulong)0).ToArray();

            /* Extend the dataset. Dataset becomes 10 x 3  */
            var size = new[] { _oldDims[0] + _currentDims[0] }.Concat(_oldDims.Skip(1)).ToArray();

            _status = H5D.set_extent(_datasetId, size);
            ulong[] offset = new[] { _oldDims[0] }.Concat(zeros.Skip(1)).ToArray();

            /* Select a hyperslab in extended portion of dataset  */
            var filespaceId = H5D.get_space(_datasetId);

            _status = H5S.select_hyperslab(filespaceId, H5S.seloper_t.SET, offset, null,
                                           _currentDims, null);

            /* Define memory space */
            var memId = H5S.create_simple(Rank, _currentDims, null);

            /* Write the data to the extended portion of dataset  */
            GCHandle hnd = GCHandle.Alloc(dataset, GCHandleType.Pinned);

            _status = H5D.write(_datasetId, _datatype, memId, filespaceId,
                                H5P.DEFAULT, hnd.AddrOfPinnedObject());
            hnd.Free();

            _currentDims = size;
            H5S.close(memId);
            H5S.close(filespaceId);
        }
Exemplo n.º 15
0
        public void H5Sselect_validTest1()
        {
            hsize_t[] dims  = { 10, 20, 30 };
            hid_t     space = H5S.create_simple(dims.Length, dims, dims);

            Assert.IsTrue(space > 0);
            hsize_t[] start = { 0, 0, 0 };
            hsize_t[] count = { 1, 1, 1 };
            hsize_t[] block = { 1, 2, 3 };
            Assert.IsTrue(
                H5S.select_hyperslab(space, H5S.seloper_t.SET, start, null,
                                     count, block) >= 0);
            Assert.IsTrue(H5S.select_valid(space) > 0);

            start[1] = 50;
            Assert.IsTrue(
                H5S.select_hyperslab(space, H5S.seloper_t.OR, start, null,
                                     count, block) >= 0);
            Assert.IsTrue(H5S.select_valid(space) == 0);

            Assert.IsTrue(H5S.close(space) >= 0);
        }
Exemplo n.º 16
0
        static void TestExtent(hid_t file, string dataSetName, ulong[] dims, ulong[] extent)
        {
            var dataSet = CreateDataSet(file, dataSetName, H5T.NATIVE_INT,
                                        dims, new ulong[] { H5S.UNLIMITED, 4 }, new ulong[] { 4, 4 });

            H5D.set_extent(dataSet, extent);

            var fileSpace = H5D.get_space(dataSet);

            H5S.select_hyperslab(fileSpace, H5S.seloper_t.SET,
                                 new ulong[] { 0, 0 }, null, new ulong[] { 2, 4 }, null);

            var memSpace = H5S.create_simple(2, new ulong[] { 2, 4 }, null);

            if (H5D.write(dataSet, H5T.NATIVE_INT, memSpace, fileSpace, H5P.DEFAULT,
                          new PinnedObject(Enumerable.Range(0, 8).ToArray())) < 0)
            {
                WriteLine("H5D.write failed.");
            }

            H5D.close(dataSet);
        }
Exemplo n.º 17
0
        public void AppendDataset(Array dataset)
        {
            if (!DatasetExists)
            {
                throw new Exception("call constructor or FirstDataset first before appending.");
            }
            oldDims     = currentDims;
            currentDims = getDims(dataset);
            int rank = dataset.Rank;

            ulong[] zeros = Enumerable.Range(0, rank).Select(z => (ulong)0).ToArray();

            /* Extend the dataset. Dataset becomes 10 x 3  */
            var size = new ulong[] { oldDims[0] + currentDims[0] }.Concat(oldDims.Skip(1)).ToArray();

            status = H5D.set_extent(datasetId, size);
            ulong[] offset = new ulong[] { oldDims[0] }.Concat(zeros.Skip(1)).ToArray();

            /* Select a hyperslab in extended portion of dataset  */
            var filespaceId = H5D.get_space(datasetId);

            status = H5S.select_hyperslab(filespaceId, H5S.seloper_t.SET, offset, null,
                                          currentDims, null);

            /* Define memory space */
            var memId = H5S.create_simple(Rank, currentDims, null);

            /* Write the data to the extended portion of dataset  */
            GCHandle hnd = GCHandle.Alloc(dataset, GCHandleType.Pinned);

            status = H5D.write(datasetId, datatype, memId, filespaceId,
                               H5P.DEFAULT, hnd.AddrOfPinnedObject());
            hnd.Free();

            currentDims = size;
            H5S.close(memId);
            //filespaceId must be closed by H5S.close() not H5D
            H5S.close(filespaceId);
        }
Exemplo n.º 18
0
        public static Array ReadDataset(long locationId, string datasetPath, ulong start = 0, ulong stride = 0, ulong block = 0, ulong count = 0)
        {
            long datasetId   = -1;
            long dataspaceId = -1;
            long typeId      = -1;

            Array result;

            try
            {
                datasetId = H5D.open(locationId, datasetPath);

                if (H5I.is_valid(datasetId) <= 0)
                {
                    throw new Exception(ErrorMessage.IOHelper_CouldNotOpenDataset);
                }

                typeId = H5D.get_type(datasetId);

                if (start == 0 && stride == 0 && block == 0 && count == 0)
                {
                    // invoke HdfHelper.Read
                    result = (Array)GeneralHelper.InvokeGenericMethod(typeof(IOHelper), null, nameof(IOHelper.Read),
                                                                      BindingFlags.Public | BindingFlags.Static,
                                                                      TypeConversionHelper.GetTypeFromHdfTypeId(typeId),
                                                                      new object[] { datasetId, DataContainerType.Dataset, Type.Missing });
                }
                else
                {
                    dataspaceId = H5D.get_space(datasetId);

                    if (H5S.select_hyperslab(dataspaceId, H5S.seloper_t.SET, new ulong[] { start }, new ulong[] { stride }, new ulong[] { count }, new ulong[] { block }) < 0)
                    {
                        throw new Exception(ErrorMessage.IOHelper_CouldNotSelectHyperslab);
                    }

                    // invoke HdfHelper.Read
                    result = (Array)GeneralHelper.InvokeGenericMethod(typeof(IOHelper), null, nameof(IOHelper.Read),
                                                                      BindingFlags.Public | BindingFlags.Static,
                                                                      TypeConversionHelper.GetTypeFromHdfTypeId(typeId),
                                                                      new object[] { datasetId, DataContainerType.Dataset, dataspaceId });
                }
            }
            finally
            {
                if (H5I.is_valid(dataspaceId) > 0)
                {
                    H5S.close(dataspaceId);
                }
                if (H5I.is_valid(typeId) > 0)
                {
                    H5T.close(typeId);
                }
                if (H5I.is_valid(datasetId) > 0)
                {
                    H5D.close(datasetId);
                }
            }

            return(result);
        }
Exemplo n.º 19
0
        private static void createVDS()
        {
            // create files
            m_a_class_file = Utilities.H5TempFile(ref m_a_class_file_name,
                                                  H5F.libver_t.LATEST, true);
            Assert.IsTrue(m_a_class_file >= 0);
            m_b_class_file = Utilities.H5TempFile(ref m_b_class_file_name,
                                                  H5F.libver_t.LATEST, true);
            Assert.IsTrue(m_b_class_file >= 0);
            m_c_class_file = Utilities.H5TempFile(ref m_c_class_file_name,
                                                  H5F.libver_t.LATEST, true);
            Assert.IsTrue(m_c_class_file >= 0);
            m_vds_class_file = Utilities.H5TempFile(ref m_vds_class_file_name);
            Assert.IsTrue(m_vds_class_file >= 0);

            //
            // create target datasets
            //
            hid_t dcpl = H5P.create(H5P.DATASET_CREATE);

            Assert.IsTrue(dcpl >= 0);
            int      fill_value = 1;
            GCHandle hnd        = GCHandle.Alloc(fill_value, GCHandleType.Pinned);

            Assert.IsTrue(H5P.set_fill_value(dcpl, H5T.NATIVE_INT,
                                             hnd.AddrOfPinnedObject()) >= 0);

            hsize_t[] dims    = { 6 };
            hid_t     src_dsp = H5S.create_simple(1, dims, null);

            // A
            fill_value = 1;
            hid_t a = H5D.create(m_a_class_file, "A", H5T.STD_I32LE, src_dsp);

            Assert.IsTrue(a >= 0);
            Assert.IsTrue(H5D.close(a) >= 0);
            // B
            fill_value = 2;
            hid_t b = H5D.create(m_b_class_file, "B", H5T.STD_I32LE, src_dsp);

            Assert.IsTrue(b >= 0);
            Assert.IsTrue(H5D.close(b) >= 0);
            // B
            fill_value = 3;
            hid_t c = H5D.create(m_c_class_file, "C", H5T.STD_I32LE, src_dsp);

            Assert.IsTrue(c >= 0);
            Assert.IsTrue(H5D.close(c) >= 0);

            //
            // create the VDS
            //
            fill_value = -1;
            hsize_t[] vds_dims = { 4, 6 };
            hid_t     vds_dsp  = H5S.create_simple(2, vds_dims, null);

            hsize_t[] start = { 0, 0 };
            hsize_t[] count = { 1, 1 };
            hsize_t[] block = { 1, 6 };

            start[0] = 0;
            Assert.IsTrue(H5S.select_hyperslab(vds_dsp, H5S.seloper_t.SET,
                                               start, null, count, block) >= 0);
            Assert.IsTrue(H5P.set_virtual(dcpl, vds_dsp,
                                          m_a_class_file_name, "A", src_dsp) >= 0);

            start[0] = 1;
            Assert.IsTrue(H5S.select_hyperslab(vds_dsp, H5S.seloper_t.SET,
                                               start, null, count, block) >= 0);
            Assert.IsTrue(H5P.set_virtual(dcpl, vds_dsp,
                                          m_b_class_file_name, "B", src_dsp) >= 0);

            start[0] = 2;
            Assert.IsTrue(H5S.select_hyperslab(vds_dsp, H5S.seloper_t.SET,
                                               start, null, count, block) >= 0);
            Assert.IsTrue(H5P.set_virtual(dcpl, vds_dsp,
                                          m_c_class_file_name, "C", src_dsp) >= 0);

            hid_t vds = H5D.create(m_vds_class_file, "VDS", H5T.STD_I32LE,
                                   vds_dsp, H5P.DEFAULT, dcpl, H5P.DEFAULT);

            Assert.IsTrue(vds >= 0);
            Assert.IsTrue(H5D.close(vds) >= 0);

            Assert.IsTrue(H5S.close(vds_dsp) >= 0);
            Assert.IsTrue(H5S.close(src_dsp) >= 0);
            Assert.IsTrue(H5P.close(dcpl) >= 0);

            hnd.Free();

            // close the satellite files
            Assert.IsTrue(H5F.close(m_a_class_file) >= 0);
            Assert.IsTrue(H5F.close(m_b_class_file) >= 0);
            Assert.IsTrue(H5F.close(m_c_class_file) >= 0);
        }
Exemplo n.º 20
0
 public void H5Sselect_hyperslabTest2()
 {
     Assert.IsFalse(
         H5S.select_hyperslab(Utilities.RandomInvalidHandle(),
                              H5S.seloper_t.SET, (ulong[])null, null, null, null) >= 0);
 }
        /// <summary>
        /// Appends a dataset to a hdf5 file. If called the first time a dataset is created
        /// </summary>
        /// <typeparam name="T">Generic parameter only primitive types are allowed</typeparam>
        /// <param name="groupId">id of the group. Can also be a file Id</param>
        /// <param name="name">name of the dataset</param>
        /// <param name="dset">The dataset</param>
        /// <returns>status of the write method</returns>
        public static hid_t AppendDataset <T>(hid_t groupId, string name, Array dset, ulong chunkX = 200) where T : struct
        {
            var rank = dset.Rank;

            ulong[] dimsExtend = Enumerable.Range(0, rank).Select(i =>
                                                                  { return((ulong)dset.GetLength(i)); }).ToArray();
            ulong[] maxDimsExtend = null;
            ulong[] dimsChunk = new ulong[] { chunkX }.Concat(dimsExtend.Skip(1)).ToArray();
            ulong[] zeros = Enumerable.Range(0, rank).Select(z => (ulong)0).ToArray();
            hid_t   status, spaceId, datasetId;


            // name = ToHdf5Name(name);
            var datatype      = GetDatatype(typeof(T));
            var typeId        = H5T.copy(datatype);
            var datasetExists = H5L.exists(groupId, name) > 0;

            /* Create a new dataset within the file using chunk
             * creation properties.  */
            if (!datasetExists)
            {
                spaceId = H5S.create_simple(dset.Rank, dimsExtend, maxDimsExtend);

                var propId = H5P.create(H5P.DATASET_CREATE);
                status    = H5P.set_chunk(propId, rank, dimsChunk);
                datasetId = H5D.create(groupId, name, datatype, spaceId,
                                       H5P.DEFAULT, propId, H5P.DEFAULT);
                /* Write data to dataset */
                GCHandle hnd = GCHandle.Alloc(dset, GCHandleType.Pinned);
                status = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                   hnd.AddrOfPinnedObject());
                hnd.Free();
                H5P.close(propId);
            }
            else
            {
                datasetId = H5D.open(groupId, name);
                spaceId   = H5D.get_space(datasetId);
                var     rank_old = H5S.get_simple_extent_ndims(spaceId);
                ulong[] maxDims  = new ulong[rank_old];
                ulong[] dims     = new ulong[rank_old];
                var     memId1   = H5S.get_simple_extent_dims(spaceId, dims, maxDims);

                ulong[] oldChunk  = null;
                int     chunkDims = 0;
                var     propId    = H5P.create(H5P.DATASET_ACCESS);
                status = H5P.get_chunk(propId, chunkDims, oldChunk);

                /* Extend the dataset. */
                var size = new ulong[] { dims[0] + dimsExtend[0] }.Concat(dims.Skip(1)).ToArray();
                status = H5D.set_extent(datasetId, size);

                /* Select a hyperslab in extended portion of dataset  */
                var filespaceId = H5D.get_space(datasetId);
                var offset = new ulong[] { dims[0] }.Concat(zeros.Skip(1)).ToArray();
                status = H5S.select_hyperslab(filespaceId, H5S.seloper_t.SET, offset, null,
                                              dimsExtend, null);

                /* Define memory space */
                var memId2 = H5S.create_simple(rank, dimsExtend, null);

                /* Write the data to the extended portion of dataset  */
                GCHandle hnd = GCHandle.Alloc(dset, GCHandleType.Pinned);
                status = H5D.write(datasetId, datatype, memId2, spaceId,
                                   H5P.DEFAULT, hnd.AddrOfPinnedObject());
                hnd.Free();
                H5S.close(memId1);
                H5S.close(memId2);
                H5D.close(filespaceId);
            }

            H5D.close(datasetId);
            H5S.close(spaceId);
            return(status);
        }