Previous: The Simple XY Example in C++ Next: The NetCDF Example Data Sets Table of contents Frames 2012 Unidata NetCDF Workshop > Using netCDF in programs

11.8 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 Simple XY Example in C++ Next: The NetCDF Example Data Sets Table of contents Frames 2012 Unidata NetCDF Workshop > Using netCDF in programs