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>());
        }
示例#2
0
        public static IEnumerable <OffsetInfo> GetCompoundInfo(Type type, bool ieee = false)
        {
            //Type t = typeof(T);
            var strtype = H5T.copy(H5T.C_S1);
            int strsize = (int)H5T.get_size(strtype);
            int curSize = 0;
            List <OffsetInfo> offsets = new List <OffsetInfo>();

            foreach (var x in type.GetFields())
            {
                OffsetInfo oi = new OffsetInfo()
                {
                    name     = x.Name,
                    type     = x.FieldType,
                    datatype = ieee ? GetDatatypeIEEE(x.FieldType) : GetDatatype(x.FieldType),
                    size     = x.FieldType == typeof(string) ? stringLength(x) : Marshal.SizeOf(x.FieldType),
                    offset   = 0 + curSize
                };
                if (oi.datatype == H5T.C_S1)
                {
                    strtype = H5T.copy(H5T.C_S1);
                    H5T.set_size(strtype, new IntPtr(oi.size));
                    oi.datatype = strtype;
                }
                if (oi.datatype == H5T.STD_I64BE)
                {
                    oi.size = oi.size * 2;
                }
                curSize = curSize + oi.size;

                offsets.Add(oi);
            }
            H5T.close(strtype);
            return(offsets);
        }
示例#3
0
        public Dictionary <string, string> TryReadDataTable(string datasetName)
        {
            Dictionary <string, string> result = null;
            var subDsDic = ds.GetSubDatasets();

            if (subDsDic.Count > 0)
            {
                H5ID h5FileId  = H5F.open(fileName, H5F.ACC_RDONLY);
                H5ID datasetId = H5D.open(h5FileId, datasetName);
                H5ID typeId    = H5D.get_type(datasetId);
                H5ID spaceId   = H5D.get_space(datasetId);
                if (H5T.get_class(typeId) == H5T.class_t.COMPOUND)
                {
                    int      numCount = H5T.get_nmembers(typeId);
                    var      size     = H5T.get_size(typeId);
                    byte[]   buffer   = new byte[size.ToInt32()];
                    GCHandle hnd      = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                    int      ndims    = H5S.get_simple_extent_ndims(spaceId);
                    if (ndims == 1)
                    {
                        result = new Dictionary <string, string>();
                        H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

                        for (uint i = 0; i < numCount; i++)
                        {
                            string name   = Marshal.PtrToStringAnsi(H5T.get_member_name(typeId, i));
                            int    offset = H5T.get_member_offset(typeId, i).ToInt32();

                            H5ID        subTypeId = H5T.get_member_type(typeId, i);
                            H5T.class_t typeClass = H5T.get_member_class(typeId, i);
                            string      value     = ReadBuffer(buffer, offset, typeClass, subTypeId);
                            result.Add(name, value);
                            H5T.close(subTypeId);
                        }
                    }

                    hnd.Free();
                }

                if (spaceId != 0)
                {
                    H5S.close(spaceId);
                }
                if (typeId != 0)
                {
                    H5T.close(typeId);
                }
                if (datasetId != 0)
                {
                    H5D.close(datasetId);
                }
                if (h5FileId != 0)
                {
                    H5F.close(h5FileId);
                }
            }

            return(result);
        }
示例#4
0
        private IntPtr CreateNativeArray(Array clrArray, long dtypeID)
        {
            var arraySize = clrArray.Length * ElementSize;

            Array oneD;

            if (clrArray.Rank > 1)
            {
                oneD = Array.CreateInstance(clrArray.ElementType(), clrArray.Length);
                Buffer.BlockCopy(clrArray, 0, oneD, 0, arraySize);
            }
            else
            {
                oneD = clrArray;
            }

            var nativeSize = arraySize;

            if (ClrType == typeof(byte))
            {
                nativeSize *= H5T.get_size(dtypeID).ToInt32();
            }
            var result = Marshal.AllocHGlobal(nativeSize);

            if (ClrType == typeof(byte))
            {
                var      maxLength   = H5T.get_size(dtypeID).ToInt32();
                string[] allStrings  = (string[])oneD;
                byte[]   actualArray = new byte[maxLength * oneD.Length];
                for (var i = 0; i < allStrings.Length; i++)
                {
                    //return System.Text.Encoding.Default.GetString(slice);
                    byte[] bytes = Encoding.ASCII.GetBytes(allStrings[i] + '\0');

                    Buffer.BlockCopy(bytes, 0, actualArray, i * maxLength, bytes.Length);
                }
                Marshal.Copy(actualArray, 0, result, actualArray.Length);
            }
            else if (ClrType == typeof(double))
            {
                Marshal.Copy((double[])oneD, 0, result, oneD.Length);
            }
            else if (ClrType == typeof(float))
            {
                Marshal.Copy((float[])oneD, 0, result, oneD.Length);
            }
            else if (ClrType == typeof(long))
            {
                Marshal.Copy((long[])oneD, 0, result, oneD.Length);
            }
            else
            {
                throw new ArgumentException("Unknown ClrType");
            }

            return(result);
        }
示例#5
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);
                        }
                    }
                }));
            }
        }
示例#6
0
        public float[] TryReadFactor(AbstractWarpDataset ds, string datasetName)
        {
            string dsPath = (ds.hdfOperator as Hdf5Operator).GetDatasetNames.Where(t => t.Contains(datasetName))
                            .FirstOrDefault();

            float[] factor    = null;
            int     h5FileId  = H5F.open(ds.fileName, H5F.ACC_RDONLY);
            int     datasetId = H5D.open(h5FileId, dsPath);
            int     typeId    = H5D.get_type(datasetId);
            int     spaceId   = H5D.get_space(datasetId);

            if (H5T.get_class(typeId) == H5T.class_t.FLOAT)
            {
                var     size = H5T.get_size(typeId);
                int     rank = H5S.get_simple_extent_ndims(spaceId);
                ulong[] dims = new ulong[rank];
                int     err  = H5S.get_simple_extent_dims(spaceId, dims, null);
                factor = new float[dims[0]];
                GCHandle hnd = GCHandle.Alloc(factor, GCHandleType.Pinned);
                H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

                hnd.Free();
            }

            if (spaceId != 0)
            {
                H5S.close(spaceId);
            }
            if (typeId != 0)
            {
                H5T.close(typeId);
            }
            if (datasetId != 0)
            {
                H5D.close(datasetId);
            }
            if (h5FileId != 0)
            {
                H5F.close(h5FileId);
            }
            return(factor);
        }
示例#7
0
        public static IEnumerable ReadStrings(string filename, string dataset)
        {
            var f = H5F.open(filename, H5F.ACC_RDONLY);

            if (f < 0)
            {
                throw new Exception("Could not open file: " + filename);
            }

            var dset = H5D.open(f, Encoding.ASCII.GetBytes(dataset), H5P.DEFAULT);

            if (dset < 0)
            {
                throw new Exception("Could not open dataset: " + dataset);
            }

            var filetype = H5D.get_type(dset);
            var sdim     = H5T.get_size(filetype) + 1;

            var space = H5D.get_space(dset);
            var ndims = H5S.get_simple_extent_ndims(space);

            ulong[] dims = new ulong[ndims];
            H5S.get_simple_extent_dims(space, dims, null);
            var memtype = H5T.copy(H5T.C_S1);
            var status  = H5T.set_size(memtype, sdim);

            int len = (int)(dims[0] * (ulong)sdim * SIZEOF_CHAR);

            byte[] buffer = new byte[len];
            IntPtr ptr    = Marshal.AllocHGlobal(len);

            status = H5D.read(dset, memtype, H5S.ALL, H5S.ALL, H5P.DEFAULT, ptr);
            Marshal.Copy(ptr, buffer, 0, len);
            Marshal.FreeHGlobal(ptr);

            string s = Encoding.ASCII.GetString(buffer);

            return(s.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries));
        }
示例#8
0
        /// <summary>
        /// Reads a string scalar attribute value.
        /// Assumes that the parent object is already open.
        /// </summary>
        /// <param name="_objectId"></param>
        /// <param name="_title"></param>
        /// <returns></returns>
        public static Hdf5Attribute GetStringAttribute(Hdf5Identifier _objectId, string _title)
        {
            int attributeId = 0;
            int typeId      = 0;

            attributeId = H5A.open(_objectId.Value, _title);
            typeId      = H5A.get_type(attributeId);
            var sizeData = H5T.get_size(typeId);
            var size     = sizeData.ToInt32();

            byte[] strBuffer = new byte[size];

            var      aTypeMem    = H5T.get_native_type(typeId, H5T.direction_t.ASCEND);
            GCHandle pinnedArray = GCHandle.Alloc(strBuffer, GCHandleType.Pinned);

            H5A.read(attributeId, aTypeMem, pinnedArray.AddrOfPinnedObject());
            pinnedArray.Free();
            H5T.close(aTypeMem);

            string value = System.Text.Encoding.ASCII.GetString(strBuffer, 0, strBuffer.Length - 1);

            var attribute = new Hdf5Attribute
            {
                Id    = attributeId.ToId(),
                Name  = _title,
                Value = value
            };

            if (attributeId > 0)
            {
                H5A.close(attributeId);
            }

            if (typeId > 0)
            {
                H5T.close(typeId);
            }

            return(attribute);
        }
示例#9
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.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);
        }
示例#10
0
        public static string[] LoadStringDataset(string filePath, string dataSetName)
        {
            //With much Help from:  https://stackoverflow.com/questions/23295545/reading-string-array-from-a-hdf5-dataset
            long fileId = H5F.open(filePath, H5F.ACC_RDONLY);

            string longJoinedString;
            int    stringLength;

            try {
                long datasetId = H5D.open(fileId, dataSetName);
                long spaceID   = H5D.get_space(datasetId);
                long dataType  = H5D.get_type(datasetId);

                int[] dimensions = GetDatasetDimensions(spaceID);

                stringLength = (int)H5T.get_size(dataType);
                byte[] buffer = new byte[dimensions[0] * stringLength];

                GCHandle gch = GCHandle.Alloc(buffer, GCHandleType.Pinned);


                try {
                    H5D.read(datasetId, dataType, H5S.ALL, H5S.ALL, H5P.DEFAULT, gch.AddrOfPinnedObject());
                    longJoinedString = Encoding.ASCII.GetString(buffer);
                }
                finally {
                    gch.Free();
                    H5D.close(dataType);
                    H5D.close(spaceID);
                    H5D.close(datasetId);
                }
            }
            finally {
                H5F.close(fileId);
            }

            return(longJoinedString.SplitInParts(stringLength).Select(ss => (string)(object)ss).ToArray());
        }
示例#11
0
        private static string ReadDataString(hid_t fileLoc, string name)
        {
            hid_t dset = H5D.open(fileLoc, name);
            hid_t type = H5D.get_type(dset);
            // H5T.is_variable_str(type);
            IntPtr size = H5T.get_size(type);

            hid_t fspace   = H5D.get_space(dset);
            hid_t mem_type = H5T.copy(type);

            H5T.set_size(mem_type, size);


            byte[]   buffer = new byte[size.ToInt32()];
            GCHandle hnd    = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            H5D.read(dset, mem_type, H5S.ALL,
                     H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();

            H5T.close(mem_type);

            // remove the last "/0"
            if (buffer[buffer.Length - 1] == 0)
            {
                byte[] buffer2 = new byte[buffer.Length - 1];
                for (int i = 0; i < buffer2.Length; i++)
                {
                    buffer2[i] = buffer[i];
                }


                return(ASCIIEncoding.ASCII.GetString(buffer2));
            }
            return(ASCIIEncoding.ASCII.GetString(buffer));
        }
示例#12
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);
        }
示例#13
0
        static void Main_Read(string[] args)
        {
            int DATA_ARRAY_LENGTH = 5;

            //var h5 = H5F.open(@"E:\HDF5\HDF5DotNet-src\examples\CSharpExample\CSharpExample1\table.h5", H5F.ACC_RDONLY);

            //var h5 = H5F.open(@"D:\test.h5", H5F.ACC_RDONLY);
            //var h5 = H5F.open(@"E:\HDF5\Hdf5DotnetTools-master\ToolTest\bin\Debug\table.h5", H5F.ACC_RDONLY);
            var h5 = H5F.open(@"E:\HDF5\test_gzip.h5", H5F.ACC_RDONLY);

            var dataset = H5D.open(h5, "trans_detail/20160929");
            var spaceid = H5D.get_space(dataset);
            var npoints = H5S.get_simple_extent_npoints(spaceid);
            //var dims = H5S.get_simple_extent_dims(spaceid);
            int rank = H5S.get_simple_extent_ndims(spaceid);

            // 是不是不能用自己的type
            var dtype = H5D.get_type(dataset);
            var dtcls = H5T.get_class(dtype);
            var size  = H5T.get_size(dtype);
            var sz    = Marshal.SizeOf(typeof(ComType));

            var dtype_n = H5T.get_nmembers(dtype);

            for (uint i = 0; i < dtype_n; ++i)
            {
                var x  = H5T.get_member_name(dtype, i);
                var x4 = Marshal.PtrToStringAnsi(x);
                var y  = H5T.get_member_type(dtype, i);
                var z  = H5T.get_class(y);
                var x1 = H5T.get_member_offset(dtype, i);
                var x3 = H5T.get_size(y);

                Console.WriteLine(x4);
                Console.WriteLine(z);
                Console.WriteLine(x1);
                //var x2 = Marshal.OffsetOf(typeof(ComType), x4).ToInt32();
                //Console.WriteLine(x2);
                Console.WriteLine(x3);
            }

            int    ss1 = Marshal.SizeOf(typeof(ComType));
            IntPtr p   = Marshal.AllocHGlobal(ss1 * 11);

            H5D.read(dataset, dtype, H5S.ALL, H5S.ALL, H5P.DEFAULT, p);
            var s = Marshal.PtrToStructure(p, typeof(ComType));

            Console.WriteLine(s);
            var s2 = Marshal.PtrToStructure(p + ss1, typeof(ComType));

            Console.WriteLine(s2);
            var s3 = Marshal.PtrToStructure(p + ss1 * 4, typeof(ComType));

            Console.WriteLine(s3);
            var s4 = Marshal.PtrToStructure(p + ss1 * 5, typeof(ComType));

            Console.WriteLine(s4);
            var s6 = Marshal.PtrToStructure(p + ss1 * 10, typeof(ComType));

            Console.WriteLine(s6);
        }
示例#14
0
        public static string ReadUnicodeString(hid_t groupId, string name)
        {
            var datasetId = H5D.open(groupId, name);
            var typeId    = H5D.get_type(datasetId);

            if (H5T.is_variable_str(typeId) > 0)
            {
                var   spaceId = H5D.get_space(datasetId);
                hid_t count   = H5S.get_simple_extent_npoints(spaceId);

                IntPtr[] rdata = new IntPtr[count];

                GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned);
                H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL,
                         H5P.DEFAULT, hnd.AddrOfPinnedObject());

                var attrStrings = new List <string>();
                for (int i = 0; i < rdata.Length; ++i)
                {
                    int attrLength = 0;
                    while (Marshal.ReadByte(rdata[i], attrLength) != 0)
                    {
                        ++attrLength;
                    }

                    byte[] buffer = new byte[attrLength];
                    Marshal.Copy(rdata[i], buffer, 0, buffer.Length);

                    string stringPart = Encoding.UTF8.GetString(buffer);

                    attrStrings.Add(stringPart);

                    H5.free_memory(rdata[i]);
                }

                hnd.Free();
                H5S.close(spaceId);
                H5D.close(datasetId);

                return(attrStrings[0]);
            }

            // Must be a non-variable length string.
            int    size = H5T.get_size(typeId).ToInt32();
            IntPtr iPtr = Marshal.AllocHGlobal(size);

            int result = H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL,
                                  H5P.DEFAULT, iPtr);

            if (result < 0)
            {
                throw new IOException("Failed to read dataset");
            }

            var strDest = new byte[size];

            Marshal.Copy(iPtr, strDest, 0, size);
            Marshal.FreeHGlobal(iPtr);

            H5D.close(datasetId);

            return(Encoding.UTF8.GetString(strDest).TrimEnd((Char)0));
        }
示例#15
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);
        }
示例#16
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);
        }
        public static IEnumerable <OffsetInfo> GetCompoundInfo(Type type, bool ieee = false)
        {
            //Type t = typeof(T);
            var strtype = H5T.copy(H5T.C_S1);
            int strsize = (int)H5T.get_size(strtype);
            int curSize = 0;
            List <OffsetInfo> offsets = new List <OffsetInfo>();

            foreach (var x in type.GetFields())
            {
                var        fldType = x.FieldType;
                OffsetInfo oi      = new OffsetInfo()
                {
                    name     = x.Name,
                    type     = fldType,
                    datatype = ieee ? GetDatatypeIEEE(fldType) : GetDatatype(fldType),
                    size     = fldType == typeof(string) ? StringLength(x) : Marshal.SizeOf(fldType),
                    offset   = 0 + curSize
                };
                if (oi.datatype == H5T.C_S1)
                {
                    strtype = H5T.copy(H5T.C_S1);
                    H5T.set_size(strtype, new IntPtr(oi.size));
                    oi.datatype = strtype;
                }
                if (oi.datatype == H5T.STD_I64BE)
                {
                    oi.size = oi.size * 2;
                }
                curSize = curSize + oi.size;

                offsets.Add(oi);
            }

            /* poging om ook properties te bewaren.
             * foreach (var x in type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))
             * {
             *  bool saveProperty = false;
             *  bool isNotPublic = x.PropertyType.Attributes != TypeAttributes.Public;
             *  foreach (Attribute attr in Attribute.GetCustomAttributes(x))
             *  {
             *      var legAttr = attr as Hdf5SaveAttribute;
             *      var kind = legAttr?.SaveKind;
             *      bool saveAndPrivateProp = isNotPublic && kind == Hdf5Save.Save;
             *      bool doNotSaveProp = (kind == Hdf5Save.DoNotSave) ;
             *      if (saveAndPrivateProp && !doNotSaveProp)
             *      {
             *          saveProperty = true;
             *          continue;
             *      }
             *
             *  }
             *  if (!saveProperty)
             *      continue;
             *  var propType = x.PropertyType;
             *  OffsetInfo oi = new OffsetInfo()
             *  {
             *      name = x.Name,
             *      type = propType,
             *      datatype = ieee ? GetDatatypeIEEE(propType) : GetDatatype(propType),
             *      size = propType == typeof(string) ? stringLength(x) : Marshal.SizeOf(propType),
             *      offset = 0 + curSize
             *  };
             *  if (oi.datatype == H5T.C_S1)
             *  {
             *      strtype = H5T.copy(H5T.C_S1);
             *      H5T.set_size(strtype, new IntPtr(oi.size));
             *      oi.datatype = strtype;
             *  }
             *  if (oi.datatype == H5T.STD_I64BE)
             *      oi.size = oi.size * 2;
             *  curSize = curSize + oi.size;
             *
             *  offsets.Add(oi);
             * }*/
            H5T.close(strtype);
            return(offsets);
        }
        public static string ReadStringAttribute(hid_t hid, string key)
        {
            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 typeClass = H5T.get_class(type);

            if (typeClass != H5T.class_t.STRING)
            {
                H5T.close(type);
                throw new Exception("Not a string attribute");
            }

            var utf8  = H5T.get_cset(type) == H5T.cset_t.UTF8;
            var ascii = H5T.get_cset(type) == H5T.cset_t.ASCII;

            if (!utf8 && !ascii)
            {
                H5T.close(type);
                H5A.close(attribute);
                throw new Exception("Neither ASCII nor UTF8.");
            }

            var isVariableString = H5T.is_variable_str(type);

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

            if (isVariableString > 0)
            {
                var space = H5A.get_space(attribute);
                if (space < 0)
                {
                    H5T.close(type);
                    H5A.close(attribute);
                    throw new Exception("H5A.get_space failed.");
                }

                hid_t count = H5S.get_simple_extent_npoints(space);
                var   rdata = new IntPtr[count];

                H5A.read(attribute, type, new PinnedObject(rdata));

                var attrStrings = new List <string>();
                for (int i = 0; i < rdata.Length; ++i)
                {
                    int attrLength = 0;
                    while (Marshal.ReadByte(rdata[i], attrLength) != 0)
                    {
                        ++attrLength;
                    }

                    byte[] buffer = new byte[attrLength];
                    Marshal.Copy(rdata[i], buffer, 0, buffer.Length);

                    string part = utf8 ? Encoding.UTF8.GetString(buffer)
                        : Encoding.ASCII.GetString(buffer);

                    attrStrings.Add(part);

                    H5.free_memory(rdata[i]);
                }

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

                return(attrStrings[0]);
            }

            // Must be a non-variable length string.
            var size            = H5T.get_size(type).ToInt32();
            var unmanagedBuffer = new UnmanagedBuffer(size);

            int result = H5A.read(attribute, type, unmanagedBuffer);

            if (result < 0)
            {
                H5T.close(type);
                H5D.close(attribute);
                throw new IOException("Failed to read attribute.");
            }

            var bytes = new byte[size];

            unmanagedBuffer.CopyTo(bytes, 0, bytes.Length);

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

            var value = utf8 ? Encoding.UTF8.GetString(bytes)
                : Encoding.ASCII.GetString(bytes);

            return(value.TrimEnd('\0'));
        }
示例#19
0
        public static IEnumerable <OffsetInfo> GetCompoundInfo(Type type, bool ieee = false)
        {
            //Type t = typeof(T);
            var strtype = H5T.copy(H5T.C_S1);
            int strsize = (int)H5T.get_size(strtype);
            int curSize = 0;
            List <OffsetInfo> offsets = new List <OffsetInfo>();

            foreach (var x in type.GetFields())
            {
                //var fldType = x.FieldType;
                //OffsetInfo oi = new OffsetInfo()
                //{
                //    name = x.Name,
                //    type = fldType,
                //    datatype = ieee ? GetDatatypeIEEE(fldType) : GetDatatype(fldType),
                //    size = fldType == typeof(string) ? StringLength(x) : Marshal.SizeOf(fldType),
                //    offset = 0 + curSize
                //};
                var fldType             = x.FieldType;
                var marshallAsAttribute = type.GetMember(x.Name).Select(m => m.GetCustomAttribute <MarshalAsAttribute>()).FirstOrDefault();

                OffsetInfo oi = new OffsetInfo()
                {
                    name     = x.Name,
                    type     = fldType,
                    datatype = !fldType.IsArray ? ieee ? GetDatatypeIEEE(fldType) : GetDatatype(fldType)
                : H5T.array_create(ieee ? GetDatatypeIEEE(fldType.GetElementType()) : GetDatatype(fldType.GetElementType()), (uint)fldType.GetArrayRank(), Enumerable.Range(0, fldType.GetArrayRank()).Select(i => (ulong)marshallAsAttribute.SizeConst).ToArray()),
                    size   = fldType == typeof(string) ? StringLength(x) : !fldType.IsArray ? Marshal.SizeOf(fldType) : Marshal.SizeOf(fldType.GetElementType()) * marshallAsAttribute.SizeConst,
                    offset = 0 + curSize
                };
                if (oi.datatype == H5T.C_S1)
                {
                    strtype = H5T.copy(H5T.C_S1);
                    H5T.set_size(strtype, new IntPtr(oi.size));
                    oi.datatype = strtype;
                }
                if (oi.datatype == H5T.STD_I64BE)
                {
                    oi.size = oi.size * 2;
                }
                curSize = curSize + oi.size;

                offsets.Add(oi);
            }

            /* poging om ook properties te bewaren.
             * foreach (var x in type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))
             * {
             *  bool saveProperty = false;
             *  bool isNotPublic = x.PropertyType.Attributes != TypeAttributes.Public;
             *  foreach (Attribute attr in Attribute.GetCustomAttributes(x))
             *  {
             *      var legAttr = attr as Hdf5SaveAttribute;
             *      var kind = legAttr?.SaveKind;
             *      bool saveAndPrivateProp = isNotPublic && kind == Hdf5Save.Save;
             *      bool doNotSaveProp = (kind == Hdf5Save.DoNotSave) ;
             *      if (saveAndPrivateProp && !doNotSaveProp)
             *      {
             *          saveProperty = true;
             *          continue;
             *      }
             *
             *  }
             *  if (!saveProperty)
             *      continue;
             *  var propType = x.PropertyType;
             *  OffsetInfo oi = new OffsetInfo()
             *  {
             *      name = x.Name,
             *      type = propType,
             *      datatype = ieee ? GetDatatypeIEEE(propType) : GetDatatype(propType),
             *      size = propType == typeof(string) ? stringLength(x) : Marshal.SizeOf(propType),
             *      offset = 0 + curSize
             *  };
             *  if (oi.datatype == H5T.C_S1)
             *  {
             *      strtype = H5T.copy(H5T.C_S1);
             *      H5T.set_size(strtype, new IntPtr(oi.size));
             *      oi.datatype = strtype;
             *  }
             *  if (oi.datatype == H5T.STD_I64BE)
             *      oi.size = oi.size * 2;
             *  curSize = curSize + oi.size;
             *
             *  offsets.Add(oi);
             * }*/
            H5T.close(strtype);
            return(offsets);
        }
示例#20
0
        private object GetAttributeValue(int _h5FileId, string attributeName)
        {
            H5AttributeId attId = H5A.open(_h5FileId, attributeName);

            if (attId == 0)
            {
                return(null);
            }
            H5DataTypeId typeId = 0;
            H5DataTypeId dtId   = 0;

            H5A.info_t    attInfo   = new H5A.info_t();
            H5DataSpaceId spaceId   = 0;
            H5DataTypeId  oldTypeId = 0;
            object        retObject = null;

            try
            {
                typeId = H5A.get_type(attId);
                H5A.get_info(attId, ref attInfo);
                dtId    = H5A.get_type(attId);
                spaceId = H5A.get_space(attId);
                IntPtr dataSize = H5T.get_size(dtId);
                //
                oldTypeId = typeId;
                typeId    = H5T.get_native_type(typeId, H5T.direction_t.DEFAULT);
                H5T.class_t typeClass = H5T.get_class(typeId);
                int         ndims     = H5S.get_simple_extent_ndims(spaceId);
                ulong[]     dims      = new ulong[ndims];
                H5S.get_simple_extent_dims(spaceId, dims, null);
                ulong dimSize = 1;
                if (dims.Length == 0)
                {
                    dimSize = 1;
                }
                else
                {
                    foreach (ulong dim in dims)
                    {
                        dimSize *= dim;
                    }
                }
                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(oldTypeId);
                    switch (dataSize.ToInt32())
                    {
                    case 1:
                        retObject = ReadArray <byte>(dimSize, attId, typeId);
                        break;

                    case 2:
                        switch (sign)
                        {
                        case H5T.sign_t.SGN_2:
                            retObject = ReadArray <Int16>(dimSize, attId, typeId);
                            break;

                        case H5T.sign_t.NONE:
                            retObject = ReadArray <UInt16>(dimSize, attId, typeId);
                            break;
                        }
                        break;

                    case 4:
                        switch (sign)
                        {
                        case H5T.sign_t.SGN_2:
                            retObject = ReadArray <Int32>(dimSize, attId, typeId);
                            break;

                        case H5T.sign_t.NONE:
                            retObject = ReadArray <UInt32>(dimSize, attId, typeId);
                            break;
                        }
                        break;

                    case 8:
                        switch (sign)
                        {
                        case H5T.sign_t.SGN_2:
                            retObject = ReadArray <Int64>(dimSize, attId, typeId);
                            break;

                        case H5T.sign_t.NONE:
                            retObject = ReadArray <UInt64>(dimSize, attId, typeId);
                            break;
                        }
                        break;
                    }
                    break;

                case H5T.class_t.FLOAT:
                    switch (dataSize.ToInt32())
                    {
                    case 4:
                        retObject = ReadArray <float>(dimSize, attId, typeId);
                        break;

                    case 8:
                        retObject = ReadArray <double>(dimSize, attId, typeId);
                        break;
                    }
                    break;

                case H5T.class_t.STRING:
                    ulong  size  = attInfo.data_size;
                    byte[] chars = ReadArray <byte>(size, attId, typeId);
                    retObject = Encoding.ASCII.GetString(chars);
                    break;

                default:
                    break;
                }
                return(retObject);
            }
            finally
            {
                if (spaceId != 0)
                {
                    H5S.close(spaceId);
                }
                if (attId != 0)
                {
                    H5A.close(attId);
                }
                if (oldTypeId != 0)
                {
                    H5T.close(oldTypeId);
                }
                if (typeId != 0)
                {
                    H5T.close(typeId);
                }
                if (dtId != 0)
                {
                    H5T.close(dtId);
                }
            }
        }
示例#21
0
        public Dictionary <string, string> GetDatasetAttributes(string originalDatasetName)
        {
            H5DataSetId   datasetId = 0;
            H5GroupId     groupId   = 0;
            H5DataTypeId  typeId    = 0;
            H5DataSpaceId spaceId   = 0;

            try
            {
                if (_h5FileId < 0)
                {
                    return(null);
                }
                string datasetName = GetDatasetFullNames(originalDatasetName, _h5FileId);
                if (string.IsNullOrEmpty(datasetName))
                {
                    return(null);
                }
                int groupIndex = datasetName.LastIndexOf('/');
                if (groupIndex == -1)
                {
                    datasetId = H5D.open(_h5FileId, datasetName);
                }
                else
                {
                    string groupName = datasetName.Substring(0, groupIndex + 1);
                    string dsName    = datasetName.Substring(groupIndex + 1);
                    groupId   = H5G.open(_h5FileId, groupName);
                    datasetId = H5D.open(groupId, dsName);
                }
                if (datasetId == 0)
                {
                    return(null);
                }
                Dictionary <string, string> attValues = new Dictionary <string, string>();

                typeId = H5D.get_type(datasetId);
                H5T.class_t type  = H5T.get_class(typeId);
                IntPtr      tSize = H5T.get_size(typeId);

                spaceId = H5D.get_space(datasetId);

                int     length = H5S.get_simple_extent_ndims(spaceId);
                ulong[] dims   = new ulong[length];
                H5S.get_simple_extent_dims(spaceId, dims, null);
                ulong storageSize = H5D.get_storage_size(datasetId);

                attValues.Add("DataSetName", datasetName);
                attValues.Add("DataType", type.ToString());
                attValues.Add("DataTypeSize", tSize.ToString() + "Byte");
                attValues.Add("Dims", String.Join("*", dims));
                attValues.Add("StorageSize", storageSize.ToString() + "Byte");


                //所有Attributes的键
                ArrayList arrayList = new ArrayList();
                GCHandle  handle    = GCHandle.Alloc(arrayList);
                ulong     n         = 0;
                // the callback is defined in H5ATest.cs
                H5A.operator_t cb = (int location_id, IntPtr attr_name, ref H5A.info_t ainfo, IntPtr op_data) =>
                {
                    GCHandle  hnd = (GCHandle)op_data;
                    ArrayList al  = (hnd.Target as ArrayList);
                    int       len = 0;
                    while (Marshal.ReadByte(attr_name, len) != 0)
                    {
                        ++len;
                    }
                    byte[] buf = new byte[len];
                    Marshal.Copy(attr_name, buf, 0, len);
                    al.Add(Encoding.UTF8.GetString(buf));
                    return(0);
                };
                H5A.iterate(datasetId, H5.index_t.NAME, H5.iter_order_t.NATIVE, ref n, cb, (IntPtr)handle);
                handle.Free();

                foreach (string attName in arrayList)
                {
                    attValues.Add(attName, ReadAttributeValue(datasetId, attName));
                }
                return(attValues);
            }
            finally
            {
                if (spaceId != 0)
                {
                    H5S.close(spaceId);
                }
                if (typeId != 0)
                {
                    H5T.close(typeId);
                }
                if (datasetId != 0)
                {
                    H5D.close(datasetId);
                }
                if (groupId != 0)
                {
                    H5G.close(groupId);
                }
            }
        }