Ejemplo n.º 1
0
        public static (long AttributeId, bool IsNew) OpenOrCreateAttribute(long locationId, string name, long attributeTypeId, ulong length, ulong[] dimensionLimitSet)
        {
            return(IOHelper.OpenOrCreateAttribute(locationId, name, attributeTypeId, () =>
            {
                long dataspaceId = -1;
                long attributeId = -1;

                try
                {
                    dataspaceId = H5S.create_simple(1, new ulong[] { length }, dimensionLimitSet);
                    attributeId = H5A.create(locationId, name, attributeTypeId, dataspaceId);

                    if (H5I.is_valid(attributeId) <= 0)
                    {
                        throw new Exception(ErrorMessage.IOHelper_CouldNotOpenOrCreateAttribute);
                    }
                }
                finally
                {
                    if (H5I.is_valid(dataspaceId) > 0)
                    {
                        H5S.close(dataspaceId);
                    }
                }

                return attributeId;
            }));
        }
Ejemplo n.º 2
0
        public static void PrepareAttribute <T>(long locationId, string name, T[] valueSet, ulong[] dimensionLimitSet, bool initializeAttribute, [CallerMemberName()] string callerMemberName = "")
        {
            Contract.Requires(valueSet != null, nameof(valueSet));

            long fileId      = -1;
            long typeId      = -1;
            long attributeId = -1;

            ulong[] dimensionSet;
            bool    isNew;

            Type elementType;

            dimensionSet = new ulong[] { 0 };
            elementType  = typeof(T);

            try
            {
                fileId = H5I.get_file_id(locationId);

                // create attribute
                typeId = TypeConversionHelper.GetHdfTypeIdFromType(fileId, elementType);
                (attributeId, isNew) = IOHelper.OpenOrCreateAttribute(locationId, name, typeId, (ulong)valueSet.LongLength, dimensionLimitSet);

                // write attribute data (regenerate attribute if necessary)
                if (initializeAttribute)
                {
                    ulong[] oldValueSetCount;

                    if (!isNew && callerMemberName != nameof(IOHelper.PrepareAttribute))
                    {
                        oldValueSetCount = IOHelper.PrepareAttributeValueSet(attributeId, ref valueSet, false);
                    }
                    else
                    {
                        oldValueSetCount = new ulong[1] {
                            (ulong)valueSet.Count()
                        }
                    };

                    if (valueSet.Count() == (int)oldValueSetCount[0])
                    {
                        IOHelper.Write(attributeId, valueSet, DataContainerType.Attribute);
                    }
                    else
                    {
                        H5A.close(attributeId);
                        H5A.delete(locationId, name);

                        IOHelper.PrepareAttribute(locationId, name, valueSet, dimensionLimitSet, true);
                    }
                }
            }
            finally
            {
                if (H5I.is_valid(attributeId) > 0)
                {
                    H5A.close(attributeId);
                }
                if (H5I.is_valid(typeId) > 0)
                {
                    H5T.close(typeId);
                }
                if (H5I.is_valid(fileId) > 0)
                {
                    H5F.close(fileId);
                }
            }
        }