Go to the previous, next section.

Reading a NetCDF File with Known Names

If you know the names of the dimensions, variables, and attributes in a netCDF file, you can write calls to read data from the file; you don't need to include the "inquire" calls that determine the dimensions, variables, and attributes. If you employ such knowledge about particular netCDF files, the program you write will lack generality. It will only work with files that have the assumed names and structure, so you will be losing some of the advantages of using the netCDF interface. However, you may be writing software that expects the user or some other program to supply variable or dimension names, perhaps as subroutine or command line arguments. In that case, the resulting program could be quite general.

When you know the names of some variables of interest and their dimensions, the order of typical C calls to read data from those variables in a netCDF file is:

    ncopen           /* open existing netCDF */
      ...
    ncdimid          /* get dimension IDs to use in accessing data */
      ...
    ncvarid          /* get variable IDs */
      ...
    ncattget         /* get attribute values, if needed */
      ...
    ncvarget         /* get values of variables */
      ...
    ncclose          /* close netCDF */

In FORTRAN, the corresponding sequence looks like this:

    NCOPN            !  open existing netCDF 
      ...
    NCDID            !  get dimension IDs to use in accessing data 
      ...
    NCVID            !  get variable IDs 
      ...
    NCAGT or NCAGTC  !  get attribute values, if needed 
      ...
    NCVGT or NCVGTC  !  get values of variables 
      ...
    NCCLOS           !  close netCDF 

First, a single call opens the netCDF file, given the file name, and returns a netCDF ID that is used to refer to the netCDF in all subsequent calls.

Next, a call to ncdimid (or NCDID) for each dimension of interest gets the dimension ID from the dimension name. Dimension IDs, like netCDF IDs, are small integers used to refer to dimensions in subsequent calls. Similarly, each required variable ID is determined from its name by a call to ncvarid (or NCVID). Once variable IDs are known, variable attribute values can be retrieved using the netCDF ID, the variable ID, and the desired attribute name as input to ncattget (or NCAGT or NCAGTC) for each desired attribute. Variable data values can be directly accessed from the netCDF file with ncvarget1 (or NCVGT1 or NCVG1C) for single values, ncvarget or ncvargetg (or NCVGT, NCVGTC, NCVGTG, or NCVGGC) for hyperslabs of values, or ncrecget for records of values. To minimize the number of disk accesses, you should remember that the last dimension in C (first dimension in FORTRAN) varies fastest when using hyperslab access.

Finally, the netCDF file can be closed with ncclose (or NCCLOS) when you are finished with it to free system resources. There is no harm in not closing a file open only for reading.

Go to the previous, next section.