NetCDF  4.6.3
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
simple_nc4_wr.c
Go to the documentation of this file.
1 
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <netcdf.h>
18 
19 /* This is the name of the data file we will create. */
20 #define FILE_NAME "simple_nc4.nc"
21 
22 /* We are writing 2D data, a 6 x 12 grid. */
23 #define NDIMS 2
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  /* When we create netCDF variables, groups, dimensions, or types,
36  * we get back an ID for each one. */
37  int ncid, x_dimid, y_dimid, varid1, varid2, grp1id, grp2id, typeid;
38  int dimids[NDIMS];
39 
40  /* This is the data array we will write. It will be filled with a
41  * progression of numbers for this example. */
42  unsigned long long data_out[NX][NY];
43 
44  /* Loop indexes, and error handling. */
45  int x, y, retval;
46 
47  /* The following struct is written as a compound type. */
48  struct s1
49  {
50  int i1;
51  int i2;
52  };
53  struct s1 compound_data[NX][NY];
54 
55  /* Create some pretend data. */
56  for (x = 0; x < NX; x++)
57  for (y = 0; y < NY; y++)
58  {
59  data_out[x][y] = x * NY + y;
60  compound_data[x][y].i1 = 42;
61  compound_data[x][y].i2 = -42;
62  }
63 
64  /* Create the file. The NC_NETCDF4 flag tells netCDF to
65  * create a netCDF-4/HDF5 file.*/
66  if ((retval = nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)))
67  ERR(retval);
68 
69  /* Define the dimensions in the root group. Dimensions are visible
70  * in all subgroups. */
71  if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
72  ERR(retval);
73  if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
74  ERR(retval);
75 
76  /* The dimids passes the IDs of the dimensions of the variable. */
77  dimids[0] = x_dimid;
78  dimids[1] = y_dimid;
79 
80  /* Define two groups, "grp1" and "grp2." */
81  if ((retval = nc_def_grp(ncid, "grp1", &grp1id)))
82  ERR (retval);
83  if ((retval = nc_def_grp(ncid, "grp2", &grp2id)))
84  ERR (retval);
85 
86  /* Define an unsigned 64bit integer variable in grp1, using dimensions
87  * in the root group. */
88  if ((retval = nc_def_var(grp1id, "data", NC_UINT64, NDIMS,
89  dimids, &varid1)))
90  ERR(retval);
91 
92  /* Write unsigned long long data to the file. For netCDF-4 files,
93  * nc_enddef will be called automatically. */
94  if ((retval = nc_put_var_ulonglong(grp1id, varid1, &data_out[0][0])))
95  ERR(retval);
96 
97  /* Create a compound type. This will cause nc_reddef to be called. */
98  if (nc_def_compound(grp2id, sizeof(struct s1), "sample_compound_type",
99  &typeid))
100  ERR(retval);
101  if (nc_insert_compound(grp2id, typeid, "i1",
102  offsetof(struct s1, i1), NC_INT))
103  ERR(retval);
104  if (nc_insert_compound(grp2id, typeid, "i2",
105  offsetof(struct s1, i2), NC_INT))
106  ERR(retval);
107 
108  /* Define a compound type variable in grp2, using dimensions
109  * in the root group. */
110  if ((retval = nc_def_var(grp2id, "data", typeid, NDIMS,
111  dimids, &varid2)))
112  ERR(retval);
113 
114  /* Write the array of struct to the file. This will cause nc_endef
115  * to be called. */
116  if ((retval = nc_put_var(grp2id, varid2, &compound_data[0][0])))
117  ERR(retval);
118 
119  /* Close the file. */
120  if ((retval = nc_close(ncid)))
121  ERR(retval);
122 
123  printf("*** SUCCESS writing example file simple_nc4.nc!\n");
124  return 0;
125 }
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:207
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_ulonglong(int ncid, int varid, const unsigned long long *op)
Write an entire variable with one call.
Definition: dvarput.c:1009
Main header file for the C API.
EXTERNL int nc_put_var(int ncid, int varid, const void *op)
Write an entire variable with one call.
Definition: dvarput.c:931
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition: dfile.c:1470
EXTERNL int nc_def_compound(int ncid, size_t size, const char *name, nc_type *typeidp)
Create a compound type.
Definition: dcompound.c:63
#define NC_INT
signed 4 byte integer
Definition: netcdf.h:38
#define NC_NETCDF4
Use netCDF-4/HDF5 format.
Definition: netcdf.h:151
EXTERNL int nc_def_grp(int parent_ncid, const char *name, int *new_ncid)
Define a new group.
Definition: dgroup.c:268
EXTERNL int nc_insert_compound(int ncid, nc_type xtype, const char *name, size_t offset, nc_type field_typeid)
Insert a named field into a compound type.
Definition: dcompound.c:99
#define NC_CLOBBER
Destroy existing file.
Definition: netcdf.h:126
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.
Definition: dfile.c:562
#define NC_UINT64
unsigned 8-byte int
Definition: netcdf.h:46

Return to the Main Unidata NetCDF page.
Generated on Sat Apr 6 2019 08:19:00 for NetCDF. NetCDF is a Unidata library.