Prev Up Next Index
Go backward to 3.1 Sub-classing the Type Hierarchy
Go up to 3.1 Sub-classing the Type Hierarchy
Go forward to 3.1.2 Sub-classing the Vector Types

3.1.1 Sub-classing the Simple Types

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.

line 10
Check to see if this variable has already been read.
line 13
The 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.
line 33
This sets the flag tested with the read_p function.
line 35
This command transfers values from the dataset's variable to the DODS instance. This is the point where the read actually happens.

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.


Tom Sgouros, July 2, 2004

Prev Up Next