Insert a named field into a compound type.
int nc_insert_array_compound(int ncid, nc_type xtype, const char *name, size_t offset, nc_type field_typeid, int ndims, const int *dim_sizes);
ncid
xtype
name
offset
field_typeid
NC_NOERR
NC_EBADID
NC_ENAMEINUSE
NC_EMAXNAME
NC_EBADNAME
NC_ENOTNC4
NC_ESTRICTNC3
NC_EHDFERR
NC_ENOTINDEFINE
NC_ETYPEDEFINED
This example comes from the test file libsrc4/tst_compounds.c, which writes data about some Star Fleet officers who are known to use netCDF data.
/* Since some aliens exists in different, or more than one, * dimensions, StarFleet keeps track of the dimensional abilities * of everyone on 7 dimensions. */ #define NUM_DIMENSIONS 7 struct dim_rec { int starfleet_id; int abilities[NUM_DIMENSIONS]; }; struct dim_rec dim_data_out[DIM_LEN], dim_data_in[DIM_LEN]; /* Create some phoney data. */ for (i=0; i<DIM_LEN; i++) { /* snip */ /* Dimensional data. */ dim_data_out[i].starfleet_id = i; for (j = 0; j < NUM_DIMENSIONS; j++) dim_data_out[i].abilities[j] = j; /* snip */ } printf("*** testing compound variable containing an array of ints..."); { nc_type field_typeid; int dim_sizes[] = {NUM_DIMENSIONS}; /* Create a file with a compound type which contains an array of * int. Write a little data. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_compound(ncid, sizeof(struct dim_rec), "SFDimRec", &typeid)) ERR; if (nc_insert_compound(ncid, typeid, "starfleet_id", HOFFSET(struct dim_rec, starfleet_id), NC_INT)) ERR; if (nc_insert_array_compound(ncid, typeid, "abilities", HOFFSET(struct dim_rec, abilities), NC_INT, 1, dim_sizes)) ERR; if (nc_inq_compound_field(ncid, xtype, 1, name, &offset, &field_typeid, &field_ndims, field_sizes)) ERR; if (strcmp(name, "abilities") || offset != 4 || field_typeid != NC_INT || field_ndims != 1 || field_sizes[0] != dim_sizes[0]) ERR; if (nc_def_dim(ncid, STARDATE, DIM_LEN, &dimid)) ERR; if (nc_def_var(ncid, "dimension_data", typeid, 1, dimids, &varid)) ERR; if (nc_put_var(ncid, varid, dim_data_out)) ERR; if (nc_close(ncid)) ERR; /* Open the file and take a look. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_var(ncid, 0, name, &xtype, &ndims, dimids, &natts)) ERR; if (strcmp(name, "dimension_data") || ndims != 1 || natts != 0 || dimids[0] != 0) ERR; if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR; if (nfields != 2 || size != sizeof(struct dim_rec) || strcmp(name, "SFDimRec")) ERR; if (nc_inq_compound_field(ncid, xtype, 1, name, &offset, &field_typeid, &field_ndims, field_sizes)) ERR; if (strcmp(name, "abilities") || offset != 4 || field_typeid != NC_INT || field_ndims != 1 || field_sizes[0] != NUM_DIMENSIONS) ERR; if (nc_get_var(ncid, varid, dim_data_in)) ERR; for (i=0; i<DIM_LEN; i++) { if (dim_data_in[i].starfleet_id != dim_data_out[i].starfleet_id) ERR; for (j = 0; j < NUM_DIMENSIONS; j++) if (dim_data_in[i].abilities[j] != dim_data_out[i].abilities[j]) ERR; } if (nc_close(ncid)) ERR; }