Ejemplo n.º 1
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.º 2
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.º 3
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.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public void H5Sis_simpleTest3()
        {
            hid_t space = H5S.create(H5S.class_t.SCALAR);

            Assert.IsTrue(space >= 0);
            Assert.IsTrue(H5S.is_simple(space) > 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
        public void H5Sget_simple_extent_npointsTest2()
        {
            hid_t space = H5S.create(H5S.class_t.NULL);

            Assert.IsTrue(space >= 0);
            Assert.IsTrue(H5S.get_simple_extent_npoints(space) == 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
        public void H5Sget_simple_extent_ndimsTest3()
        {
            hid_t space = H5S.create(H5S.class_t.SCALAR);

            Assert.IsTrue(space >= 0);
            Assert.IsTrue(H5S.get_simple_extent_ndims(space) == 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
Ejemplo n.º 10
0
        public void H5Sis_simpleTest2()
        {
            hid_t space = H5S.create(H5S.class_t.NULL);

            Assert.IsTrue(space >= 0);
            Assert.IsTrue(H5S.is_simple(space) == 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
Ejemplo n.º 11
0
        public void H5Sselect_noneTest3()
        {
            hid_t space = H5S.create(H5S.class_t.NULL);

            Assert.IsTrue(space > 0);
            Assert.IsTrue(H5S.select_none(space) >= 0);
            Assert.IsTrue(H5S.get_select_npoints(space) == 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
Ejemplo n.º 12
0
        public void H5Sselect_allTest4()
        {
            hid_t space = H5S.create(H5S.class_t.SCALAR);

            Assert.IsTrue(space > 0);
            Assert.IsTrue(H5S.select_all(space) >= 0);
            Assert.IsTrue(H5S.get_select_npoints(space) == 1);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
Ejemplo n.º 13
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);
                }
            }
        }
Ejemplo n.º 15
0
        public void H5Sset_extent_simpleTest1()
        {
            hid_t space = H5S.create(H5S.class_t.NULL);

            Assert.IsTrue(space > 0);

            hsize_t[] dims = { 10, 20, 30 };
            Assert.IsTrue(
                H5S.set_extent_simple(space, 3, dims, null) >= 0);

            Assert.IsTrue(H5S.close(space) >= 0);
        }
Ejemplo n.º 16
0
        public static unsafe void AddDataspaceScalar(long fileId, ContainerType container)
        {
            var spaceId = H5S.create(H5S.class_t.SCALAR);
            var data    = new double[] { -1.2234234e-3 };

            fixed(void *dataPtr = data)
            {
                TestUtils.Add(container, fileId, "dataspace", "scalar", H5T.NATIVE_DOUBLE, dataPtr, spaceId);
            }

            H5S.close(spaceId);
        }
Ejemplo n.º 17
0
        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);
        }
Ejemplo n.º 18
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);
        }
Ejemplo n.º 19
0
        public void H5Sextent_copyTest1()
        {
            hsize_t[] dims  = { 1, 2, 3 };
            hid_t     space = H5S.create_simple(dims.Length, dims, dims);

            Assert.IsTrue(space > 0);
            hid_t space1 = H5S.create(H5S.class_t.NULL);

            Assert.IsTrue(space1 > 0);
            Assert.IsTrue(H5S.extent_copy(space1, space) >= 0);
            Assert.IsTrue(H5S.is_simple(space1) > 0);
            Assert.IsTrue(H5S.close(space1) >= 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
Ejemplo n.º 20
0
        private void DatasetCreateProcedure()
        {
            string name  = Thread.CurrentThread.Name;
            hid_t  space = H5S.create(H5S.class_t.SCALAR);

            Assert.IsTrue(space >= 0);

            hid_t dset = H5D.create(m_shared_file_id, name, H5T.STD_I32BE,
                                    space);

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

            Assert.IsTrue(H5S.close(space) >= 0);
        }
Ejemplo n.º 21
0
        public void H5ScreateTest1()
        {
            hid_t space = H5S.create(H5S.class_t.NULL);

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

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

            space = H5S.create(H5S.class_t.SIMPLE);
            Assert.IsTrue(space > 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
Ejemplo n.º 22
0
        public static int WriteAsciiString(hid_t groupId, string name, string str)
        {
            var spaceNullId   = H5S.create(H5S.class_t.NULL);
            var spaceScalarId = H5S.create(H5S.class_t.SCALAR);

            // create two datasets of the extended ASCII character set
            // store as H5T.FORTRAN_S1 -> space padding

            int strLength = str.Length;

            ulong[] dims = { (ulong)strLength, 1 };

            /* Create the dataset. */
            //name = ToHdf5Name(name);

            var spaceId   = H5S.create_simple(1, dims, null);
            var datasetId = H5D.create(groupId, name,
                                       H5T.FORTRAN_S1, spaceId);

            H5S.close(spaceId);

            // we write from C and must provide null-terminated strings

            byte[] wdata = new byte[strLength * 2];
            //for (int i = 0; i < strLength; ++i)
            //{
            //    wdata[2 * i] = (byte)i;
            //}
            for (int i = 0; i < strLength; ++i)
            {
                wdata[2 * i] = Convert.ToByte(str[i]);
            }

            var memId = H5T.copy(H5T.C_S1);

            H5T.set_size(memId, new IntPtr(2));
            //H5T.set_strpad(memId, H5T.str_t.NULLTERM);
            GCHandle hnd    = GCHandle.Alloc(wdata, GCHandleType.Pinned);
            int      result = H5D.write(datasetId, memId, H5S.ALL,
                                        H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();
            H5T.close(memId);
            H5D.close(datasetId);
            return(result);
        }
Ejemplo n.º 23
0
        static void WriteFile(string filePath)
        {
            var file = H5F.create(filePath, H5F.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 ulong[RANK] {
                DIM0, DIM1
            };
            var dataSpace = H5S.create_simple(RANK, dims, null);

            var dataSet = H5D.create(file, "/group/dataset", H5T.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, H5T.NATIVE_INT, H5S.ALL, H5S.ALL, H5P.DEFAULT, new PinnedObject(data));

            var hello = "早上好!";

            WriteStringAttribute(dataSet, "string", hello, true, false);
            WriteStringAttribute(dataSet, "string-ascii", "Hello, world!", false, false);
            WriteStringAttribute(dataSet, "string-vlen", hello, true, true);

            dataSpace = H5S.create(H5S.class_t.SCALAR);
            var doubleAttribute = H5A.create(dataSet, "double", H5T.NATIVE_DOUBLE, dataSpace);

            H5S.close(dataSpace);
            H5A.write(doubleAttribute, H5T.NATIVE_DOUBLE, new PinnedObject(new double[] { Math.PI }));
            H5A.close(doubleAttribute);

            H5D.close(dataSet);
            H5F.close(file);
        }
Ejemplo n.º 24
0
        public static void ClassInit(TestContext testContext)
        {
            // create test files which persists across file tests
            m_v0_class_file = Utilities.H5TempFile(ref m_v0_class_file_name,
                                                   H5F.libver_t.EARLIEST);
            Assert.IsTrue(m_v0_class_file >= 0);
            m_v2_class_file = Utilities.H5TempFile(ref m_v2_class_file_name);
            Assert.IsTrue(m_v2_class_file >= 0);

            m_space_null = H5S.create(H5S.class_t.NULL);
            Assert.IsTrue(m_space_null >= 0);
            m_space_scalar = H5S.create(H5S.class_t.SCALAR);
            Assert.IsTrue(m_space_scalar >= 0);

            m_acpl = H5P.create(H5P.ATTRIBUTE_CREATE);
            Assert.IsTrue(m_acpl >= 0);
            Assert.IsTrue(H5P.set_char_encoding(m_acpl, H5T.cset_t.UTF8) >= 0);
        }
Ejemplo n.º 25
0
        public static int WriteUnicodeString(hid_t groupId, string name, string str, H5T.str_t strPad = H5T.str_t.SPACEPAD)
        {
            byte[] wdata = Encoding.UTF8.GetBytes(str);

            hid_t spaceId = H5S.create(H5S.class_t.SCALAR);

            hid_t dtype = H5T.create(H5T.class_t.STRING, new IntPtr(wdata.Length));

            H5T.set_cset(dtype, H5T.cset_t.UTF8);
            H5T.set_strpad(dtype, strPad);

            hid_t datasetId = H5D.create(groupId, name, dtype, spaceId);

            GCHandle hnd    = GCHandle.Alloc(wdata, GCHandleType.Pinned);
            int      result = H5D.write(datasetId, dtype, H5S.ALL,
                                        H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();

            H5T.close(dtype);
            H5D.close(datasetId);
            H5S.close(spaceId);
            return(result);
        }
Ejemplo n.º 26
0
        public static int WriteUnicodeString(long groupId, string name, string str, H5T.str_t strPad = H5T.str_t.SPACEPAD)
        {
            byte[] wdata = Hdf5Utils.StringToByte(str);

            long spaceId = H5S.create(H5S.class_t.SCALAR);

            long dtype = H5T.create(H5T.class_t.STRING, new IntPtr(wdata.Length));

            H5T.set_cset(dtype, Hdf5Utils.GetCharacterSet(Settings.CharacterSetType));
            H5T.set_strpad(dtype, strPad);

            long datasetId = H5D.create(groupId, Hdf5Utils.NormalizedName(name), dtype, spaceId);

            GCHandle hnd    = GCHandle.Alloc(wdata, GCHandleType.Pinned);
            int      result = H5D.write(datasetId, dtype, H5S.ALL,
                                        H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();

            H5T.close(dtype);
            H5D.close(datasetId);
            H5S.close(spaceId);
            return(result);
        }
Ejemplo n.º 27
0
        public static void WriteHdfAttribute(H5ObjectWithAttributes fileOrdatasetId, HDFAttributeDef hDFAttributeDef)
        {
            H5DataSpaceId dataSpaceId = H5S.create_simple(1, new long[] { (long)hDFAttributeDef.Size });
            H5DataTypeId  dataTypeId  = null;
            H5AttributeId attributeId = null;

            try
            {
                switch (Type.GetTypeCode(hDFAttributeDef.Type))
                {
                case TypeCode.Byte:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_U8LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <byte> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Char:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_U8LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <char> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Double:
                    //dataTypeId = H5T.copy(H5T.H5Type.IEEE_F64BE);
                    dataTypeId  = H5T.copy(H5T.H5Type.IEEE_F64LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <double> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Int16:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_I16LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <Int16> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Int32:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_I32LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <Int32> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Int64:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_I64LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <Int64> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Object:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_REF_OBJ);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <object> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Single:
                    dataTypeId  = H5T.copy(H5T.H5Type.IEEE_F32LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <Single> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.String:
                    dataTypeId  = H5T.copy(H5T.H5Type.C_S1);
                    dataSpaceId = H5S.create(H5S.H5SClass.SCALAR);
                    attributeId = WriteStringAttribute(fileOrdatasetId, hDFAttributeDef, dataSpaceId, dataTypeId);
                    break;

                case TypeCode.UInt16:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_U16LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <UInt16> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.UInt32:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_U32LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <UInt32> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.UInt64:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_U64LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <UInt64> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;
                }
            }
            catch (Exception ex)
            {
                int i = 9;
                int j = i;
            }
            finally
            {
                if (attributeId != null)
                {
                    H5A.close(attributeId);
                }
            }
        }
Ejemplo n.º 28
0
        public static void WriteLabelWorkingSet(long fileId_inp, bool overwrite_inp)
        {
            var LabelClassInfos = Labeling.GetAllIdsNamesAndMaterials();

            int status = 0;

            ulong[] dims = new ulong[2];
            dims[0] = 2;
            dims[1] = (ulong)LabelClassInfos.Count;

            long group_id = 0;

            if (overwrite_inp)
            {
                group_id = H5G.open(fileId_inp, "labelWorkingSet");
            }
            else
            {
                group_id = H5G.create(fileId_inp, "labelWorkingSet");
            }

            for (int i = 0; i < LabelClassInfos.Count; i++)
            {
                GCHandle labelID = GCHandle.Alloc(LabelClassInfos.ElementAt(i).Key, GCHandleType.Pinned);

                string labelClassName = LabelClassInfos.ElementAt(i).Value.Item1;

                long attr_id = H5A.open(group_id, labelClassName);

                if (attr_id >= 0)
                {
                    //attribute exists
                    status = H5A.write(attr_id, H5T.NATIVE_UINT32, labelID.AddrOfPinnedObject());
                }
                else
                {
                    //attribute has to be created
                    attr_id = H5A.create(group_id, LabelClassInfos.ElementAt(i).Value.Item1, H5T.NATIVE_UINT32, H5S.create(H5S.class_t.SCALAR));
                    status  = H5A.write(attr_id, H5T.NATIVE_UINT32, labelID.AddrOfPinnedObject());
                }

                status = H5A.close(attr_id);
            }

            status = H5G.close(group_id);
        }
        public static bool WriteVariableStringAttribute(hid_t hid, string key, IEnumerable <string> values, bool utf8)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

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

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

            if (exists == 0) // Attribute doesn't exist
            {
#if true
                var type = H5T.create(H5T.class_t.STRING, H5T.VARIABLE);
                if (type < 0)
                {
                    throw new Exception("Failed to create string type");
                }
#else
                var type = H5T.copy(H5T.C_S1);
                if (type < 0)
                {
                    throw new Exception("Failed to create string type");
                }

                if (H5T.set_size(type, H5T.VARIABLE) < 0)
                {
                    H5T.close(type);
                    throw new Exception("Failed to set type size");
                }
#endif

                if (utf8)
                {
                    if (H5T.set_cset(type, H5T.cset_t.UTF8) < 0)
                    {
                        H5T.close(type);
                        throw new Exception("Failed to set cset");
                    }
                }

                if (H5T.set_strpad(type, H5T.str_t.NULLTERM) < 0)
                {
                    H5T.close(type);
                    throw new Exception("Failed to set strpad");
                }

                var space = values.Count() == 1 ? H5S.create(H5S.class_t.SCALAR)
                    : H5S.create_simple(1, new ulong[1] {
                    (ulong)values.Count()
                }, null);
                if (space < 0)
                {
                    H5T.close(type);
                    throw new Exception("Failed to create data space");
                }

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

                var pinnedObjects = new PinnedObject[values.Count()];
                var data          = new IntPtr[values.Count()];
                int count         = 0;
                foreach (string str in values)
                {
                    var bytes = str.ToBytes(utf8);
                    pinnedObjects[count] = new PinnedObject(bytes);
                    data[count]          = pinnedObjects[count];
                    count += 1;
                }

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

                H5T.close(type);
                H5A.close(attribute);
            }
            else
            {
                // Attribute exists.
                var attribute = H5A.open(hid, key);
                if (attribute < 0)
                {
                    throw new Exception(string.Format("Failed to open attribute \"{0}\"", key));
                }

                var type = H5A.get_type(attribute);
                if (type < 0)
                {
                    H5A.close(attribute);
                    throw new Exception("Failed to get data type");
                }

                var pinnedObjects = new PinnedObject[values.Count()];
                var data          = new IntPtr[values.Count()];
                int count         = 0;
                foreach (string str in values)
                {
                    var bytes = str.ToBytes(utf8);
                    pinnedObjects[count] = new PinnedObject(bytes);
                    data[count]          = pinnedObjects[count];
                    count += 1;
                }

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

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

            return(true);
        }
Ejemplo n.º 30
0
        public static void ClassInit(TestContext testContext)
        {
            // create test files which persists across file tests
            m_v0_class_file = Utilities.H5TempFile(ref m_v0_class_file_name,
                                                   H5F.libver_t.EARLIEST);
            Assert.IsTrue(m_v0_class_file >= 0);
            m_v2_class_file = Utilities.H5TempFile(ref m_v2_class_file_name);
            Assert.IsTrue(m_v2_class_file >= 0);

            m_space_null = H5S.create(H5S.class_t.NULL);
            Assert.IsTrue(m_space_null >= 0);
            m_space_scalar = H5S.create(H5S.class_t.SCALAR);
            Assert.IsTrue(m_space_scalar >= 0);

            // create two datasets of the extended ASCII character set
            // store as H5T.FORTRAN_S1 -> space padding

            hsize_t[] dims = { 256 };

            hid_t space = H5S.create_simple(1, dims, null);

            m_v0_ascii_dset = H5D.create(m_v0_class_file, "ASCII",
                                         H5T.FORTRAN_S1, space);
            m_v2_ascii_dset = H5D.create(m_v2_class_file, "ASCII",
                                         H5T.FORTRAN_S1, space);
            Assert.IsTrue(H5S.close(space) >= 0);

            // we write from C and must provide null-terminated strings

            byte[] wdata = new byte[512];
            for (int i = 0; i < 256; ++i)
            {
                wdata[2 * i] = (byte)i;
            }

            hid_t mem_type = H5T.copy(H5T.C_S1);

            Assert.IsTrue(H5T.set_size(mem_type, new IntPtr(2)) >= 0);

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

            Assert.IsTrue(H5D.write(m_v0_ascii_dset, mem_type, H5S.ALL,
                                    H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0);
            Assert.IsTrue(H5D.write(m_v2_ascii_dset, mem_type, H5S.ALL,
                                    H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0);
            hnd.Free();

            Assert.IsTrue(H5T.close(mem_type) >= 0);

            // create UTF-8 encoded test datasets

            hid_t dtype = H5T.create(H5T.class_t.STRING, H5T.VARIABLE);

            Assert.IsTrue(H5T.set_cset(dtype, H5T.cset_t.UTF8) >= 0);
            Assert.IsTrue(H5T.set_strpad(dtype, H5T.str_t.SPACEPAD) >= 0);

            hid_t dspace = H5S.create_simple(1,
                                             new hsize_t[] { (hsize_t)m_utf8strings.Count }, null);

            m_v0_utf8_dset = H5D.create(m_v0_class_file, "UTF-8", dtype, dspace);
            Assert.IsTrue(m_v0_utf8_dset >= 0);
            m_v2_utf8_dset = H5D.create(m_v2_class_file, "UTF-8", dtype, dspace);
            Assert.IsTrue(m_v2_utf8_dset >= 0);

            GCHandle[] hnds   = new GCHandle[m_utf8strings.Count];
            IntPtr[]   wdata1 = new IntPtr[m_utf8strings.Count];

            for (int i = 0; i < m_utf8strings.Count; ++i)
            {
                hnds[i] = GCHandle.Alloc(
                    Encoding.UTF8.GetBytes((string)m_utf8strings[i]),
                    GCHandleType.Pinned);
                wdata1[i] = hnds[i].AddrOfPinnedObject();
            }

            hnd = GCHandle.Alloc(wdata1, GCHandleType.Pinned);
            Assert.IsTrue(H5D.write(m_v0_utf8_dset, dtype, H5S.ALL, H5S.ALL,
                                    H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0);
            Assert.IsTrue(H5D.write(m_v2_utf8_dset, dtype, H5S.ALL, H5S.ALL,
                                    H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0);
            hnd.Free();

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

            Assert.IsTrue(H5S.close(dspace) >= 0);
            Assert.IsTrue(H5T.close(dtype) >= 0);
        }