Previous: The C API Next: The C++ API Table of contents Frames User guide
2008 Unidata NetCDF Workshop for Developers and Data Providers > NetCDF APIs

5.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 User guide
2008 Unidata NetCDF Workshop for Developers and Data Providers > NetCDF APIs