#include <stdlib.h>
#include <stdio.h>
 
#define FILE_NAME "simple_nc4.nc"
 
#define NDIMS 2
#define NX 6
#define NY 12
 
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
 
int
main()
{
   
   int ncid, x_dimid, y_dimid, varid1, varid2, grp1id, grp2id, typeid;
   int dimids[NDIMS];
 
   
   unsigned long long data_out[NX][NY];
 
   
   int x, y, retval;
 
   
   struct s1
   {
         int i1;
         int i2;
   };
   struct s1 compound_data[NX][NY];
 
   
   for (x = 0; x < NX; x++)
      for (y = 0; y < NY; y++)
      {
         data_out[x][y] = x * NY + y;
         compound_data[x][y].i1 = 42;
         compound_data[x][y].i2 = -42;
      }
 
   
      ERR(retval);
 
   
   if ((retval = 
nc_def_dim(ncid, 
"x", NX, &x_dimid)))
 
      ERR(retval);
   if ((retval = 
nc_def_dim(ncid, 
"y", NY, &y_dimid)))
 
      ERR(retval);
 
   
   dimids[0] = x_dimid;
   dimids[1] = y_dimid;
 
   
      ERR (retval);
      ERR (retval);
 
   
                            dimids, &varid1)))
      ERR(retval);
 
   
      ERR(retval);
 
   
                       &typeid))
      ERR(retval);
                          offsetof(
struct s1, i1), 
NC_INT))
      ERR(retval);
                          offsetof(
struct s1, i2), 
NC_INT))
      ERR(retval);
 
   
   if ((retval = 
nc_def_var(grp2id, 
"data", 
typeid, NDIMS,
 
                            dimids, &varid2)))
      ERR(retval);
 
   
   if ((retval = 
nc_put_var(grp2id, varid2, &compound_data[0][0])))
 
      ERR(retval);
 
   
      ERR(retval);
 
   printf("*** SUCCESS writing example file simple_nc4.nc!\n");
   return 0;
}
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.
EXTERNL int nc_def_dim(int ncid, const char *name, size_t len, int *idp)
Define a new dimension.
EXTERNL int nc_def_grp(int parent_ncid, const char *name, int *new_ncid)
Define a new group.
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.
EXTERNL int nc_def_compound(int ncid, size_t size, const char *name, nc_type *typeidp)
Create a compound type.
EXTERNL int nc_put_var(int ncid, int varid, const void *op)
Write an entire variable with one call.
int nc_put_var_ulonglong(int ncid, int varid, const unsigned long long *op)
Write an entire variable with one call.
EXTERNL int nc_def_var(int ncid, const char *name, nc_type xtype, int ndims, const int *dimidsp, int *varidp)
Define a new variable.
Main header file for the C API.
#define NC_NETCDF4
Use netCDF-4/HDF5 format.
#define NC_INT
signed 4 byte integer
#define NC_CLOBBER
Destroy existing file.
#define NC_UINT64
unsigned 8-byte int