Ejemplo n.º 1
0
        /// <summary>
        /// Writes the entire matrix table
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="matName"></param>
        /// <param name="rowIndex"></param>
        /// <returns></returns>
        public void SetMatrix <T>(string matName, T[,] data)
        {
            // check that matrix exists
            if (tables.ContainsKey(matName))
            {
                H5DataSetId matId;
                tables.TryGetValue(matName, out matId);

                H5DataTypeId  matDataId = H5D.getType(matId);
                H5DataSpaceId spaceId   = H5S.create_simple(2, Shape);

                long[] start    = { 0, 0 };
                long[] count    = { Shape[0], Shape[1] };
                var    h5matrix = new H5Array <T>(data);

                H5S.selectHyperslab(spaceId, H5S.SelectOperator.SET, start, count);
                H5DataSpaceId readSpaceId = H5S.create_simple(2, count);

                H5D.write(matId, matDataId, readSpaceId, spaceId, H5P.create(H5P.PropertyListClass.DATASET_XFER), h5matrix);
                H5S.close(spaceId);
                H5S.close(readSpaceId);
            }
            else
            {
                Console.WriteLine("table {0} not found in matrix file", matName);
            }
            return;
        }
Ejemplo n.º 2
0
        public void H5SdecodeTest1()
        {
            hsize_t[] dims  = { 1, 2, 3 };
            hid_t     space = H5S.create_simple(dims.Length, dims, dims);

            Assert.IsTrue(space > 0);

            size_t nalloc = new IntPtr();

            Assert.IsTrue(H5S.encode(space, null, ref nalloc) >= 0);

            byte[] buf = new byte [nalloc.ToInt32()];
            Assert.IsTrue(H5S.encode(space, buf, ref nalloc) >= 0);

            Assert.IsTrue(H5S.close(space) >= 0);

            space = H5S.decode(buf);
            Assert.IsTrue(space >= 0);

            Assert.IsTrue(H5S.get_simple_extent_ndims(space) == dims.Length);
            hsize_t[] tdims = new hsize_t[dims.Length];
            Assert.IsTrue(
                H5S.get_simple_extent_dims(space, tdims, null) == dims.Length);

            for (int i = 0; i < dims.Length; ++i)
            {
                Assert.IsTrue(tdims[i] == dims[i]);
            }

            Assert.IsTrue(H5S.close(space) >= 0);
        }
Ejemplo n.º 3
0
        public void AppendOrCreateDataset(Array dataset)
        {
            if (_chunkDims == null)
            {
                _chunkDims = new[]
                { Convert.ToUInt64(dataset.GetLongLength(0)), Convert.ToUInt64(dataset.GetLongLength(1)) };

                Rank         = dataset.Rank;
                _currentDims = GetDims(dataset);

                /* Create the data space with unlimited dimensions. */
                _spaceId = H5S.create_simple(Rank, _currentDims, _maxDims);

                /* Modify dataset creation properties, i.e. enable chunking  */
                _propId = H5P.create(H5P.DATASET_CREATE);
                _status = H5P.set_chunk(_propId, Rank, _chunkDims);

                /* Create a new dataset within the file using chunk creation properties.  */
                _datasetId = H5D.create(GroupId, Hdf5Utils.NormalizedName(Datasetname), _datatype, _spaceId, H5P.DEFAULT, _propId);

                /* Write data to dataset */
                GCHandle hnd = GCHandle.Alloc(dataset, GCHandleType.Pinned);
                _status = H5D.write(_datasetId, _datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                    hnd.AddrOfPinnedObject());
                hnd.Free();
                H5S.close(_spaceId);
                _spaceId = -1;
            }
            else
            {
                AppendDataset(dataset);
            }
        }
Ejemplo n.º 4
0
        public static unsafe void AddSomeLinks(long fileId)
        {
            long res;

            var groupId     = H5G.create(fileId, "simple");
            var groupId_sub = H5G.create(groupId, "sub");

            // datasets
            var dataspaceId1 = H5S.create_simple(1, new ulong[] { 1 }, new ulong[] { 1 });
            var datasetId1   = H5D.create(fileId, "D", H5T.NATIVE_INT8, dataspaceId1);
            var data1        = new byte[] { 1 };

            fixed(void *ptr = data1)
            {
                res = H5D.write(datasetId1, H5T.NATIVE_INT8, dataspaceId1, dataspaceId1, 0, new IntPtr(ptr));
            }

            res = H5D.close(datasetId1);
            res = H5S.close(dataspaceId1);

            var dataspaceId2 = H5S.create_simple(1, new ulong[] { 1 }, new ulong[] { 1 });
            var datasetId2   = H5D.create(groupId, "D1", H5T.NATIVE_INT8, dataspaceId2);

            res = H5D.close(datasetId2);
            res = H5S.close(dataspaceId2);

            var dataspaceId3 = H5S.create_simple(1, new ulong[] { 1 }, new ulong[] { 1 });
            var datasetId3   = H5D.create(groupId_sub, "D1.1", H5T.NATIVE_INT8, dataspaceId3);

            res = H5D.close(datasetId3);
            res = H5S.close(dataspaceId3);

            res = H5G.close(groupId);
            res = H5G.close(groupId_sub);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 写数据集属性
        /// </summary>
        public void WriteDatasetAttribute(string datasetName, string attrName, string value)
        {
            H5DataSetId   datasetId = H5D.open(_fileId, datasetName);
            H5DataTypeId  typeId    = H5T.copy(H5T.H5Type.C_S1);
            H5DataSpaceId spaceId   = H5S.create(H5S.H5SClass.SCALAR);

            H5T.setSize(typeId, value.Length);
            H5AttributeId attrId = H5A.create(datasetId, attrName, typeId, spaceId);

            if (value != "")
            {
                H5Array <byte> buffer = new H5Array <byte>(Encoding.Default.GetBytes(value));
                H5A.write(attrId, typeId, buffer);
            }

            if (typeId != null)
            {
                H5T.close(typeId);
            }
            if (spaceId != null)
            {
                H5S.close(spaceId);
            }
            if (attrId != null)
            {
                H5A.close(attrId);
            }
            if (datasetId != null)
            {
                H5D.close(datasetId);
            }
        }
Ejemplo n.º 6
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;
        }
Ejemplo n.º 7
0
        public void H5DfillTest1()
        {
            hsize_t[] dims  = { 10 };
            hid_t     space = H5S.create_simple(1, dims, null);

            Assert.IsTrue(H5S.select_all(space) >= 0);

            double[] v    = new double[10];
            double   fill = 1.0;

            GCHandle v_hnd    = GCHandle.Alloc(v, GCHandleType.Pinned);
            GCHandle fill_hnd = GCHandle.Alloc(fill, GCHandleType.Pinned);

            Assert.IsTrue(
                H5D.fill(fill_hnd.AddrOfPinnedObject(), H5T.NATIVE_DOUBLE,
                         v_hnd.AddrOfPinnedObject(), H5T.NATIVE_DOUBLE, space) >= 0);
            fill_hnd.Free();
            v_hnd.Free();

            for (int i = 0; i < v.Length; ++i)
            {
                Assert.IsTrue(v[i] == 1.0);
            }

            Assert.IsTrue(H5S.close(space) >= 0);
        }
        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);
        }
Ejemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static Hdf5Dataspace GetDataspace(Hdf5Identifier _datasetId)
        {
            var dataspaceId = H5D.get_space(_datasetId.Value).ToId();

            int rank = H5S.get_simple_extent_ndims(dataspaceId.Value);

            ulong[] dims    = new ulong[rank];
            ulong[] maxDims = new ulong[rank];
            H5S.get_simple_extent_dims(dataspaceId.Value, dims, maxDims);

            Hdf5Dataspace dataspace = new Hdf5Dataspace
            {
                Id = dataspaceId,
                NumberOfDimensions = rank
            };

            for (int i = 0; i < dims.Length; i++)
            {
                Hdf5DimensionProperty property = new Hdf5DimensionProperty
                {
                    CurrentSize = dims[i],
                    //MaximumSize = maxDims[i]
                };
                dataspace.DimensionProperties.Add(property);
            }

            H5S.close(dataspaceId.Value);

            return(dataspace);
        }
Ejemplo n.º 10
0
        public void H5Dcreate_anonTest2()
        {
            hsize_t[] dims     = { 10, 10, 10 };
            hsize_t[] max_dims = { H5S.UNLIMITED, H5S.UNLIMITED, H5S.UNLIMITED };
            hid_t     space    = H5S.create_simple(3, dims, max_dims);

            hid_t dcpl = H5P.create(H5P.DATASET_CREATE);

            Assert.IsTrue(dcpl >= 0);
            hsize_t[] chunk = { 64, 64, 64 };
            Assert.IsTrue(H5P.set_chunk(dcpl, 3, chunk) >= 0);
            Assert.IsTrue(H5P.set_deflate(dcpl, 9) >= 0);

            hid_t dset = H5D.create_anon(m_v0_test_file, H5T.IEEE_F32BE,
                                         space, dcpl);

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

            dset = H5D.create_anon(m_v2_test_file, H5T.IEEE_F32BE,
                                   space, dcpl);
            Assert.IsTrue(dset >= 0);
            Assert.IsTrue(H5D.close(dset) >= 0);

            Assert.IsTrue(H5P.close(dcpl) >= 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
Ejemplo n.º 11
0
        public void H5DiterateTest1()
        {
            int[] buf = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            IntPtr count_ptr = Marshal.AllocHGlobal(sizeof(int));

            Marshal.WriteInt32(count_ptr, 0);

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

            Assert.IsTrue(space >= 0);
            Assert.IsTrue(H5S.select_all(space) >= 0);

            GCHandle buf_hnd = GCHandle.Alloc(buf, GCHandleType.Pinned);

            H5D.operator_t cb = DelegateMethod;
            Assert.IsTrue(
                H5D.iterate(buf_hnd.AddrOfPinnedObject(), H5T.NATIVE_INT,
                            space, cb, count_ptr) >= 0);

            buf_hnd.Free();

            int count = Marshal.ReadInt32(count_ptr);

            // expect the sum of the buffer elements
            Assert.IsTrue(count == 45);

            Assert.IsTrue(H5S.close(space) >= 0);
            Marshal.FreeHGlobal(count_ptr);
        }
Ejemplo n.º 12
0
        // information: https://www.hdfgroup.org/ftp/HDF5/examples/examples-by-api/hdf5-examples/1_8/C/H5T/h5ex_t_cmpd.c
        //or: https://www.hdfgroup.org/HDF5/doc/UG/HDF5_Users_Guide-Responsive%20HTML5/index.html#t=HDF5_Users_Guide%2FDatatypes%2FHDF5_Datatypes.htm%3Frhtocid%3Dtoc6.5%23TOC_6_8_Complex_Combinationsbc-22

        public static int WriteCompounds <T>(hid_t groupId, string name, IEnumerable <T> list) //where T : struct
        {
            Type type = typeof(T);
            var  size = Marshal.SizeOf(type);
            var  cnt  = list.Count();

            var typeId = create_type(type);

            var   log10 = (int)Math.Log10(cnt);
            ulong pow   = (ulong)Math.Pow(10, log10);
            ulong c_s   = Math.Min(1000, pow);

            ulong[] chunk_size = new ulong[] { c_s };

            ulong[] dims = new ulong[] { (ulong)cnt };

            long dcpl = 0;

            if (list.Count() == 0 || log10 == 0)
            {
            }
            else
            {
                dcpl = create_property(chunk_size);
            }

            // Create dataspace.  Setting maximum size to NULL sets the maximum
            // size to be the current size.
            var spaceId = H5S.create_simple(dims.Length, dims, null);

            // Create the dataset and write the compound data to it.
            var datasetId = H5D.create(groupId, name, typeId, spaceId, H5P.DEFAULT, dcpl);

            IntPtr p = Marshal.AllocHGlobal(size * (int)dims[0]);

            var          ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            foreach (var strct in list)
            {
                writer.Write(getBytes(strct));
            }
            var bytes = ms.ToArray();

            GCHandle hnd      = GCHandle.Alloc(bytes, GCHandleType.Pinned);
            var      statusId = H5D.write(datasetId, typeId, spaceId, H5S.ALL,
                                          H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();

            /*
             * Close and release resources.
             */
            H5D.close(datasetId);
            H5S.close(spaceId);
            H5T.close(typeId);
            H5P.close(dcpl);
            Marshal.FreeHGlobal(p);
            return(statusId);
        }
Ejemplo n.º 13
0
        public void H5Dget_storage_sizeTest1()
        {
            hsize_t[] dims  = { 1024, 2048 };
            hid_t     space = H5S.create_simple(2, dims, null);

            hid_t dcpl = H5P.create(H5P.DATASET_CREATE);

            Assert.IsTrue(dcpl >= 0);
            Assert.IsTrue(
                H5P.set_alloc_time(dcpl, H5D.alloc_time_t.EARLY) >= 0);

            hid_t dset = H5D.create(m_v0_test_file, "dset", H5T.STD_I16LE,
                                    space, H5P.DEFAULT, dcpl);

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

            dset = H5D.create(m_v2_test_file, "dset", H5T.STD_I16LE,
                              space, H5P.DEFAULT, dcpl);
            Assert.IsTrue(dset >= 0);
            Assert.IsTrue(H5D.get_storage_size(dset) == 4194304);
            Assert.IsTrue(H5D.close(dset) >= 0);

            Assert.IsTrue(H5P.close(dcpl) >= 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
Ejemplo n.º 14
0
        private bool setOMXFileAttributes()
        {
            // write OMX attributes
            H5DataSpaceId dspace;
            H5DataTypeId  dtype;
            H5AttributeId attr;

            // OMX version
            dspace = H5S.create(H5S.H5SClass.SCALAR);
            dtype  = H5T.copy(H5T.H5Type.C_S1); // string datatype
            H5T.setSize(dtype, dllVersion[0].Length);
            attr = H5A.create(fileId, omxVersionName, dtype, dspace);
            ASCIIEncoding ascii = new ASCIIEncoding();

            H5A.write(attr, dtype, new H5Array <System.Byte>(ascii.GetBytes(dllVersion[0])));
            H5A.close(attr);

            // OMX shape - only 2D tables
            dspace = H5S.create_simple(1, new long[] { 2 });
            dtype  = H5T.copy(H5T.H5Type.NATIVE_INT);
            attr   = H5A.create(fileId, omxShapeAttr, dtype, dspace);
            int[] shape = new int[2];
            shape[0] = (int)Shape[0];
            shape[1] = (int)Shape[1];
            H5A.write <int>(attr, dtype, new H5Array <int>(shape));
            H5S.close(dspace);
            H5A.close(attr);

            return(true);
        }
Ejemplo n.º 15
0
        private void Write(H5GroupId parent, string name, IEnumerable <IMeasurement> measurements)
        {
            H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] {
                (long)measurements.Count()
            });


            // Set compression options for dataset
            H5PropertyListId dataSetPropertyList = H5P.create(H5P.PropertyListClass.DATASET_CREATE);

            H5P.setDeflate(dataSetPropertyList, NumericDataCompression);
            H5P.setChunk(dataSetPropertyList, new long[] { (long)measurements.Count() });

            H5DataSetId dataSetId = H5D.create(parent,
                                               name,
                                               measurement_t,
                                               spaceId,
                                               new H5PropertyListId(H5P.Template.DEFAULT),
                                               dataSetPropertyList,
                                               new H5PropertyListId(H5P.Template.DEFAULT));

            MeasurementT[] ms       = new MeasurementT[measurements.Count()];
            int            ilmCount = 0;

            foreach (IMeasurement m in measurements)
            {
                MeasurementT mt = Convert(m);
                ms[ilmCount++] = mt;
            }

            H5D.write <MeasurementT>(dataSetId, measurement_t, new H5Array <MeasurementT>(ms));

            H5D.close(dataSetId);
            H5S.close(spaceId);
        }
        public static int WritePrimitiveAttribute <T>(hid_t groupId, string name, Array attributes, string datasetName = null) //where T : struct
        {
            var tmpId = groupId;

            if (!string.IsNullOrWhiteSpace(datasetName))
            {
                var datasetId = H5D.open(groupId, datasetName);
                if (datasetId > 0)
                {
                    groupId = datasetId;
                }
            }
            int rank = attributes.Rank;

            ulong[] dims = Enumerable.Range(0, rank).Select(i =>
                                                            { return((ulong)attributes.GetLength(i)); }).ToArray();
            ulong[]  maxDims     = null;
            var      spaceId     = H5S.create_simple(rank, dims, maxDims);
            var      datatype    = GetDatatype(typeof(T));
            var      typeId      = H5T.copy(datatype);
            var      attributeId = H5A.create(groupId, name, datatype, spaceId);
            GCHandle hnd         = GCHandle.Alloc(attributes, GCHandleType.Pinned);
            var      result      = H5A.write(attributeId, datatype, hnd.AddrOfPinnedObject());

            hnd.Free();

            H5A.close(attributeId);
            H5S.close(spaceId);
            H5T.close(typeId);
            if (tmpId != groupId)
            {
                H5D.close(groupId);
            }
            return(result);
        }
Ejemplo n.º 17
0
        public void H5SencodeTest2()
        {
            size_t nalloc = IntPtr.Zero;

            Assert.IsFalse(H5S.encode(Utilities.RandomInvalidHandle(),
                                      null, ref nalloc) >= 0);
        }
Ejemplo n.º 18
0
        public static double[,] ReadFieldData2D(string file, string dataSet)
        {
            H5FileId     fileId      = H5F.open(file, H5F.OpenMode.ACC_RDONLY);
            H5DataSetId  fDataSetId  = H5D.open(fileId, dataSet);
            H5DataTypeId fDataTypeId = H5D.getType(fDataSetId);

            long[] dims = H5S.getSimpleExtentDims(H5D.getSpace(fDataSetId)).ToArray();
            double[,] data = new double[dims[0], dims[1]];
            H5D.read(fDataSetId, fDataTypeId, new H5Array <double>(data));

            double[,] fieldValues = new double[dims[1], dims[0]];
            for (int i = 0; i < dims[1]; i++)
            {
                for (int j = 0; j < dims[0]; j++)
                {
                    fieldValues[i, j] = (double)data[j, i];
                }
            }

            H5T.close(fDataTypeId);
            H5D.close(fDataSetId);
            H5F.close(fileId);

            return(fieldValues);
        }
Ejemplo n.º 19
0
        //Load weights from hdf5 file. Weights must be saved as a vector per layer
        public static float[] loadH5(string path, string dsname)
        {
            //Get file id
            var h5fid = H5F.open(path, H5F.OpenMode.ACC_RDONLY);
            //Get dataset id
            var h5did = H5D.open(h5fid, dsname);
            //Dataset size
            var h5space = H5D.getSpace(h5did);
            var h5size  = H5S.getSimpleExtentDims(h5space);

            //Dataset size to array
            var S = h5size.ToArray();

            //Empty double array for the data
            double[] data = new double[S[0]];

            //Read the dataset

            var h5array = new H5Array <double>(data);
            var h5dtype = H5D.getType(h5did);

            H5D.read(h5did, h5dtype, h5array);

            //Convert to float
            float[] newarray = new float[data.Length];

            Parallel.For(0, data.Length, (k) =>
            {
                newarray[k] = (float)data[k];
            });

            return(newarray);
        }
Ejemplo n.º 20
0
        public static double ReadAttribute(string file, string dataSetOrGroup, string attribute)
        {
            double attr = Double.NaN;

            try
            {
                H5FileId      fileId     = H5F.open(file, H5F.OpenMode.ACC_RDONLY);
                H5ObjectInfo  objectInfo = H5O.getInfoByName(fileId, dataSetOrGroup);
                H5GroupId     groupId    = null;
                H5DataSetId   dataSetId  = null;
                H5AttributeId attrId;

                if (objectInfo.objectType == H5ObjectType.GROUP)
                {
                    groupId = H5G.open(fileId, dataSetOrGroup);
                    attrId  = H5A.open(groupId, attribute);
                }
                else
                {
                    dataSetId = H5D.open(fileId, dataSetOrGroup);
                    attrId    = H5A.open(dataSetId, attribute);
                }
                H5DataTypeId attrTypeId = H5A.getType(attrId);

                double[] dAttrs = new double[] { };
                if (H5T.equal(attrTypeId, H5T.copy(H5T.H5Type.NATIVE_FLOAT)))
                {
                    float[] fAttrs = new float[H5S.getSimpleExtentNPoints(H5A.getSpace(attrId))];
                    H5A.read(attrId, attrTypeId, new H5Array <float>(fAttrs));
                    dAttrs = (from f in fAttrs select(double) f).ToArray();
                }
                else if (H5T.equal(attrTypeId, H5T.copy(H5T.H5Type.NATIVE_DOUBLE)))
                {
                    dAttrs = new double[H5S.getSimpleExtentNPoints(H5A.getSpace(attrId))];
                    H5A.read(attrId, attrTypeId, new H5Array <double>(dAttrs));
                }

                H5T.close(attrTypeId);
                H5A.close(attrId);
                if (groupId != null)
                {
                    H5G.close(groupId);
                }
                if (dataSetId != null)
                {
                    H5D.close(dataSetId);
                }
                H5F.close(fileId);

                return((double)dAttrs[0]);
            }

            catch (HDFException e)
            {
                Console.WriteLine("Error: Unhandled HDF5 exception");
                Console.WriteLine(e.Message);
            }

            return(attr);
        }
Ejemplo n.º 21
0
        public void FirstDataset(Array dataset)
        {
            if (FalseGroupId)
            {
                throw new Exception("cannot call FirstDataset because group or file couldn't be created");
            }
            if (DatasetExists)
            {
                throw new Exception("cannot call FirstDataset because dataset already exists");
            }

            Rank        = dataset.Rank;
            currentDims = GetDims(dataset);

            /* Create the data space with unlimited dimensions. */
            spaceId = H5S.create_simple(Rank, currentDims, maxDims);

            /* Modify dataset creation properties, i.e. enable chunking  */
            propId = H5P.create(H5P.DATASET_CREATE);
            status = H5P.set_chunk(propId, Rank, chunkDims);

            /* Create a new dataset within the file using chunk creation properties.  */
            datasetId = H5D.create(GroupId, Datasetname, datatype, spaceId, H5P.DEFAULT, propId, H5P.DEFAULT);

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

            status = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                               hnd.AddrOfPinnedObject());
            hnd.Free();
            H5S.close(spaceId);
        }
Ejemplo n.º 22
0
        public static double[, ,] ReadFieldData3D(string fileName)
        {
            H5FileId     fileId      = H5F.open(fileName, H5F.OpenMode.ACC_RDONLY);
            H5DataSetId  fDataSetId  = H5D.open(fileId, "/FieldData/FD/f0");
            H5DataTypeId fDataTypeId = H5D.getType(fDataSetId);

            if (!H5T.equal(fDataTypeId, H5T.copy(H5T.H5Type.NATIVE_FLOAT)))
            {
                Console.WriteLine("Error: Invalid dataset type, expected {0}", H5T.H5Type.NATIVE_FLOAT);
            }

            long[] dims = H5S.getSimpleExtentDims(H5D.getSpace(fDataSetId)).ToArray();
            if (dims.Length != 3)
            {
                Console.WriteLine("Error: Invalid field data dimensions");
            }

            float[, ,] data = new float[dims[0], dims[1], dims[2]];
            H5D.read(fDataSetId, fDataTypeId, new H5Array <float>(data));

            // Reorder
            double[, ,] fieldValues = new double[dims[2], dims[1], dims[0]];
            for (int i = 0; i < dims[0]; i++)
            {
                for (int j = 0; j < dims[1]; j++)
                {
                    for (int k = 0; k < dims[2]; k++)
                    {
                        fieldValues[k, j, i] = data[i, j, k];
                    }
                }
            }

            return(fieldValues);
        }
Ejemplo n.º 23
0
        public static unsafe void AddDataspaceNull(long fileId, ContainerType container)
        {
            var spaceId = H5S.create(H5S.class_t.NULL);

            TestUtils.Add(container, fileId, "dataspace", "null", H5T.NATIVE_DOUBLE, null, spaceId);
            H5S.close(spaceId);
        }
        public static int WriteDatasetFromArray <T>(hid_t groupId, string name, Array dset, string datasetName = null) //where T : struct
        {
            int rank = dset.Rank;

            ulong[] dims = Enumerable.Range(0, rank).Select(i => { return((ulong)dset.GetLength(i)); }).ToArray();

            ulong[] maxDims  = null;
            var     spaceId  = H5S.create_simple(rank, dims, maxDims);
            var     datatype = GetDatatype(typeof(T));
            var     typeId   = H5T.copy(datatype);

            if (datatype == H5T.C_S1)
            {
                H5T.set_size(datatype, new IntPtr(2));
            }
            var      datasetId = H5D.create(groupId, name, datatype, spaceId);
            GCHandle hnd       = GCHandle.Alloc(dset, GCHandleType.Pinned);
            var      result    = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                           hnd.AddrOfPinnedObject());

            hnd.Free();
            H5D.close(datasetId);
            H5S.close(spaceId);
            H5T.close(typeId);
            return(result);
        }
Ejemplo n.º 25
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));
                }
            });
        }
Ejemplo n.º 26
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..];
Ejemplo n.º 27
0
    private static Hdf5Container_LidarDaimler ReadContainer(string sFilePath_inp)
    {
        int status = 0;

        long file_id          = H5F.open(sFilePath_inp, H5F.ACC_RDWR);
        long testDataset_id   = H5D.open(file_id, "distance");
        long testDataspace_id = H5D.get_space(testDataset_id);

        ulong[] dims = new ulong[2];
        status = H5S.get_simple_extent_dims(testDataspace_id, dims, null);

        int rows = Convert.ToInt32(dims[0]);
        int cols = Convert.ToInt32(dims[1]);

        Hdf5Container_LidarDaimler outContainer = new Hdf5Container_LidarDaimler(rows, cols)
        {
            _distances          = Hdf5IO.GetFloatDataset(H5D.open(file_id, "distance"), rows, cols),
            _intensity          = Hdf5IO.GetFloatDataset(H5D.open(file_id, "intensity"), rows, cols),
            _labelProbabilities = Hdf5IO.GetFloatDataset(H5D.open(file_id, "labelProbabilities"), rows, cols),
            _labelWorkingSet    = Hdf5IO.GetLabelWorkingSet(H5G.open(file_id, "labelWorkingSet")),
            _labels             = Hdf5IO.GetUintDataset(H5D.open(file_id, "labels"), rows, cols),
            _pointValid         = Hdf5IO.GetIntDataset(H5D.open(file_id, "pointValid"), rows, cols),
            _sensorX            = Hdf5IO.GetFloatDataset(H5D.open(file_id, "sensorX"), rows, cols),
            _sensorY            = Hdf5IO.GetFloatDataset(H5D.open(file_id, "sensorY"), rows, cols),
            _sensorZ            = Hdf5IO.GetFloatDataset(H5D.open(file_id, "sensorZ"), rows, cols),
            _vehicleX           = Hdf5IO.GetFloatDataset(H5D.open(file_id, "vehicleX"), rows, cols),
            _vehicleY           = Hdf5IO.GetFloatDataset(H5D.open(file_id, "vehicleY"), rows, cols),
            _vehicleZ           = Hdf5IO.GetFloatDataset(H5D.open(file_id, "vehicleZ"), rows, cols)
        };

        status = H5F.close(file_id);

        return(outContainer);
    }
Ejemplo n.º 28
0
        private static void WriteAttribute(H5ObjectWithAttributes target, string name, string value)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Name must not be empty (or null)", "name");
            }

            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentException("Value must not be empty or null", "value");
            }

            H5DataTypeId dtype;

            byte[] strdata = EncodeStringData(value, out dtype);

            H5DataSpaceId spaceId     = H5S.create(H5S.H5SClass.SCALAR);
            H5AttributeId attributeId = H5A.create(target, name, dtype, spaceId);

            H5A.write(attributeId, dtype, new H5Array <byte>(strdata));

            H5A.close(attributeId);
            H5T.close(dtype);
            H5S.close(spaceId);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Dispose function as suggested in the stackoverflow discussion below
        /// See: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238
        /// </summary>
        /// <param name="itIsSafeToAlsoFreeManagedObjects"></param>
        protected virtual void Dispose(bool itIsSafeToAlsoFreeManagedObjects)
        {
            if (!Hdf5Utils.GetRealName(GroupId, Datasetname, string.Empty).valid)
            {
                Hdf5Utils.LogInfo?.Invoke("Dataset does not exist.");
                return;
            }

            if (_datasetId >= 0)
            {
                H5D.close(_datasetId);
            }

            if (_propId >= 0)
            {
                H5P.close(_propId);
            }

            if (_spaceId >= 0)
            {
                H5S.close(_spaceId);
            }

            if (itIsSafeToAlsoFreeManagedObjects)
            {
            }
        }
Ejemplo n.º 30
0
        public void H5Dget_spaceTest1()
        {
            hsize_t[] dims  = { 1024, 2048 };
            hid_t     space = H5S.create_simple(3, dims, null);

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

            Assert.IsTrue(dset >= 0);
            hid_t space1 = H5D.get_space(dset);

            Assert.IsTrue(space1 >= 0);
            Assert.IsTrue(H5S.extent_equal(space, space1) > 0);
            Assert.IsTrue(H5S.close(space1) >= 0);
            Assert.IsTrue(H5D.close(dset) >= 0);

            dset = H5D.create(m_v2_test_file, "dset", H5T.STD_I16LE,
                              space);
            Assert.IsTrue(dset >= 0);
            space1 = H5D.get_space(dset);
            Assert.IsTrue(space1 >= 0);
            Assert.IsTrue(H5S.extent_equal(space, space1) > 0);
            Assert.IsTrue(H5S.close(space1) >= 0);
            Assert.IsTrue(H5D.close(dset) >= 0);

            Assert.IsTrue(H5S.close(space) >= 0);
        }