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

Return to the Main Unidata NetCDF page.
Generated on Fri May 11 2018 21:22:26 for NetCDF. NetCDF is a Unidata library.