nc2raster: Convert Raster layer to a matrix or array

Description Usage Arguments Value Author(s) Examples

View source: R/nc2raster.r

Description

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

Usage

1
2
nc2raster(nc, varname, t=layer, lonname="lon", latname="lat", 
    layer, date=T)

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.

lonname

character string indicating the name of the longitude-variable of ncdf4-objects and '.nc'-files to plot (default is 'lon')

latname

character string indicating the name of the latitude-variable of ncdf4-objects and '.nc'-files to plot (default is 'lat')

layer, t

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

date

whether the layer names should be set to the date of the ncdf-file layer (default is TRUE, format is 'X%Y%m%d').

Value

RasterLayer or RasterStack

Author(s)

Robert K. Bauer

Examples

 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
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 March 23, 2020, 5:06 p.m.

Related to nc2raster in oceanmap...