Next: , Previous: NF_INQ_VAR family, Up: Variables


4.6 Write a Single Data Value: NF_PUT_VAR1_ type

The functions NF_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.

Take care when using the simplest forms of this interface with record variables when you don't specify how many records are to be read. If you try to read all the values of a record variable into an array but there are more records in the file than you assume, more data will be read than you expect, which may cause a segmentation violation.

Usage

     INTEGER FUNCTION  NF_PUT_VAR1_TEXT(INTEGER NCID, INTEGER VARID,
                                        INTEGER INDEX(*), CHARACTER CHVAL)
     INTEGER FUNCTION  NF_PUT_VAR1_INT1(INTEGER NCID, INTEGER VARID,
                                        INTEGER INDEX(*), INTEGER*1 I1VAL)
     INTEGER FUNCTION  NF_PUT_VAR1_INT2(INTEGER NCID, INTEGER VARID,
                                        INTEGER INDEX(*), INTEGER*2 I2VAL)
     INTEGER FUNCTION  NF_PUT_VAR1_INT (INTEGER NCID, INTEGER VARID,
                                        INTEGER INDEX(*), INTEGER   IVAL)
     INTEGER FUNCTION  NF_PUT_VAR1_REAL(INTEGER NCID, INTEGER VARID,
                                        INTEGER INDEX(*), REAL      RVAL)
     INTEGER FUNCTION  NF_PUT_VAR1_DOUBLE(INTEGER NCID, INTEGER VARID,
                                        INTEGER INDEX(*), DOUBLE    DVAL)
NCID
NetCDF ID, from a previous call to NF_OPEN or NF_CREATE.
VARID
Variable ID.
INDEX
The index of the data value to be written. The indices are relative to 1, so for example, the first data value of a two-dimensional variable would have index (1,1). The elements of index must correspond to the variable's dimensions. Hence, if the variable uses the unlimited dimension, the last index would correspond to the record number.
CHVAL
I1VAL
I2VAL
IVAL
RVAL
DVAL
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.

Errors

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

Example

Here is an example using NF_PUT_VAR1_DOUBLE to set the (4,3,2) 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 lon, lat, and time, so we want to set the value of rh that corresponds to the fourth lon value, the third lat value, and the second time value:

     INCLUDE 'netcdf.inc'
        ...
     INTEGER  STATUS             ! error status
     INTEGER  NCID
     INTEGER  RHID               ! variable ID
     INTEGER  RHINDX(3)          ! where to put value
     DATA RHINDX /4, 3, 2/
        ...
     STATUS = NF_OPEN ('foo.nc', NF_WRITE, NCID)
     IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
        ...
     STATUS = NF_INQ_VARID (NCID, 'rh', RHID)  ! get ID
     IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
     STATUS = NF_PUT_VAR1_DOUBLE (NCID, RHID, RHINDX, 0.5)
     IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)