Next: , Previous: nc_inq_var, Up: Variables


6.21 Write a Single Data Value: nc_put_var1_ type

The functions nc_put_var1_ type put a single data value of the specified type into a variable of an open netCDF dataset that is in data mode. Inputs are the netCDF ID, the variable ID, an index that specifies which value to add or alter, and the data value. The value is converted to the external data type of the variable, if necessary.

The functions for types ubyte, ushort, uint, longlong, ulonglong, and string are only available for netCDF-4/HDF5 files.

The nc_put_var1() function will write a variable of any type, including user defined type. For this function, the type of the data in memory must match the type of the variable - no data conversion is done.

Usage

     int nc_put_var1_text  (int ncid, int varid, const size_t index[],
                            const char *tp);
     int nc_put_var1_uchar (int ncid, int varid, const size_t index[],
                            const unsigned char *up);
     int nc_put_var1_schar (int ncid, int varid, const size_t index[],
                            const signed char *cp);
     int nc_put_var1_short (int ncid, int varid, const size_t index[],
                            const short *sp);
     int nc_put_var1_int   (int ncid, int varid, const size_t index[],
                            const int *ip);
     int nc_put_var1_long  (int ncid, int varid, const size_t index[],
                            const long *lp);
     int nc_put_var1_float (int ncid, int varid, const size_t index[],
                            const float *fp);
     int nc_put_var1_double(int ncid, int varid, const size_t index[],
                            const double *dp);
     int nc_put_var1_ubyte (int ncid, int varid, const size_t index[],
                            const unsigned char *up);
     int nc_put_var1_ushort(int ncid, int varid, const size_t index[],
                            const unsigned short *sp);
     int nc_put_var1_uint  (int ncid, int varid, const size_t index[],
                            const unsigned int *ip);
     int nc_put_var1_longlong(int ncid, int varid, const size_t index[],
                              const long long *ip);
     int nc_put_var1_ulonglong(int ncid, int varid, const size_t index[],
                              const unsigned long long *ip);
     int nc_put_var1_string(int ncid, int varid, const size_t index[],
                            const char **ip);
     int nc_put_var1(int ncid, int varid, const size_t *indexp,
                     const void *op);
ncid
NetCDF ID, from a previous call to nc_open or nc_create.
varid
Variable ID.
index[]
The index of the data value to be written. The indices are relative to 0, so for example, the first data value of a two-dimensional variable would have index (0,0). The elements of index must correspond to the variable's dimensions. Hence, if the variable uses the unlimited dimension, the first index would correspond to the unlimited dimension.
tp
up
cp
sp
ip
lp
fp
dp
Pointer to the data value to be written. If the type of data values differs from the netCDF variable type, type conversion will occur. See Type Conversion.

Return Codes

Example

Here is an example using nc_put_var1_double to set the (1,2,3) element of the variable named rh to 0.5 in an existing netCDF dataset named foo.nc. For simplicity in this example, we assume that we know that rh is dimensioned with time, lat, and lon, so we want to set the value of rh that corresponds to the second time value, the third lat value, and the fourth lon value:

     #include <netcdf.h>
        ...
     int  status;                          /* error status */
     int  ncid;                            /* netCDF ID */
     int  rh_id;                           /* variable ID */
     static size_t rh_index[] = {1, 2, 3}; /* where to put value */
     static double rh_val = 0.5;           /* value to put */
        ...
     status = nc_open("foo.nc", NC_WRITE, &ncid);
     if (status != NC_NOERR) handle_error(status);
        ...
     status = nc_inq_varid (ncid, "rh", &rh_id);
     if (status != NC_NOERR) handle_error(status);
        ...
     status = nc_put_var1_double(ncid, rh_id, rh_index, &rh_val);
     if (status != NC_NOERR) handle_error(status);