Create an enum type. Provide an ncid, a name, and a base integer type.
After calling this function, fill out the type with repeated calls to nc_insert_enum (see nc_insert_enum). Call nc_insert_enum once for each value you wish to make part of the enumeration.
int nc_def_enum(int ncid, nc_type base_typeid, const char *name,
nc_type *typeidp);
ncidbase_typeidnametypeidpNC_NOERRNC_EBADIDNC_ENAMEINUSENC_EMAXNAMENC_EBADNAMENC_ENOTNC4NC_ESTRICTNC3NC_EHDFERRNC_EPERMNC_ENOTINDEFINEThe following example, from libsrc4/tst_enums.c, shows the creation and use of an enum type, including the use of a fill value.
int dimid, varid;
size_t num_members_in;
int class_in;
unsigned char value_in;
enum clouds { /* a C enumeration */
CLEAR=0,
CUMULONIMBUS=1,
STRATUS=2,
STRATOCUMULUS=3,
CUMULUS=4,
ALTOSTRATUS=5,
NIMBOSTRATUS=6,
ALTOCUMULUS=7,
CIRROSTRATUS=8,
CIRROCUMULUS=9,
CIRRUS=10,
MISSING=255};
struct {
char *name;
unsigned char value;
} cloud_types[] = {
{"Clear", CLEAR},
{"Cumulonimbus", CUMULONIMBUS},
{"Stratus", STRATUS},
{"Stratocumulus", STRATOCUMULUS},
{"Cumulus", CUMULUS},
{"Altostratus", ALTOSTRATUS},
{"Nimbostratus", NIMBOSTRATUS},
{"Altocumulus", ALTOCUMULUS},
{"Cirrostratus", CIRROSTRATUS},
{"Cirrocumulus", CIRROCUMULUS},
{"Cirrus", CIRRUS},
{"Missing", MISSING}
};
int var_dims[VAR2_RANK];
unsigned char att_val;
unsigned char cloud_data[DIM2_LEN] = {
CLEAR, STRATUS, CLEAR, CUMULONIMBUS, MISSING};
unsigned char cloud_data_in[DIM2_LEN];
if (nc_create(FILE_NAME, NC_CLOBBER | NC_NETCDF4, &ncid)) ERR;
/* Create an enum type. */
if (nc_def_enum(ncid, NC_UBYTE, TYPE2_NAME, &typeid)) ERR;
num_members = (sizeof cloud_types) / (sizeof cloud_types[0]);
for (i = 0; i < num_members; i++)
if (nc_insert_enum(ncid, typeid, cloud_types[i].name,
&cloud_types[i].value)) ERR;
/* Declare a station dimension */
if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimid)) ERR;
/* Declare a variable of the enum type */
var_dims[0] = dimid;
if (nc_def_var(ncid, VAR2_NAME, typeid, VAR2_RANK, var_dims, &varid)) ERR;
/* Create and write a variable attribute of the enum type */
att_val = MISSING;
if (nc_put_att(ncid, varid, ATT2_NAME, typeid, ATT2_LEN, &att_val)) ERR;
if (nc_enddef(ncid)) ERR;
/* Store some data of the enum type */
if(nc_put_var(ncid, varid, cloud_data)) ERR;
/* Write the file. */
if (nc_close(ncid)) ERR;