var.put.nc | R Documentation |
Write the contents of a NetCDF variable.
var.put.nc(ncfile, variable, data, start=NA, count=NA, na.mode=4, pack=FALSE,
cache_bytes=NA, cache_slots=NA, cache_preemption=NA)
Arguments marked "netcdf4"
are optional for datasets in that format and ignored for other formats.
ncfile |
Object of class | |||||||||||||||||||||||||
variable |
ID or name of the NetCDF variable. | |||||||||||||||||||||||||
data |
An R vector or array of data to be written to the NetCDF variable. Values are taken from | |||||||||||||||||||||||||
start |
A vector of indices specifying the element where writing starts along each dimension of | |||||||||||||||||||||||||
count |
A vector of integers specifying the number of values to write along each dimension of | |||||||||||||||||||||||||
na.mode |
For explanation of attribute conventions used by mode 4, please see: https://docs.unidata.ucar.edu/nug/current/attribute_conventions.html | |||||||||||||||||||||||||
pack |
Variables are packed if | |||||||||||||||||||||||||
cache_bytes |
( | |||||||||||||||||||||||||
cache_slots |
( | |||||||||||||||||||||||||
cache_preemption |
( |
This function writes values to a NetCDF variable. Data values in R are automatically converted to the correct type of NetCDF variable.
Text represented by R type character
can be written to NetCDF types NC_CHAR
and NC_STRING
, and R type raw
can be written to NetCDF type NC_CHAR
. When writing to NC_CHAR
variables, character
variables have an implied dimension corresponding to the string length. This implied dimension must be defined explicitly as the fastest-varying dimension of the NC_CHAR
variable, and it must be included as the first element of arguments start
and count
taken by this function.
Due to the lack of native support for 64-bit integers in R, NetCDF types NC_INT64
and NC_UINT64
require special attention. This function accepts the usual R integer
(signed 32-bit) and numeric
(double precision) types, but to represent integers larger than about 53-bits without truncation, integer64
vectors are also supported.
NetCDF numeric variables cannot portably represent NA
values from R. NetCDF does allow attributes to be defined for variables, and several conventions exist for attributes that define missing values and valid ranges. The convention in use can be specified by argument na.mode
. Values of NA
in argument data
are converted to a missing or fill value before writing to the NetCDF variable. Unusual cases can be handled directly in user code by setting na.mode=3
.
Variables of user-defined types are supported, subject to conditions on the corresponding data structures in R. "compound"
arrays must be stored in R as lists, with items named for the compound fields; items of base NetCDF data types are stored as R arrays, with leading dimensions from the field dimensions (if any) and trailing dimensions from the NetCDF variable. "enum"
arrays are stored in R as factor arrays. "opaque"
arrays are stored in R as raw (byte) arrays, with a leading dimension for bytes of the opaque type and trailing dimensions from the NetCDF variable. "vlen"
arrays are stored in R as a list with dimensions of the NetCDF variable; items in the list may have different lengths; base NetCDF data types are stored as R vectors.
To reduce the storage space required by a NetCDF file, numeric variables can be packed into types of lower precision. The packing operation involves subtraction of attribute add_offset
before division by attribute scale_factor
. This packing operation is performed automatically for variables defined with the attributes add_offset
and/or scale_factor
if argument pack
is set to TRUE
. If pack
is FALSE
, data
values are assumed to be packed correctly and are written to the variable without alteration.
Data in a NetCDF variable is represented as a multi-dimensional array. The number and length of dimensions is determined when the variable is created. The start
and count
arguments of this routine indicate where the writing starts and the number of values to write along each dimension.
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).
The arguments marked for "netcdf4"
format refer to the chunk cache used for reading and writing variables. Default cache settings are defined by the NetCDF library, and they can be adjusted for each variable to improve performance in some applications.
NC_BYTE
is always interpreted as signed. For best performance, it is recommended that the definition of dimensions, variables and attributes is completed before variables are read or written.
Pavel Michna, Milton Woods
https://www.unidata.ucar.edu/software/netcdf/
## Create a new NetCDF dataset and define two dimensions
file1 <- tempfile("var.put_", fileext=".nc")
nc <- create.nc(file1)
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 _FillValue attribute for temperature
att.put.nc(nc, "temperature", "_FillValue", "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 subsets of the data:
var.put.nc(nc, "time", mytime, start=2, count=1)
var.put.nc(nc, "temperature", mytemperature[3:4,2], start=c(3,2), count=c(2,1))
var.put.nc(nc, "name", myname[3:4], start=c(NA,3), count=c(NA,2))
sync.nc(nc)
## Put all of the data:
var.put.nc(nc, "time", mytime)
var.put.nc(nc, "temperature", mytemperature)
var.put.nc(nc, "name", myname)
close.nc(nc)
unlink(file1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.