2008 Unidata NetCDF Workshop for Developers and Data Providers > User Defined Types in NetCDF-4
16.9 Enum Example
This example shows how to use enums from the C API.
char brady_name[NUM_BRADYS][NC_MAX_NAME + 1] = {"Mike", "Carol", "Greg", "Marsha",
"Peter", "Jan", "Bobby", "Whats-her-face",
"Alice"};
unsigned char brady_value[NUM_BRADYS] = {0, 1,2,3,4,5,6,7,8};
unsigned char data[BRADY_DIM_LEN] = {0, 4, 8};
unsigned char value_in;
/* Create a file. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
/* Create an enum type based on unsigned bytes. */
if (nc_def_enum(ncid, NC_UBYTE, BRADYS, &typeid)) ERR;
for (i = 0; i < NUM_BRADYS; i++)
if (nc_insert_enum(ncid, typeid, brady_name[i],
&brady_value[i])) ERR;
/* Check it out. */
if (nc_inq_user_type(ncid, typeid, name_in, &base_size_in, &base_nc_type_in,
&nfields_in, &class_in)) ERR;
if (strcmp(name_in, BRADYS) || base_size_in != 1 ||
base_nc_type_in != NC_UBYTE || nfields_in != NUM_BRADYS || class_in != NC_ENUM) ERR;
if (nc_inq_enum(ncid, typeid, name_in, &base_nc_type, &base_size_in, &num_members)) ERR;
if (strcmp(name_in, BRADYS) || base_nc_type != NC_UBYTE || base_size_in != 1 ||
num_members != NUM_BRADYS) ERR;
for (i = 0; i < NUM_BRADYS; i++)
{
if (nc_inq_enum_member(ncid, typeid, i, name_in, &value_in)) ERR;
if (strcmp(name_in, brady_name[i]) || value_in != brady_value[i]) ERR;
}
/* Write an att of this enum type. */
if (nc_put_att(ncid, NC_GLOBAL, ATT_NAME, typeid, BRADY_DIM_LEN, data)) ERR;
/* Close the file. */
if (nc_close(ncid)) ERR;
2008 Unidata NetCDF Workshop for Developers and Data Providers > User Defined Types in NetCDF-4