Exemplo n.º 1
0
        public void H5Fget_nameTest2()
        {
            StringBuilder nameBuilder = new StringBuilder(256);

            Assert.IsTrue(
                H5F.get_name(Utilities.RandomInvalidHandle(), nameBuilder,
                             new IntPtr(nameBuilder.Capacity)).ToInt32() < 0);
        }
Exemplo n.º 2
0
        public void H5Fget_nameTest1()
        {
            StringBuilder nameBuilder = new StringBuilder(256);

            Assert.IsTrue(
                H5F.get_name(m_v0_test_file, nameBuilder,
                             new IntPtr(nameBuilder.Capacity)).ToInt32() >= 0);

            string name = nameBuilder.ToString();

            // names should match
            Assert.AreEqual(m_v0_test_file_name, name);

            Assert.IsTrue(
                H5F.get_name(m_v2_test_file, nameBuilder,
                             new IntPtr(nameBuilder.Capacity)).ToInt32() >= 0);

            name = nameBuilder.ToString();
            // names should match
            Assert.AreEqual(m_v2_test_file_name, name);
        }
Exemplo n.º 3
0
        public static List <CampaignInfo> InternalUpdateCampaignInfoSet(long fileId, bool isLazyLoading, List <CampaignInfo> campaignInfoSet = null, string campaignGroupPath = "")
        {
            ulong idx;

            idx = 0;

            if (campaignInfoSet == null)
            {
                campaignInfoSet = new List <CampaignInfo>();
            }

            GeneralHelper.SuppressErrors(() => H5L.iterate(fileId, H5.index_t.NAME, H5.iter_order_t.INC, ref idx, Callback, Marshal.StringToHGlobalAnsi("/")));

            return(campaignInfoSet);

            int Callback(long campaignGroupId, IntPtr intPtrName, ref H5L.info_t info, IntPtr intPtrUserData)
            {
                ulong idx2 = 0;

                long groupId   = -1;
                long datasetId = -1;

                int level;
                int formatVersion;

                string name;
                string fullName;
                string userData;

                H5O.type_t    objectType;
                CampaignInfo  currentCampaignInfo;
                StringBuilder filePath;
                DateTime      dateTime;

                //
                if (H5A.exists(fileId, "format_version") > 0) // raw file
                {
                    formatVersion = IOHelper.ReadAttribute <int>(fileId, "format_version").First();
                }
                else // virtual file
                {
                    formatVersion = -1;
                }

                filePath = new StringBuilder(260);
                H5F.get_name(fileId, filePath, new IntPtr(260));
                dateTime = DateTime.ParseExact(IOHelper.ReadAttribute <string>(fileId, "date_time").First(), "yyyy-MM-ddTHH-mm-ssZ", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

                name     = Marshal.PtrToStringAnsi(intPtrName);
                userData = Marshal.PtrToStringAnsi(intPtrUserData);
                fullName = GeneralHelper.CombinePath(userData, name);
                level    = userData.Split("/".ToArray()).Count();

                // this is necessary, since H5Oget_info_by_name is slow because it wants verbose object header data
                // and H5G_loc_info is not directly accessible
                // only chance is to modify source code (H5Oget_info_by_name)
                datasetId = H5D.open(campaignGroupId, name);

                if (H5I.is_valid(datasetId) > 0)
                {
                    objectType = H5O.type_t.DATASET;
                }
                else
                {
                    groupId = H5G.open(campaignGroupId, name);

                    if (H5I.is_valid(groupId) > 0)
                    {
                        objectType = H5O.type_t.GROUP;
                    }
                    else
                    {
                        objectType = H5O.type_t.UNKNOWN;
                    }
                }

                switch (level)
                {
                case 1:
                case 2:
                    break;

                case 3:

                    if (objectType == H5O.type_t.GROUP)
                    {
                        if (!string.IsNullOrWhiteSpace(campaignGroupPath) && fullName != campaignGroupPath)
                        {
                            return(0);
                        }

                        currentCampaignInfo = campaignInfoSet.FirstOrDefault(campaignInfo => campaignInfo.Name == fullName);

                        if (currentCampaignInfo == null)
                        {
                            currentCampaignInfo = new CampaignInfo(fullName, null, isLazyLoading);
                            campaignInfoSet.Add(currentCampaignInfo);
                        }

                        currentCampaignInfo.Update(new FileContext(fileId, formatVersion, dateTime, filePath.ToString()));
                    }

                    break;
                }

                if (objectType == H5O.type_t.GROUP && level < 3)
                {
                    H5L.iterate(groupId, H5.index_t.NAME, H5.iter_order_t.INC, ref idx2, Callback, Marshal.StringToHGlobalAnsi(fullName));
                }

                // clean up
                if (H5I.is_valid(groupId) > 0)
                {
                    H5G.close(groupId);
                }
                if (H5I.is_valid(datasetId) > 0)
                {
                    H5D.close(datasetId);
                }

                return(0);
            }
        }
Exemplo n.º 4
0
        internal static (List <Hdf5Element> tree, List <Hdf5Element> flat) ReadFileStructure(string fileName)
        {
            var attributes = new List <Hdf5AttributeElement>();
            var elements   = new List <Hdf5Element>();
            var structure  = new List <Hdf5Element>();

            if (!File.Exists(fileName))
            {
                Hdf5Utils.LogError?.Invoke($"File {fileName} does not exist");
                return(structure, elements);
            }

            long fileId = H5F.open(fileName, H5F.ACC_RDONLY);

            if (fileId < 0)
            {
                Hdf5Utils.LogError?.Invoke($"Could not open file {fileName}");
                return(structure, elements);
            }

            try
            {
                StringBuilder filePath = new StringBuilder(260);
                H5F.get_name(fileId, filePath, new IntPtr(260));
                ulong idx            = 0;
                bool  reEnableErrors = Settings.ErrorLoggingEnable;

                Settings.EnableErrorReporting(false);
                H5L.iterate(fileId, H5.index_t.NAME, H5.iter_order_t.INC, ref idx, Callback,
                            Marshal.StringToHGlobalAnsi("/"));
                Settings.EnableErrorReporting(reEnableErrors);
            }
            catch (Exception e)
            {
                Hdf5Utils.LogError?.Invoke($"Error during reading file structure of {fileName}. Error:{e}");
            }
            finally
            {
                if (fileId > 0)
                {
                    H5F.close(fileId);
                }
            }

            int Callback(long elementId, IntPtr intPtrName, ref H5L.info_t info, IntPtr intPtrUserData)
            {
                ulong idx2      = 0;
                long  groupId   = -1;
                long  datasetId = -1;

                H5O.type_t      objectType;
                var             name        = Marshal.PtrToStringAnsi(intPtrName);
                var             userData    = Marshal.PtrToStringAnsi(intPtrUserData);
                var             fullName    = CombinePath(userData, name);
                Hdf5ElementType elementType = Hdf5ElementType.Unknown;

                // this is necessary, since H5Oget_info_by_name is slow because it wants verbose object header data
                // and H5G_loc_info is not directly accessible
                // only chance is to modify source code (H5Oget_info_by_name)
                groupId = (H5L.exists(elementId, name) >= 0) ? H5G.open(elementId, name) : -1L;
                if (H5I.is_valid(groupId) > 0)
                {
                    objectType  = H5O.type_t.GROUP;
                    elementType = Hdf5ElementType.Group;
                }
                else
                {
                    datasetId = H5D.open(elementId, name);
                    if ((H5I.is_valid(datasetId) > 0))
                    {
                        objectType  = H5O.type_t.DATASET;
                        elementType = Hdf5ElementType.Dataset;
                    }
                    else
                    {
                        objectType  = H5O.type_t.UNKNOWN;
                        elementType = Hdf5ElementType.Group;
                    }
                }


                var parent = elements.FirstOrDefault(e =>
                {
                    var index   = fullName.LastIndexOf("/", StringComparison.Ordinal);
                    var partial = fullName.Substring(0, index);
                    return(partial.Equals(e.Name));
                });

                if (parent == null)
                {
                    var element = new Hdf5Element(fullName, elementType, null, attributes, elementId, false);
                    attributes.Clear();
                    structure.Add(element);
                    elements.Add(element);
                }
                else
                {
                    var element = new Hdf5Element(fullName, elementType, parent, attributes, elementId, false);
                    attributes.Clear();
                    parent.AddChild(element);
                    elements.Add(element);
                }

                if (objectType == H5O.type_t.GROUP)
                {
                    H5L.iterate(groupId, H5.index_t.NAME, H5.iter_order_t.INC, ref idx2, Callback,
                                Marshal.StringToHGlobalAnsi(fullName));
                }

                // clean up
                if (H5I.is_valid(groupId) > 0)
                {
                    H5G.close(groupId);
                }

                if (H5I.is_valid(datasetId) > 0)
                {
                    H5D.close(datasetId);
                }

                return(0);
            }

            int AttributeCallback(long location_id, IntPtr attr_name, ref H5A.info_t ainfo, IntPtr op_data)
            {
                var name = Marshal.PtrToStringAnsi(attr_name);

                var att = ReadStringAttributes(location_id, name, String.Empty);

                if (att.success && att.items.Any())
                {
                    attributes.Add(new Hdf5AttributeElement(name, att.items.First()));
                }


                //var typeId = H5A.get_type(location_id);
                //H5T.class_t classType = H5T.get_class(location_id);
                //var cset = H5T.get_cset(location_id);
                //var padd = H5T.get_strpad(location_id);
                return(0);
            }

            return(structure, elements);
        }