var.put.nc {RNetCDF} | R Documentation |
Put values to a NetCDF variable.
var.put.nc(ncfile, variable, data, start=NA, count=NA, na.mode=0)
ncfile |
Object of class "NetCDF " which points to the NetCDF dataset (as returned from open.nc ). |
variable |
ID or name of the variable. |
data |
The (multidimensional) array containing the data to write. |
start |
A vector of indices indicating where to start writing the passed values (beginning at 1). The length of this vector must equal the number of dimensions the variable has. Order is leftmost varying fastest (as got from print.nc ; opposite to the CDL conventions). If set to NA , writing starts for each dimension at position 1. |
count |
A vector of integers indicating the count of values to write along each dimension. Order is leftmost varying fastest (as got from print.nc ; opposite to the CDL conventions). The length of this vector must equal the number of dimensions the variable has. If set to NA , the dimesions are taken from data . |
na.mode |
Set the mode how missing values (NA ) are handled: 0=accept _FillValue or missing_value attribute, 1=accept only _FillValue attribute, 2=accept only missing_value attribute. |
This function writes values to a NetCDF variable. Type conversion is done by the NetCDF library itself. This means, that double precision values are passed from R to the corresponding C-function, no matter which type the variable has.
Only the R type character
is treated separately. When writing values of type NC_CHAR
, it is mandatory that the first element of count
contains the value of this dimension's length (usually max_string_length
), the maximum string length is given by this value. R arrays of type character
need therefore one additional dimension when written to a NetCDF dataset.
Values of NA
are supported if the variable's missing value attribute (as defined in na.mode
) is set. They are converted to the corresponding value before written to disk. If na.mode=0
and both attributes are defined, the value of _FillValue
is used.
Data in a NetCDF file is conceived as being a multi-dimensional array. The number and length of dimensions is determined when the variable is created. The start
and count
indices that this routine takes indicate where the writing starts along each dimension, and the count of values along each dimension to write.
Awkwardness arises mainly from one thing: NetCDF data are written with the last dimension varying fastest, whereas R works opposite. Thus, the order of the dimensions according to the CDL conventions (e.g., time, latitude, longitude) is reversed in the R array (e.g., longitude, latitude, time).
NC_BYTE
is always interpreted as signed.
Pavel Michna
http://www.unidata.ucar.edu/packages/netcdf/
## Create a new NetCDF dataset and define two dimensions nc <- create.nc("foo.nc") dim.def.nc(nc, "station", 5) dim.def.nc(nc, "time", unlim=TRUE) dim.def.nc(nc, "max_string_length", 32) ## Create three variables, one as coordinate variable var.def.nc(nc, "time", "NC_INT", "time") var.def.nc(nc, "temperature", "NC_DOUBLE", c(0,1)) var.def.nc(nc, "name", "NC_CHAR", c("max_string_length", "station")) ## Put some missing_value attribute for temperature att.put.nc(nc, "temperature", "missing_value", "NC_DOUBLE", -99999.9) ## Define variable values mytime <- c(1:2) mytemperature <- c(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, NA, NA, 9.9) myname <- c("alfa", "bravo", "charlie", "delta", "echo") dim(mytemperature) <- c(5,2) ## Put the data with indicated start/count var.put.nc(nc, "time", mytime, 1, length(mytime)) var.put.nc(nc, "temperature", mytemperature, c(1,1), c(5,2)) var.put.nc(nc, "name", myname, c(1,1), c(32,5)) sync.nc(nc) ## Put the data with default start/count var.put.nc(nc, "time", mytime) var.put.nc(nc, "temperature", mytemperature) var.put.nc(nc, "name", myname) close.nc(nc)