NetCDF 4.9.3
Loading...
Searching...
No Matches
simple_nc4_wr.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. */
17
18#include <stdlib.h>
19#include <stdio.h>
20#include <netcdf.h>
21
22/* This is the name of the data file we will create. */
23#define FILE_NAME "simple_nc4.nc"
24
25/* We are writing 2D data, a 6 x 12 grid. */
26#define NDIMS 2
27#define NX 6
28#define NY 12
29
30/* Handle errors by printing an error message and exiting with a
31 * non-zero status. */
32#define ERRCODE 2
33#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
34
35int
36main()
37{
38 /* When we create netCDF variables, groups, dimensions, or types,
39 * we get back an ID for each one. */
40 int ncid, x_dimid, y_dimid, varid1, varid2, grp1id, grp2id, typeid;
41 int dimids[NDIMS];
42
43 /* This is the data array we will write. It will be filled with a
44 * progression of numbers for this example. */
45 unsigned long long data_out[NX][NY];
46
47 /* Loop indexes, and error handling. */
48 int x, y, retval;
49
50 /* The following struct is written as a compound type. */
51 struct s1
52 {
53 int i1;
54 int i2;
55 };
56 struct s1 compound_data[NX][NY];
57
58 /* Create some pretend data. */
59 for (x = 0; x < NX; x++)
60 for (y = 0; y < NY; y++)
61 {
62 data_out[x][y] = x * NY + y;
63 compound_data[x][y].i1 = 42;
64 compound_data[x][y].i2 = -42;
65 }
66
67 /* Create the file. The NC_NETCDF4 flag tells netCDF to
68 * create a netCDF-4/HDF5 file.*/
69 if ((retval = nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)))
70 ERR(retval);
71
72 /* Define the dimensions in the root group. Dimensions are visible
73 * in all subgroups. */
74 if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
75 ERR(retval);
76 if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
77 ERR(retval);
78
79 /* The dimids passes the IDs of the dimensions of the variable. */
80 dimids[0] = x_dimid;
81 dimids[1] = y_dimid;
82
83 /* Define two groups, "grp1" and "grp2." */
84 if ((retval = nc_def_grp(ncid, "grp1", &grp1id)))
85 ERR (retval);
86 if ((retval = nc_def_grp(ncid, "grp2", &grp2id)))
87 ERR (retval);
88
89 /* Define an unsigned 64bit integer variable in grp1, using dimensions
90 * in the root group. */
91 if ((retval = nc_def_var(grp1id, "data", NC_UINT64, NDIMS,
92 dimids, &varid1)))
93 ERR(retval);
94
95 /* Write unsigned long long data to the file. For netCDF-4 files,
96 * nc_enddef will be called automatically. */
97 if ((retval = nc_put_var_ulonglong(grp1id, varid1, &data_out[0][0])))
98 ERR(retval);
99
100 /* Create a compound type. This will cause nc_reddef to be called. */
101 if (nc_def_compound(grp2id, sizeof(struct s1), "sample_compound_type",
102 &typeid))
103 ERR(retval);
104 if (nc_insert_compound(grp2id, typeid, "i1",
105 offsetof(struct s1, i1), NC_INT))
106 ERR(retval);
107 if (nc_insert_compound(grp2id, typeid, "i2",
108 offsetof(struct s1, i2), NC_INT))
109 ERR(retval);
110
111 /* Define a compound type variable in grp2, using dimensions
112 * in the root group. */
113 if ((retval = nc_def_var(grp2id, "data", typeid, NDIMS,
114 dimids, &varid2)))
115 ERR(retval);
116
117 /* Write the array of struct to the file. This will cause nc_endef
118 * to be called. */
119 if ((retval = nc_put_var(grp2id, varid2, &compound_data[0][0])))
120 ERR(retval);
121
122 /* Close the file. */
123 if ((retval = nc_close(ncid)))
124 ERR(retval);
125
126 printf("*** SUCCESS writing example file simple_nc4.nc!\n");
127 return 0;
128}
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition dfile.c:1300
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.
Definition dfile.c:398
EXTERNL int nc_def_dim(int ncid, const char *name, size_t len, int *idp)
Define a new dimension.
Definition ddim.c:121
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
EXTERNL int nc_def_compound(int ncid, size_t size, const char *name, nc_type *typeidp)
Create a compound type.
Definition dcompound.c:63
EXTERNL int nc_put_var(int ncid, int varid, const void *op)
Write an entire variable with one call.
Definition dvarput.c:922
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_NETCDF4
Use netCDF-4/HDF5 format.
Definition netcdf.h:162
#define NC_INT
signed 4 byte integer
Definition netcdf.h:38
#define NC_CLOBBER
Destroy existing file.
Definition netcdf.h:138
#define NC_UINT64
unsigned 8-byte int
Definition netcdf.h:46