NetCDF  4.9.2
simple_nc4_wr.c
/* Copyright 2019 University Corporation for Atmospheric
Research/Unidata. See COPYRIGHT file for conditions of use. */
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>
/* This is the name of the data file we will create. */
#define FILE_NAME "simple_nc4.nc"
/* We are writing 2D data, a 6 x 12 grid. */
#define NDIMS 2
#define NX 6
#define NY 12
/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
int
main()
{
/* When we create netCDF variables, groups, dimensions, or types,
* we get back an ID for each one. */
int ncid, x_dimid, y_dimid, varid1, varid2, grp1id, grp2id, typeid;
int dimids[NDIMS];
/* This is the data array we will write. It will be filled with a
* progression of numbers for this example. */
unsigned long long data_out[NX][NY];
/* Loop indexes, and error handling. */
int x, y, retval;
/* The following struct is written as a compound type. */
struct s1
{
int i1;
int i2;
};
struct s1 compound_data[NX][NY];
/* Create some pretend data. */
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;
}
/* Create the file. The NC_NETCDF4 flag tells netCDF to
* create a netCDF-4/HDF5 file.*/
if ((retval = nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)))
ERR(retval);
/* Define the dimensions in the root group. Dimensions are visible
* in all subgroups. */
if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
ERR(retval);
if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
ERR(retval);
/* The dimids passes the IDs of the dimensions of the variable. */
dimids[0] = x_dimid;
dimids[1] = y_dimid;
/* Define two groups, "grp1" and "grp2." */
if ((retval = nc_def_grp(ncid, "grp1", &grp1id)))
ERR (retval);
if ((retval = nc_def_grp(ncid, "grp2", &grp2id)))
ERR (retval);
/* Define an unsigned 64bit integer variable in grp1, using dimensions
* in the root group. */
if ((retval = nc_def_var(grp1id, "data", NC_UINT64, NDIMS,
dimids, &varid1)))
ERR(retval);
/* Write unsigned long long data to the file. For netCDF-4 files,
* nc_enddef will be called automatically. */
if ((retval = nc_put_var_ulonglong(grp1id, varid1, &data_out[0][0])))
ERR(retval);
/* Create a compound type. This will cause nc_reddef to be called. */
if (nc_def_compound(grp2id, sizeof(struct s1), "sample_compound_type",
&typeid))
ERR(retval);
if (nc_insert_compound(grp2id, typeid, "i1",
offsetof(struct s1, i1), NC_INT))
ERR(retval);
if (nc_insert_compound(grp2id, typeid, "i2",
offsetof(struct s1, i2), NC_INT))
ERR(retval);
/* Define a compound type variable in grp2, using dimensions
* in the root group. */
if ((retval = nc_def_var(grp2id, "data", typeid, NDIMS,
dimids, &varid2)))
ERR(retval);
/* Write the array of struct to the file. This will cause nc_endef
* to be called. */
if ((retval = nc_put_var(grp2id, varid2, &compound_data[0][0])))
ERR(retval);
/* Close the file. */
if ((retval = nc_close(ncid)))
ERR(retval);
printf("*** SUCCESS writing example file simple_nc4.nc!\n");
return 0;
}
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_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:920
int nc_put_var_ulonglong(int ncid, int varid, const unsigned long long *op)
Write an entire variable with one call.
Definition: dvarput.c:998
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:153
#define NC_INT
signed 4 byte integer
Definition: netcdf.h:38
#define NC_CLOBBER
Destroy existing file.
Definition: netcdf.h:129
#define NC_UINT64
unsigned 8-byte int
Definition: netcdf.h:46