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