2010 Unidata NetCDF Workshop > Introduction to the NetCDF APIs and Example Programs
9.27 Issues in Writing Generic NetCDF Software
There are several issues to be aware of in writing software designed to
read arbitrary netCDF data.
The general structure of utility programs, such as the NCO
utilities or programs to copy netCDF data from a source file to a
target file is straightforward:
- Find out how many dimensions are in the source file using a file
inquire function.
- For each dimension, find out its name and length and create a
corresponding dimension in the output file. Preserve the unlimited
dimension, if needed.
- Find out how many variables are in the source file using a file
inquire function.
- For each source variable, find out its name, type, shape, and
number of attributes. Create a corresponding variable in the output
file. Loop through its attributes in the source file and create
corresponding attributes, including all their values in the target
file.
- For each global (file-level) attribute, find its name, type, and
values. Create corresponding global attribute in the target
file.
- For each variable, copy all its data values from the source file
to the target file.
This last step can be tricky, because:
- The code must work for a variable with any number of
dimensions, so a simple set of nested loops with one loop for each
dimension cannot be used.
- A variable may have more data
than can fit in memory, so it is impractical to read all the values
into memory at once and then write them all out.
- Even a
single row of variable values may be too large to fit in memory, for example
a variable of type double with a most rapidly varying dimension of
size 231
. One solution: treat dimension indices like an odometer to
iterate through variable data in a single loop.
NetCDF-4 note:
The enhanced data model for netCDF-4 adds arbitrarily
nested groups and nested user-defined data types that include variable
length arrays. These add a need for recursion and object-oriented
programming to deal with a potentially infinite number of types.
2010 Unidata NetCDF Workshop > Introduction to the NetCDF APIs and Example Programs