Next: , Previous: Reading Known, Up: Use of the NetCDF Library


1.3 Reading a netCDF Dataset with Unknown Names

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:

          NF90_OPEN                 ! open existing netCDF dataset
            ...
          NF90_INQUIRE              ! find out what is in it
               ...
             NF90_INQUIRE_DIMENSION ! get dimension names, lengths
               ...
             NF90_INQUIRE_VARIABLE  ! get variable names, types, shapes
                  ...
                NF90_INQ_ATTNAME    ! get attribute names
                  ...
                NF90_INQUIRE_ATTRIBUTE ! get other attribute information
                  ...
                NF90_GET_ATT        ! get attribute values
                  ...
             NF90_GET_VAR           ! get values of variables
               ...
          NF90_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 NF90_INQUIRE 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 NF90_INQUIRE_DIMENSION 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 NF90_INQUIRE_VARIABLE 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 NF90_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 NF90_INQUIRE_ATTRIBUTE returns its type and length. Given the type and length, you can allocate enough space to hold the attribute values. Then a call to NF90_GET_ATT returns the attribute values.

Once the IDs and shapes of netCDF variables are known, data values can be accessed by calling NF90_GET_VAR.