示例#1
0
        public void H5Aopen_by_idxTest1()
        {
            hid_t att = H5A.create(m_v2_test_file, "A", H5T.IEEE_F64LE,
                                   m_space_scalar);

            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);
            att = H5A.create(m_v2_test_file, "AA", H5T.IEEE_F64LE,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

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

            att = H5A.open_by_idx(m_v2_test_file, ".", H5.index_t.NAME,
                                  H5.iter_order_t.NATIVE, 1);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            att = H5A.create(m_v0_test_file, "A", H5T.IEEE_F64LE,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            att = H5A.open_by_idx(m_v0_test_file, ".", H5.index_t.NAME,
                                  H5.iter_order_t.NATIVE, 0);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);
        }
示例#2
0
        public void H5Aget_info_by_idxTest1()
        {
            H5A.info_t info = new H5A.info_t();
            hid_t      att  = H5A.create(m_v2_test_file, "A", H5T.IEEE_F64LE,
                                         m_space_scalar);

            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);
            att = H5A.create(m_v2_test_file, "B", H5T.IEEE_F64LE,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            Assert.IsTrue(H5A.get_info_by_idx(m_v2_test_file, ".",
                                              H5.index_t.NAME, H5.iter_order_t.NATIVE, 0,
                                              ref info) >= 0);
            Assert.IsTrue(H5A.get_info_by_idx(m_v2_test_file, ".",
                                              H5.index_t.NAME, H5.iter_order_t.NATIVE, 1,
                                              ref info) >= 0);

            att = H5A.create(m_v0_test_file, "A", H5T.IEEE_F64LE,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);
            Assert.IsTrue(H5A.get_info_by_idx(m_v0_test_file, ".",
                                              H5.index_t.NAME, H5.iter_order_t.NATIVE, 0,
                                              ref info) >= 0);

            Assert.IsFalse(H5A.get_info_by_idx(m_v0_test_file, ".",
                                               H5.index_t.NAME, H5.iter_order_t.NATIVE, 1,
                                               ref info) >= 0);
        }
示例#3
0
        public void H5Aget_info_by_nameTest1()
        {
            H5A.info_t info = new H5A.info_t();
            hid_t      att  = H5A.create(m_v2_test_file, "A", H5T.IEEE_F64LE,
                                         m_space_scalar);

            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);
            att = H5A.create(m_v2_test_file, "B", H5T.IEEE_F64LE,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            Assert.IsTrue(H5A.get_info_by_name(m_v2_test_file, ".", "A",
                                               ref info) >= 0);
            Assert.IsTrue(H5A.get_info_by_name(m_v2_test_file, ".", "B",
                                               ref info) >= 0);

            att = H5A.create(m_v0_test_file, "A", H5T.IEEE_F64LE,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);
            Assert.IsTrue(H5A.get_info_by_name(m_v0_test_file, ".", "A",
                                               ref info) >= 0);

            Assert.IsFalse(H5A.get_info_by_name(m_v0_test_file, ".", "B",
                                                ref info) >= 0);
        }
示例#4
0
        public void H5AdeleteTest1()
        {
            hid_t att = H5A.create(m_v0_class_file, "DNA", H5T.IEEE_F32BE,
                                   m_space_null);

            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            att = H5A.create(m_v2_class_file, "DNA", H5T.IEEE_F32BE,
                             m_space_null);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            att = H5A.create(m_v0_class_file, "DSA", H5T.IEEE_F32BE,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            att = H5A.create(m_v2_class_file, "DSA", H5T.IEEE_F32BE,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            Assert.IsTrue(H5A.delete(m_v0_class_file, "DNA") >= 0);
            Assert.IsTrue(H5A.delete(m_v0_class_file, "DSA") >= 0);
            Assert.IsTrue(H5A.delete(m_v2_class_file, "DNA") >= 0);
            Assert.IsTrue(H5A.delete(m_v2_class_file, "DSA") >= 0);
        }
示例#5
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);
        }
示例#6
0
        public void H5AreadTest1()
        {
            double[] x   = { Math.PI };
            IntPtr   buf = Marshal.AllocHGlobal(8);
            hid_t    att = H5A.create(m_v2_test_file, "A", H5T.IEEE_F64LE,
                                      m_space_scalar);

            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.read(att, H5T.IEEE_F64BE, buf) >= 0);
            Assert.IsTrue(H5A.read(att, H5T.NATIVE_DOUBLE, buf) >= 0);
            Marshal.Copy(buf, x, 0, 1);
            Assert.IsTrue(x[0] == 0.0);
            Assert.IsTrue(H5A.close(att) >= 0);

            att = H5A.create(m_v0_test_file, "A", H5T.IEEE_F64LE,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.read(att, H5T.IEEE_F64BE, buf) >= 0);
            Assert.IsTrue(H5A.read(att, H5T.NATIVE_DOUBLE, buf) >= 0);
            Marshal.Copy(buf, x, 0, 1);
            Assert.IsTrue(x[0] == 0.0);
            Assert.IsTrue(H5A.close(att) >= 0);

            Marshal.FreeHGlobal(buf);
        }
示例#7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="_objectId"></param>
        /// <param name="_attribute"></param>
        public static void UpdateAttribute(Hdf5Identifier _objectId, Hdf5Attribute _attribute)
        {
            Hdf5Identifier attributeId = H5A.open(_objectId.Value, _attribute.Name).ToId();

            if (attributeId.Value > 0)
            {
                Hdf5DataType  type     = TypeHelper.GetDataTypeFromAttribute(attributeId);
                Hdf5DataTypes enumType = TypeHelper.GetDataTypesEnum(_attribute.Value.GetType());

                if (type.Type == enumType)
                {
                    if (enumType == Hdf5DataTypes.String)
                    {
                        H5A.close(attributeId.Value);
                        DeleteAttribute(_objectId, _attribute.Name);
                        Hdf5Attribute attribute = CreateAttribute(_objectId, _attribute.Name, _attribute.Value);

                        if (attribute != null)
                        {
                            _attribute.Id    = attribute.Id;
                            _attribute.Name  = attribute.Name;
                            _attribute.Value = attribute.Value;
                        }
                    }
                    else
                    {
                        WriteObjectValue(type, attributeId, _attribute.Value);
                    }
                }
                else
                {
                    throw new Hdf5TypeMismatchException();
                }
            }
        }
        public static T ReadEnumAttribute <T>(hid_t hid, string key) where T : struct, IConvertible
        {
            var attribute = H5A.open(hid, key);

            if (attribute < 0)
            {
                throw new ArgumentException(string.Format("Attribute {0} not found.", key));
            }

            var type = H5A.get_type(attribute);

            if (type < 0)
            {
                H5A.close(attribute);
                throw new Exception("H5A.get_type failed.");
            }

            var size = H5T.get_size(type).ToInt32();

            if (size == 0)
            {
                H5T.close(type);
                H5A.close(attribute);
                throw new Exception("H5T.get_size failed.");
            }

            var unmanagedBuffer = new UnmanagedBuffer(size);

            H5A.read(attribute, type, unmanagedBuffer);

            H5T.close(type);
            H5A.close(attribute);

            return(unmanagedBuffer.ReadEnum <T>());
        }
示例#9
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);
        }
示例#10
0
        private bool getOMXFileAttributes()
        {
            string version = "unknown";

            // OMX Version
            H5AttributeId verId = H5A.open(fileId, omxVersionName);

            H5T.H5TClass verCl = H5T.getClass(H5A.getType(verId));

            if (verCl.Equals(H5T.H5TClass.STRING))
            {
                Byte[]        buff = getAttribute <byte>(verId);
                ASCIIEncoding enc  = new ASCIIEncoding();
                version = enc.GetString(buff);
            }
            else if (verCl.Equals(H5T.H5TClass.FLOAT))
            {
                float[] buff = getAttribute <float>(verId);
                version = buff.ToString();
                // produces garbage - bail out from here...
                throw new NotImplementedException();
            }
            else
            {
                throw new NotImplementedException();
            }
            //Console.WriteLine("omx version: {0}", version);
            this.OmxVersion = version;
            H5A.close(verId);

            // matrix shape
            H5AttributeId shId = H5A.open(fileId, omxShapeAttr);

            H5T.H5TClass shCl = H5T.getClass(H5A.getType(shId));

            if (shCl.Equals(H5T.H5TClass.INTEGER))
            {
                int[] shape = getAttribute <int>(shId);

                this.Shape[0] = (long)shape[0];
                this.Shape[1] = (long)shape[1];
            }
            else if (shCl.Equals(H5T.H5TClass.FLOAT))
            {
                float[] shape = getAttribute <float>(shId);

                this.Shape[0] = (long)shape[0];
                this.Shape[1] = (long)shape[1];

                // returns garbage, bail out from here
                throw new NotImplementedException();
            }
            else
            {
                throw new NotImplementedException();
            }

            H5A.close(shId);
            return(true);
        }
示例#11
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);
        }
示例#12
0
        /// <summary>
        /// Reads the data from the specified attribute.
        /// </summary>
        /// <typeparam name="T">The data type.</typeparam>
        /// <param name="locationId">The location ID.</param>
        /// <param name="attributeName">The name of the attribute.</param>
        /// <returns>Returns a set of type T which represents the read data.</returns>
        public static T[] ReadAttribute <T>(long locationId, string attributeName)
        {
            long attributeId = -1;

            T[] result;

            try
            {
                attributeId = H5A.open(locationId, attributeName);

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

                result = IOHelper.Read <T>(attributeId, DataContainerType.Attribute);
            }
            finally
            {
                if (H5I.is_valid(attributeId) > 0)
                {
                    H5A.close(attributeId);
                }
            }

            return(result);
        }
        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);
        }
示例#14
0
        private static void WriteFile(string filePath)
        {
            var file = H5F.create(filePath, H5F.CreateMode.ACC_TRUNC);

            var group = H5G.create(file, "/group");

            H5G.close(group);

            const int RANK = 2;
            const int DIM0 = 3;
            const int DIM1 = 4;
            var       dims = new long[RANK] {
                DIM0, DIM1
            };
            var dataSpace = H5S.create_simple(RANK, dims);

            var dataSet = H5D.create(file, "/group/dataset", H5T.H5Type.NATIVE_INT, dataSpace);

            H5S.close(dataSpace);

            var data = new int[DIM0, DIM1]
            {
                { 1, 2, 3, 4 },
                { 5, 6, 7, 8 },
                { 9, 10, 11, 12 }
            };

            H5D.write(dataSet, new H5DataTypeId(H5T.H5Type.NATIVE_INT), new H5Array <int>(data));

            var dataType = new H5DataTypeId(H5T.H5Type.NATIVE_INT);

            dataSpace = H5S.create(H5S.H5SClass.SCALAR);
            var integerAttribute = H5A.create(dataSet, "int", dataType, dataSpace);

            H5A.write(integerAttribute, dataType, new H5Array <int>(new int[1] {
                42
            }));
            H5A.close(integerAttribute);
            H5S.close(dataSpace);
            //H5T.close(dataType); // Read-only.

            var str      = "Hello, world!";
            var strBytes = Encoding.ASCII.GetBytes(str);

            // There is a H5T.get_cset, but there does not seem to be a way of setting the character encoding, i.e. set_cset.
            dataType = H5T.copy(H5T.H5Type.C_S1);
            H5T.setSize(dataType, strBytes.Length);
            dataSpace = H5S.create(H5S.H5SClass.SCALAR);
            var stringAttribute = H5A.create(dataSet, "string", dataType, dataSpace);

            H5A.write(stringAttribute, dataType, new H5Array <byte>(strBytes));
            H5A.close(stringAttribute);
            H5S.close(dataSpace);
            H5T.close(dataType);

            H5D.close(dataSet);

            H5F.close(file);
        }
示例#15
0
        public static (int success, hid_t CreatedgroupId) WriteStringAttributes(hid_t groupId, string name, IEnumerable <string> strs, string datasetName = null)
        {
            hid_t tmpId = groupId;

            if (!string.IsNullOrWhiteSpace(datasetName))
            {
                hid_t datasetId = H5D.open(groupId, datasetName);
                if (datasetId > 0)
                {
                    groupId = datasetId;
                }
            }

            // create UTF-8 encoded attributes
            hid_t datatype = H5T.create(H5T.class_t.STRING, H5T.VARIABLE);

            H5T.set_cset(datatype, H5T.cset_t.UTF8);
            H5T.set_strpad(datatype, H5T.str_t.SPACEPAD);

            int   strSz   = strs.Count();
            hid_t spaceId = H5S.create_simple(1,
                                              new ulong[] { (ulong)strSz }, null);

            var attributeId = H5A.create(groupId, name, datatype, spaceId);

            GCHandle[] hnds  = new GCHandle[strSz];
            IntPtr[]   wdata = new IntPtr[strSz];

            int cntr = 0;

            foreach (string str in strs)
            {
                hnds[cntr] = GCHandle.Alloc(
                    Encoding.UTF8.GetBytes(str),
                    GCHandleType.Pinned);
                wdata[cntr] = hnds[cntr].AddrOfPinnedObject();
                cntr++;
            }

            var hnd = GCHandle.Alloc(wdata, GCHandleType.Pinned);

            var result = H5A.write(attributeId, datatype, hnd.AddrOfPinnedObject());

            hnd.Free();

            for (int i = 0; i < strSz; ++i)
            {
                hnds[i].Free();
            }

            H5A.close(attributeId);
            H5S.close(spaceId);
            H5T.close(datatype);
            if (tmpId != groupId)
            {
                H5D.close(groupId);
            }
            return(result, attributeId);
        }
示例#16
0
        public override void Dispose()
        {
            base.Dispose();

            if (ID > 0)
            {
                ID = H5A.close(ID);
            }
        }
示例#17
0
        public static bool WriteStringAttribute(hid_t hid, string key, string value, bool utf8 = true, bool variable = true)
        {
            if (H5A.exists(hid, key) == 0)
            {
                // Attribute doesn't exist.
                return(variable ? CreateOrOverwriteVariableStringAttribute(hid, key, new string[] { value }, utf8)
                    : CreateOrOverwriteFixedStringAttribute(hid, key, value, utf8));
            }
            else
            {
                var attribute = H5A.open(hid, key);
                if (attribute < 0)
                {
                    return(false);
                }

                var type = H5A.get_type(attribute);
                if (type < 0)
                {
                    H5A.close(attribute);
                    return(false);
                }

                var typeClass = H5T.get_class(type);
                if (typeClass == H5T.class_t.NO_CLASS)
                {
                    H5T.close(type);
                    H5T.close(attribute);
                    return(false);
                }

                if (typeClass != H5T.class_t.STRING)
                {
                    H5T.close(type);
                    H5T.close(attribute);
                    throw new ArgumentException("H5T.get_class(type) != H5T.class_t.STRING");
                }

                utf8 = H5T.get_cset(type) == H5T.cset_t.UTF8;

                bool ok;
                if (H5T.is_variable_str(type) > 0)
                {
                    ok = CreateOrOverwriteVariableStringAttribute(hid, key, new string[] { value }, utf8);
                }
                else
                {
                    ok = CreateOrOverwriteFixedStringAttribute(hid, key, value, utf8);
                }

                H5T.close(type);
                H5T.close(attribute);

                return(ok);
            }
        }
示例#18
0
        public static bool WriteScalarNumericAttribute <T>(hid_t hid, string key, T value, hid_t type) where T : struct
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            var exists = H5A.exists(hid, key);

            if (exists < 0)
            {
                throw new Exception("H5A.exists failed");
            }

            if (exists == 0)
            {
                var space = H5S.create(H5S.class_t.SCALAR);
                if (space < 0)
                {
                    throw new Exception("Failed to create scalar space");
                }

                var attribute = H5A.create(hid, key, type, space);
                if (attribute < 0)
                {
                    H5S.close(space);
                    throw new Exception(string.Format("Failed to create attribute \"{0}\"", key));
                }

                H5S.close(space);

                object boxedValue = value;
                H5A.write(attribute, type, new PinnedObject(boxedValue));

                H5A.close(attribute);

                return(true);
            }
            else
            {
                var attribute = H5A.open(hid, key);
                if (attribute < 0)
                {
                    throw new Exception(string.Format("Failed to open attribute \"{0}\"", key));
                }

                try
                {
                    return(WriteScalarNumericAttribute(attribute, value, type));
                }
                finally
                {
                    H5A.close(attribute);
                }
            }
        }
        public static bool WriteFixedStringAttribute(hid_t hid, string key, string value, bool utf8)
        {
            var exists = H5A.exists(hid, key);

            if (exists < 0)
            {
                throw new Exception("H5A.exists failed");
            }

            if (exists == 0) // Attribute doesn't exist
            {
                var bytes = value.ToBytes(utf8);

                var type = CreateFixedStringType(bytes, utf8);

                var space = H5S.create(H5S.class_t.SCALAR);
                if (space < 0)
                {
                    H5T.close(type);
                    throw new Exception("Failed to create scalar space");
                }

                var attribute = H5A.create(hid, key, type, space);
                if (attribute < 0)
                {
                    H5S.close(space);
                    H5T.close(type);
                    throw new Exception(string.Format("Failed to create attribute \"{0}\"", key));
                }

                H5A.write(attribute, type, new PinnedObject(bytes));

                H5A.close(attribute);
                H5S.close(space);
                H5T.close(type);

                return(true);
            }
            else
            {
                var attribute = H5A.open(hid, key);
                if (attribute < 0)
                {
                    throw new Exception(string.Format("Failed to open attribute \"{0}\"", key));
                }

                try
                {
                    return(WriteFixedStringAttribute(attribute, value, utf8));
                }
                finally
                {
                    H5A.close(attribute);
                }
            }
        }
示例#20
0
        public object this[string key]
        {
            get
            {
                return(Host.With <object>((objId) =>
                {
                    long attrId = 0;
                    long dtypeId = 0;
                    try
                    {
                        attrId = H5A.open(objId, key);
                        if (attrId < 0)
                        {
                            throw new ArgumentException($"Unknown Attribute: {key}", nameof(key));
                        }
                        dtypeId = H5A.get_type(attrId);
                        int size = H5T.get_size(dtypeId).ToInt32();

                        IntPtr iPtr = Marshal.AllocHGlobal(size);
                        int result = H5A.read(attrId, dtypeId, iPtr);
                        if (result < 0)
                        {
                            throw new IOException("Failed to read attribute");
                        }

                        if (H5T.equal(dtypeId, H5T.NATIVE_INT64) > 0)
                        {
                            var dest = new long[1];
                            Marshal.Copy(iPtr, dest, 0, 1);
                            Marshal.FreeHGlobal(iPtr);
                            return dest[0];
                        }
                        else // Must be a string...
                        {
                            var dest = new byte[size];
                            Marshal.Copy(iPtr, dest, 0, size);
                            Marshal.FreeHGlobal(iPtr);
                            return Encoding.ASCII.GetString(dest).TrimEnd((Char)0);
                        }

//                        return null;
                    }
                    finally
                    {
                        if (attrId > 0)
                        {
                            H5A.close(attrId);
                        }
                        if (dtypeId > 0)
                        {
                            H5T.close(dtypeId);
                        }
                    }
                }));
            }
        }
示例#21
0
        // Generate string attribute
        // GroupName: target group for the new attribute
        // AttName: attribute name
        // AttContent: content for the attribute, has to be a string
        public static void StringAttributeGenerator(H5GroupId GroupName, string AttName, string AttContent)
        {
            char[]        AttContentChar = AttContent.ToCharArray();
            byte[]        asciiStr       = ASCIIEncoding.ASCII.GetBytes(AttContentChar);
            int           length         = asciiStr.Length;
            H5AttributeId attributeId    = H5A.create(GroupName, AttName, H5T.create(H5T.CreateClass.STRING, length), H5S.create(H5S.H5SClass.SCALAR));

            H5A.write(attributeId, H5T.create(H5T.CreateClass.STRING, length), new H5Array <byte>(asciiStr));
            H5A.close(attributeId);
        }
示例#22
0
        public void H5AiterateTest1()
        {
            hid_t att = H5A.create(m_v2_test_file, "IEEE_F32BE",
                                   H5T.IEEE_F32BE, m_space_scalar);

            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            att = H5A.create(m_v2_test_file, "IEEE_F64BE", H5T.IEEE_F64BE,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            att = H5A.create(m_v2_test_file, "NATIVE_B8", H5T.NATIVE_B8,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            ArrayList al      = new ArrayList();
            GCHandle  hnd     = GCHandle.Alloc(al);
            IntPtr    op_data = (IntPtr)hnd;
            hsize_t   n       = 0;

            // the callback is defined in H5ATest.cs
            H5A.operator_t cb = DelegateMethod;
            Assert.IsTrue(H5A.iterate(m_v2_test_file, H5.index_t.NAME,
                                      H5.iter_order_t.NATIVE, ref n, cb, op_data) >= 0);
            // we should have 3 elements in the array list
            Assert.IsTrue(al.Count == 3);

            att = H5A.create(m_v0_test_file, "IEEE_F32BE",
                             H5T.IEEE_F32BE, m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            att = H5A.create(m_v0_test_file, "IEEE_F64BE", H5T.IEEE_F64BE,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            att = H5A.create(m_v0_test_file, "NATIVE_B8", H5T.NATIVE_B8,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            al.Clear();
            n = 0;
            Assert.IsTrue(H5A.iterate(m_v0_test_file, H5.index_t.NAME,
                                      H5.iter_order_t.NATIVE, ref n, cb, op_data) >= 0);
            // we should have 3 elements in the array list
            Assert.IsTrue(al.Count == 3);

            hnd.Free();
        }
示例#23
0
        // Generate double attributes
        // GroupName: target group for the new attribute
        // AttName: attribute name
        // AttContent: content for the attribute, has to be a single floating number, here 64bit double is used, to generate subgroups in Data
        public static void DoubleAttributeGenerator(H5GroupId GroupName, string AttName, double AttContent)
        {
            double[] AttArray = new double[1] {
                AttContent
            };
            long[] dims = new long[1];
            dims[0] = AttArray.Length;
            H5AttributeId attributeId = H5A.create(GroupName, AttName, H5T.copy(H5T.H5Type.NATIVE_DOUBLE), H5S.create_simple(1, dims));

            H5A.write(attributeId, H5T.copy(H5T.H5Type.NATIVE_FLOAT), new H5Array <double>(AttArray));
            H5A.close(attributeId);
        }
示例#24
0
        // Generate floating number attributes
        // GroupName: target group for the new attribute
        // AttName: attribute name
        // AttContent: content for the attribute, has to be a single floating number, here 32bit floating number is used, consider using 64bit double if necessary
        public static void NumberAttributeGenerator(H5GroupId GroupName, string AttName, float AttContent)
        {
            float[] AttArray = new float[1] {
                AttContent
            };
            long[] dims = new long[1];
            dims[0] = AttArray.Length;
            H5AttributeId attributeId = H5A.create(GroupName, AttName, H5T.copy(H5T.H5Type.NATIVE_FLOAT), H5S.create_simple(1, dims));

            H5A.write(attributeId, H5T.copy(H5T.H5Type.NATIVE_FLOAT), new H5Array <float>(AttArray));
            H5A.close(attributeId);
        }
示例#25
0
        static void test_attr_plist()
        {
            try
            {
                Console.Write("Testing attribute property lists");
                hssize_t[] dims = { 256, 512 };

                const string PLST_FILE_NAME = ("tattr_plist.h5");
                hssize_t[]   dims1          = { SPACE1_DIM1, SPACE1_DIM2, SPACE1_DIM3 };
                hssize_t[]   dims2          = { ATTR1_DIM };

                // Create file.
                H5FileId fileId = H5F.create(PLST_FILE_NAME, H5F.CreateMode.ACC_TRUNC);

                // Create dataspace for dataset.
                H5DataSpaceId space1_Id = H5S.create_simple(SPACE1_RANK, dims1);

                // Create a dataset.
                H5DataSetId dsetId = H5D.create(fileId, DSET1_NAME, H5T.H5Type.NATIVE_UCHAR, space1_Id);

                // Create dataspace for attribute.
                H5DataSpaceId space2_Id = H5S.create_simple(ATTR1_RANK, dims2);

                // Create default property list for attribute.
                H5PropertyListId plist
                    = H5P.create(H5P.PropertyListClass.ATTRIBUTE_CREATE);

                // Create an attribute for the dataset using the property list.
                H5AttributeId attrId = H5A.create(dsetId, ATTR1_NAME, new H5DataTypeId(H5T.H5Type.NATIVE_INT), space2_Id, plist);

                // Close all objects.
                H5S.close(space1_Id);
                H5S.close(space2_Id);
                H5P.close(plist);
                H5A.close(attrId);
                H5D.close(dsetId);
                H5F.close(fileId);

                Console.WriteLine("\t\t\tPASSED");
            }
            catch (HDFException anyHDF5E)
            {
                Console.WriteLine(anyHDF5E.Message);
                nerrors++;
            }
            catch (System.Exception sysE)
            {
                Console.WriteLine(sysE.TargetSite);
                Console.WriteLine(sysE.Message);
                nerrors++;
            }
        } // test_attr_plist
示例#26
0
        static void ReadFile(string filePath)
        {
            var file = H5F.open(filePath, H5F.ACC_RDONLY);

            var dataSet = H5D.open(file, "/group/dataset");

            var dataSpace = H5D.get_space(dataSet);

            var rank = H5S.get_simple_extent_ndims(dataSpace);

            if (rank == 2)
            {
                var dims = new ulong[2];
                H5S.get_simple_extent_dims(dataSpace, dims, null);

                var data = new int[dims[0], dims[1]];
                H5D.read(dataSet, H5T.NATIVE_INT, H5S.ALL, H5S.ALL, H5P.DEFAULT, new PinnedObject(data));

                for (int i = 0; i < data.GetLength(0); ++i)
                {
                    for (int j = 0; j < data.GetLength(1); ++j)
                    {
                        Write($"{data[i,j],3}");
                    }
                    WriteLine();
                }
            }

            H5S.close(dataSpace);

            var doubleAttribute = H5A.open(dataSet, "double");

#if false
            double pi     = 0.0;
            var    handle = GCHandle.Alloc(pi, GCHandleType.Pinned);
            H5A.read(doubleAttribute, H5T.NATIVE_DOUBLE, handle.AddrOfPinnedObject());
            handle.Free();
            WriteLine($"PI = {pi}");
#else
            var values = new double[1];
            H5A.read(doubleAttribute, H5T.NATIVE_DOUBLE, new PinnedObject(values));
            WriteLine($"PI = {values[0]}");
#endif
            H5A.close(doubleAttribute);

            WriteLine($"string: {ReadStringAttribute(dataSet, "string")}");
            WriteLine($"string-ascii: {ReadStringAttribute(dataSet, "string-ascii")}");
            WriteLine($"string-vlen: {ReadStringAttribute(dataSet, "string-vlen")}");

            H5D.close(dataSet);
            H5F.close(file);
        }
        private void AttributeCreateProcedure()
        {
            string name  = Thread.CurrentThread.Name;
            hid_t  space = H5S.create(H5S.class_t.NULL);

            Assert.IsTrue(space >= 0);

            Assert.IsTrue(
                H5A.close(H5A.create(m_shared_file_id, name, H5T.STD_I32BE,
                                     space)) >= 0);

            Assert.IsTrue(H5S.close(space) >= 0);
        }
示例#28
0
        private static void WriteAttribute(H5ObjectWithAttributes target, string name, long value)
        {
            H5DataTypeId dtype = H5T.copy(H5T.H5Type.NATIVE_LLONG);

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

            H5A.write(attributeId, dtype, new H5Array <long>(new[] { value }));

            H5A.close(attributeId);
            H5T.close(dtype);
            H5S.close(spaceId);
        }
示例#29
0
        public static unsafe void Add(ContainerType container, long fileId, string groupName, string elementName, long typeId, void *dataPtr, long spaceId, long cpl = 0, long apl = 0)
        {
            long res;
            long groupId;

            if (H5L.exists(fileId, groupName) > 0)
            {
                groupId = H5G.open(fileId, groupName);
            }
            else
            {
                groupId = H5G.create(fileId, groupName);
            }

            long id;

            if (container == ContainerType.Dataset)
            {
                id = H5D.create(groupId, Encoding.UTF8.GetBytes(elementName), typeId, spaceId, dcpl_id: cpl, dapl_id: apl);

                if (id == -1)
                {
                    throw new Exception("Could not create dataset.");
                }

                if ((int)dataPtr != 0)
                {
                    res = H5D.write(id, typeId, spaceId, H5S.ALL, 0, new IntPtr(dataPtr));
                }

                res = H5D.close(id);
            }
            else
            {
                id = H5A.create(groupId, Encoding.UTF8.GetBytes(elementName), typeId, spaceId, acpl_id: cpl);

                if (id == -1)
                {
                    throw new Exception("Could not create attribute.");
                }

                if ((int)dataPtr != 0)
                {
                    res = H5A.write(id, typeId, new IntPtr(dataPtr));
                }

                res = H5A.close(id);
            }

            res = H5G.close(groupId);
        }
示例#30
0
        private static void WriteAttribute(H5ObjectWithAttributes target, string name, double[] value)
        {
            H5DataTypeId dtype = H5T.copy(H5T.H5Type.NATIVE_DOUBLE);

            H5DataSpaceId spaceId = H5S.create_simple(1, new[] { value.LongCount() });

            H5AttributeId attributeId = H5A.create(target, name, dtype, spaceId);

            H5A.write(attributeId, dtype, new H5Array <double>(value));

            H5A.close(attributeId);
            H5T.close(dtype);
            H5S.close(spaceId);
        }