public static IEnumerable <T> ReadCompounds <T>(hid_t groupId, string name) where T : struct
        {
            Type  type   = typeof(T);
            hid_t typeId = 0;
            // open dataset
            var datasetId = H5D.open(groupId, name);

            typeId = CreateType(type);
            var compoundSize = Marshal.SizeOf(type);

            /*
             * Get dataspace and allocate memory for read buffer.
             */
            var spaceId = H5D.get_space(datasetId);
            int rank    = H5S.get_simple_extent_ndims(spaceId);

            ulong[] dims  = new ulong[rank];
            var     ndims = H5S.get_simple_extent_dims(spaceId, dims, null);
            int     rows  = Convert.ToInt32(dims[0]);

            byte[] bytes = new byte[rows * compoundSize];
            // Read the data.
            GCHandle hnd     = GCHandle.Alloc(bytes, GCHandleType.Pinned);
            IntPtr   hndAddr = hnd.AddrOfPinnedObject();

            H5D.read(datasetId, typeId, spaceId, H5S.ALL, H5P.DEFAULT, hndAddr);
            int             counter = 0;
            IEnumerable <T> strcts  = Enumerable.Range(1, rows).Select(i =>
            {
                byte[] select = new byte[compoundSize];
                Array.Copy(bytes, counter, select, 0, compoundSize);
                T s     = fromBytes <T>(select);
                counter = counter + compoundSize;
                return(s);
            });

            /*
             * Close and release resources.
             */
            H5D.vlen_reclaim(typeId, spaceId, H5P.DEFAULT, hndAddr);
            hnd.Free();
            H5D.close(datasetId);
            H5S.close(spaceId);
            H5T.close(typeId);

            return(strcts);
        }
Ejemplo n.º 2
0
        public void H5Dvlen_reclaimTest1()
        {
            // write a VLEN dataset

            hid_t vlen = H5T.vlen_create(H5T.NATIVE_INT);

            Assert.IsTrue(vlen >= 0);

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

            Assert.IsTrue(space >= 0);

            hid_t dset = H5D.create(m_v0_test_file, "vlen", vlen, space);

            Assert.IsTrue(space >= 0);
            hid_t dset1 = H5D.create(m_v2_test_file, "vlen", vlen, space);

            Assert.IsTrue(space >= 0);

            H5T.hvl_t[] wdata  = new H5T.hvl_t [dims[0]];
            GCHandle[]  whndl  = new GCHandle [wdata.Length];
            int[][]     jagged = new int[wdata.Length][];
            for (int i = 0; i < wdata.Length; ++i)
            {
                jagged[i]    = new int [i + 1];
                whndl[i]     = GCHandle.Alloc(jagged[i], GCHandleType.Pinned);
                wdata[i].len = new IntPtr(i + 1);
                wdata[i].p   = whndl[i].AddrOfPinnedObject();
            }

            GCHandle wdata_hndl = GCHandle.Alloc(wdata, GCHandleType.Pinned);

            Assert.IsTrue(H5D.write(dset, vlen, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                    wdata_hndl.AddrOfPinnedObject()) >= 0);
            Assert.IsTrue(H5D.write(dset1, vlen, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                    wdata_hndl.AddrOfPinnedObject()) >= 0);
            wdata_hndl.Free();

            for (int i = 0; i < wdata.Length; ++i)
            {
                whndl[i].Free();
            }

            // read it back

            H5T.hvl_t[] rdata      = new H5T.hvl_t[dims[0]];
            GCHandle    rdata_hndl = GCHandle.Alloc(rdata, GCHandleType.Pinned);

            Assert.IsTrue(H5D.read(dset, vlen, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                   rdata_hndl.AddrOfPinnedObject()) >= 0);

            for (int i = 0; i < rdata.Length; ++i)
            {
                Assert.IsTrue(rdata[i].len.ToInt32() == i + 1);
            }

            Assert.IsTrue(H5D.vlen_reclaim(vlen, space, H5P.DEFAULT,
                                           rdata_hndl.AddrOfPinnedObject()) >= 0);

            Assert.IsTrue(H5D.read(dset1, vlen, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                   rdata_hndl.AddrOfPinnedObject()) >= 0);

            for (int i = 0; i < rdata.Length; ++i)
            {
                Assert.IsTrue(rdata[i].len.ToInt32() == i + 1);
            }

            // reclaim the space
            Assert.IsTrue(H5D.vlen_reclaim(vlen, space, H5P.DEFAULT,
                                           rdata_hndl.AddrOfPinnedObject()) >= 0);

            rdata_hndl.Free();

            Assert.IsTrue(H5D.close(dset1) >= 0);
            Assert.IsTrue(H5D.close(dset) >= 0);
            Assert.IsTrue(H5T.close(vlen) >= 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }