Go to the previous, next section.

Write a Hyperslab of Values

The function ncvarput (or NCVPT or NCVPTC for FORTRAN) writes a hyperslab of values into a netCDF variable of an open netCDF file. The hyperslab is specified by giving a corner and a vector of edge lengths. The values are specified as a vector whose elements are ordered by assuming that the last dimension of the hyperslab varies fastest for C, the first dimension varies fastest for FORTRAN. The netCDF file must be in data mode.

In case of an error, ncvarput returns -1; NCVPT returns a nonzero value in rcode. Possible causes of errors include:

ncvarput: C Interface

int ncvarput(int ncid, int varid, const long start[], const long count[],
             const void *values);

ncid
NetCDF ID, returned from a previous call to ncopen or nccreate.

varid
Variable ID, returned from a previous call to ncvardef or ncvarid.

start
A vector of long integers specifying the multidimensional index of the corner of the hyperslab 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 size of start must be the same as the number of dimensions of the specified variable. The elements of start must correspond to the variable's dimensions in order. Hence, if the variable is a record variable, the first index would correspond to the starting record number for writing the data values.

count
A vector of long integers specifying the multidimensional edge lengths from the corner of the hyperslab where the first of the data values will be written. To write a single value, for example, specify count as (1, 1, ..., 1). The size of count is the number of dimensions of the specified variable. The elements of count correspond 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.

value
Pointer to a block of data values to be written. The order in which the data will be written to the netCDF variable is with the last dimension of the specified hyperslab varying fastest. The pointer is declared to be of the type void * because it can point to data of any of the basic netCDF types. The data should be of the appropriate type for the netCDF variable. Warning: neither the compiler nor the netCDF software can detect whether the wrong type of data is used.

Here is an example using ncvarput to add or change all the values of the variable named rh to 0.5 in an existing netCDF file named `foo.nc'. For simplicity in this example, we assume that we know that rh is dimensioned with time, lat, and lon, and that there are three time values, five lat values, and ten lon values.

#include "netcdf.h"
   ...
#define TIMES 3
#define LATS  5
#define LONS  10
int  ncid;                         /* netCDF ID */
int  rh_id;                        /* variable ID */
static long start[] = {0, 0, 0};    /* start at first value */
static long count[] = {TIMES, LATS, LONS};
double rh_vals[TIMES*LATS*LONS];   /* array to hold values */
int i;
   ...
ncid = ncopen("foo.nc", NC_WRITE);
   ...
rh_id = ncvarid (ncid, "rh");
   ...
for (i = 0; i < TIMES*LATS*LONS; i++)
    rh_vals[i] = 0.5;
/* write hyperslab of values into netCDF variable */
ncvarput(ncid, rh_id, start, count, (void *) rh_vals);

NCVPT: FORTRAN Interface

      SUBROUTINE NCVPT (INTEGER NCID, INTEGER VARID,
     +                  INTEGER START(*), INTEGER COUNT(*),
     +                  type VALUES, INTEGER RCODE)

      SUBROUTINE NCVPTC(INTEGER NCID, INTEGER VARID,
     +                  INTEGER START(*), INTEGER COUNTS(*),
     +                  CHARACTER*(*) STRING, INTEGER LENSTR,
     +                  INTEGER RCODE)

There are two FORTRAN subroutines, NCVPT and NCVPTC, for writing a hyperslab of values into a netCDF variable. The first writes numeric values into a variable of numeric type, and the second writes character values into a variable of character type.

NCID
NetCDF ID, returned from a previous call to NCOPN or NCCRE.

VARID
Variable ID, returned from a previous call to NCVDEF or NCVID.

START
A vector of integers specifying the multidimensional index of the corner of the hyperslab where the first of the data values will be written. The indices are relative to 1, so for example, the first data value of a variable would have index (1, 1, ..., 1). The size of START must be the same as the number of dimensions of the specified variable. The elements of START must correspond to the variable's dimensions in order. Hence, if the variable is a record variable, the last index would correspond to the starting record number for writing the data values.

COUNT
A vector of integers specifying the multidimensional edge lengths from the corner of the hyperslab where the first of the data values will be written. To write a single value, for example, specify COUNT as (1, 1, ..., 1). The size of COUNT is the number of dimensions of the specified variable. The elements of COUNT correspond to the variable's dimensions. Hence, if the variable is a record variable, the last element of COUNT corresponds to a count of the number of records to write.

VALUES
For NCVPT, the block of data values to be written. The order in which the data will be written into the specified hyperslab is with the first dimension varying fastest (like the ordinary FORTRAN convention). The data may be of a type corresponding to any of the netCDF types NCSHORT, NCLONG, NCFLOAT, or NCDOUBLE, but must be appropriate for the type of the netCDF variable. Warning: neither the compiler nor the netCDF software can detect whether the wrong type of data is used.

STRING
For NCVPTC, the characters to be written. The order in which the characters will be written to the netCDF variable is with the first dimension of the specified hyperslab varying fastest (like the FORTRAN convention). The data may be of a type corresponding to the netCDF types NCCHAR or NCBYTE.

LENSTR
For NCVPTC, the total declared length (in characters) of the STRING argument. This should be at least as large as the product of the elements of the COUNT vector. Note that this is not necessarily the same as the value returned by the FORTRAN LEN function, because an array argument may be provided.

RCODE
Returned error code. If no errors occurred, 0 is returned.

Here is an example using NCVPT to add or change all the values of the variable named rh to 0.5 in an existing netCDF file 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 (NDIMS=3)         ! number of dimensions
      PARAMETER (TIMES=3, LATS=5, LONS=10) ! dimension sizes
      INTEGER  NCID, RCODE
      INTEGER  RHID               ! variable ID
      INTEGER  START(NDIMS), COUNT(NDIMS) ! hyperslab
      DOUBLE RHVALS(LONS, LATS, TIMES)      
      DATA START /1, 1, 1/        ! start at first value
      DATA COUNT /LONS, LATS, TIMES/
         ...
      NCID = NCOPN ('foo.nc', NCWRITE, RCODE)
         ...
      RHID = NCVID (NCID, 'rh', RCODE)   ! get ID
      DO 10 ILON = 1, LONS
         DO 10 ILAT = 1, LATS
            DO 10 ITIME = 1, TIMES
               RHVALS(ILON, ILAT, ITIME) = 0.5
   10 CONTINUE
      CALL NCVPT (NCID, RHID, START, COUNT, RHVALS, RCODE)

Go to the previous, next section.