Description Usage Arguments Details Note Author(s) References Examples
Write the contents of a NetCDF variable.
1 |
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 |
Set the mode for handling missing values ( |
pack |
Variables are packed if |
This function writes values to a NetCDF variable. Type conversion is performed by the NetCDF library, so that numeric values in R are automatically converted to the correct type of NetCDF variable.
However, text represented by R types raw
and character
can only be written to NetCDF type NC_CHAR
. The dimensions of R raw
variables map directly to NetCDF dimensions, but 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.
Values of NA
are supported in numeric variables if the variable's missing value attribute (as defined in na.mode
) is set. They are converted to the corresponding value before writing to disk. If na.mode=0
and both attributes are defined, the value of _FillValue
is used.
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 two attributes add_offset
and 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).
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
http://www.unidata.ucar.edu/software/netcdf/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | ## Create a new NetCDF dataset and define two dimensions
nc <- create.nc("var.put.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 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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.