Example #1
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
Example #2
0
        } // test_attr_plist

        static void test_attr_compound_write()
        {
            try
            {
                Console.Write("Testing write attributes with compound datatype");

                const int NX = 256; // data set dimension
                const int NY = 512;

                // Create a file.
                H5FileId fileId = H5F.create(COMP_FNAME, H5F.CreateMode.ACC_TRUNC);

                // Create dataspace for dataset.
                hssize_t[]    dims    = { NX, NY };
                H5DataSpaceId spaceId = H5S.create_simple(SPACE1_RANK, dims);

                // Create a dataset.
                H5DataSetId dsetId = H5D.create(fileId, DSET1_NAME, H5T.H5Type.NATIVE_UCHAR, spaceId);

                // Close dataset's dataspace
                H5S.close(spaceId);

                // this number 16 needs to be verified.
                // Create the attribute datatype.
                H5DataTypeId typeId = H5T.create(H5T.CreateClass.COMPOUND, 16);

                //tid1 = H5Tcreate(H5T_COMPOUND, sizeof(struct attr4_struct));
                int attr4_field1_off = 0;
                int attr4_field2_off = 1;
                int attr4_field3_off = 5;

                H5T.insert(typeId, "c", attr4_field1_off, H5T.H5Type.STD_U8LE);
                H5T.insert(typeId, "i", attr4_field2_off, H5T.H5Type.NATIVE_INT);
                H5T.insert(typeId, "l", attr4_field3_off, H5T.H5Type.STD_I64BE);

                // Create dataspace for first attribute.
                hssize_t[] dims2 = { ATTR4_DIM1, ATTR4_DIM2 };
                spaceId = H5S.create_simple(ATTR4_RANK, dims2);

                // Create complex attribute for the dataset.
                H5AttributeId attrId = H5A.create(dsetId, ATTR4_NAME, typeId, spaceId);

                // Try to create the same attribute again (should fail.)
                try
                {
                    attrId = H5A.create(dsetId, ATTR4_NAME, typeId, spaceId);

                    // should fail, but didn't, print an error message.
                    Console.WriteLine("\ntest_attr_compound_write: Attempting to create an existing attribute.");
                    nerrors++;
                }
                catch (HDFException) { } // does nothing, it should fail

                // Allocate space for the points & check arrays
                attr4_struct[,] attr_data4 = 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++)
                    {
                        attr_data4[ii, jj].c = 't';
                        attr_data4[ii, jj].i = nn++;
                        attr_data4[ii, jj].l = (ii * 10 + jj * 100) * nn;
                    }
                }

                // Write complex attribute data.
                H5A.write(attrId, typeId, new H5Array <attr4_struct>(attr_data4));

                // Close all objects and file.
                H5A.close(attrId);
                H5S.close(spaceId);
                H5T.close(typeId);
                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_write