NetCDF  4.6.3
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
simple_xy_rd.c
Go to the documentation of this file.
1 
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <netcdf.h>
14 
15 /* This is the name of the data file we will read. */
16 #define FILE_NAME "simple_xy.nc"
17 
18 /* We are reading 2D data, a 6 x 12 grid. */
19 #define NX 6
20 #define NY 12
21 
22 /* Handle errors by printing an error message and exiting with a
23  * non-zero status. */
24 #define ERRCODE 2
25 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
26 
27 int
28 main()
29 {
30  /* This will be the netCDF ID for the file and data variable. */
31  int ncid, varid;
32 
33  int data_in[NX][NY];
34 
35  /* Loop indexes, and error handling. */
36  int x, y, retval;
37 
38  /* Open the file. NC_NOWRITE tells netCDF we want read-only access
39  * to the file.*/
40  if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
41  ERR(retval);
42 
43  /* Get the varid of the data variable, based on its name. */
44  if ((retval = nc_inq_varid(ncid, "data", &varid)))
45  ERR(retval);
46 
47  /* Read the data. */
48  if ((retval = nc_get_var_int(ncid, varid, &data_in[0][0])))
49  ERR(retval);
50 
51  /* Check the data. */
52  for (x = 0; x < NX; x++)
53  for (y = 0; y < NY; y++)
54  if (data_in[x][y] != x * NY + y)
55  return ERRCODE;
56 
57  /* Close the file, freeing all resources. */
58  if ((retval = nc_close(ncid)))
59  ERR(retval);
60 
61  printf("*** SUCCESS reading example file %s!\n", FILE_NAME);
62  return 0;
63 }
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.