コード例 #1
0
ファイル: WarpDataset.cs プロジェクト: v2535v/FYProjection
        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
        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);
        }
コード例 #3
0
ファイル: WarpDataset.cs プロジェクト: v2535v/FYProjection
        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);
        }
コード例 #4
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);
                }
            }
        }
コード例 #5
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);
                }
            }
        }
コード例 #6
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);
        }