forked from Unidata/netcdf-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
109 lines (86 loc) · 3.09 KB
/
file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* This example program is part of Unidata's netCDF library for
scientific data access.
This program demonstrates various ways to create a netCDF file,
open an existing file, and close a file.
Ed Hartnett, 5/29/4
$Id: file.c,v 1.1 2004/07/26 14:04:42 ed Exp $
*/
#include <netcdf.h>
#include <stdio.h>
/* This macro handles errors by outputting a message to stdout and
then exiting. */
#define BAIL(e) do { \
printf("Bailing out in file %s, line %d, error:%s.\n", \
__FILE__, __LINE__, nc_strerror(e)); \
return e; \
} while (0)
#define FILENAME "test.nc"
#define VARNAME "var1"
#define DIMNAME "d1"
#define NUMDIMS 1
#define DIMLEN 10
#define ERROR 2 /* exit code for example failure */
int
main()
{
/* These are netCDF IDs for file, dimension, and variable. */
int ncid, dimid, varid;
/* This array will hold one ID for each dimension in the
variable, in this case, one. */
int dimids[NUMDIMS];
/* This is some one-dimensional phoney data to write and read. */
int data_out[] = {0,1,2,3,4,5,6,7,8,9};
int data_in[DIMLEN];
int i, res;
/* Create a classic format netCDF file, overwritting any file of
this name that may already exist. */
if ((res = nc_create(FILENAME, NC_CLOBBER, &ncid)))
BAIL(res);
/* Define a dimension. The functions will return a dimension ID to
dimid. */
if ((res = nc_def_dim(ncid, DIMNAME, DIMLEN, &dimid)))
BAIL(res);
/* Define a variable. First we must specify which dimensions this
variable uses, by adding their dimension IDs to the dimids array
that we pass into nc_def_var. In this example, there is just one
dimension. */
dimids[0] = dimid;
if ((res = nc_def_var(ncid, VARNAME, NC_INT, NUMDIMS, dimids, &varid)))
BAIL(res);
/* The enddef function tells the library that we are done with
defining metadata in the newly-created file, and now want to
write some data. */
if ((res = nc_enddef(ncid)))
BAIL(res);
/* Write our phoney integer data. Since we've already specified the
shape of this variable, we only need to provide a pointer to the
start of the data. */
if ((res = nc_put_var_int(ncid, varid, data_out)))
BAIL(res);
/* Close the file. This flushes all buffers and frees any resources
associated with the file. We are closing the file here so that
we can demonstrate nc_open. Usually we wouldn't close the file
until done with it. */
if ((res = nc_close(ncid)))
BAIL(res);
/* Now open the file for read-only access. */
if ((res = nc_open(FILENAME, NC_NOWRITE, &ncid)))
BAIL(res);
/* Find the varid that represents our data. */
if ((res = nc_inq_varid(ncid, VARNAME, &varid)))
BAIL(res);
/* Read the data, all in one lump. */
if ((res = nc_get_var_int(ncid, varid, data_in)))
BAIL(res);
/* Ensure we got the data we expected. */
for (i=0; i<DIMLEN; i++)
if (data_in[i] != data_out[i])
{
fprintf(stderr, "Unexpected value!\n");
return ERROR;
}
/* Close the file again. */
if ((res = nc_close(ncid)))
BAIL(res);
return 0;
}