Next: , Previous: NF_PUT_VAR1_ type, Up: Variables


4.7 Write an Entire Variable: NF_PUT_VAR_ type

The NF_PUT_VAR_ type family of functions write all the values of a variable into a netCDF variable of an open netCDF dataset. This is the simplest interface to use for writing a value in a scalar variable or whenever all the values of a multidimensional variable can all be written at once. The values to be written are associated with the netCDF variable by assuming that the last dimension of the netCDF variable varies fastest in the C interface. The values are 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 written. If you try to write all the values of a record variable into a netCDF file that has no record data yet (hence has 0 records), nothing will be written. Similarly, if you try to write all of a record variable but there are more records in the file than you assume, more data may be written to the file than you supply, which may result in a segmentation violation.

Usage

     INTEGER FUNCTION NF_PUT_VAR_TEXT  (INTEGER NCID, INTEGER VARID,
                                        CHARACTER*(*) TEXT)
     INTEGER FUNCTION NF_PUT_VAR_INT1  (INTEGER NCID, INTEGER VARID,
                                        INTEGER*1 I1VALS(*))
     INTEGER FUNCTION NF_PUT_VAR_INT2  (INTEGER NCID, INTEGER VARID,
                                        INTEGER*2 I2VALS(*))
     INTEGER FUNCTION NF_PUT_VAR_INT   (INTEGER NCID, INTEGER VARID,
                                        INTEGER IVALS(*))
     INTEGER FUNCTION NF_PUT_VAR_REAL  (INTEGER NCID, INTEGER VARID,
                                        REAL RVALS(*))
     INTEGER FUNCTION NF_PUT_VAR_DOUBLE(INTEGER NCID, INTEGER VARID,
                                        DOUBLE DVALS(*))
NCID
NetCDF ID, from a previous call to NF_OPEN or NF_CREATE.
VARID
Variable ID.
TEXT
I1VALS
I2VALS
IVALS
RVALS
DVALS
The block of data values to be written. The data should be of the type appropriate for the function called. You cannot put CHARACTER data into a numeric variable or numeric data into a text variable. For numeric data, if the type of data differs from the netCDF variable type, type conversion will occur (see Type Conversion). The order in which the data will be written into the specified variable is with the first dimension varying fastest (like the ordinary FORTRAN convention).

Errors

Members of the NF_PUT_VAR_ type family return 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_VAR_DOUBLE to add or change all the values 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, and that there are ten lon values, five lat values, and three time values.

     INCLUDE 'netcdf.inc'
        ...
     PARAMETER (TIMES=3, LATS=5, LONS=10) ! dimension lengths
     INTEGER  STATUS, NCID, TIMES
     INTEGER  RHID                        ! variable ID
     DOUBLE RHVALS(LONS, LATS, TIMES)
        ...
     STATUS = NF_OPEN ('foo.nc', NF_WRITE, NCID)
     IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
        ...
     STATUS = NF_INQ_VARID (NCID, 'rh', RHID)
     IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
     DO 10 ILON = 1, LONS
        DO 10 ILAT = 1, LATS
           DO 10 ITIME = 1, TIMES
              RHVALS(ILON, ILAT, ITIME) = 0.5
     10 CONTINUE
     STATUS = NF_PUT_var_DOUBLE (NCID, RHID, RHVALS)
     IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)