NetCDF  4.9.2
simple_xy_wr.c
Go to the documentation of this file.
1 
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <netcdf.h>
14 
15 /* This is the name of the data file we will create. */
16 #define FILE_NAME "simple_xy.nc"
17 
18 /* We are writing 2D data, a 6 x 12 grid. */
19 #define NDIMS 2
20 #define NX 6
21 #define NY 12
22 
23 /* Handle errors by printing an error message and exiting with a
24  * non-zero status. */
25 #define ERRCODE 2
26 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
27 
28 int
29 main()
30 {
31  /* When we create netCDF variables and dimensions, we get back an
32  * ID for each one. */
33  int ncid, x_dimid, y_dimid, varid;
34  int dimids[NDIMS];
35 
36  /* This is the data array we will write. It will be filled with a
37  * progression of numbers for this example. */
38  int data_out[NX][NY];
39 
40  /* Loop indexes, and error handling. */
41  int x, y, retval;
42 
43  /* Create some pretend data. If this wasn't an example program, we
44  * would have some real data to write, for example, model
45  * output. */
46  for (x = 0; x < NX; x++)
47  for (y = 0; y < NY; y++)
48  data_out[x][y] = x * NY + y;
49 
50  /* Always check the return code of every netCDF function call. In
51  * this example program, any retval which is not equal to NC_NOERR
52  * (0) will cause the program to print an error message and exit
53  * with a non-zero return code. */
54 
55  /* Create the file. The NC_CLOBBER parameter tells netCDF to
56  * overwrite this file, if it already exists.*/
57  if ((retval = nc_create(FILE_NAME, NC_CLOBBER, &ncid)))
58  ERR(retval);
59 
60  /* Define the dimensions. NetCDF will hand back an ID for each. */
61  if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
62  ERR(retval);
63  if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
64  ERR(retval);
65 
66  /* The dimids array is used to pass the IDs of the dimensions of
67  * the variable. */
68  dimids[0] = x_dimid;
69  dimids[1] = y_dimid;
70 
71  /* Define the variable. The type of the variable in this case is
72  * NC_INT (4-byte integer). */
73  if ((retval = nc_def_var(ncid, "data", NC_INT, NDIMS,
74  dimids, &varid)))
75  ERR(retval);
76 
77  /* End define mode. This tells netCDF we are done defining
78  * metadata. */
79  if ((retval = nc_enddef(ncid)))
80  ERR(retval);
81 
82  /* Write the pretend data to the file. Although netCDF supports
83  * reading and writing subsets of data, in this case we write all
84  * the data in one operation. */
85  if ((retval = nc_put_var_int(ncid, varid, &data_out[0][0])))
86  ERR(retval);
87 
88  /* Close the file. This frees up any internal netCDF resources
89  * associated with the file, and flushes any buffers. */
90  if ((retval = nc_close(ncid)))
91  ERR(retval);
92 
93  printf("*** SUCCESS writing example file simple_xy.nc!\n");
94  return 0;
95 }
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition: dfile.c:1302
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.
Definition: dfile.c:400
EXTERNL int nc_enddef(int ncid)
Leave define mode.
Definition: dfile.c:1029
EXTERNL int nc_def_dim(int ncid, const char *name, size_t len, int *idp)
Define a new dimension.
Definition: ddim.c:121
int nc_put_var_int(int ncid, int varid, const int *op)
Write an entire variable with one call.
Definition: dvarput.c:950
EXTERNL int nc_def_var(int ncid, const char *name, nc_type xtype, int ndims, const int *dimidsp, int *varidp)
Define a new variable.
Definition: dvar.c:214
Main header file for the C API.
#define NC_INT
signed 4 byte integer
Definition: netcdf.h:38
#define NC_CLOBBER
Destroy existing file.
Definition: netcdf.h:129