Next: Function Naming Conventions, Previous: C interface changes from NetCDF 2 to NetCDF 3, Up: C interface changes from NetCDF 2 to NetCDF 3
First, here's an example of C code that uses the netCDF-2 interface:
void *bufferp; NF_TYPE xtype; ncvarinq(ncid, varid, ..., &xtype, ... ... /* allocate bufferp based on dimensions and type */ ... if (ncvarget(ncid, varid, start, count, bufferp) == -1) { fprintf(stderr, "Can't get data, error code = %d\n",ncerr); /* deal with it */ ... } switch(xtype) { /* deal with the data, according to type */ ... case NF_FLOAT: fanalyze((float *)bufferp); break; case NF_DOUBLE: danalyze((double *)bufferp); break; }
Here's how you might handle this with the new netCDF-3 C interface:
/* * I want to use doubles for my analysis. */ double dbuf[NDOUBLES]; int status; /* So, I use the function that gets the data as doubles. */ status = NF_GET_VARA_DOUBLE(NCID, varid, start, count, dbuf) if (status != NF_NOERR) { fprintf(stderr, "Can't get data: %s\n", NF_STRERROR(STATUS)); /* deal with it */ ... } danalyze(dbuf);
The example above illustrates changes in function names, data type conversion, and error handling, discussed in detail in the sections below.