Exemplo n.º 1
0
        static void test_enum_dtype(H5FileId fileId)
        {
            short i, j;

            string[] mname = { "RED", "GREEN", "BLUE", "YELLOW", "PINK", "PURPLE", "ORANGE", "WHITE" };
            short[,] spoints2 = new short[DIM0, DIM1];
            short[,] scheck2  = new short[DIM0, DIM1];
            try
            {
                Console.Write("Testing enumeration datatypes");

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

                // Construct enum type based on native type
                H5DataTypeId etype = H5T.enumCreate(H5T.H5Type.NATIVE_SHORT);

                // Insert members to type.
                for (i = 0; i < 8; i++)
                {
                    H5T.enumInsert(etype, mname[i], ref i);
                }

                // Assign a name to the enum type, close it, and open it by name.
                H5T.commit(fileId, "Color Type", etype);
                H5T.close(etype);
                H5DataTypeId color_type = H5T.open(fileId, "Color Type");

                // Check its class
                H5T.H5TClass tcls = H5T.getClass(color_type);
                if (tcls != H5T.H5TClass.ENUM)
                {
                    Console.WriteLine("test_enum: class of color_type = {0} is incorrect, should be ENUM", tcls);
                }

                // Create the dataset
                H5DataSetId dsetId = H5D.create(fileId, DSET_ENUM_NAME, color_type, dspace);

                // Construct enum type based on native type in memory.
                H5DataTypeId etype_m = H5T.enumCreate(H5T.H5Type.NATIVE_SHORT);

                // Insert members to type.
                for (i = 0; i < 8; i++)
                {
                    H5T.enumInsert(etype_m, mname[i], ref i);
                }

                // Initialize the dataset and buffer.
                for (i = 0; i < DIM0; i++)
                {
                    for (j = 0; j < DIM1; j++)
                    {
                        spoints2[i, j] = i;
                        scheck2[i, j]  = 0;
                    }
                }
                // Write the data to the dataset.
                H5D.write(dsetId, etype_m, new H5Array <short>(spoints2));

                // Close objects.
                H5D.close(dsetId);
                H5T.close(color_type);
                H5S.close(dspace);
                H5T.close(etype_m);

                // Open dataset again to check the type.
                dsetId = H5D.open(fileId, DSET_ENUM_NAME);

                // Get dataset's datatype.
                H5DataTypeId dstype = H5D.getType(dsetId);

                // Get the datatype's class and check that it is of class ENUM.
                H5T.H5TClass tclass = H5T.getClass(dstype);
                if (tclass != H5T.H5TClass.ENUM)
                {
                    Console.WriteLine("Type should be an enum class");
                    nerrors++;
                }

                // Check name of an enum value.
                int    memb_num  = 2;
                string memb_name = H5T.enumNameOf(dstype, ref memb_num);
                if (memb_name != "BLUE")
                {
                    Console.WriteLine("Member name of value 2 should be BLUE");
                    nerrors++;
                }

                // Check value of an enum name.
                int memb_value = 0;
                H5T.enumValueOf(dstype, memb_name, out memb_value);
                if (memb_value != 2)
                {
                    Console.WriteLine("Member value of BLUE should be 2");
                    nerrors++;
                }

                // Check member's value by member number.
                H5T.getMemberValue(dstype, 4, out memb_value);

                // Read data back.
                H5D.read(dsetId, dstype, new H5Array <short>(scheck2));

                // Close objects.
                H5D.close(dsetId);
                H5T.close(dstype);

                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++;
            }
        } // end of test_enum_dtype