[,ncdfVariable-method {ncdfCF}R Documentation

Extract data for a variable

Description

Extract data from a ncdfVariable instance, optionally sub-setting the dimensions to load only data of interest.

Usage

## S4 method for signature 'ncdfVariable'
x[i, j, ..., drop = TRUE]

Arguments

x

An ncdfVariable instance to extract the data of.

i, j, ...

Expressions, one for each dimension of x, that select a number of elements along each dimension. If any expressions are missing, the entire dimension is extracted. The values for the arguments may be an integer vector or a function that returns an integer vector. The range of the values in the vector will be used. See examples, below.

drop

Logical, ignored. Dimensions are never dropped. Any degenerate dimensions are returned as such, with dimnames and appropriate attributes set.

Details

If all the data of the variable in x is to be extracted, simply use ⁠[]⁠ (unlike with regular arrays, this is required, otherwise the details of the variable are printed on the console).

The indices into the dimensions to be subset can be specified in a variety of ways; in practice it should (resolve to) be a vector of integers. A range (e.g. 100:200), an explicit vector (⁠c(23, 46, 3, 45, 17⁠), a sequence (⁠seq(from = 78, to = 100, by = 2⁠), all work. Note, however, that only a single range is generated from the vector so these examples resolve to 100:200, 3:46, and 78:100, respectively. It is also possible to use a custom function as an argument.

This method works with "bare" indices into the dimensions of the array. If you want to use domain values of the dimensions (e.g. longitude values or timestamps) to extract part of the variable array, use the subset() method.

Value

An array with dimnames and other attributes set.

Examples

fn <- system.file("extdata",
  "pr_day_EC-Earth3-CC_ssp245_r1i1p1f1_gr_20240101-20241231_vncdfCF.nc",
  package = "ncdfCF")
ds <- open_ncdf(fn)
pr <- ds[["pr"]]

# How are the dimensions organized?
dimnames(pr)

# Precipitation data for March for a single location
x <- pr[5, 12, 61:91]
str(x)

# Summer precipitation over the full spatial extent
summer <- pr[, , 173:263]
str(summer)


[Package ncdfCF version 0.1.1 Index]