var.get.nc | R Documentation |
Read the contents of a NetCDF variable.
var.get.nc(ncfile, variable, start=NA, count=NA,
na.mode=4, collapse=TRUE, unpack=FALSE, rawchar=FALSE, fitnum=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. | |||||||||||||||||||||||||
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 |
Missing values in the NetCDF dataset are converted to
For explanation of attribute conventions used by mode 4, please see: https://docs.unidata.ucar.edu/nug/current/attribute_conventions.html | |||||||||||||||||||||||||
collapse |
| |||||||||||||||||||||||||
unpack |
Packed variables are unpacked if | |||||||||||||||||||||||||
rawchar |
This option only relates to NetCDF variables of type | |||||||||||||||||||||||||
fitnum |
By default, all numeric variables are read into R as double precision values. When
| |||||||||||||||||||||||||
cache_bytes |
( | |||||||||||||||||||||||||
cache_slots |
( | |||||||||||||||||||||||||
cache_preemption |
( |
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 a NetCDF variable that are deemed to be missing are automatically converted to NA
in the results returned to R. Unusual cases can be handled directly in user code by setting na.mode=3
.
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/or 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
. For NC_STRING
, the R type is character
. Numeric variables are read as double precision by default, but the smallest R type that exactly represents each external type is used if fitnum
is TRUE
.
Variables of user-defined types are supported. "compound"
arrays are read into R as lists, with items named for the compound fields; items of base NetCDF data types are converted to R arrays, with leading dimensions from the field dimensions (if any) and trailing dimensions from the NetCDF variable. "enum"
arrays are read into R as factor arrays. "opaque"
arrays are read into 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 read into R as a list with dimensions of the NetCDF variable; items in the list may have different lengths; base NetCDF data types are converted to R vectors.
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.
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.
Pavel Michna, Milton Woods
https://www.unidata.ucar.edu/software/netcdf/
## Create a new NetCDF dataset and define two dimensions
file1 <- tempfile("var.get_", 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")
## 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)
unlink(file1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.