Next: , Previous: Creation, Up: Useful Functions


3.2 Reading NetCDF Files of Known Structure

To read a netCDF file of known structure, you need to:

open the file
Specify the file name and whether you want read-write or read-only access.
read variable or attribute data
Read the data or attributes of interest.
close the file
Release all resources associated with this file.

Use ncdump to learn the structure of a file (use the -h option). For more information about ncdump see NetCDF Utilities.

3.2.1 Numbering of NetCDF IDs

In C, Fortran 77, and Fortran 90, netCDF objects are identified by an integer: the ID. NetCDF functions use this ID to identify the object. It's helpful for the programmer to understand these IDs.

Open data files, dimensions, variables, and attributes are each numbered independently, and are always numbered in the order in which they were defined. (They also appear in this order in ncdump output.) Numbering starts with 0 in C, and 1 in Fortran 77/90.

For example, the first variable defined in a file will have an ID of 0 in C programs, and 1 in Fortran programs, and functions that apply to a variable will need to know the ID of the variable you mean.

(The numbering of files is an exception: file IDs are assigned by the operating system when a file is opened, and are not permanently associated with the file. IDs for netCDF dimensions and variables are persistent, but deleting an attribute changes subsequent attribute numbers.)

Although netCDF refers to everything by an integer id (varid, dimid, attnum), there are inquiry functions which, given a name, will return an ID. For example, in the C API, nc_inq_varid will take a character string (the name), and give back the ID of the variable of that name. The variable ID is then used in subsequent calls (to read the data, for example).

Other inquiry functions exist to further describe the file. (see Inquiry Functions).