Beispiel #1
0
        public static string GetAttributeValue(H5ObjectWithAttributes objectWithAttributes, string name)
        {
            if (objectWithAttributes is null)
            {
                throw new ArgumentNullException(nameof(objectWithAttributes));
            }
            if (name is null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            H5AttributeId h5AttributeId = H5A.open(objectWithAttributes, name);
            H5DataTypeId  h5DataTypeId  = H5A.getType(h5AttributeId);

            if (H5T.isVariableString(h5DataTypeId))
            {
                VariableLengthString[] variableLengthStrings = new VariableLengthString[1];
                H5A.read(h5AttributeId, h5DataTypeId, new H5Array <VariableLengthString> (variableLengthStrings));
                H5T.close(h5DataTypeId);
                H5A.close(h5AttributeId);
                return(variableLengthStrings[0].ToString());
            }
            byte[] bytes = new byte[H5T.getSize(h5DataTypeId)];
            H5A.read(h5AttributeId, h5DataTypeId, new H5Array <byte> (bytes));
            H5T.close(h5DataTypeId);
            H5A.close(h5AttributeId);
            return(Encoding.ASCII.GetString(bytes));
        }
Beispiel #2
0
        private static void WriteAttribute(H5ObjectWithAttributes target, string name, string value)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Name must not be empty (or null)", "name");
            }

            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentException("Value must not be empty or null", "value");
            }

            H5DataTypeId dtype;

            byte[] strdata = EncodeStringData(value, out dtype);

            H5DataSpaceId spaceId     = H5S.create(H5S.H5SClass.SCALAR);
            H5AttributeId attributeId = H5A.create(target, name, dtype, spaceId);

            H5A.write(attributeId, dtype, new H5Array <byte>(strdata));

            H5A.close(attributeId);
            H5T.close(dtype);
            H5S.close(spaceId);
        }
Beispiel #3
0
        private static void WriteKeywords(H5ObjectWithAttributes objectID, ISet <string> keywords)
        {
            if (keywords.Count == 0)
            {
                return;
            }

            WriteAttribute(objectID, "keywords", string.Join(",", keywords));
        }
Beispiel #4
0
        private static H5AttributeId WriteStringAttribute(H5ObjectWithAttributes fileOrdatasetId, HDFAttributeDef hDFAttributeDef, H5DataSpaceId dataSpaceId, H5DataTypeId dataTypeId)
        {
            string attValue     = Convert.ToString(hDFAttributeDef.Value);
            int    stringLength = attValue.Length;

            H5T.setSize(dataTypeId, stringLength);
            H5AttributeId attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);

            byte[] bs = Encoding.Default.GetBytes(attValue);
            H5A.write(attributeId, dataTypeId, new H5Array <byte>(bs));
            return(attributeId);
        }
Beispiel #5
0
        private static void WriteAttribute(H5ObjectWithAttributes target, string name, long value)
        {
            H5DataTypeId dtype = H5T.copy(H5T.H5Type.NATIVE_LLONG);

            H5DataSpaceId spaceId     = H5S.create(H5S.H5SClass.SCALAR);
            H5AttributeId attributeId = H5A.create(target, name, dtype, spaceId);

            H5A.write(attributeId, dtype, new H5Array <long>(new[] { value }));

            H5A.close(attributeId);
            H5T.close(dtype);
            H5S.close(spaceId);
        }
Beispiel #6
0
        private static void WriteAttribute(H5ObjectWithAttributes target, string name, double[] value)
        {
            H5DataTypeId dtype = H5T.copy(H5T.H5Type.NATIVE_DOUBLE);

            H5DataSpaceId spaceId = H5S.create_simple(1, new[] { value.LongCount() });

            H5AttributeId attributeId = H5A.create(target, name, dtype, spaceId);

            H5A.write(attributeId, dtype, new H5Array <double>(value));

            H5A.close(attributeId);
            H5T.close(dtype);
            H5S.close(spaceId);
        }
Beispiel #7
0
 public static void close(H5ObjectWithAttributes oid)
 {
     if (oid is H5DataSetId)
     {
         H5D.close(oid as H5DataSetId);
     }
     else if (oid is H5GroupId)
     {
         H5G.close(oid as H5GroupId);
     }
     else
     {
         throw new ArgumentException();
     }
 }
        private static void WriteAttribute(H5ObjectWithAttributes target, string name, long value)
        {
            H5DataTypeId dtype = H5T.copy(H5T.H5Type.NATIVE_LLONG);

            H5DataSpaceId spaceId = H5S.create(H5S.H5SClass.SCALAR);
            H5AttributeId attributeId = H5A.create(target, name, dtype, spaceId);
            H5A.write(attributeId, dtype, new H5Array<long>(new[] { value }));

            H5A.close(attributeId);
            H5T.close(dtype);
            H5S.close(spaceId);
        }
Beispiel #9
0
        public static void WriteHdfAttribute(H5ObjectWithAttributes fileOrdatasetId, HDFAttributeDef hDFAttributeDef)
        {
            H5DataSpaceId dataSpaceId = H5S.create_simple(1, new long[] { (long)hDFAttributeDef.Size });
            H5DataTypeId  dataTypeId  = null;
            H5AttributeId attributeId = null;

            try
            {
                switch (Type.GetTypeCode(hDFAttributeDef.Type))
                {
                case TypeCode.Byte:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_U8LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <byte> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Char:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_U8LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <char> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Double:
                    //dataTypeId = H5T.copy(H5T.H5Type.IEEE_F64BE);
                    dataTypeId  = H5T.copy(H5T.H5Type.IEEE_F64LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <double> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Int16:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_I16LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <Int16> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Int32:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_I32LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <Int32> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Int64:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_I64LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <Int64> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Object:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_REF_OBJ);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <object> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.Single:
                    dataTypeId  = H5T.copy(H5T.H5Type.IEEE_F32LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <Single> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.String:
                    dataTypeId  = H5T.copy(H5T.H5Type.C_S1);
                    dataSpaceId = H5S.create(H5S.H5SClass.SCALAR);
                    attributeId = WriteStringAttribute(fileOrdatasetId, hDFAttributeDef, dataSpaceId, dataTypeId);
                    break;

                case TypeCode.UInt16:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_U16LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <UInt16> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.UInt32:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_U32LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <UInt32> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;

                case TypeCode.UInt64:
                    dataTypeId  = H5T.copy(H5T.H5Type.STD_U64LE);
                    attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId);
                    WriteTAttribute <UInt64> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId);

                    break;
                }
            }
            catch (Exception ex)
            {
                int i = 9;
                int j = i;
            }
            finally
            {
                if (attributeId != null)
                {
                    H5A.close(attributeId);
                }
            }
        }
 private static void WriteAttribute(H5ObjectWithAttributes target, string name, decimal value)
 {
     WriteAttribute(target, name, (double)value);
 }
Beispiel #11
0
        /// <summary>
        /// 得到指定属性集合中指定属性名的属性值,未对异常进行处理
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        private String getAttributeValue(H5ObjectWithAttributes obj, String attributeName)
        {
            H5AttributeId attId = null;

            attId = H5A.open(obj, attributeName);
            if (attId == null)
            {
                return(null);
            }

            H5DataTypeId    typeId       = null;
            H5DataTypeId    dtId         = null;
            H5AttributeInfo attInfo      = null;
            H5DataSpaceId   spaceId      = null;
            object          attributeVal = null;

            typeId  = H5A.getType(attId);
            attInfo = H5A.getInfo(attId);
            dtId    = H5A.getType(attId);
            spaceId = H5A.getSpace(attId);
            int dataSize = H5T.getSize(dtId);

            typeId = H5T.getNativeType(typeId, H5T.Direction.DEFAULT);
            H5T.H5TClass typeClass = H5T.getClass(typeId);
            long[]       dims      = H5S.getSimpleExtentDims(spaceId);
            if (dims.Length == 0)
            {
                dims    = new long[1];
                dims[0] = 1;
            }
            switch (typeClass)
            {
            case H5T.H5TClass.STRING:
                long   size  = attInfo.dataSize;
                byte[] chars = readAttribute <byte>(size, attId, typeId);
                attributeVal = Encoding.ASCII.GetString(chars);
                break;

            case H5T.H5TClass.INTEGER:
                H5T.Sign sign = H5T.Sign.TWOS_COMPLEMENT;
                sign = H5T.getSign(typeId);
                switch (dataSize)
                {
                case 1:
                    attributeVal = readAttribute <byte>(dims[0], attId, typeId);
                    break;

                case 2:
                    switch (sign)
                    {
                    case H5T.Sign.TWOS_COMPLEMENT:
                        attributeVal = readAttribute <Int16>(dims[0], attId, typeId);
                        break;

                    case H5T.Sign.UNSIGNED:
                        attributeVal = readAttribute <UInt16>(dims[0], attId, typeId);
                        break;
                    }
                    break;

                case 4:
                    switch (sign)
                    {
                    case H5T.Sign.TWOS_COMPLEMENT:
                        attributeVal = readAttribute <Int32>(dims[0], attId, typeId);
                        break;

                    case H5T.Sign.UNSIGNED:
                        attributeVal = readAttribute <UInt32>(dims[0], attId, typeId);
                        break;
                    }
                    break;

                case 8:
                    switch (sign)
                    {
                    case H5T.Sign.TWOS_COMPLEMENT:
                        attributeVal = readAttribute <Int64>(dims[0], attId, typeId);
                        break;

                    case H5T.Sign.UNSIGNED:
                        attributeVal = readAttribute <UInt64>(dims[0], attId, typeId);
                        break;
                    }
                    break;
                }
                break;

            case H5T.H5TClass.FLOAT:
                switch (dataSize)
                {
                case 4:
                    attributeVal = readAttribute <float>(dims[0], attId, typeId);
                    break;

                case 8:
                    attributeVal = readAttribute <double>(dims[0], attId, typeId);
                    break;
                }
                break;
            }

            if (spaceId != null)
            {
                H5S.close(spaceId);
            }
            if (attId != null)
            {
                H5A.close(attId);
            }
            if (typeId != null)
            {
                H5T.close(typeId);
            }
            if (dtId != null)
            {
                H5T.close(dtId);
            }

            return(arrayToString(attributeVal));
        }
Beispiel #12
0
 private static void WriteAttribute(H5ObjectWithAttributes target, string name, double value)
 {
     WriteAttribute(target, name, new[] { value });
 }
Beispiel #13
0
 internal HDF5Attribute(H5ObjectWithAttributes _parentObjectId, string _name)
 {
     m_ParentObjectId = _parentObjectId;
     m_AttributeId    = H5A.open(_parentObjectId, _name);
     m_AttributeInfo  = H5A.getInfo(m_AttributeId);
 }
Beispiel #14
0
 private static void WriteAttribute(H5ObjectWithAttributes target, string name, decimal value)
 {
     WriteAttribute(target, name, (double)value);
 }
        private static void WriteAttribute(H5ObjectWithAttributes target, string name, double[] value)
        {
            H5DataTypeId dtype = H5T.copy(H5T.H5Type.NATIVE_DOUBLE);

            H5DataSpaceId spaceId = H5S.create_simple(1, new[] { value.LongCount() });

            H5AttributeId attributeId = H5A.create(target, name, dtype, spaceId);
            H5A.write(attributeId, dtype, new H5Array<double>(value));

            H5A.close(attributeId);
            H5T.close(dtype);
            H5S.close(spaceId);
        }
 private static void WriteAttribute(H5ObjectWithAttributes target, string name, double value)
 {
     WriteAttribute(target, name, new[] {value});
 }
        private static void WriteAttribute(H5ObjectWithAttributes target, string name, string value)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("Name must not be empty (or null)", "name");

            if (string.IsNullOrEmpty(value))
                throw new ArgumentException("Value must not be empty or null", "value");

            H5DataTypeId dtype;
            byte[] strdata = EncodeStringData(value, out dtype);

            H5DataSpaceId spaceId = H5S.create(H5S.H5SClass.SCALAR);
            H5AttributeId attributeId = H5A.create(target, name, dtype, spaceId);
            H5A.write(attributeId, dtype, new H5Array<byte>(strdata));

            H5A.close(attributeId);
            H5T.close(dtype);
            H5S.close(spaceId);
        }
        private static void WriteKeywords(H5ObjectWithAttributes objectID, ISet<string> keywords)
        {
            if (keywords.Count == 0)
                return;

            WriteAttribute(objectID, "keywords", string.Join(",", keywords));
        }
Beispiel #19
0
        //private string ArrayToString<T>(T[] v)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    //sb.Append("[");
        //    foreach (T t in v)
        //    {
        //        sb.Append(t.ToString());
        //        sb.Append(",");
        //    }
        //    if (sb.Length > 1)
        //        sb.Remove(sb.Length - 1, 1);
        //    //sb.Append("]");
        //    return sb.ToString();
        //}

        private object GetAttributeValue(H5ObjectWithAttributes obj, string attributeName)
        {
            H5AttributeId attId = null;

            attId = H5A.open(obj, attributeName);
            if (attId == null)
            {
                return(null);
            }
            H5DataTypeId    typeId    = null;
            H5DataTypeId    dtId      = null;
            H5AttributeInfo attInfo   = null;
            H5DataSpaceId   spaceId   = null;
            H5DataTypeId    oldTypeId = null;
            object          retObject = null;

            try
            {
                typeId  = H5A.getType(attId);
                attInfo = H5A.getInfo(attId);
                dtId    = H5A.getType(attId);
                spaceId = H5A.getSpace(attId);
                int dataSize = H5T.getSize(dtId);
                //
                oldTypeId = typeId;
                typeId    = H5T.getNativeType(typeId, H5T.Direction.DEFAULT);
                H5T.H5TClass typeClass = H5T.getClass(typeId);
                long[]       dims      = H5S.getSimpleExtentDims(spaceId);
                long         dimSize   = 1;
                if (dims.Length == 0)
                {
                    dimSize = 1;
                }
                else
                {
                    foreach (long dim in dims)
                    {
                        dimSize *= dim;
                    }
                }
                switch (typeClass)
                {
                case H5T.H5TClass.STRING:
                    long   size  = attInfo.dataSize;
                    byte[] chars = ReadArray <byte>(size, attId, typeId);
                    retObject = Encoding.ASCII.GetString(chars);
                    break;

                case H5T.H5TClass.INTEGER:
                    H5T.Sign sign = H5T.Sign.TWOS_COMPLEMENT;
                    sign = H5T.getSign(oldTypeId);
                    switch (dataSize)
                    {
                    case 1:
                        retObject = ReadArray <byte>(dimSize, attId, typeId);
                        break;

                    case 2:
                        switch (sign)
                        {
                        case H5T.Sign.TWOS_COMPLEMENT:
                            retObject = ReadArray <Int16>(dimSize, attId, typeId);
                            break;

                        case H5T.Sign.UNSIGNED:
                            retObject = ReadArray <UInt16>(dimSize, attId, typeId);
                            break;
                        }
                        break;

                    case 4:
                        switch (sign)
                        {
                        case H5T.Sign.TWOS_COMPLEMENT:
                            retObject = ReadArray <Int32>(dimSize, attId, typeId);
                            break;

                        case H5T.Sign.UNSIGNED:
                            retObject = ReadArray <UInt32>(dimSize, attId, typeId);
                            break;
                        }
                        break;

                    case 8:
                        switch (sign)
                        {
                        case H5T.Sign.TWOS_COMPLEMENT:
                            retObject = ReadArray <Int64>(dimSize, attId, typeId);
                            break;

                        case H5T.Sign.UNSIGNED:
                            retObject = ReadArray <UInt64>(dimSize, attId, typeId);
                            break;
                        }
                        break;
                    }
                    break;

                case H5T.H5TClass.FLOAT:
                    switch (dataSize)
                    {
                    case 4:
                        retObject = ReadArray <float>(dimSize, attId, typeId);
                        break;

                    case 8:
                        retObject = ReadArray <double>(dimSize, attId, typeId);
                        break;
                    }
                    break;
                }
                return(retObject);
            }
            finally
            {
                if (spaceId != null)
                {
                    H5S.close(spaceId);
                }
                if (attId != null)
                {
                    H5A.close(attId);
                }
                if (oldTypeId != null)
                {
                    H5T.close(oldTypeId);
                }
                if (typeId != null)
                {
                    H5T.close(typeId);
                }
                if (dtId != null)
                {
                    H5T.close(dtId);
                }
            }
        }
Beispiel #20
0
        private string ReadAttributeValue(H5ObjectWithAttributes obj, string attributeName)
        {
            object v = GetAttributeValue(obj, attributeName);

            return(TryArrayToString(v));
        }