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