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


5.9 Inserting an Array Field into a Compound Type: nc_insert_array_compound

Insert a named field into a compound type.

Usage

     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
The ID of the file that contains the array type and the compound type.
xtype
The typeid for this compound type, as returned by nc_def_compound, or nc_inq_var.
name
The name of the new field.
offset
Offset in byte from the beginning of the compound type for this field.
field_typeid
The base type of the array to be inserted.

Errors

NC_NOERR
No error.
NC_EBADID
Bad group id.
NC_ENAMEINUSE
That name is in use. Field names must be unique within a compound type.
NC_EMAXNAME
Name exceed 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_ENOTINDEFINE
Not in define mode.
NC_ETYPEDEFINED
Attempt to change type that has already been committed. The first time the file leaves define mode, all defined types are committed, and can't be changed. If you wish to add an array to a compound type, you must do so before the compound type is committed.

Example

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;
        }