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