Next: ncmpi_put_var_<type>, Previous: ncmpi_inq_var Family, Up: Variables [Index]
The functions ncmpi_put_var1_<type> put a single data value of the specified type into a variable of an opened netCDF file 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.
<type > for API names | <C type> for API arguments |
text | char |
schar | signed char |
short | short |
int | int |
float | float |
double | double |
uchar | unsigned char |
ushort | unsigned short |
uint | unsigned int |
longlong | long long |
ulonglong | unsigned longlong |
These API must be called while the file is in data mode.
The corresponding collective APIs have the suffix name "_all" and must be called in collective data mode.
int ncmpi_put_var1_<type> (int ncid, int varid, const MPI_Offset index[], const <C type> *buf); int ncmpi_put_var1 (int ncid, int varid, const MPI_Offset index[], const void *buf, MPI_Offset bufcount, MPI_Datatype buftype); int ncmpi_put_var1_<type>_all (int ncid, int varid, const MPI_Offset index[], const <C type> *buf); int ncmpi_put_var1_all (int ncid, int varid, const MPI_Offset index[], const void *buf, MPI_Offset bufcount, MPI_Datatype buftype);
ncid
NetCDF ID, from a previous call to ncmpi_open or ncmpi_create.
varid
Variable ID. Different MPI processes may use different variable IDs.
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.
buf
Pointer to the data value to be written. If the type of data values differs from the netCDF variable type, type conversion will occur.
bufcount
An integer indicates the number of MPI derived data type elements in the buf to be written to the file.
buftype
An MPI derived data type that describes the memory layout of buf. Starting from PnetCDF version 1.6.0, buftype can be MPI_DATATYPE_NULL. In this case, bufcount is ignored and the buf’s data type must match the type of the variable defined in the file - no data conversion will be done.
ncmpi_put_var1_<type> returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
Here is an example using ncmpi_put_var1_double_all to set the elements at starting indices (1+rank*10, 2+rank*10, 3+rank*10) of the variable named rh in parallel to 0.5+rank 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. If run sequentially (i.e. one MPI process), this example is equivalent to to set the values of rh that corresponds to the second time value, the third lat value, and the fourth lon value:
#include <pnetcdf.h> ... int status; /* error status */ int rank; /* MPI process rank ID */ int ncid; /* netCDF ID */ int rh_id; /* variable ID */ MPI_Offset rh_index[] = {1, 2, 3}; /* where to put value */ double rh_val = 0.5 + rank; /* value to put */ ... /* Open file in write mode */ status = ncmpi_open(MPI_COMM_WORLD, "foo.nc", NC_WRITE, MPI_INFO_NULL, &ncid); if (status != NC_NOERR) handle_error(status); ... /* Obtain the variabe ID */ status = ncmpi_inq_varid(ncid, "rh", &rh_id); if (status != NC_NOERR) handle_error(status); ... /* Set the starting indices. Indices are different among processes */ MPI_Comm_rank(MPI_COMM_WORLD, &rank); rh_index[0] += rank*10; rh_index[1] += rank*10; rh_index[2] += rank*10; /* Each MPI process writes a single value to a different location of variable rh */ status = ncmpi_put_var1_double_all(ncid, rh_id, rh_index, &rh_val); if (status != NC_NOERR) handle_error(status);
Next: ncmpi_put_var_<type>, Previous: ncmpi_inq_var Family, Up: Variables [Index]