public static string ReadUnicodeString(hid_t groupId, string name)
        {
            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.NULLTERM);

            var datasetId = H5D.open(groupId, name);
            var typeId    = H5D.get_type(datasetId);

            var    classId = H5T.get_class(typeId);
            var    order   = H5T.get_order(typeId);
            IntPtr size    = H5T.get_size(typeId);
            int    strLen  = (int)size;

            var   spaceId = H5D.get_space(datasetId);
            hid_t count   = H5S.get_simple_extent_npoints(spaceId);

            IntPtr[] rdata = new IntPtr[count];
            byte[]   wdata = new byte[strLen];

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

            H5D.read(datasetId, datatype, H5S.ALL, H5S.ALL,
                     H5P.DEFAULT, hnd.AddrOfPinnedObject());

            for (int i = 0; i < strLen; i++)
            {
                Marshal.ReadByte(rdata[0], i);
            }
            Marshal.Copy(rdata[0], wdata, 0, strLen);
            string s = Encoding.UTF8.GetString(wdata);

            hnd.Free();
            H5S.close(spaceId);
            H5T.close(datatype);
            H5D.close(datasetId);
            return(s);
        }
Exemple #2
0
        public static string ReadUnicodeString(hid_t groupId, string name)
        {
            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);

            var datasetId = H5D.open(groupId, name);
            var typeId    = H5D.get_type(datasetId);

            var    classId = H5T.get_class(typeId);
            var    order   = H5T.get_order(typeId);
            IntPtr size    = H5T.get_size(typeId);
            int    strLen  = (int)size;

            var spaceId = H5D.get_space(datasetId);

            byte[] wdata = new byte[strLen];

            //IntPtr ptr = new IntPtr();
            GCHandle hnd = GCHandle.Alloc(wdata, GCHandleType.Pinned);

            H5D.read(datasetId, datatype, H5S.ALL, H5S.ALL,
                     H5P.DEFAULT, hnd.AddrOfPinnedObject());
            hnd.Free();

            //int len = 0;
            //while (Marshal.ReadByte(ptr, len) != 0) { ++len; }
            //byte[] name_buf = new byte[len];
            //Marshal.Copy(ptr, name_buf, 0, len);
            string s = Encoding.UTF8.GetString(wdata);

            H5S.close(spaceId);
            H5T.close(datatype);
            H5D.close(datasetId);
            return(s);
        }
Exemple #3
0
        private string ReadBuffer(byte[] buffer, int offset, H5T.class_t typeClass, int typeId)
        {
            string result   = string.Empty;
            IntPtr dataSize = H5T.get_size(typeId);

            H5T.order_t order = H5T.get_order(typeId);
            byte[]      temp  = new byte[dataSize.ToInt32()];

            Array.Copy(buffer, offset, temp, 0, dataSize.ToInt32());
            if (order == H5T.order_t.BE && typeClass != H5T.class_t.STRING)
            {
                Array.Reverse(temp);
            }
            switch (typeClass)
            {
            case H5T.class_t.NO_CLASS:
                break;

            case H5T.class_t.INTEGER:
                // H5T.Sign.TWOS_COMPLEMENT;
                H5T.sign_t sign = H5T.get_sign(typeId);

                switch (dataSize.ToInt32())
                {
                case 1:
                    result = ((double)BitConverter.ToChar(temp, 0)).ToString("G");
                    break;

                case 2:
                    switch (sign)
                    {
                    case H5T.sign_t.SGN_2:
                        result = ((double)BitConverter.ToInt16(temp, 0)).ToString("G");
                        break;

                    case H5T.sign_t.NONE:
                        result = ((double)BitConverter.ToUInt16(temp, 0)).ToString("G");
                        break;
                    }
                    break;

                case 4:
                    switch (sign)
                    {
                    case H5T.sign_t.SGN_2:
                        result = ((double)BitConverter.ToInt32(temp, 0)).ToString("G");
                        break;

                    case H5T.sign_t.NONE:
                        result = ((double)BitConverter.ToUInt32(temp, 0)).ToString("G");
                        break;
                    }
                    break;

                case 8:
                    switch (sign)
                    {
                    case H5T.sign_t.SGN_2:
                        result = ((double)BitConverter.ToInt64(temp, 0)).ToString("G");
                        break;

                    case H5T.sign_t.NONE:
                        result = ((double)BitConverter.ToUInt64(temp, 0)).ToString("G");
                        break;
                    }
                    break;
                }
                break;

            case H5T.class_t.FLOAT:
                switch (dataSize.ToInt32())
                {
                case 4:
                {
                    result = BitConverter.ToSingle(temp, 0).ToString("G");
                    break;
                }

                case 8:
                {
                    result = BitConverter.ToDouble(temp, 0).ToString("G");
                    break;
                }
                }
                break;

            case H5T.class_t.STRING:
            {
                GCHandle handler = GCHandle.Alloc(temp, GCHandleType.Pinned);
                var      str     = Marshal.PtrToStringAnsi(handler.AddrOfPinnedObject());
                handler.Free();
                result = str;
                break;
            }

            default:
                break;
            }
            return(result);
        }
Exemple #4
0
        public static Hdf5DataType GetDataTypeByType(Hdf5Identifier _typeId)
        {
            var typeClass = H5T.get_class(_typeId.Value);
            var typeSize  = (int)H5T.get_size(_typeId.Value);
            var typeSign  = H5T.get_sign(_typeId.Value);
            var typeOrder = H5T.get_order(_typeId.Value);

            Hdf5DataType dt = new Hdf5DataType
            {
                Id   = _typeId,
                Size = typeSize,
            };

            if (typeOrder == H5T.order_t.BE)
            {
                dt.ByteOrder = Hdf5ByteOrder.BigEndian;
            }
            else if (typeOrder == H5T.order_t.LE)
            {
                dt.ByteOrder = Hdf5ByteOrder.LittleEndian;
            }
            else if (typeOrder == H5T.order_t.ONE)
            {
                dt.ByteOrder = Hdf5ByteOrder.None;
            }

            if (typeClass == H5T.class_t.INTEGER)
            {
                if (typeSign == H5T.sign_t.NONE)
                {
                    if (typeSize == 1)
                    {
                        dt.Type       = Hdf5DataTypes.UInt8;
                        dt.NativeType = H5T.NATIVE_UINT8.ToId();
                    }
                    else if (typeSize == 2)
                    {
                        dt.Type       = Hdf5DataTypes.UInt16;
                        dt.NativeType = H5T.NATIVE_UINT16.ToId();
                    }
                    else if (typeSize == 4)
                    {
                        dt.Type       = Hdf5DataTypes.UInt32;
                        dt.NativeType = H5T.NATIVE_UINT32.ToId();
                    }
                    else if (typeSize == 8)
                    {
                        dt.Type       = Hdf5DataTypes.UInt64;
                        dt.NativeType = H5T.NATIVE_UINT64.ToId();
                    }
                }
                else
                {
                    if (typeSize == 1)
                    {
                        dt.Type       = Hdf5DataTypes.Int8;
                        dt.NativeType = H5T.NATIVE_INT8.ToId();
                    }
                    else if (typeSize == 2)
                    {
                        dt.Type       = Hdf5DataTypes.Int16;
                        dt.NativeType = H5T.NATIVE_INT16.ToId();
                    }
                    else if (typeSize == 4)
                    {
                        dt.Type       = Hdf5DataTypes.Int32;
                        dt.NativeType = H5T.NATIVE_INT32.ToId();
                    }
                    else if (typeSize == 8)
                    {
                        dt.Type       = Hdf5DataTypes.Int64;
                        dt.NativeType = H5T.NATIVE_INT64.ToId();
                    }
                }
            }
            else if (typeClass == H5T.class_t.FLOAT)
            {
                if (typeSize == 4)
                {
                    dt.Type       = Hdf5DataTypes.Single;
                    dt.NativeType = H5T.NATIVE_FLOAT.ToId();
                }
                else if (typeSize == 8)
                {
                    dt.Type       = Hdf5DataTypes.Double;
                    dt.NativeType = H5T.NATIVE_DOUBLE.ToId();
                }
            }
            else if (typeClass == H5T.class_t.STRING)
            {
                dt.Type       = Hdf5DataTypes.String;
                dt.NativeType = H5T.C_S1.ToId();
            }

            return(dt);
        }