NetCDF  4.6.3
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
simple_nc4_rd.c
Go to the documentation of this file.
1 
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <netcdf.h>
18 
19 /* This is the name of the data file we will read. */
20 #define FILE_NAME "simple_nc4.nc"
21 
22 /* We are reading 2D data, a 6 x 12 grid. */
23 #define NX 6
24 #define NY 12
25 
26 /* Handle errors by printing an error message and exiting with a
27  * non-zero status. */
28 #define ERRCODE 2
29 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
30 
31 int
32 main()
33 {
34  /* There will be netCDF IDs for the file, each group, and each
35  * variable. */
36  int ncid, varid1, varid2, grp1id, grp2id;
37 
38  unsigned long long data_in[NX][NY];
39 
40  /* Loop indexes, and error handling. */
41  int x, y, retval;
42 
43  /* The following struct is written as a compound type. */
44  struct s1
45  {
46  int i1;
47  int i2;
48  };
49  struct s1 compound_data[NX][NY];
50 
51  /* Open the file. NC_NOWRITE tells netCDF we want read-only access
52  * to the file.*/
53  if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
54  ERR(retval);
55 
56  /* Get the group ids of our two groups. */
57  if ((retval = nc_inq_ncid(ncid, "grp1", &grp1id)))
58  ERR(retval);
59  if ((retval = nc_inq_ncid(ncid, "grp2", &grp2id)))
60  ERR(retval);
61 
62  /* Get the varid of the uint64 data variable, based on its name, in
63  * grp1. */
64  if ((retval = nc_inq_varid(grp1id, "data", &varid1)))
65  ERR(retval);
66 
67  /* Read the data. */
68  if ((retval = nc_get_var_ulonglong(grp1id, varid1, &data_in[0][0])))
69  ERR(retval);
70 
71  /* Get the varid of the compound data variable, based on its name,
72  * in grp2. */
73  if ((retval = nc_inq_varid(grp2id, "data", &varid2)))
74  ERR(retval);
75 
76  /* Read the data. */
77  if ((retval = nc_get_var(grp2id, varid2, &compound_data[0][0])))
78  ERR(retval);
79 
80  /* Check the data. */
81  for (x = 0; x < NX; x++)
82  for (y = 0; y < NY; y++)
83  {
84  if (data_in[x][y] != x * NY + y ||
85  compound_data[x][y].i1 != 42 ||
86  compound_data[x][y].i2 != -42)
87  return ERRCODE;
88  }
89 
90  /* Close the file, freeing all resources. */
91  if ((retval = nc_close(ncid)))
92  ERR(retval);
93 
94  printf("*** SUCCESS reading example file %s!\n", FILE_NAME);
95  return 0;
96 }
int nc_get_var_ulonglong(int ncid, int varid, unsigned long long *ip)
Read an entire variable in one call.
Definition: dvarget.c:1123
EXTERNL int nc_inq_ncid(int ncid, const char *name, int *grp_ncid)
Return the group ID for a group given the name.
Definition: dgroup.c:56
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
EXTERNL int nc_get_var(int ncid, int varid, void *ip)
Read an entire variable in one call.
Definition: dvarget.c:1045
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.