Exemple #1
0
        } // test_h5s_scalar_write

        static void test_select()
        {
            try
            {
                const string FILE_NAME = ("Select.h5");

                const int MSPACE1_RANK = 1;     // Rank of the first dataset in memory
                const int MSPACE1_DIM  = 50;    // Dataset size in memory
                const int FSPACE_RANK  = 2;     // Dataset rank as it is stored in the file
                const int FSPACE_DIM1  = 8;     // Dimension sizes of the dataset as it is
                const int FSPACE_DIM2  = 12;

                // We will read dataset back from the file to the dataset in memory with these
                // dataspace parameters.
                const int MSPACE_RANK = 2;
                const int MSPACE_DIM1 = 8;
                const int MSPACE_DIM2 = 9;
                int[]     vector      = new int[MSPACE1_DIM];
                int[]     values      = new int[4];

                // New values to be written
                values[0] = 53;
                values[1] = 59;
                values[2] = 61;
                values[3] = 67;

                Console.Write("Testing basic selection");

                // Buffers' initialization.
                vector[0] = vector[MSPACE1_DIM - 1] = -1;
                int i, j;
                for (i = 1; i < MSPACE1_DIM - 1; i++)
                {
                    vector[i] = i;
                }

                // Create the file.
                H5FileId fileId = H5F.create(FILE_NAME, H5F.CreateMode.ACC_TRUNC);

                // Create dataspace for the dataset in the file.
                hssize_t[]    fdim   = { FSPACE_DIM1, FSPACE_DIM2 };
                H5DataSpaceId fspace = H5S.create_simple(FSPACE_RANK, fdim);

                // Create dataset in the file.
                H5DataSetId dataset = H5D.create(fileId, "Matrix in file", H5T.H5Type.NATIVE_INT, fspace);

                // Select hyperslab for the dataset in the file, using 3x2 blocks,
                // (4,3) stride and (2,4) count starting at the position (0,1).
                hssize_t[] start  = { 0, 1 }; /* Start of hyperslab */
                hssize_t[] stride = { 4, 3 };
                hssize_t[] count  = { 2, 4 };
                hssize_t[] block  = { 3, 2 };
                H5S.selectStridedHyperslab(fspace, H5S.SelectOperator.SET, start, stride, count, block);

                // Create dataspace for the dataset.
                hssize_t[]    dim1    = { MSPACE1_DIM }; // Dimension size of the first dataset(in memory)
                H5DataSpaceId mspace1 = H5S.create_simple(MSPACE1_RANK, dim1);

                // Select hyperslab.
                // We will use 48 elements of the vector buffer starting at the second element.
                // Selected elements are 1 2 3 ... 48
                start[0]  = 1;
                stride[0] = 1;
                count[0]  = 48;
                block[0]  = 1;
                H5S.selectStridedHyperslab(mspace1, H5S.SelectOperator.SET, start, stride, count, block);

                // Write selection from the vector buffer to the dataset in the file.
                H5D.write(dataset, new H5DataTypeId(H5T.H5Type.NATIVE_INT), mspace1, fspace,
                          new H5PropertyListId(H5P.Template.DEFAULT), new H5Array <int>(vector));

                // Reset the selection for the file dataspace.
                H5S.selectNone(fspace);

                // Close objects and file.
                H5S.close(mspace1);
                H5S.close(fspace);
                H5D.close(dataset);
                H5F.close(fileId);

                // Reopen the file.
                fileId = H5F.open(FILE_NAME, H5F.OpenMode.ACC_RDONLY);

                // Open the dataset.
                dataset = H5D.open(fileId, "Matrix in file");

                // Get dataspace of the dataset.
                fspace = H5D.getSpace(dataset);

                // Select first hyperslab for the dataset in the file.
                start[0]  = 1; start[1] = 2;
                block[0]  = 1; block[1] = 1;
                stride[0] = 1; stride[1] = 1;
                count[0]  = 3; count[1] = 4;
                H5S.selectStridedHyperslab(fspace, H5S.SelectOperator.SET, start, stride, count, block);

                // Add second selected hyperslab to the selection.
                start[0]  = 2; start[1] = 4;
                block[0]  = 1; block[1] = 1;
                stride[0] = 1; stride[1] = 1;
                count[0]  = 6; count[1] = 5;
                H5S.selectStridedHyperslab(fspace, H5S.SelectOperator.OR, start, stride, count, block);

                // Create memory dataspace.
                hssize_t[] mdim = { MSPACE_DIM1, MSPACE_DIM2 }; // Dimension sizes of the dataset in memory when we
                // read selection from the dataset on the disk */
                H5DataSpaceId mspace2 = H5S.create_simple(MSPACE_RANK, mdim);

                // Select two hyperslabs in memory. Hyperslabs has the same
                // size and shape as the selected hyperslabs for the file dataspace.
                start[0]  = 0; start[1] = 0;
                block[0]  = 1; block[1] = 1;
                stride[0] = 1; stride[1] = 1;
                count[0]  = 3; count[1] = 4;
                H5S.selectStridedHyperslab(mspace2, H5S.SelectOperator.SET, start, stride, count, block);

                start[0]  = 1; start[1] = 2;
                block[0]  = 1; block[1] = 1;
                stride[0] = 1; stride[1] = 1;
                count[0]  = 6; count[1] = 5;
                H5S.selectStridedHyperslab(mspace2, H5S.SelectOperator.OR, start, stride, count, block);

                // Initialize data buffer.
                int[,] matrix_out = new int[MSPACE_DIM1, MSPACE_DIM2]; // Buffer to read from the dataset
                for (i = 0; i < MSPACE_DIM1; i++)
                {
                    for (j = 0; j < MSPACE_DIM2; j++)
                    {
                        matrix_out[i, j] = 0;
                    }
                }

                int[,] results = { { 10, 0, 11, 12, 0,  0,  0, 0, 0 },
                                   { 18, 0, 19, 20, 0, 21, 22, 0, 0 },
                                   {  0, 0,  0,  0, 0,  0,  0, 0, 0 },
                                   {  0, 0, 27, 28, 0, 29, 30, 0, 0 },
                                   {  0, 0, 35, 36, 0, 37, 38, 0, 0 },
                                   {  0, 0, 43, 44, 0, 45, 46, 0, 0 },
                                   {  0, 0,  0,  0, 0,  0,  0, 0, 0 },
                                   {  0, 0,  0,  0, 0,  0,  0, 0, 0 } };

                // Read data back to the buffer matrix_out.
                H5D.read(dataset, new H5DataTypeId(H5T.H5Type.NATIVE_INT), mspace2, fspace,
                         new H5PropertyListId(H5P.Template.DEFAULT), new H5Array <int>(matrix_out));

                // Check read data.
                for (i = 0; i < MSPACE_DIM1; i++)
                {
                    for (j = 0; j < MSPACE_DIM2; j++)
                    {
                        if (matrix_out[i, j] != results[i, j])
                        {
                            Console.WriteLine("test_select: Incorrect read data: {0}, should be {1}", matrix_out[i, j], results[i, j]);
                            nerrors++;
                        }
                    }
                }
                // Close objects and file.
                H5S.close(mspace2);
                H5S.close(fspace);
                H5D.close(dataset);
                H5F.close(fileId);

                Console.WriteLine("\t\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_select