Creating a read function for the simple type classes is a fairly
straightforward operation. Simply read the function using the data
access API's standard protocol, and use the type class's val2buf
function to load the data into the type object.
The following function is taken from the DODS implementation of the
NetCDF data access library. (See the file
src/nc-dods/NCByte.cc.)
NCByte::read(const String &dataset, int &error)
{
int varid; /* variable Id */
nc_type datatype; /* variable data type */
long cor[MAX_NC_DIMS]; /* corner coordinates */
int num_dim; /* number of dim. in variable */
long nels = -1; /* number of elements in buffer */
int id;
if (read_p()) // already done
return true;
int ncid = lncopen(dataset, NC_NOWRITE); /* netCDF id */
if (ncid == -1) {
cerr << "ncopen failed on " << dataset<< endl;
return false;
}
varid = lncvarid( ncid, name());
(void)lncvarinq( ncid, varid, (char *)0, &datatype,
&num_dim, (int *)0, (int *)0);
if(nels == -1){
for (id = 0; id < num_dim; id++)
cor[id] = 0;
}
if (datatype == NC_BYTE){
dods_byte Dbyte;
(void) lncvarget1 (ncid, varid, cor, &Dbyte);
set_read_p(true);
val2buf( &Dbyte );
(void) lncclose(ncid);
return true;
}
return false;
}
The following points are worth consideration about the above example.
lncopen() function call is simply the
ncopen() function from the NetCDF library. Also, the
lncvarid function is renamed ncvarid and so on. The
names have been changed to avoid link-time problems. (Remember that
the whole point of this exercise is to create a new ncopen()
function and its friends.
read_p
function.
Note the use of dods_byte in the code example. The DODS
configuration process creates definitions for the simple data types
like this one. They are stored in config_dap.h.
The read member function is used by the constraint expression
evaluator to extract data from a dataset during evaluation of the
constraint expression. This is particularly important to remember
because the read member function for a simple data type will be
called when reading an aggregate type such as Structure.