예제 #1
0
        private void createHD5DataObject(H5GroupId h5GroupId, string pathName, ref HD5DataSetObject dataObject)
        {
            H5DataSetId   datasetid  = null;
            H5DataSpaceId spaceid    = null;
            H5DataTypeId  dataTypeid = null;

            try
            {
                dataObject.GroupId     = h5GroupId;
                datasetid              = H5D.open(h5GroupId, pathName);
                dataObject.DatasetID   = datasetid;
                dataObject.DatasetName = pathName;
                spaceid = H5D.getSpace(datasetid);
                var dims = H5S.getSimpleExtentDims(spaceid);
                dataTypeid     = H5D.getType(datasetid);
                dataObject.Dim = dims.Length;
                HDF5DotNet.H5T.H5TClass classType = H5T.getClass(dataTypeid);
                int      size = H5T.getSize(dataTypeid);
                H5T.Sign sign = H5T.Sign.TWOS_COMPLEMENT;
                if (classType == H5T.H5TClass.INTEGER)
                {
                    sign = H5T.getSign(dataTypeid);
                }
                //var rank = H5S.getSimpleExtentNDims(space);
                //var statu = H5S.getSimpleExtentDims(space);
                Boolean bString = H5T.isVariableString(dataTypeid);
                //String name = H5T.getMemberName(dataType, 0);
                // var type2 = H5T.getNativeType(dataType, H5T.Direction.DEFAULT);
                Type type = getTypeof(classType, size, sign);
                dataObject.DataType = type;
                dataObject.Data     = readData(dataObject);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally{
                if (datasetid != null)
                {
                    H5D.close(datasetid);
                }
                if (spaceid != null)
                {
                    H5S.close(spaceid);
                }
                if (dataTypeid != null)
                {
                    H5T.close(dataTypeid);
                }
            }
        }
예제 #2
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));
        }
예제 #3
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);
                }
            }
        }
예제 #4
0
        private void ReadOldDataSetData(string dataSetName, int bandIndex, out int bandWidth, out int bandHeight, out enumDataType dataType, out object retObject)
        {
            bandHeight = bandWidth = 0;
            dataType   = enumDataType.UInt16;
            retObject  = null;
            H5FileId      _h5FileId = null;
            H5DataSpaceId spaceid   = null;
            H5DataSetId   dataSetId = null;

            try
            {
                _h5FileId = H5F.open(fileName, H5F.OpenMode.ACC_RDONLY);
                //先找出含有指定波段的数据集
                dataSetId = H5D.open(_h5FileId, dataSetName);
                spaceid   = H5D.getSpace(dataSetId);
                long[] dims = H5S.getSimpleExtentDims(spaceid);  //得到数据数组的大小,比如[3,1800,2048]
                int    rank = H5S.getSimpleExtentNDims(spaceid); //得到数据数组的维数,比如3
                int    size = 0;
                if (rank == 1)
                {
                    bandHeight = bandWidth = 1;
                    size       = bandWidth * bandHeight * rank;
                }
                else if (rank == 2)
                {
                    bandWidth  = Convert.ToInt32(dims[0]);
                    bandHeight = Convert.ToInt32(dims[1]);
                    size       = bandWidth * bandHeight;
                }
                else if (rank == 3)
                {
                    List <long> r = dims.ToList <long>();
                    r.Sort();
                    long[] temp = r.ToArray();

                    bandWidth  = Convert.ToInt32(temp[1]);
                    bandHeight = Convert.ToInt32(temp[2]);
                    size       = bandWidth * bandHeight * Convert.ToInt32(temp[0]);
                }
                int          outSize   = bandWidth * bandHeight;
                H5DataTypeId typeId    = H5D.getType(dataSetId);
                H5T.H5TClass typeClass = H5T.getClass(typeId);//得到数据集的类型
                int          dataSize  = H5T.getSize(typeId);
                H5DataTypeId newTypeId = null;
                switch (typeClass)
                {
                case H5T.H5TClass.INTEGER:
                    H5T.Sign sign = H5T.Sign.TWOS_COMPLEMENT;
                    sign = H5T.getSign(typeId);
                    switch (dataSize)
                    {
                    case 1:
                        newTypeId = H5T.copy(H5T.H5Type.NATIVE_B8);
                        retObject = ReadArray <byte>(size, dataSetId, newTypeId, bandIndex, outSize);
                        dataType  = enumDataType.Byte;
                        break;

                    case 2:
                        switch (sign)
                        {
                        case H5T.Sign.TWOS_COMPLEMENT:
                            newTypeId = H5T.copy(H5T.H5Type.NATIVE_SHORT);
                            retObject = ReadArray <Int16>(size, dataSetId, newTypeId, bandIndex, outSize);
                            dataType  = enumDataType.Int16;
                            break;

                        case H5T.Sign.UNSIGNED:
                            newTypeId = H5T.copy(H5T.H5Type.NATIVE_USHORT);
                            retObject = ReadArray <UInt16>(size, dataSetId, newTypeId, bandIndex, outSize);
                            dataType  = enumDataType.UInt16;
                            break;
                        }
                        break;

                    case 4:
                        switch (sign)
                        {
                        case H5T.Sign.TWOS_COMPLEMENT:
                            newTypeId = H5T.copy(H5T.H5Type.NATIVE_INT);
                            retObject = ReadArray <Int32>(size, dataSetId, newTypeId, bandIndex, outSize);
                            dataType  = enumDataType.Int32;
                            break;

                        case H5T.Sign.UNSIGNED:
                            newTypeId = H5T.copy(H5T.H5Type.NATIVE_UINT);
                            retObject = ReadArray <UInt32>(size, dataSetId, newTypeId, bandIndex, outSize);
                            dataType  = enumDataType.UInt32;
                            break;
                        }
                        break;

                    case 8:
                        switch (sign)
                        {
                        case H5T.Sign.TWOS_COMPLEMENT:
                            newTypeId = H5T.copy(H5T.H5Type.NATIVE_LONG);
                            retObject = ReadArray <Int64>(size, dataSetId, newTypeId, bandIndex, outSize);
                            dataType  = enumDataType.Int64;
                            break;

                        case H5T.Sign.UNSIGNED:
                            newTypeId = H5T.copy(H5T.H5Type.NATIVE_ULONG);
                            retObject = ReadArray <UInt64>(size, dataSetId, newTypeId, bandIndex, outSize);
                            dataType  = enumDataType.UInt64;
                            break;
                        }
                        break;
                    }
                    break;

                case H5T.H5TClass.FLOAT:
                    switch (dataSize)
                    {
                    case 4:
                        newTypeId = H5T.copy(H5T.H5Type.NATIVE_FLOAT);
                        retObject = ReadArray <float>(size, dataSetId, newTypeId, bandIndex, outSize);
                        dataType  = enumDataType.Float;
                        break;

                    case 8:
                        newTypeId = H5T.copy(H5T.H5Type.NATIVE_DOUBLE);
                        retObject = ReadArray <double>(size, dataSetId, newTypeId, bandIndex, outSize);
                        dataType  = enumDataType.Double;
                        break;
                    }
                    break;
                }
            }
            finally
            {
                H5S.close(spaceid);
                H5D.close(dataSetId);
                H5F.close(_h5FileId);
            }
        }
예제 #5
0
 /// <summary>
 /// this still need to improve later.
 /// </summary>
 /// <param name="classType"></param>
 /// <param name="size"></param>
 /// <param name="sign"></param>
 /// <returns></returns>
 private Type getTypeof(H5T.H5TClass classType, int size, H5T.Sign sign)
 {
     if (sign == H5T.Sign.UNSIGNED)
     {
         if (classType == H5T.H5TClass.INTEGER)
         {
             if (size == 2)
             {
                 return(typeof(UInt16));
             }
             else if (size == 4)
             {
                 return(typeof(UInt32));
             }
             else if (size == 8)
             {
                 return(typeof(UInt64));
             }
         }
         // remain the same
         if (classType == H5T.H5TClass.FLOAT)
         {
             if (size == 4)
             {
                 return(typeof(float));
             }
             else if (size == 8)
             {
                 return(typeof(double));
             }
         }
     }
     else
     {
         if (classType == H5T.H5TClass.INTEGER)
         {
             if (size == 2)
             {
                 return(typeof(Int16));
             }
             else if (size == 4)
             {
                 return(typeof(Int32));
             }
             else if (size == 8)
             {
                 return(typeof(Int64));
             }
         }
         if (classType == H5T.H5TClass.FLOAT)
         {
             if (size == 4)
             {
                 return(typeof(float));
             }
             else if (size == 8)
             {
                 return(typeof(double));
             }
         }
         if (classType == H5T.H5TClass.STRING)
         {
             return(typeof(String));
         }
         if (classType == H5T.H5TClass.ENUM)
         {
             return(typeof(Enum));
         }
         if (classType == H5T.H5TClass.BITFIELD)
         {
             return(typeof(Boolean));
         }
         if (classType == H5T.H5TClass.ARRAY)
         {
             return(typeof(Array));
         }
         if (classType == H5T.H5TClass.COMPOUND)
         {
             return(typeof(Object));
         }
     }
     return(null);
 }
예제 #6
0
        } // test_vlen_dtype

        static void test_copy()
        {
            try
            {
                Console.Write("Testing copying datatypes");

                // Make a copy of a predefined type.
                H5DataTypeId inttype = H5T.copy(H5T.H5Type.NATIVE_INT);
                int          intsize = H5T.getSize(inttype);

                // Make a copy of that type.
                H5DataTypeId tcopy1      = H5T.copy(inttype);
                int          tcopy1_size = H5T.getSize(tcopy1);

                // The sizes of the copies should be the same.
                if (intsize != tcopy1_size)
                {
                    Console.WriteLine("test_copy: copy types incorrectly");
                    nerrors++;
                }

                // Close second type
                H5T.close(tcopy1);

                /*
                 * Test copy a datatype from a dataset.
                 */
                // Create a new file.
                H5FileId fileId = H5F.create("sometypes.h5", H5F.CreateMode.ACC_TRUNC);

                // Create a dataset with a simple dataspace.
                hssize_t[]    dims   = { DIM0, DIM1 };
                H5DataSpaceId dspace = H5S.create_simple(2, dims);
                H5DataSetId   dset   = H5D.create(fileId, "test_types", inttype, dspace);

                // Obtain the datatype from the dataset.
                H5DataTypeId dstype = H5T.copy(dset);

                // Check this datatype's class, size, and sign.
                H5T.H5TClass tclass = H5T.getClass(dstype);
                if (tclass != H5T.H5TClass.INTEGER)
                {
                    Console.WriteLine("test_copy: copy of dataset's datatype has incorrect class");
                    nerrors++;
                }

                int tsize = H5T.getSize(dstype);
                if (tsize != intsize)
                {
                    Console.WriteLine("test_copy: copy of dataset's datatype has incorrect size");
                    nerrors++;
                }

                H5T.Sign tsign = H5T.getSign(dstype);
                if (tsign != H5T.Sign.TWOS_COMPLEMENT)
                {
                    Console.WriteLine("test_copy: copy of dataset's datatype has incorrect sign, {0}", tsign);
                    nerrors++;
                }

                // Close objects.
                H5T.close(inttype);
                H5S.close(dspace);
                H5T.close(dstype);
                H5D.close(dset);
                H5F.close(fileId);

                Console.WriteLine("\t\t\t\tPASSED");
            }
            catch (HDFException anyHDF5E)
            {
                Console.WriteLine(anyHDF5E.Message);
                nerrors++;
            }
            catch (System.Exception sysE)
            {
                Console.WriteLine(sysE.TargetSite);
                Console.WriteLine(sysE.Message);
                nerrors++;
            }
        } // test_copy