Exemple #1
0
 public Hdf5ElementBase(string name, Hdf5ElementType type, Hdf5ElementBase parent, long id, bool isLazyLoading)
 {
     Name          = name;
     Type          = type;
     Parent        = parent;
     IsLazyLoading = isLazyLoading;
     Id            = id;
 }
Exemple #2
0
        public static bool ItemExists(long groupId, string groupName, Hdf5ElementType type)
        {
            switch (type)
            {
            case Hdf5ElementType.Group:
            case Hdf5ElementType.Dataset:
                return(H5L.exists(groupId, NormalizedName(groupName)) > 0);

            case Hdf5ElementType.Attribute:
                return(H5A.exists(groupId, NormalizedName(groupName)) > 0);

            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }
Exemple #3
0
        private static long GetId(long parentId, string name, long dataType, long spaceId, Hdf5ElementType type)
        {
            string normalizedName = NormalizedName(name);
            bool   exists         = ItemExists(parentId, normalizedName, type);

            if (exists)
            {
                LogMessage($"{normalizedName} already exists", Hdf5LogLevel.Debug);
                if (!Hdf5.Settings.OverrideExistingData)
                {
                    if (Hdf5.Settings.ThrowOnError)
                    {
                        throw new Hdf5Exception($"{normalizedName} already exists");
                    }

                    return(-1);
                }
            }

            var datasetId = -1L;

            switch (type)
            {
            case Hdf5ElementType.Unknown:
                break;

            case Hdf5ElementType.Group:
            case Hdf5ElementType.Dataset:
                if (exists)
                {
                    H5L.delete(parentId, normalizedName);
                    // datasetId = H5D.open(parentId, normalizedName);
                }
                datasetId = H5D.create(parentId, normalizedName, dataType, spaceId);
                break;

            case Hdf5ElementType.Attribute:
                if (exists)
                {
                    H5A.delete(parentId, normalizedName);
                }

                datasetId = H5A.create(parentId, normalizedName, dataType, spaceId);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }

            if (datasetId == -1L)
            {
                string error = $"Unable to create dataset for {normalizedName}";
                LogMessage($"{normalizedName} already exists", Hdf5LogLevel.Error);
                if (Hdf5.Settings.ThrowOnError)
                {
                    throw new Hdf5Exception(error);
                }
            }
            return(datasetId);
        }
Exemple #4
0
 public Hdf5Element(string name, Hdf5ElementType type, Hdf5ElementBase parent, long id, bool isLazyLoading) : base(name, type, parent, id, isLazyLoading)
 {
     Children = new List <Hdf5Element>();
 }
Exemple #5
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);
        }
Exemple #6
0
 public Hdf5Element(string name, Hdf5ElementType type, Hdf5ElementBase parent, List <Hdf5AttributeElement> attributes, long id, bool isLazyLoading) : base(name, type, parent, id, isLazyLoading)
 {
     Children   = new List <Hdf5Element>();
     Attributes = attributes.ToList();
 }