Description Usage Arguments Details Value Note Author(s) References Examples
Read the contents of a NetCDF variable.
1 2 |
ncfile |
Object of class " |
variable |
ID or name of the NetCDF variable. |
start |
A vector of indices specifying the element where reading starts along each dimension of |
count |
A vector of integers specifying the number of values to read along each dimension of |
na.mode |
Set the mode for handling missing values ( |
collapse |
|
unpack |
Packed variables are unpacked if |
rawchar |
This option only relates to NetCDF variables of type |
This function returns the value of a variable. Numeric variables are always returned in R double precision, no matter what precision they have in the on-disk dataset. NetCDF variables of type NC_CHAR
are returned as R character
or raw
variables, as specified by argument rawchar
.
Values of NA
are supported in numeric variables. Values in the data file that match the variable's missing value attribute (as defined in na.mode
) are automatically converted to NA
before being returned to the user. 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 are sometimes "packed" into types of lower precision. The original data can be recovered (approximately) by multiplication of the stored values by attribute scale_factor
followed by addition of attribute add_offset
. This unpacking operation is performed automatically for variables with attributes scale_factor
and add_offset
if argument unpack
is set to TRUE
. If unpack
is FALSE
, values are read from each 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 reading starts and the number of values to read along each dimension.
The argument collapse
allows to keep degenerated dimensions (if set to FALSE
). As default, array dimensions with length=1 are omitted (e.g., an array with dimensions [2,1,3,4] in the NetCDF dataset is returned as [2,3,4]).
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).
An array with dimensions determined by count
and a data type that depends on the type of variable
. For NetCDF variables of type NC_CHAR
, the R type is either character
or raw
, as specified by argument rawchar
. All other NetCDF variables are returned to R as type numeric
(double precision).
The dimension order in the R array is reversed relative to the order reported by NetCDF commands such as ncdump
, because NetCDF arrays are stored in row-major (C) order whereas R arrays are stored in column-major (Fortran) order.
Arrays of type character
drop the fastest-varying dimension of the corresponding NC_CHAR
array, because this dimension corresponds to the length of the individual character
elements. For example, an NC_CHAR
array with dimensions (5,10) would be returned as a character
vector containing 5 elements, each with a maximum length of 10 characters.
NC_BYTE
is always interpreted as signed.
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 35 36 37 38 | ## Create a new NetCDF dataset and define two dimensions
nc <- create.nc("var.get.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")
## Put the data
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)
## Get the data (or a subset)
var.get.nc(nc, 0)
var.get.nc(nc, "temperature")
var.get.nc(nc, "temperature", c(3,1), c(1,1))
var.get.nc(nc, "temperature", c(3,2))
var.get.nc(nc, "temperature", c(NA,2), c(NA,1))
var.get.nc(nc, "name")
var.get.nc(nc, "name", c(1,2), c(4,2))
var.get.nc(nc, "name", c(1,2), c(NA,2))
close.nc(nc)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.