Loading...
Searching...
No Matches
Reading Classic Format NetCDF Files of Unknown Structure

Perhaps you would like to write your software to handle more general cases, so that you don't have to adjust your source every time the grid size changes, or a variable is added to the file.

There are inquiry functions that let you find out everything you need to know about a file. These functions contain “inq” or “INQ” in their names.

Using the inquiry functions, it is possible to write code that will read and understand any netCDF file, whatever its contents. (For example, ncdump does just that.)

First use nc_inq(), which will tell you how many variables and global attributes there are in the file.

Start with global attribute 0, and proceed to natts - 1, the number of global attributes minus one. The nc_inq_att() function will tell you the name, type, and length of each global attribute.

Then start with dimid 0, and proceed to dimid ndims - 1, calling nc_inq_dim(). This will tell you the name and length of each dimension, and whether it is unlimited.

Then start with varid 0, and proceed to varid nvars - 1, calling nc_inq_var(). This will tell you the number of dimensions of this variable, and their associated IDs. It will also get the name and type of this variable, and whether there are any attributes attached. If there are attributes attached, use the nc_inq_att() function to get their names, types, and lengths.

(To read an attribute, use the appropriate nc_get_att_<TYPE> function, like nc_get_att_int() to get the data from an attribute that is an array of integers.)

There are also functions that return an item's ID, given its name. To find IDs from the names, use functions nc_inq_dimid(), nc_inq_attnum(), and nc_inq_varid().

The inquiry functions are:

- nc_inq() Find number of dimensions, variables, and global attributes, and the unlimited dimid.
- nc_inq_att() Find attribute name, type, and length.
- nc_inq_dim() Find dimension name and length.
- nc_inq_var() Find variable name, type, num dimensions, dim IDs, and num attributes.
- nc_inq_dimid() Find dimension ID from its name.
- nc_inq_varid() Find variable ID from its name.
- nc_inq_format() Find file format: classic CDF-1, 64-bit offset CDF-2, or 64-bit data CDF-5
- nc_inq_libvers() Find the netCDF library version.