Next: , Previous: nc_insert_array_compound, Up: User Defined Data Types


5.10 Learn About a Compound Type: nc_inq_compound

Get the number of fields, len, and name of a compound type.

Usage

     int nc_inq_compound(int ncid, nc_type xtype, char *name, size_t *sizep,
                         size_t *nfieldsp);
ncid
The ID of any group in the file that contains the compound type.
xtype
The typeid for this compound type, as returned by nc_def_compound, or nc_inq_var.
name
Pointer to an already allocated char array which will get the name, as a null terminated string. It will have a maximum length of NC_MAX_NAME + 1. Ignored if NULL.
sizep
A pointer to a size_t. The size of the compound type will be put here. Ignored if NULL.
nfieldsp
A pointer to a size_t. The number of fields in the compound type will be placed here. Ignored if NULL.

Return Codes

NC_NOERR
No error.
NC_EBADID
Couldn't find this ncid.
NC_ENOTNC4
Not a netCDF-4/HDF5 file.
NC_ESTRICTNC3
A netCDF-4/HDF5 file, but with CLASSIC_MODEL. No user defined types are allowed in the classic model.
NC_EBADTYPE
This type not a compound type.
NC_EBADTYPEID
Bad type id.
NC_EHDFERR
An error was reported by the HDF5 layer.

Example

The following example is from the test program libsrc4/tst_compounds.c. See also the example for See nc_insert_array_compound.

     #define BATTLES_WITH_KLINGONS "Number_of_Battles_with_Klingons"
     #define DATES_WITH_ALIENS "Dates_with_Alien_Hotties"
     
        struct s1
        {
              int i1;
              int i2;
        };
     
        /* Create a file with a compound type. */
        if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
        if (nc_def_compound(ncid, sizeof(struct s1), SVC_REC, &typeid)) ERR;
        if (nc_inq_compound(ncid, typeid, name, &size, &nfields)) ERR;
        if (nfields) 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;
     
        /* Check the compound type. */
        if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR;
        if (nfields != 2 || size != 8 || strcmp(name, SVC_REC)) ERR;
        if (nc_inq_compound_name(ncid, xtype, name)) ERR;
        if (strcmp(name, SVC_REC)) ERR;
        if (nc_inq_compound_size(ncid, xtype, &size)) ERR;
        if (size != 8) ERR;
        if (nc_inq_compound_nfields(ncid, xtype, &nfields)) ERR;
        if (nfields != 2) ERR;
        if (nc_inq_compound_field(ncid, xtype, 0, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
        if (strcmp(name, BATTLES_WITH_KLINGONS) || offset != 0 || (field_xtype != NC_INT || field_ndims != 0)) ERR;
        if (nc_inq_compound_field(ncid, xtype, 1, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
        if (strcmp(name, DATES_WITH_ALIENS) || offset != 4 || field_xtype != NC_INT) ERR;
        if (nc_inq_compound_fieldname(ncid, xtype, 1, name)) ERR;
        if (strcmp(name, DATES_WITH_ALIENS)) ERR;
        if (nc_inq_compound_fieldindex(ncid, xtype, BATTLES_WITH_KLINGONS, &fieldid)) ERR;
        if (fieldid != 0) ERR;
        if (nc_inq_compound_fieldoffset(ncid, xtype, 1, &offset)) ERR;
        if (offset != 4) ERR;
        if (nc_inq_compound_fieldtype(ncid, xtype, 1, &field_xtype)) ERR;
        if (field_xtype != NC_INT) ERR;