5.6 Creating a Compound Type: nc_def_compound
Create a compound type. Provide an ncid, a name, and a total size (in
bytes) of one element of the completed compound type.
After calling this function, fill out the type with repeated calls to
nc_insert_compound (see nc_insert_compound). Call
nc_insert_compound once for each field you wish to insert into the
compound type.
Note that there does not seem to be a way to read such types into
structures in Fortran 90 (and there are no structures in Fortran 77).
Usage
int nc_def_compound(int ncid, size_t size, char *name, nc_type *typeidp);
ncid- The groupid where this compound type will be created.
size- The size, in bytes, of the compound type.
name- The name of the new compound type.
typeidp- A pointer to an nc_type. The typeid of the new type will be placed there.
Errors
NC_NOERR- No error.
NC_EBADID- Bad group id.
NC_ENAMEINUSE- That name is in use.
NC_EMAXNAME- Name exceeds max length NC_MAX_NAME.
NC_EBADNAME- Name contains illegal characters.
NC_ENOTNC4- Attempting a netCDF-4 operation on a netCDF-3 file. NetCDF-4
operations can only be performed on files defined with a create mode
which includes flag NC_NETCDF4. (see nc_open).
NC_ESTRICTNC3- This file was created with the strict netcdf-3 flag, therefore
netcdf-4 operations are not allowed. (see nc_open).
NC_EHDFERR- An error was reported by the HDF5 layer.
NC_EPERM- Attempt to write to a read-only file.
NC_ENOTINDEFINE- Not in define mode.
Example
struct s1
{
int i1;
int i2;
};
struct s1 data[DIM_LEN], data_in[DIM_LEN];
/* Create a file with a compound type. Write a little data. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_compound(ncid, sizeof(struct s1), SVC_REC, &typeid)) ERR;
if (nc_insert_compound(ncid, typeid, BATTLES_WITH_KLINGONS,
HOFFSET(struct s1, i1), NC_INT)) ERR;
if (nc_insert_compound(ncid, typeid, DATES_WITH_ALIENS,
HOFFSET(struct s1, i2), NC_INT)) ERR;
if (nc_def_dim(ncid, STARDATE, DIM_LEN, &dimid)) ERR;
if (nc_def_var(ncid, SERVICE_RECORD, typeid, 1, dimids, &varid)) ERR;
if (nc_put_var(ncid, varid, data)) ERR;
if (nc_close(ncid)) ERR;