Exemplo n.º 1
0
        /// <summary>
        /// Add an attribute and write the values to the file
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_name"></param>
        /// <param name="_value"></param>
        /// <returns></returns>
        public Hdf5Attribute Add <T>(string _name, T _value)
        {
            if (Exists(_name))
            {
                throw new Hdf5AttributeAlreadyExistException();
            }

            //If the parent is the file, then just create the attribute (file already open).
            if (ParentObject.Id.Equals(ParentObject.FileId))
            {
                return(AttributeHelper.CreateAttributeAddToList(ParentObject.Id, this, _name, _value));
            }

            Hdf5Attribute attribute = null;

            //Otherwise open the object and then call the function.
            var id = H5O.open(ParentObject.FileId.Value, ParentObject.Path.FullPath).ToId();

            if (id.Value > 0)
            {
                attribute = AttributeHelper.CreateAttributeAddToList(id, this, _name, _value);
                H5O.close(id.Value);
            }

            return(attribute);
        }
Exemplo n.º 2
0
        public static object GetObject(Hdf5Identifier _fileId, AbstractHdf5Object _parent, string _objectName)
        {
            Hdf5Path combinedPath = _parent.Path.Append(_objectName);
            object   output       = null;

            if (combinedPath != null)
            {
                string fullPath = combinedPath.FullPath;

                H5O.info_t gInfo = new H5O.info_t();
                H5O.get_info_by_name(_fileId.Value, fullPath, ref gInfo);

                var id = H5O.open(_fileId.Value, fullPath).ToId();
                if (id.Value > 0)
                {
                    if (gInfo.type == H5O.type_t.DATASET)
                    {
                        output = DatasetHelper.LoadDataset(_fileId, id, fullPath);
                    }

                    if (gInfo.type == H5O.type_t.GROUP)
                    {
                        Hdf5Group group = new Hdf5Group(_fileId, id, fullPath);
                        group.FileId = _fileId;
                        group.LoadChildObjects();
                        output = group;
                    }

                    H5O.close(id.Value);
                }
            }

            return(output);
        }
Exemplo n.º 3
0
        public static void Write1DArray <T>(Hdf5Dataset _dataset, T[] _array)
        {
            if (_dataset.Dataspace.NumberOfDimensions != 1)
            {
                throw new Hdf5ArrayDimensionsMismatchException();
            }

            if ((ulong)_array.Length != _dataset.Dataspace.DimensionProperties[0].CurrentSize)
            {
                throw new Hdf5ArraySizeMismatchException();
            }

            var datasetId = H5O.open(_dataset.FileId.Value, _dataset.Path.FullPath).ToId();

            GCHandle arrayHandle = GCHandle.Alloc(_array, GCHandleType.Pinned);

            var typeId = H5T.copy(_dataset.DataType.NativeType.Value).ToId();

            int result = H5D.write(
                datasetId.Value,
                typeId.Value,
                H5S.ALL,
                H5S.ALL,
                H5P.DEFAULT,
                arrayHandle.AddrOfPinnedObject());

            arrayHandle.Free();

            H5T.close(typeId.Value);
            H5O.close(datasetId.Value);

            FileHelper.FlushToFile(_dataset.FileId);
        }
Exemplo n.º 4
0
        public static bool GroupExists(hid_t hid, string groupName)
        {
            if (string.IsNullOrEmpty(groupName))
            {
                throw new ArgumentException("groupName");
            }

            var exists = H5L.exists(hid, groupName);

            if (exists < 0)
            {
                throw new HDF5Exception("H5L.exists failed");
            }

            if (exists == 0)
            {
                return(false);
            }

            var objectId = H5O.open(hid, groupName);

            if (objectId < 0)
            {
                throw new HDF5Exception("H5O.open failed");
            }

            var result = GroupExists(objectId);

            H5O.close(objectId);

            return(result);
        }
Exemplo n.º 5
0
        public void H5OcloseTest4()
        {
            hid_t space = H5S.create(H5S.class_t.SCALAR);

            Assert.IsTrue(space >= 0);
            Assert.IsTrue(H5O.close(space) < 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
Exemplo n.º 6
0
        public void H5OcloseTest1()
        {
            hid_t gid = H5G.create(m_v0_test_file, "A");

            Assert.IsTrue(gid >= 0);
            Assert.IsTrue(H5O.close(gid) >= 0);

            gid = H5G.create(m_v2_test_file, "A");
            Assert.IsTrue(gid >= 0);
            Assert.IsTrue(H5O.close(gid) >= 0);
        }
Exemplo n.º 7
0
        public void H5OopenTest1()
        {
            Assert.IsTrue(
                H5G.close(H5G.create(m_v0_test_file, "A/B/C", m_lcpl)) >= 0);
            hid_t obj = H5O.open(m_v0_test_file, "A/B");

            Assert.IsTrue(obj >= 0);
            Assert.IsTrue(H5O.close(obj) >= 0);

            Assert.IsTrue(
                H5G.close(H5G.create(m_v2_test_file, "A/B/C", m_lcpl)) >= 0);
            obj = H5O.open(m_v2_test_file, "A/B");
            Assert.IsTrue(obj >= 0);
            Assert.IsTrue(H5O.close(obj) >= 0);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Update the attribute and write the values to the file
        /// </summary>
        /// <param name="_attribute"></param>
        public void Update(Hdf5Attribute _attribute)
        {
            //If the parent is the file, then just update the attribute (file already open).
            if (ParentObject.Id.Equals(ParentObject.FileId))
            {
                AttributeHelper.UpdateAttribute(ParentObject.Id, _attribute);
            }

            //Otherwise open the object and then update the attribute.
            var id = H5O.open(ParentObject.FileId.Value, ParentObject.Path.FullPath).ToId();

            if (id.Value > 0)
            {
                AttributeHelper.UpdateAttribute(id, _attribute);
                H5O.close(id.Value);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Remove the attribute from the list and delete the attibute from the file
        /// </summary>
        /// <param name="_attribute"></param>
        public void Delete(Hdf5Attribute _attribute)
        {
            //If the parent is the file, then just delete the attribute (file already open).
            if (ParentObject.Id.Equals(ParentObject.FileId))
            {
                AttributeHelper.DeleteAttribute(ParentObject.Id, _attribute.Name);
            }
            else
            {
                //Otherwise open the object then delete the attibute
                var id = H5O.open(ParentObject.FileId.Value, ParentObject.Path.FullPath).ToId();
                if (id.Value > 0)
                {
                    AttributeHelper.DeleteAttribute(id, _attribute.Name);
                    H5O.close(id.Value);
                }
            }

            Remove(_attribute);
        }
Exemplo n.º 10
0
        public void H5Oopen_by_idxTest1()
        {
            Assert.IsTrue(
                H5G.close(H5G.create(m_v0_test_file, "A")) >= 0);
            Assert.IsTrue(
                H5G.close(H5G.create(m_v0_test_file, "AA")) >= 0);
            Assert.IsTrue(
                H5G.close(H5G.create(m_v0_test_file, "AAA")) >= 0);
            Assert.IsTrue(
                H5G.close(H5G.create(m_v0_test_file, "AAAA")) >= 0);

            hid_t obj = H5O.open_by_idx(m_v0_test_file, ".", H5.index_t.NAME,
                                        H5.iter_order_t.NATIVE, 0);

            Assert.IsTrue(obj >= 0);
            Assert.IsTrue(H5O.close(obj) >= 0);

            obj = H5O.open_by_idx(m_v0_test_file, ".", H5.index_t.NAME,
                                  H5.iter_order_t.NATIVE, 2);
            Assert.IsTrue(obj >= 0);
            Assert.IsTrue(H5O.close(obj) >= 0);

            Assert.IsTrue(
                H5G.close(H5G.create(m_v2_test_file, "A")) >= 0);
            Assert.IsTrue(
                H5G.close(H5G.create(m_v2_test_file, "AA")) >= 0);
            Assert.IsTrue(
                H5G.close(H5G.create(m_v2_test_file, "AAA")) >= 0);
            Assert.IsTrue(
                H5G.close(H5G.create(m_v2_test_file, "AAAA")) >= 0);

            obj = H5O.open_by_idx(m_v2_test_file, ".", H5.index_t.NAME,
                                  H5.iter_order_t.NATIVE, 0);
            Assert.IsTrue(obj >= 0);
            Assert.IsTrue(H5O.close(obj) >= 0);

            obj = H5O.open_by_idx(m_v2_test_file, ".", H5.index_t.NAME,
                                  H5.iter_order_t.NATIVE, 2);
            Assert.IsTrue(obj >= 0);
            Assert.IsTrue(H5O.close(obj) >= 0);
        }
Exemplo n.º 11
0
        public static Array GetData(Hdf5Dataset _dataset)
        {
            var id = H5O.open(_dataset.FileId.Value, _dataset.Path.FullPath).ToId();

            if (id.Value > 0)
            {
                Array a = null;

                if (_dataset.Dataspace.NumberOfDimensions == 1)
                {
                    a = SingleDimension(id, _dataset);
                }
                else if (_dataset.Dataspace.NumberOfDimensions == 2)
                {
                    a = TwoDimension(id, _dataset);
                }

                H5O.close(id.Value);

                return(a);
            }

            return(null);
        }
Exemplo n.º 12
0
        private void GetGroupDatasetNames(string groupName)
        {
            ulong         pos        = 0;
            List <string> groupNames = new List <string>();
            Int32         groupId    = H5G.open(_h5FileId, groupName);

            if (groupId > 0)
            {
                ArrayList al      = new ArrayList();
                GCHandle  hnd     = GCHandle.Alloc(al);
                IntPtr    op_data = (IntPtr)hnd;
                H5L.iterate(groupId, H5.index_t.NAME, H5.iter_order_t.NATIVE, ref pos,
                            delegate(int _objectId, IntPtr _namePtr, ref H5L.info_t _info, IntPtr _data)
                {
                    string objectName = Marshal.PtrToStringAnsi(_namePtr);
                    groupNames.Add(objectName);
                    return(0);
                }, op_data);
                hnd.Free();
                H5G.close(groupId);

                foreach (var itemName in groupNames)
                {
                    //判断是不是数据集
                    string curPath = string.Empty;
                    if (groupName == "/")
                    {
                        curPath = itemName;
                    }
                    else
                    {
                        curPath = groupName + itemName;
                    }
                    H5O.info_t gInfo = new H5O.info_t();
                    H5O.get_info_by_name(_h5FileId, curPath, ref gInfo);
                    var objId = H5O.open(_h5FileId, curPath);
                    if (objId > 0)
                    {
                        if (gInfo.type == H5O.type_t.DATASET)
                        {
                            _datasetNames.Add(curPath);
                        }
                        if (gInfo.type == H5O.type_t.GROUP)
                        {
                            GetGroupDatasetNames(curPath + "/");
                        }
                        H5O.close(objId);
                    }
                }
            }



            //    H5GroupId h5GroupId = H5G.open(_h5FileId, groupName);
            //try
            //{

            //    long dscount = H5G.getNumObjects(h5GroupId);
            //    for (int i = 0; i < dscount; i++)
            //    {
            //        string objname = H5G.getObjectNameByIndex(h5GroupId, (ulong)i);
            //        ObjectInfo objInfo = H5G.getObjectInfo(h5GroupId, objname, false);
            //        switch (objInfo.objectType)
            //        {
            //            case H5GType.DATASET:
            //                if (objInfo.objectType == H5GType.DATASET)
            //                {
            //                    if (groupName == "/")
            //                        _datasetNames.Add(objname);
            //                    else
            //                        _datasetNames.Add(groupName + objname);
            //                }
            //                break;
            //            case H5GType.GROUP:
            //                if (groupName == "/")
            //                    GetGroupDatasetNames(objname + "/");
            //                else
            //                    GetGroupDatasetNames(groupName + objname + "/");
            //                break;
            //            case H5GType.LINK:
            //                break;
            //            case H5GType.TYPE:
            //                break;
            //            default:
            //                break;
            //        }
            //    }
            //}
            //finally
            //{
            //    if (h5GroupId != null)
            //        H5G.close(h5GroupId);
            //}
        }
Exemplo n.º 13
0
        public void H5OcloseTest3()
        {
            hid_t gid = Utilities.RandomInvalidHandle();

            Assert.IsTrue(H5O.close(gid) < 0);
        }