Creating a general raster data set and writing data to it are separate operations in the GR programming model. Once an array is defined it is ready to store data. No definitions are retained about the size, contents or number of components of a general raster data set from one data set to the next or from one file to the next.
C: file_id = Hopen(filename, access_mode, number_of_desc); gr_id = GRstart(file_id); ri_id = GRcreate(gr_id, name, ncomp, number_type, il, dim_sizes); <Optional operations> status = GRendaccess(ri_id); status = GRend(gr_id); status = Hclose(file_id); FORTRAN: file_id = hopen(filename, access_mode, number_of_desc) gr_id = mgstart(filename, access_mode) ri_id = mgcreat(gr_id, name, ncomp, number_type, il, dim_sizes); <Optional operations> status = mgendac(ri_id) status = mgend(gr_id) status = hclose(file_id)
GRcreate defines a new general raster data set using the arguments name, ncomp, number_type, il and dim_sizes. Once a data set is created, you cannot change its name, data type, dimension or number of components. GRcreate does not actually perform the write; this occurs only when GRendaccess is called.
TABLE 8C GRcreate Parameter List
C:
#include "hdf.h" #include "mfgr.h" #define X_LENGTH 5 #define Y_LENGTH 10 main( ) { int32 gr_id, ri_id, file_id, status; int32 dimsizes[2], ncomp, il; /* Create and open the file. */ file_id = Hopen("Example1.hdf", DFACC_CREATE, 0); /* Initiate the GR interface. */ gr_id = GRstart(file_id); /* Define the number of components and dimensions of the image. */ ncomp = 2; il = MFGR_INTERLACE_PIXEL; dimsizes[0] = X_LENGTH; dimsizes[1] = Y_LENGTH; /* Create the image array. */ ri_id = GRcreate(gr_id, "Image_array_1", ncomp, DFNT_INT16, il, dimsizes); /* Terminate access to the image array. */ status = GRendaccess(gr_id); /* Terminate access to the GR interface and close the file */ status = GRend(ri_id); /* Close the file. */ status = Hclose(file_id); }
PROGRAM CREATE IMAGE ARRAY integer*4 gr_id, ri_id, file_id, dimsizes(2), ncomp, il integer mgstart, mgcreat, mgendac, mgend, hopen, hclose integer*4 X_LENGTH, Y_LENGTH, status parameter (X_LENGTH = 5, Y_LENGTH = 10, + MFGR_INTERLACE_PIXEL = 0) C DFACC_CREATE and DFNT_INT16 are defined in hdf.h. integer*4 DFACC_CREATE, DFNT_INT16 parameter (DFACC_CREATE = 4, DFNT_INT16 = 22) C Create and open the file. file_id = hopen("Example1.hdf", DFACC_CREATE, 0) C Initiate the GR interface. gr_id = mgstart(file_id) C Define the number of components and dimensions of the image C array. ncomp = 2 il = MFGR_INTERLACE_PIXEL dimsizes(1) = Y_LENGTH dimsizes(2) = X_LENGTH C Create the image array. ri_id = mgcreat(gr_id, `Ex_array_1', ncomp, DFNT_INT16, il, + dimsizes) C Terminate access to the image array. status = mgendac(ri_id) C Terminate access to the GR interface and close the file. status = mgend(gr_id) C Close the file. status = hclose(file_id) end
8.5.3 Writing General Raster Images: GRwriteimage
GRwriteimage is the routine used to either completely or partially fill an n-component image array. It can also skip a specified number of image array elements between write operations along each dimension.
C: file_id = Hopen(filename, access_mode, number_of_desc); gr_id = GRstart(file_id); ri_id = GRcreate(gr_id, name, ncomp, number_type, il, dim_sizes); status = GRwriteimage(ri_id, start, stride, edge, data); status = GRendaccess(gr_id); status = GRend(ri_id); status = Hclose(file_id); FORTRAN: file_id = hopen(filename, access_mode, number_of_desc) gr_id = mgstart(file_id) ri_id = mgcreat(gr_id, name, ncomp, number_type, il, dim_sizes); status = mgwrimg(ri_id, start, stride, edge, data) status = mgendac(ri_id) status = mgend(gr_id) status = hclose(file_id)
As with SD arrays, subsets of general raster images can be written. (In n-dimensional SD arrays these two-dimensional subsets are referred to as "slabs".) A subset is specified by giving an array consisting of the coordinate location of the origin vertex and an array consisting of lengths of each of the two dimensions.
TABLE 8D GRwriteimage Parameter List
C:
#include "hdf.h" #include "mfgr.h" #define X_LENGTH 15 #define Y_LENGTH 10 main( ) { int32 gr_id, ri_id, file_id, status; int32 dims[2], start[2], edges[2], ncomp, il; int16 image_data[Y_LENGTH][X_LENGTH][2], i, j; /* Create and open the file. */ file_id = Hopen("Example2.hdf", DFACC_CREATE, 0); /* Initiate the GR interface. */ gr_id = GRstart(file_id); /* Define the number of components and dimensions of the image. */ ncomp = 2; il = MFGR_INTERLACE_PIXEL; dims[0] = X_LENGTH; dims[1] = Y_LENGTH; /* Create the array. */ ri_id = GRcreate(gr_id, "Ex_array_2", ncomp, DFNT_INT16, il, dims); /* Fill the stored-data array with values. */ for (j = 0; j < Y_LENGTH; j++) { for (i = 0; i < X_LENGTH; i++) { image_data[j][i][0] = (i + j) + 1; image_data[j][i][1] = (i + j) + 1; } } /* Define the location, pattern, and size of the data set */ for (i = 0; i < 2; i++) { start[i] = 0; edges[i] = dims[i]; } /* Write the stored data to the image array. */ status = GRwriteimage(ri_id, start, NULL, edges, (VOIDP)image_data); /* Terminate access to the array. */ status = GRendaccess(ri_id); /* Terminate access to the GR interface. */ status = GRend(gr_id); /* Close the file. */ status = Hclose(file_id); }
PROGRAM WRITE ARRAY integer*4 ri_id, gr_id, file_id, ncomp, il integer dims(2), start(2), edges(2), stride(2) integer i, j, status integer mgstart, mgcreat, mgwrimg, mgendac, mgend integer hopen, hclose integer*4 DFACC_CREATE, DFNT_INT16 integer*4 X_LENGTH, Y_LENGTH parameter (DFACC_CREATE = 4, DFNT_INT16 = 22, X_LENGTH = 15, + Y_LENGTH = 10, MFGR_INTERLACE_PIXEL = 0) integer*2 image_data(2, X_LENGTH, Y_LENGTH) C Create and open the file. file_id = hopen(`Example2.hdf', DFACC_CREATE, 0) C Initiate the GR interface. gr_id = mgstart(file_id) C Define the number of components and dimensions of the image. ncomp = 2 il = MFGR_INTERLACE_PIXEL dims(1) = X_LENGTH dims(2) = Y_LENGTH C Create the data set. ri_id = mgcreat(gr_id, `Ex_array_2', ncomp, DFNT_INT16, il, dims) C Fill the stored-data array with values. do 20 j = 1, Y_LENGTH do 10 i = 1, X_LENGTH image_data(1, i, j) = i + j - 1 image_data(2, i, j) = i + j - 1 10 continue 20 continue C Define the location, pattern, and size of the data set C that will be written to. start(1) = 0 start(2) = 0 edges(1) = X_LENGTH edges(2) = Y_LENGTH stride(1) = 1 stride(2) = 1 C Write the stored data to the image array. status = mgwrimg(ri_id, start, stride, edges, image_data) C Terminate access to the array. status = mgendac(ri_id) C Terminate access to the GR interface. status = mgend(gr_id) C Close the file. status = hclose(file_id) end