예제 #1
0
        static void test_attr_basic_write()
        {
            try
            {
                Console.Write("Testing Basic Scalar Attribute Writing Functions");

                // Create a new file using H5F_ACC_TRUNC access,
                // default file creation properties, and default file
                // access properties.
                H5FileId fileId = H5F.create(FILE_NAME, H5F.CreateMode.ACC_TRUNC);

                // Copy datatype for use.
                H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_INT);

                // Open the root group.
                H5GroupId groupId = H5G.open(fileId, "/");

                // Create dataspace for attribute.
                hssize_t[]    gdims     = { GATTR_DIM1, GATTR_DIM2 };
                H5DataSpaceId gspace_Id = H5S.create_simple(GATTR_RANK, gdims);

                // Create an attribute for the group.
                H5AttributeId attrId = H5A.create(groupId, RGATTR_NAME, typeId, gspace_Id);
                H5A.close(attrId);
                H5G.close(groupId);

                // Create a group in this file.
                groupId = H5G.create(fileId, GROUP1_NAME);

                // Create an attribute for group /Group1.
                attrId = H5A.create(groupId, GATTR_NAME, typeId, gspace_Id);
                H5A.write(attrId, typeId, new H5Array <int>(gattr_data));

                // Create the dataspace.
                hssize_t[]    dims1     = { SPACE1_DIM1, SPACE1_DIM2 };
                H5DataSpaceId space1_Id = H5S.create_simple(SPACE1_RANK, dims1);

                // Create a dataset using default properties.
                H5DataSetId dsetId = H5D.create(fileId, DSET1_NAME, H5T.H5Type.NATIVE_UCHAR, space1_Id);

                // Close objects and file.
                H5A.close(attrId);
                H5D.close(dsetId);
                H5G.close(groupId);
                H5F.close(fileId);

                // Open the file again.
                fileId = H5F.open(FILE_NAME, H5F.OpenMode.ACC_RDWR);

                // Open the root group.
                groupId = H5G.open(fileId, "/");

                // Open attribute again.
                attrId = H5A.open(groupId, RGATTR_NAME);

                // Close attribute and root group.
                H5A.close(attrId);
                H5G.close(groupId);

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

                // Create the dataspace for dataset's attribute.
                hssize_t[]    attdims    = { ATTR1_DIM };
                H5DataSpaceId attspaceId = H5S.create_simple(ATTR1_RANK, attdims);

                // Create an attribute for the dataset.
                attrId = H5A.create(dsetId, D1ATTR1_NAME, typeId, attspaceId);

                // Try to create the same attribute again (should fail.)
                try
                {
                    H5AttributeId attr_twice = H5A.create(dsetId, D1ATTR1_NAME, typeId, attspaceId);

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

                // Write attribute information.
                int[] attr_data1 = new int[] { 512, -234, 98123 }; /* Test data for 1st attribute */
                H5A.write(attrId, typeId, new H5Array <int>(attr_data1));

                // Create another attribute for the dataset.
                H5AttributeId attr2Id = H5A.create(dsetId, D1ATTR2_NAME, typeId, attspaceId);

                // Write attribute information.
                int[] attr_data2 = new int[] { 256, 11945, -22107 };
                H5A.write(attr2Id, typeId, new H5Array <int>(attr_data2));

                // Read attribute information immediately, without closing attribute.
                int[] read_data1 = new int[3];
                H5A.read(attrId, typeId, new H5Array <int>(read_data1));

                // Verify values read in.
                int ii;
                for (ii = 0; ii < ATTR1_DIM; ii++)
                {
                    if (attr_data1[ii] != read_data1[ii])
                    {
                        Console.WriteLine("\ntest_attr_basic_write: check1: read value differs from input: read {0} - input {1}", read_data1[ii], attr_data1[ii]);
                        nerrors++;
                    }
                }

                // Close attributes.
                H5A.close(attrId);
                H5A.close(attr2Id);

                // Open attribute again and verify its name.
                attrId = H5A.openIndex(dsetId, 0);

                string attr_name = H5A.getName(attrId);
                if (attr_name != D1ATTR1_NAME)
                {
                    Console.WriteLine("\ntest_attr_basic_write: attribute name incorrect: is {0} - should be {1}", attr_name, D1ATTR1_NAME);
                    nerrors++;
                }

                // Close attribute.
                H5A.close(attrId);

                // Open the second attribute again and verify its name.
                attr2Id   = H5A.openIndex(dsetId, 1);
                attr_name = H5A.getName(attr2Id);
                if (attr_name != D1ATTR2_NAME)
                {
                    Console.WriteLine("\ntest_attr_basic_write: attribute name incorrect: is {0} - should be {1}", attr_name, D1ATTR2_NAME);
                    nerrors++;
                }
                // Close all objects.
                H5A.close(attr2Id);
                H5S.close(space1_Id);
                H5S.close(gspace_Id);
                H5D.close(dsetId);
                H5F.close(fileId);

                Console.WriteLine("\tPASSED");
            } // end try block
            catch (HDFException anyHDF5E)
            {
                Console.WriteLine(anyHDF5E.Message);
                nerrors++;
            }
            catch (System.Exception sysE)
            {
                Console.WriteLine(sysE.TargetSite);
                Console.WriteLine(sysE.Message);
                nerrors++;
            }
        } // test_attr_basic_write