Previous: The C API Next: The C++ API Table of contents Frames 2010 Unidata NetCDF Workshop > Introduction to the NetCDF APIs and Example Programs

9.2 C API Example
C API example for reading data.
  ...
#include <netcdf.h>
  ...

/* Handle errors by printing an error message and exiting */
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}

  ...
   /* netCDF file ID and variable ID */
   int ncid, varid;

   /* array into which we will read values of 2D netCDF variable */
   float rh_array[NLAT][NLON]
  ...
   /* Open the file with read-only access, indicated by NC_NOWRITE flag */
   if ((retval = nc_open("foo.nc", NC_NOWRITE, &ncid)))
      ERR(retval);

   /* Get the id of the variable named "rh" */
   if ((retval = nc_inq_varid(ncid, "rh", &varid)))
      ERR(retval);

   /* Read entire variable "rh" as floats, rh_array must be big enough! */
   if ((retval = nc_get_var_float(ncid, varid, &rh_array[0][0])))
      ERR(retval);

   /* Close the file, freeing all resources. */
   if ((retval = nc_close(ncid)))
      ERR(retval);
  ...

 


Previous: The C API Next: The C++ API Table of contents Frames 2010 Unidata NetCDF Workshop > Introduction to the NetCDF APIs and Example Programs