Previous: The Problem Next: Benefits of Iterators Table of contents Frames 2012 Unidata NetCDF Workshop > Developing generic netCDF software

25.4 A Solution
The nccopy utility solves this problem with iterators, implemented in C.

The C code is three functions, nc_get_iter(), nc_next_iter(), and nc_free_iter(). These functions are declared in ncdump/nciter.h and implemented in ncdump/nciter.c

Psuedocode for how they're used:

Use nc_get_iter() to get an iterator for my variable

While (iterator not done)
  use nc_next_iter() to get parameters specifying next block of data
  use a netCDF function to read the specified block
  do something with the values in that block of data 

Free the iterator when done with it

C code that adds 1 to every value of a numeric variable of any type:

#include "nciter.h"
 ...
#define BUFSIZ 1000000                 /* size of buffer, in bytes */
double data[BUFSIZ / sizeof(double)];  /* memory buffer for data */
nciter_t *iter;                        /* handle for iteration state */
size_t nvals;                          /* number of values to access */
size_t start[NC_MAX_DIMS], count[NC_MAX_DIMS];

/* get initialized iterator structure for variable varid */
stat = nc_get_iter(ncid, varid, BUFSIZ, &iter);

/* nc_next_iter() initializes start and count on first call,
 * changes start and count to iterate through whole variable on
 * subsequent calls, returns 0 when no data left for this variable. */
while((nvals = nc_next_iter(iter, start, count)) > 0) {
    /* read in a block of data (converting to doubles if necessary) */
    stat = nc_get_vara_double(ncid, varid, start, count, data);
    if(stat != NC_NOERR) err(...);
    /* add 1 to every value */
    for(int i = 0; i < nvals; i++)
        data[i] += 1.0;
    /* now write the changed data back out (converting back if necessary */
    stat = nc_out_vara_double(ncid, varid, start, count, data);
    if(stat != NC_NOERR) err(...);
}
stat = nc_free_iter(iter);  /* free iterator for this variable */

 


Previous: The Problem Next: Benefits of Iterators Table of contents Frames 2012 Unidata NetCDF Workshop > Developing generic netCDF software