NetCDF  4.6.3
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
simple_xy_nc4_rd.c
Go to the documentation of this file.
1 
16 #include <config.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <netcdf.h>
20 
21 /* This is the name of the data file we will read. */
22 #define FILE_NAME "simple_xy_nc4.nc"
23 
24 /* We are reading 2D data, a 6 x 12 grid. */
25 #define NX 6
26 #define NY 12
27 
28 /* Handle errors by printing an error message and exiting with a
29  * non-zero status. */
30 #define ERRCODE 2
31 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
32 
33 int
34 main()
35 {
36  /* This will be the netCDF ID for the file and data variable. */
37  int ncid, varid;
38 
39  int data_in[NX][NY];
40 
41  /* Loop indexes, and error handling. */
42  int x, y, retval;
43 
44  /* Open the file. NC_NOWRITE tells netCDF we want read-only access
45  * to the file.*/
46  if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
47  ERR(retval);
48 
49  /* Get the varid of the data variable, based on its name. */
50  if ((retval = nc_inq_varid(ncid, "data", &varid)))
51  ERR(retval);
52 
53  /* Read the data. */
54  if ((retval = nc_get_var_int(ncid, varid, &data_in[0][0])))
55  ERR(retval);
56 
57  /* Check the data. */
58  for (x = 0; x < NX; x++)
59  for (y = 0; y < NY; y++)
60  if (data_in[x][y] != x * NY + y)
61  return ERRCODE;
62 
63  /* Close the file, freeing all resources. */
64  if ((retval = nc_close(ncid)))
65  ERR(retval);
66 
67  printf("*** SUCCESS reading example file %s!\n", FILE_NAME);
68  return 0;
69 }
Main header file for the C API.
#define NC_NOWRITE
Set read-only access for nc_open().
Definition: netcdf.h:123
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition: dfile.c:1470
int nc_get_var_int(int ncid, int varid, int *ip)
Read an entire variable in one call.
Definition: dvarget.c:1075
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
Open an existing netCDF file.
Definition: dfile.c:828
EXTERNL int nc_inq_varid(int ncid, const char *name, int *varidp)
Find the ID of a variable, from the name.
Definition: dvarinq.c:62

Return to the Main Unidata NetCDF page.
Generated on Sat Apr 6 2019 08:19:00 for NetCDF. NetCDF is a Unidata library.