NetCDF
4.8.1
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. */
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
39
int
40
main()
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
}
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.