nc2raster: Convert Raster layer to a matrix or array

View source: R/nc2raster.r

nc2rasterR Documentation

Convert Raster layer to a matrix or array

Description

nc2raster converts a netcdf-file ('.nc'-file) or ncdf4-object to a Raster* object, setting the time variable as layer name.

Usage

nc2raster(nc, varname, t=layer, layer, verbose=FALSE)

Arguments

nc

character string indicating the filepath to a netcdf-file ('.nc'-file), or a ncdf4-object.

varname

character string indicating the name of the netcdf-variable to be selected.

layer, t

layer/time stemp to select in multi-layer files.

verbose

should information about the netcdf file, including the variables and dimensions it contains, be printed during loading? (default is FALSE)

Value

RasterLayer or RasterStack

Author(s)

Robert K. Bauer

Examples

path <- system.file("test_files", package="oceanmap")
nfiles <- Sys.glob(paste0(path,'/*.nc'))[1] # load sample-'.nc'-files

nc2raster(nfiles[1],"Conc",layer=1) # RasterLayer
nc2raster(nfiles[1],"Conc",layer=1:4) # RasterStack

library('ncdf4')
nc <- nc_open(nfiles[1])
nc2raster(nc,"Conc",layer=1:4) # RasterStack

###### load & plot sample netcdf-file ('.nc'-file)

### option a) load netcdf-file with ncdf4-package and plot it
library('ncdf4')
ncdf <- nc_open(nfiles[1])
print(ncdf)
v(obj = ncdf, cbpos="r")

### option b) load and plot netcdf-file as RasterStack object
nc <- nc2raster(nfiles[1])
v(nc,cbpos="r") # plot RasterStack object
v(nfiles[1], cbpos="r",replace.na=TRUE) # plot directly netcdf-file

### option c) plot netcdf-file directly
v(nfiles[1], cbpos="r") # plot RasterStack object

###### plot multiple layers:
par(mfrow=c(2,2))
v(nfiles[1], t=1:4, cbpos="r", replace.na=TRUE, subplot = TRUE)


oceanmap documentation built on Nov. 10, 2023, 5:08 p.m.

Related to nc2raster in oceanmap...