Next: Adding, Previous: Reading Known, Up: Use of the NetCDF Library
It is possible to write programs (e.g., generic software) which do such things as processing every variable, without needing to know in advance the names of these variables. Similarly, the names of dimensions and attributes may be unknown.
Names and other information about netCDF objects may be obtained from netCDF datasets by calling inquire functions. These return information about a whole netCDF dataset, a dimension, a variable, or an attribute. The following template illustrates how they are used:
NF_OPEN ! open existing netCDF dataset ... NF_INQ ! find out what is in it ... NF_INQ_DIM ! get dimension names, lengths ... NF_INQ_VAR ! get variable names, types, shapes ... NF_INQ_ATTNAME ! get attribute names ... NF_INQ_ATT ! get attribute values ... NF_GET_ATT ! get attribute values ... NF_GET_VAR ! get values of variables ... NF_CLOSE ! close netCDF dataset
As in the previous example, a single call opens the existing netCDF dataset, returning a netCDF ID. This netCDF ID is given to the NF_INQ routine, which returns the number of dimensions, the number of variables, the number of global attributes, and the ID of the unlimited dimension, if there is one.
All the inquire functions are inexpensive to use and require no I/O, since the information they provide is stored in memory when a netCDF dataset is first opened.
Dimension IDs use consecutive integers, beginning at 1. Also dimensions, once created, cannot be deleted. Therefore, knowing the number of dimension IDs in a netCDF dataset means knowing all the dimension IDs: they are the integers 1, 2, 3, ... up to the number of dimensions. For each dimension ID, a call to the inquire function NF_INQ_DIM returns the dimension name and length.
Variable IDs are also assigned from consecutive integers 1, 2, 3, ... up to the number of variables. These can be used in NF_INQ_VAR calls to find out the names, types, shapes, and the number of attributes assigned to each variable.
Once the number of attributes for a variable is known, successive calls to NF_INQ_ATTNAME return the name for each attribute given the netCDF ID, variable ID, and attribute number. Armed with the attribute name, a call to NF_INQ_ATT returns its type and length. Given the type and length, you can allocate enough space to hold the attribute values. Then a call to a member of the NF_GET_ATT family returns the attribute values.
Once the IDs and shapes of netCDF variables are known, data values can be accessed by calling a member of the NF_GET_VAR1 family for single values, or members of the NF_GET_VAR, NF_GET_VARA, NF_GET_VARS, or NF_GET_VARM for various kinds of array access.