Next: , Previous: nc_put_vars_ type, Up: Variables


4.10 Write a Mapped Array of Values: nc_put_varm_ type

The nc_put_varm_ type family of functions writes a mapped array section of values into a netCDF variable of an open netCDF dataset. The mapped array section is specified by giving a corner, a vector of counts, a stride vector, and an index mapping vector. The index mapping vector is a vector of integers that specifies the mapping between the dimensions of a netCDF variable and the in-memory structure of the internal data array. No assumptions are made about the ordering or length of the dimensions of the data array. The netCDF dataset must be in data mode.

Usage

     int nc_put_varm_text  (int ncid, int varid, const size_t start[],
                            const size_t count[], const ptrdiff_t stride[],
                            const ptrdiff_t imap[], const char *tp);
     int nc_put_varm_uchar (int ncid, int varid, const size_t start[],
                            const size_t count[], const ptrdiff_t stride[],
                            const ptrdiff_t imap[], const unsigned char *up);
     int nc_put_varm_schar (int ncid, int varid, const size_t start[],
                            const size_t count[], const ptrdiff_t stride[],
                            const ptrdiff_t imap[], const signed char *cp);
     int nc_put_varm_short (int ncid, int varid, const size_t start[],
                            const size_t count[], const ptrdiff_t stride[],
                            const ptrdiff_t imap[], const short *sp);
     int nc_put_varm_int   (int ncid, int varid, const size_t start[],
                            const size_t count[], const ptrdiff_t stride[],
                            const ptrdiff_t imap[], const int *ip);
     int nc_put_varm_long  (int ncid, int varid, const size_t start[],
                            const size_t count[], const ptrdiff_t stride[],
                            const ptrdiff_t imap[], const long *lp);
     int nc_put_varm_float (int ncid, int varid, const size_t start[],
                            const size_t count[], const ptrdiff_t stride[],
                            const ptrdiff_t imap[], const float *fp);
     int nc_put_varm_double(int ncid, int varid, const size_t start[],
                            const size_t count[], const ptrdiff_t stride[],
                            const ptrdiff_t imap[], const double *dp);
ncid
NetCDF ID, from a previous call to nc_open or nc_create.
varid
Variable ID.
start
A vector of size_t integers specifying the index in the variable where the first of the data values will be written. The indices are relative to 0, so for example, the first data value of a variable would have index (0, 0, ... , 0). The elements of start correspond, in order, to the variable's dimensions. Hence, if the variable is a record variable, the first index corresponds to the starting record number for writing the data values.
count
A vector of size_t integers specifying the number of indices selected along each dimension. To write a single value, for example, specify count as (1, 1, ... , 1). The elements of count correspond, in order, to the variable's dimensions. Hence, if the variable is a record variable, the first element of count corresponds to a count of the number of records to write.
stride
A vector of ptrdiff_t integers that specifies the sampling interval along each dimension of the netCDF variable. The elements of the stride vector correspond, in order, to the netCDF variable's dimensions (stride[0] gives the sampling interval along the most slowly varying dimension of the netCDF variable). Sampling intervals are specified in type-independent units of elements (a value of 1 selects consecutive elements of the netCDF variable along the corresponding dimension, a value of 2 selects every other element, etc.). A NULL stride argument is treated as (1, 1, ... , 1).
imap
A vector of ptrdiff_t integers that specifies the mapping between the dimensions of a netCDF variable and the in-memory structure of the internal data array. The elements of the index mapping vector correspond, in order, to the netCDF variable's dimensions (imap[0] gives the distance between elements of the internal array corresponding to the most slowly varying dimension of the netCDF variable). Distances between elements are specified in type-independent units of elements (the distance between internal elements that occupy adjacent memory locations is 1 and not the element's byte-length as in netCDF 2). A NULL argument means the memory-resident values have the same structure as the associated netCDF variable.
tp
up
cp
sp
ip
lp
fp
dp
Pointer to the location used for computing where the data values will be found; the data should be of the type appropriate for the function called. If the type of data values differs from the netCDF variable type, type conversion will occur. See Type Conversion.

Errors

nc_put_varm_ type returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

The following imap vector maps in the trivial way a 4x3x2 netCDF variable and an internal array of the same shape:

     float a[4][3][2];       /* same shape as netCDF variable */
     int   imap[3] = {6, 2, 1};
                             /* netCDF dimension       inter-element distance */
                             /* ----------------       ---------------------- */
                             /* most rapidly varying       1                  */
                             /* intermediate               2 (=imap[2]*2)     */
                             /* most slowly varying        6 (=imap[1]*3)     */

Using the imap vector above with nc_put_varm_float obtains the same result as simply using nc_put_var_float.

Here is an example of using nc_put_varm_float to write – from a transposed, internal array – a netCDF variable named rh which is described by the C declaration float rh[6][4] (note the size and order of the dimensions):

     #include <netcdf.h>
        ...
     #define NDIM 2               /* rank of netCDF variable */
     int ncid;                    /* netCDF ID */
     int status;                  /* error status */
     int rhid;                    /* variable ID */
     static size_t start[NDIM]    /* netCDF variable start point: */
                      = {0, 0};   /* first element */
     static size_t count[NDIM]    /* size of internal array: entire netCDF */
                      = {6, 4};   /* variable; order corresponds to netCDF */
                                  /* variable -- not internal array */
     static ptrdiff_t stride[NDIM]/* variable subsampling intervals: */
                      = {1, 1};   /* sample every netCDF element */
     static ptrdiff_t imap[NDIM]  /* internal array inter-element distances; */
                      = {1, 6};   /* would be {4, 1} if not transposing */
     float rh[4][6];              /* note transposition of netCDF variable */
                                  /* dimensions */
        ...
     status = nc_open("foo.nc", NC_WRITE, &ncid);
     if (status != NC_NOERR) handle_error(status);
        ...
     status = nc_inq_varid(ncid, "rh", &rhid);
     if (status != NC_NOERR) handle_error(status);
        ...
     status = nc_put_varm_float(ncid, rhid, start, count, stride, imap, rh);
     if (status != NC_NOERR) handle_error(status);

Here is another example of using nc_put_varm_float to write – from a transposed, internal array – a subsample of the same netCDF variable, by writing every other point of the netCDF variable:

     #include <netcdf.h>
        ...
     #define NDIM 2                /* rank of netCDF variable */
     int ncid;                     /* netCDF ID */
     int status;                   /* error status */
     int rhid;                     /* variable ID */
     static size_t start[NDIM]     /* netCDF variable start point: */
                      = {0, 0};    /* first element */
     static size_t count[NDIM]     /* size of internal array: entire */
                        = {3, 2};  /* (subsampled) netCDF variable; order of */
                                   /* dimensions corresponds to netCDF */
                                   /* variable -- not internal array */
     static ptrdiff_t stride[NDIM] /* variable subsampling intervals: */
                      = {2, 2};    /* sample every other netCDF element */
     static ptrdiff_t imap[NDIM]   /* internal array inter-element distances; */
                      = {1, 3};    /* would be {2, 1} if not transposing */
     float rh[2][3];               /* note transposition of (subsampled) */
                                   /* netCDF variable dimensions */
        ...
     status = nc_open("foo.nc", NC_WRITE, &ncid);
     if (status != NC_NOERR) handle_error(status);
        ...
     status = nc_inq_varid(ncid, "rh", &rhid);
     if (status != NC_NOERR) handle_error(status);
        ...
     status = nc_put_varm_float(ncid, rhid, start, count, stride, imap, rh);
     if (status != NC_NOERR) handle_error(status);