NetCDF
4.8.1
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
32
int
33
main()
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
}
NC_NOWRITE
#define NC_NOWRITE
Set read-only access for nc_open().
Definition:
netcdf.h:125
nc_get_var_int
int nc_get_var_int(int ncid, int varid, int *ip)
Definition:
dvarget.c:1063
nc_close
EXTERNL int nc_close(int ncid)
Definition:
dfile.c:1291
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
Return to the Main Unidata NetCDF page.
Generated on Mon Aug 23 2021 13:28:32 for NetCDF. NetCDF is a
Unidata
library.