示例#1
0
        static void test_compound_dtype(H5FileId fileId)
        {
            uint i, j, n;

            try
            {
                Console.Write("Testing compound datatypes");

                // Allocate space for the points & check arrays
                s1[,] points = new s1[DIM0, DIM1];
                s1[,] check  = new s1[DIM0, DIM1];

                // Initialize the dataset
                for (i = n = 0; i < DIM0; i++)
                {
                    for (j = 0; j < DIM1; j++)
                    {
                        points[i, j].c = 't';
                        points[i, j].i = n++;
                        points[i, j].l = (i * 10 + j * 100) * n;
                    }
                }

                // Create the data space
                hssize_t[]    dims    = { DIM0, DIM1 };
                H5DataSpaceId spaceId = H5S.create_simple(2, dims);

                // Create compound datatype for disk storage
                H5DataTypeId typeId = H5T.create(H5T.CreateClass.COMPOUND, 16);

                // Insert members
                H5T.insert(typeId, "c", 0, H5T.H5Type.STD_U8LE);
                H5T.insert(typeId, "i", 1, H5T.H5Type.STD_U32LE);
                H5T.insert(typeId, "l", 5, H5T.H5Type.STD_I64BE);

                // Create the dataset
                H5DataSetId dsetId = H5D.create(fileId, DSET_COMPOUND_NAME, typeId, spaceId);

                // Write the dataset
                H5D.write(dsetId, typeId, new H5Array <s1>(points));

                // Close dataset and dataspace
                H5D.close(dsetId);
                H5S.close(spaceId);
                H5T.close(typeId);

                // Open dataset again to check various functions.
                dsetId = H5D.open(fileId, DSET_COMPOUND_NAME);

                // Get its type and native type.
                H5DataTypeId dset_typeId = H5D.getType(dsetId);
                H5DataTypeId native_type = H5T.getNativeType(dset_typeId, H5T.Direction.DEFAULT);

                // Check name against this list
                string[] memb_names   = { "c", "i", "l" };
                int[]    memb_offsets = { 0, 1, 5 };

                H5DataTypeId mtypeId;              // member type
                H5T.H5TClass memb_cls1, memb_cls2; // member classes retrieved different ways
                string       memb_name;            // member name
                int          memb_idx;             // member index

                // Get the number of members in the type.
                int nmembers = H5T.getNMembers(native_type);

                // For each member, check its name, class, index, and size.
                for (int ii = 0; ii < nmembers; ii++)
                {
                    // Get the type of the ith member.
                    mtypeId = H5T.getMemberType(native_type, ii);

                    // Get the name of the ith member.
                    memb_name = H5T.getMemberName(native_type, ii);
                    if (memb_name != memb_names[ii])
                    {
                        Console.WriteLine("test_compound_dtypes: incorrect member name, {0}, for member no {1}", memb_name, i);
                        nerrors++;
                    }

                    // Get the class of the ith member and then verify the class.
                    memb_cls1 = H5T.getMemberClass(native_type, ii);
                    if (memb_cls1 != H5T.H5TClass.INTEGER)
                    {
                        Console.WriteLine("test_compound_dtypes: incorrect class, {0}, for member no {1}", memb_cls1, ii);
                        nerrors++;
                    }

                    // Get the class via type id
                    memb_cls2 = H5T.getClass(mtypeId);
                    if (memb_cls1 != memb_cls2)
                    {
                        Console.WriteLine("test_compound_dtypes: H5T.getMemberClass and H5T.getClass return different classes for the same type.");
                        nerrors++;
                    }

                    // Get member's index back from its name and verify it.
                    memb_idx = H5T.getMemberIndex(dset_typeId, memb_name);
                    if (memb_idx != ii)
                    {
                        Console.WriteLine("test_compound_dtypes: H5T.getMemberName and/or H5T.getMemberIndex returned false values.");
                        nerrors++;
                    }

                    // Get member's offset and verify it.
                    int memb_offset = H5T.getMemberOffset(dset_typeId, ii);
                    if (memb_offset != memb_offsets[ii])
                    {
                        Console.WriteLine("test_compound_dtypes: Incorrect offset value {0}, should be {1}.", memb_offset, memb_offsets[ii]);
                        nerrors++;
                    }

                    // Get size of the member's type and verify it.
                    int tsize = H5T.getSize(mtypeId);
                    switch (ii)
                    {
                    case 0:
                        //Console.WriteLine("tsize = {0}, STD_U8LE = {1}", tsize, H5T.getSize(H5T.H5Type.STD_U8LE));
                        if (tsize != H5T.getSize(H5T.H5Type.STD_U8LE))
                        {
                            Console.WriteLine("test_compound_dtypes: First member has incorrect size");
                            nerrors++;
                        }
                        break;

                    case 1:
                        if (tsize != H5T.getSize(H5T.H5Type.STD_U32LE))
                        {
                            Console.WriteLine("test_compound_dtypes: Second member has incorrect size");
                            nerrors++;
                        }
                        break;

                    case 2:
                        if (tsize != H5T.getSize(H5T.H5Type.STD_I64BE))
                        {
                            Console.WriteLine("test_compound_dtypes: Third member has incorrect size");
                            nerrors++;
                        }
                        break;

                    default:
                        Console.WriteLine("test_compound_dtypes: Only 3 members.");
                        break;
                    } // end switch

                    // Close current member type.
                    H5T.close(mtypeId);
                } // end for

                // Close objects.
                H5T.close(dset_typeId);
                H5T.close(native_type);
                H5D.close(dsetId);

                Console.WriteLine("\t\t\t\tPASSED");
            } // end of try block
            catch (HDFException anyHDF5E)
            {
                Console.WriteLine(anyHDF5E.Message);
                nerrors++;
            }
            catch (System.Exception sysE)
            {
                Console.WriteLine(sysE.TargetSite);
                Console.WriteLine(sysE.Message);
                nerrors++;
            }
        } // test_compound_dtype
        public void GetDataset <T>(H5FileId fileId, string datasetName, string groupName, T[, ,] datasetOut, DataValueType type)
        {
            H5GroupId   groupId   = H5G.open(fileId, groupName);
            H5DataSetId dataSetId = H5D.open(groupId, datasetName /*"EV_Emissive"*/);

            switch (type)
            {
            case DataValueType.FLOAT:
                H5DataTypeId tidfloat = new H5DataTypeId(H5T.H5Type.NATIVE_FLOAT);

                // Read the array back
                H5D.read(dataSetId, tidfloat,
                         new H5Array <T>(datasetOut));//(dataSetId, tid1, new H5Array<int>(vlReadBackArray));
                // H5T.close(tidfloat);
                break;

            case DataValueType.INT:
                H5DataTypeId tidint = new H5DataTypeId(H5T.H5Type.NATIVE_INT);



                //  H5T.H5TClass c =  H5T.getMemberClass(tid0);


                // Read the array back
                H5D.read(dataSetId, tidint,
                         new H5Array <T>(datasetOut));//(dataSetId, tid1, new H5Array<int>(vlReadBackArray));


                //H5T.close(tidint);
                break;

            case DataValueType.COMPOUND:
                H5DataTypeId tid0 = H5D.getType(dataSetId);

                int nMember = H5T.getNMembers(tid0);

                H5DataSpaceId spaceid = H5D.getSpace(dataSetId);
                long[]        dims    = H5S.getSimpleExtentDims(spaceid);//得到数据数组的大小,比如[3,1800,2048]
                int           length  = 1;
                for (int i = 0; i < dims.Length; i++)
                {
                    length *= (int)dims[i];
                }

                for (int i = 0; i < nMember; i++)
                {
                    string       memberName   = H5T.getMemberName(tid0, i);
                    H5DataTypeId memberTypeId = H5T.getMemberType(tid0, i);
                    H5T.H5TClass dataClass    = H5T.getClass(memberTypeId); //得到数据集的类型
                    string       typeName     = dataClass.ToString();

                    if (typeName == "INTEGER")    //目前先只支持整形的
                    {
                        H5DataTypeId tidtmp = H5T.create(H5T.CreateClass.COMPOUND, sizeof(int));
                        H5T.insert(tidtmp, memberName, 0, H5T.H5Type.NATIVE_INT);

                        int[] dataTmp = new int[length];
                        H5D.read(dataSetId, tidtmp, new H5Array <int>(dataTmp));

                        for (int j = 0; j < length; j++)
                        {
                            datasetOut[0, j, i] = (T)Convert.ChangeType(dataTmp[j], datasetOut[0, j, i].GetType());
                        }
                    }
                }

                H5S.close(spaceid);
                break;

            default:
                break;
            }



            H5D.close(dataSetId);
            H5G.close(groupId);
            //H5F.close(fileId);
        }
示例#3
0
        } // test_attr_compound_write

        static void test_attr_compound_read()
        {
            try
            {
                Console.Write("Testing read attribute with compound datatype");

                // Open file.
                H5FileId fileId = H5F.open(COMP_FNAME, H5F.OpenMode.ACC_RDWR);

                // Open the dataset.
                H5DataSetId dsetId = H5D.open(fileId, DSET1_NAME);

                // Verify the correct number of attributes for this dataset.
                H5ObjectInfo oinfo = H5O.getInfo(dsetId);
                if (oinfo.nAttributes != 1)
                {
                    Console.WriteLine("\ntest_attr_basic_read: incorrect number of attributes: read {0} - should be {1}",
                                      oinfo.nAttributes, 1);
                    nerrors++;
                }

                // Open first attribute for the dataset.
                H5AttributeId attrId = H5A.openByIndex(dsetId, ".", H5IndexType.CRT_ORDER, H5IterationOrder.INCREASING, 0);

                // Verify dataspace.
                H5DataSpaceId spaceId = H5A.getSpace(attrId);

                int rank = H5S.getSimpleExtentNDims(spaceId);
                if (rank != ATTR4_RANK)
                {
                    Console.WriteLine("\ntest_attr_compound_read: incorrect rank = {0} - should be {1}", rank, ATTR4_RANK);
                    nerrors++;
                }

                long[] dims = H5S.getSimpleExtentDims(spaceId);
                if (dims[0] != ATTR4_DIM1)
                {
                    Console.WriteLine("\ntest_attr_compound_read: incorrect dim[0] = {0} - should be {1}", dims[0], ATTR4_DIM1);
                    nerrors++;
                }
                if (dims[1] != ATTR4_DIM2)
                {
                    Console.WriteLine("\ntest_attr_compound_read: incorrect dim[1] = {0} - should be {1}", dims[1], ATTR4_DIM2);
                    nerrors++;
                }

                // Close dataspace.
                H5S.close(spaceId);

                // Verify datatype of the attribute.
                H5DataTypeId typeId = H5A.getType(attrId);

                H5T.H5TClass t_class = H5T.getClass(typeId);
                if (t_class != H5T.H5TClass.COMPOUND)
                {
                    Console.WriteLine("test_compound_dtypes: H5T.getMemberClass and H5T.getClass return different classes for the same type.");
                    nerrors++;
                }
                int nfields = H5T.getNMembers(typeId);
                if (nfields != 3)
                {
                    Console.WriteLine("test_compound_dtypes: H5T.getMemberClass and H5T.getClass return different classes for the same type.");
                    nerrors++;
                }

                // Check name against this list
                string[] memb_names   = { ATTR4_FIELDNAME1, ATTR4_FIELDNAME2, ATTR4_FIELDNAME3 };
                int[]    memb_offsets = { 0, 1, 5 }; // list of member offsets

                H5DataTypeId mtypeId;                // member type
                H5T.H5TClass memb_cls1;              // member classes retrieved different ways
                string       memb_name;              // member name
                int          memb_idx;               // member index
                int          memb_offset, idx;       // member offset, loop index

                // how to handle int versus uint for memb_idx and idx???

                // For each member, check its name, class, index, and size.
                for (idx = 0; idx < nfields; idx++)
                {
                    // Get the type of the ith member to test other functions later.
                    mtypeId = H5T.getMemberType(typeId, idx);

                    // Get the name of the ith member.
                    memb_name = H5T.getMemberName(typeId, idx);
                    if (memb_name != memb_names[idx])
                    {
                        Console.WriteLine("test_compound_dtypes: incorrect member name, {0}, for member no {1}", memb_name, idx);
                        nerrors++;
                    }

                    // Get the class of the ith member and then verify the class.
                    memb_cls1 = H5T.getMemberClass(typeId, idx);
                    if (memb_cls1 != H5T.H5TClass.INTEGER)
                    {
                        Console.WriteLine("test_compound_dtypes: incorrect class, {0}, for member no {1}", memb_cls1, idx);
                        nerrors++;
                    }

                    // Get member's index back using its name and verify it.
                    memb_idx = H5T.getMemberIndex(typeId, memb_name);
                    if (memb_idx != idx)
                    {
                        Console.WriteLine("test_attr_compound_read: H5T.getMemberName and/or H5T.getMemberIndex returned false values.");
                        nerrors++;
                    }

                    // Get member's offset and verify it.
                    memb_offset = H5T.getMemberOffset(typeId, idx);
                    if (memb_offset != memb_offsets[idx])
                    {
                        Console.WriteLine("test_attr_compound_read: H5T.getMemberOffset returned incorrect value - {0}, should be {1}", memb_offset, memb_offsets[idx]);
                        nerrors++;
                    }

                    // Get member's size and verify it.
                    int tsize = H5T.getSize(mtypeId);
                    switch (idx)
                    {
                    case 0:
                        if (tsize != H5T.getSize(H5T.H5Type.STD_U8LE))
                        {
                            Console.WriteLine("test_attr_compound_read: H5T.getSize returned incorrect value.");
                            nerrors++;
                        }
                        break;

                    case 1:
                        if (tsize != H5T.getSize(H5T.H5Type.NATIVE_INT))
                        {
                            Console.WriteLine("test_attr_compound_read: H5T.getSize returned incorrect value.");
                            nerrors++;
                        }
                        break;

                    case 2:
                        if (tsize != H5T.getSize(H5T.H5Type.STD_I64BE))
                        {
                            Console.WriteLine("test_attr_compound_read: H5T.getSize returned incorrect value.");
                            nerrors++;
                        }
                        break;

                    default:
                        Console.WriteLine("test_attr_compound_read: We should only have 3 members.");
                        nerrors++;
                        break;
                    } // end switch

                    // Close current member type.
                    H5T.close(mtypeId);
                } // end for

                // Prepare the check array to verify read data.  It should be the same as the attr_data4 array
                // in the previous test function test_attr_compound_write.
                attr4_struct[,] check = new attr4_struct[ATTR4_DIM1, ATTR4_DIM2];

                // Initialize the dataset
                int ii, jj, nn;
                for (ii = nn = 0; ii < ATTR4_DIM1; ii++)
                {
                    for (jj = 0; jj < ATTR4_DIM2; jj++)
                    {
                        check[ii, jj].c = 't';
                        check[ii, jj].i = nn++;
                        check[ii, jj].l = (ii * 10 + jj * 100) * nn;
                    }
                }

                // Read attribute information.
                attr4_struct[,] read_data4 = new attr4_struct[ATTR4_DIM1, ATTR4_DIM2];
                H5A.read(attrId, typeId, new H5Array <attr4_struct>(read_data4));

                // Verify values read in.
                for (ii = 0; ii < ATTR4_DIM1; ii++)
                {
                    for (jj = 0; jj < ATTR4_DIM2; jj++)
                    {
                        if ((check[ii, jj].c != read_data4[ii, jj].c) ||
                            (check[ii, jj].i != read_data4[ii, jj].i) ||
                            (check[ii, jj].l != read_data4[ii, jj].l))
                        {
                            Console.WriteLine("test_attr_compound_read: Incorrect read data: {0}, should be {1}", read_data4[ii, jj], check[ii, jj]);
                            nerrors++;
                        }
                    }
                }

                // Close resources.
                H5T.close(typeId);
                H5A.close(attrId);
                H5D.close(dsetId);
                H5F.close(fileId);

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