ncvar_slice: Read a slice from a netCDF file

Description Usage Arguments Value See Also Examples

View source: R/nc_slice.R

Description

Reads an array of data (a slice) from an existing netCDF file. Typically this array is two-dimensional and meant to be plotted.

Usage

1
ncvar_slice(nc, varid, ...)

Arguments

nc

netCDF handle, from nc_open

varid

the variable to read the data from. Needs to be an actual variable, not a dimension variable. Can be a string with the name of the variable or an object of class ncvar4. If left unspecified, the function will determine if there is only one variable in the file and, if so, read from that.

...

ranges of the values to extract, for each dimension. Each range is a vector of numbers, in the unit of the corresponding dimension, and everything between the minimum and maximum of these numbers will be extracted. Ranges can be specified either as named arguments, whose name matches one of the dimensions, or are assumed to be in the same order as the dimensions of the netCDF variable. Unspecified or NA elements are fully extracted.

Value

A list containing vectors for each dimension (of length > 1) and one array containing the extracted data. For two-dimensional slices, this list is suitable for plotting functions such as image, contour and persp after changing the names to "x", "y", and "z". Convenience functions are provided to make this conversion, convert the slice into a data.frame, or plot the slice directly.

See Also

as.xyz.ncslice4 and as.data.frame.ncslice4 to convert a slice to various usual formats; plot.ncslice4 to plot a slice.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
nc <- nc_open(system.file("extdata", "sresa1b_ncar_ccsm3.nc", package="ncdf4helpers"))
# extract everything
x <- ncvar_slice(nc, "tas")
# inspect the result
str(x)
# extract some specific ranges
x <- ncvar_slice(nc, "tas", lon=c(100, 200))
x <- ncvar_slice(nc, "tas", lat=10:80)
x <- ncvar_slice(nc, "tas", lon=c(0,300), lat=c(10:20))
x <- ncvar_slice(nc, "tas", lon=c(0,50), lat=NA)
nc_close(nc)

# plot the result
image(as.xyz(x))
# or simply
image(x)

# convert the slice into a data.frame for later manipulation
head(as.data.frame(x))

jiho/ncdf4helpers documentation built on May 19, 2019, 10:31 a.m.