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

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

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

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

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

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

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

                return(ok);
            }
        }
示例#3
0
        private static bool IsHDF5String(hid_t fileLoc, string name)
        {
            hid_t dset = H5D.open(fileLoc, name);
            hid_t type = H5D.get_type(dset);

            H5T.class_t cl = H5T.get_class(type);


            return(cl == H5T.class_t.STRING);
        }
示例#4
0
        public static T ReadScalarNumericAttribute <T>(hid_t attribute) where T : struct
        {
            var space = H5A.get_space(attribute);

            if (space < 0)
            {
                throw new Exception("Failed to get space");
            }

            if (H5S.get_simple_extent_type(space) != H5S.class_t.SCALAR)
            {
                H5S.close(space);
                throw new Exception("Not a scalar data space");
            }

            H5S.close(space);

            var type = H5A.get_type(attribute);

            if (type < 0)
            {
                throw new Exception("Failed to get type");
            }

            var typeClass = H5T.get_class(type);

            H5T.close(type);
            if (typeClass != H5T.class_t.INTEGER && typeClass != H5T.class_t.FLOAT)
            {
                throw new Exception("Not an integral or floating point data type");
            }

            // TODO:
            // Check if value can be cast to type of this attribute.
            //
            // TODO:
            // T[] value = new T[1];
            // H5A.read(attribute, NumericTypeToHDF5Type<T>(), new PinnedObject(value));
            object boxedValue = new T();

            if (H5A.read(attribute, NumericTypeToHDF5Type <T>(), new PinnedObject(boxedValue)) < 0)
            {
                throw new Exception("Failed to read attribute");
            }

            return((T)boxedValue);
        }
示例#5
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);
        }
示例#6
0
        public static bool WriteScalarNumericAttribute <T>(hid_t attribute, T value, hid_t type) where T : struct
        {
            var space = H5A.get_space(attribute);

            if (space < 0)
            {
                throw new Exception("Failed to get space");
            }

            if (H5S.get_simple_extent_type(space) != H5S.class_t.SCALAR)
            {
                H5S.close(space);
                throw new Exception("Not a scalar data space");
            }

            H5S.close(space);

            var attributeType = H5A.get_type(attribute);

            if (attributeType < 0)
            {
                throw new Exception("Failed to get type");
            }

            var attributeTypeClass = H5T.get_class(attributeType);

            H5T.close(attributeType);
            if (attributeTypeClass != H5T.class_t.INTEGER && attributeTypeClass != H5T.class_t.FLOAT)
            {
                throw new Exception("Not an integral or floating point data type");
            }

            // TODO:
            // Check if value can be cast to type of this attribute.
            object boxedValue = value;

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

            return(true);
        }
示例#7
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);
        }
示例#8
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);
        }
        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'));
        }
        public static bool WriteFixedStringAttribute(hid_t attribute, string value, bool utf8)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            var space = H5A.get_space(attribute);

            if (space < 0)
            {
                throw new Exception("Failed to get data space");
            }

            if (H5S.get_simple_extent_type(space) != H5S.class_t.SCALAR)
            {
                H5S.close(space);
                throw new Exception("Not a scalar data space");
            }

            H5S.close(space);

            var type = H5A.get_type(attribute);

            if (type < 0)
            {
                throw new Exception("Failed to get type");
            }

            var typeClass = H5T.get_class(type);

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

            var isVariableString = H5T.is_variable_str(type);

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

            if (isVariableString > 0)
            {
                throw new Exception("Not a fixed string attribute");
            }

            // TODO: Do we really need to create a new memory type here?
            var bytes = value.ToBytes(utf8);

            var memType = CreateFixedStringType(bytes, utf8);

            // TODO: type or memType here?
            H5A.write(attribute, memType, new PinnedObject(bytes));

            H5T.close(memType);

            return(true);
        }
示例#11
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);
                }
            }
        }
示例#12
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);
                }
            }
        }
示例#13
0
        private static List <HDF5info> ScanInfo(hid_t gId, List <HDF5info> fields, string fullname)
        {
            IntPtr MAX_NAME = new IntPtr(1024);

            System.Text.StringBuilder group_name  = new System.Text.StringBuilder();
            System.Text.StringBuilder member_name = new System.Text.StringBuilder();
            IntPtr len = H5I.get_name(gId, group_name, MAX_NAME);

            hsize_t nobj = new hsize_t();

            H5G.get_num_objs(gId, ref nobj);

            for (int i = 0; i < (int)nobj; i++)
            {
                member_name          = new System.Text.StringBuilder();
                member_name.Capacity = 1024;
                IntPtr len2    = H5G.get_objname_by_idx(gId, (ulong)i, member_name, MAX_NAME);
                int    objtype = H5G.get_objtype_by_idx(gId, (ulong)i);

                if (objtype == 0) //group
                {
                    hid_t gId2 = H5G.open(gId, member_name.ToString());
                    fields = ScanInfo(gId2, fields, string.Format("{0}/{1}", fullname, member_name));
                }
                else if (objtype == 1) //Object is a dataset.
                {
                    HDF5info hDF5Info = new HDF5info();
                    hid_t    dset     = H5D.open(gId, member_name.ToString());
                    hid_t    fspace   = H5D.get_space(dset);

                    hid_t count = H5S.get_simple_extent_ndims(fspace);
                    hid_t type  = H5D.get_type(dset);


                    hDF5Info.HDFclass = getH5Tstring(type);
                    hDF5Info.field    = string.Format("{0}/{1}", fullname, member_name);
                    if (H5T.get_class(type) == H5T.class_t.STRING)
                    {
                        hDF5Info.description = nirs.io.ReadDataString(gId, string.Format("{0}", member_name));
                    }
                    else if (H5T.get_class(type) == H5T.class_t.FLOAT |
                             H5T.get_class(type) == H5T.class_t.INTEGER)
                    {
                        hsize_t[] dims    = new hsize_t[count];
                        hsize_t[] maxdims = new hsize_t[count];
                        H5S.get_simple_extent_dims(fspace, dims, maxdims);
                        if (dims.Length == 1 & dims[0] == 1)
                        {
                            var val = nirs.io.ReadDataValue(gId, string.Format("{0}", member_name));
                            hDF5Info.description = string.Format("{0}", val);
                        }
                        else if (dims.Length == 1 & dims[0] > 1)
                        {
                            hDF5Info.description = string.Format("Vector <{0} x 1>", dims[0]);
                        }
                        else
                        {
                            hDF5Info.description = string.Format("Array <{0} x {1}>", dims[0], dims[1]);

                            if (hDF5Info.field.Contains("dataTimeSeries") & dims[0] > dims[1])
                            {
                                hDF5Info.description += " TRANSPOSE WARNING ";
                            }
                            if (hDF5Info.field.Contains("Pos") & dims[1] != 3)
                            {
                                hDF5Info.description += " TRANSPOSE WARNING ";
                            }
                            if (hDF5Info.field.Contains("stim") & hDF5Info.field.Contains("data") & dims[1] != 3)
                            {
                                hDF5Info.description += " TRANSPOSE WARNING ";
                            }
                        }
                    }
                    else
                    {
                        hDF5Info.description = "";
                    }
                    fields.Add(hDF5Info);
                }
            }


            H5G.close(gId);


            return(fields);
        }
示例#14
0
        private static string getH5Tstring(hid_t typeT)
        {
            // TODO

            return(String.Format("{0}", H5T.get_class(typeT)));
        }
示例#15
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);
        }
示例#16
0
        private List <float[]> ReadDataSetToSingle(AbstractWarpDataset srcbandpro, int[] bands)
        {
            List <float[]> datas    = new List <float[]>();
            var            prjBands = PrjBandTable.GetPrjBands(srcbandpro);
            H5ID           h5FileId = H5F.open(srcbandpro.fileName, H5F.ACC_RDONLY);

            foreach (int index in bands)
            {
                //Single[] data = new Single[srcSize.Width * srcSize.Height];
                var    bandIndex = prjBands[index - 1].DataSetIndex;
                string dsName    = "CALChannel" + bandIndex.ToString("00");


                H5ID datasetId = H5D.open(h5FileId, dsName);
                if (datasetId <= 0)
                {
                    throw new ArgumentNullException(string.Format("FY4辐射定标,未找到名称为{0}的数据.",
                                                                  "CALChannel" + index.ToString("00")));
                }
                H5ID typeId  = H5D.get_type(datasetId);
                H5ID spaceId = H5D.get_space(datasetId);
                if (H5T.get_class(typeId) == H5T.class_t.FLOAT)
                {
                    int     rank    = H5S.get_simple_extent_ndims(spaceId);
                    ulong[] dims    = new ulong[rank];
                    ulong[] maxDims = new ulong[rank];
                    H5S.get_simple_extent_dims(spaceId, dims, maxDims);

                    float[]  buffer = new float[dims[0]];
                    GCHandle hnd    = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                    H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());
                    hnd.Free();
                    if (buffer.Any(t => t > Math.Pow(10, 10) || t < -Math.Pow(10, 10)))
                    {
                        for (int i = 0; i < buffer.Length; i++)
                        {
                            var t = BitConverter.GetBytes(buffer[i]);
                            Array.Reverse(t);
                            buffer[i] = BitConverter.ToSingle(t, 0);
                        }
                    }

                    datas.Add(buffer);
                }

                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(datas);
        }
示例#17
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);
        }
示例#18
0
        private int depth_first(HObject parentObject, int nTotal)
        {
            Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject}): start");

            int    nelems;
            string fullPath = null;
            string ppath    = null;
            long   gid      = -1;

            H5Group pgroup = (H5Group)parentObject;

            ppath = pgroup.getPath();

            if (ppath == null)
            {
                fullPath = HObject.SEPARATOR;
            }
            else
            {
                fullPath = ppath + pgroup.getName() + HObject.SEPARATOR;
            }

            nelems = 0;
            try
            {
                gid = pgroup.open();
                var info = new H5G.info_t();
                H5G.get_info(gid, ref info);
                nelems = (int)info.nlinks;
            }
            catch (Exception ex)
            {
                nelems = -1;
                Hdf5Utils.LogError?.Invoke($"depth_first({parentObject}): H5Gget_info(gid {gid}) failure: {ex}");
            }

            if (nelems <= 0)
            {
                pgroup.close(gid);
                Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject}): nelems <= 0");
                Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject}): finish");
                return(nTotal);
            }

            // since each call of H5.H5Gget_objname_by_idx() takes about one second.
            // 1,000,000 calls take 12 days. Instead of calling it in a loop,
            // we use only one call to get all the information, which takes about
            // two seconds
            int[] objTypes = new int[nelems];
            //long[] fNos = new long[nelems];
            //long[] objRefs = new long[nelems];
            string[]     objNames = new string[nelems];
            H5L.info_t[] infos    = new H5L.info_t[nelems];
            try
            {
                int i = 0;
                int callback(long group, IntPtr name, ref H5L.info_t info, IntPtr op_data)
                {
                    string realName = Marshal.PtrToStringAuto(name);

                    objTypes[i] = (int)info.type;
                    objNames[i] = realName;
                    infos[i]    = info;

                    return(i++);
                }

                ulong pos = 0;
                H5L.iterate(gid, indexType, indexOrder, ref pos, callback, IntPtr.Zero);

                //for (ulong i = 0; i < (ulong)nelems; i++)
                //{


                //    H5G.info_t info = new H5G.info_t();
                //    H5G.get_info_by_idx(fid, fullPath, indexType, indexOrder, i, ref info);
                //    infos[i] = info;


                //}

                // H5.H5Gget_obj_info_full(fid, fullPath, objNames, objTypes, null, fNos, objRefs, indexType, indexOrder);
            }
            catch (Exception ex)
            {
                Hdf5Utils.LogError?.Invoke($"depth_first({parentObject}): failure: {ex}");
                Hdf5Utils.LogError?.Invoke($"depth_first({parentObject}): finish");
                return(nTotal);
            }

            int nStart = getStartMembers();
            int nMax   = getMaxMembers();

            string obj_name;
            int    obj_type;

            // Iterate through the file to see members of the group
            for (int i = 0; i < nelems; i++)
            {
                obj_name = objNames[i];
                obj_type = objTypes[i];
                Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject}): obj_name={obj_name}, obj_type={obj_type}");

                if (obj_name == null)
                {
                    Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject}): continue after null obj_name");
                    continue;
                }

                nTotal++;

                if (nMax > 0)
                {
                    if ((nTotal - nStart) >= nMax)
                    {
                        break; // loaded enough objects
                    }
                }

                bool skipLoad = (nTotal > 0) && (nTotal < nStart);

                // create a new group
                if (obj_type == HDF5Constants.H5O_TYPE_GROUP)
                {
                    H5Group g = new H5Group(this, obj_name, fullPath, pgroup);

                    pgroup.addToMemberList(g);

                    // detect and stop loops
                    // a loop is detected if there exists object with the same
                    // object ID by tracing path back up to the root.
                    bool    hasLoop = false;
                    H5Group tmpObj  = (H5Group)parentObject;

                    while (tmpObj != null)
                    {
                        if (tmpObj.equalsOID(new IntPtr((int)infos[i].u.address)) && (tmpObj.getPath() != null))
                        {
                            hasLoop = true;
                            break;
                        }
                        else
                        {
                            tmpObj = (H5Group)tmpObj.getParent();
                        }
                    }

                    // recursively go through the next group
                    // stops if it has loop.
                    if (!hasLoop)
                    {
                        nTotal = depth_first(g, nTotal);
                    }
                }
                else if (skipLoad)
                {
                    continue;
                }
                else if (obj_type == HDF5Constants.H5O_TYPE_DATASET)
                {
                    long        did    = -1;
                    long        tid    = -1;
                    H5T.class_t tclass = H5T.class_t.NO_CLASS;
                    try
                    {
                        did = H5D.open(fid, fullPath + obj_name, HDF5Constants.H5P_DEFAULT);
                        if (did >= 0)
                        {
                            tid = H5D.get_type(did);

                            tclass = H5T.get_class(tid);
                            if ((tclass == HDF5Constants.H5T_ARRAY) || (tclass == HDF5Constants.H5T_VLEN))
                            {
                                // for ARRAY, the type is determined by the base type
                                long btid = H5T.get_super(tid);

                                tclass = H5T.get_class(btid);

                                try
                                {
                                    H5T.close(btid);
                                }
                                catch (Exception ex)
                                {
                                    Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject})[{i}] dataset {obj_name} H5Tclose(btid {btid}) failure: {ex}");
                                }
                            }
                        }
                        else
                        {
                            Hdf5Utils.LogError?.Invoke($"depth_first({parentObject})[{i}] {obj_name} dataset open failure");
                        }
                    }
                    catch (Exception ex)
                    {
                        Hdf5Utils.LogError?.Invoke($"depth_first({parentObject})[{i}] {obj_name} dataset access failure: {ex}");
                    }
                    finally
                    {
                        try
                        {
                            H5T.close(tid);
                        }
                        catch (Exception ex)
                        {
                            Hdf5Utils.LogError?.Invoke($"depth_first({parentObject})[{i}] dataset {obj_name} H5Tclose(tid {tid}) failure: {ex}");
                        }
                        try
                        {
                            H5D.close(did);
                        }
                        catch (Exception ex)
                        {
                            Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject})[{i}] dataset {obj_name} H5Dclose(did {did}) failure: {ex}");
                        }
                    }
                    //todo:
                    //Dataset d = null;
                    //if (tclass == HDF5Constants.H5T_COMPOUND)
                    //{
                    //    // create a new compound dataset
                    //    d = new H5CompoundDS(this, obj_name, fullPath, oid); // deprecated!
                    //}
                    //else
                    //{
                    //    // create a new scalar dataset
                    //    d = new H5ScalarDS(this, obj_name, fullPath, oid); // deprecated!
                    //}

                    // pgroup.addToMemberList(d);
                }
                else if (obj_type == HDF5Constants.H5O_TYPE_NAMED_DATATYPE)
                {
                    //Datatype t = new H5Datatype(this, obj_name, fullPath, oid); // deprecated!

                    //pgroup.addToMemberList(t);
                }
                else if (obj_type == HDF5Constants.H5O_TYPE_UNKNOWN)
                {
                    //H5Link link = new H5Link(this, obj_name, fullPath, oid);

                    // pgroup.addToMemberList(link);
                    continue; // do the next one, if the object is not identified.
                }
            } // ( i = 0; i < nelems; i++)

            pgroup.close(gid);

            Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject}): finish");
            return(nTotal);
        }