Skip to content
Prev 21399 / 29559 Next

raster - 4D Bricks

Are you sure that your "b" is still really a RasterBrick? That message
comes if you give the functions character:

library(raster)
dropLayer("sometext", 1)
Error in (function (classes, fdef, mtable)  :
  unable to find an inherited method for function ?dropLayer? for
signature ?"character"?

(Basically because the functions are all generics, they scans through
available methods and tells you it can't find one for "character").

It works for me, see contrived example below that I had on hand due to
obscure testing many years ago.
(There is warning related to subsetting a linked-4D file, but that
seems harmless).

But, also try selecting only the bands you want at read time (or even
just use a stack), so

b <- brick("/blah/blah/ncep_07c_tho_all_1948-2010.mon.nc", level = 3,
band = 1:5)

Cheers, Mike.

## create a 4D NetCDF file in R
## load NetCDF package
library(RNetCDF)
## generate 4D data - 1:N
dims <- c(2, 3, 2, 2)
a <- array(data = 1:prod(dims), dim = dims)

## create NetCDF file
nc <- create.nc("test-1.nc")
## coordinate values for NetCDF (to test transform and band metadata)
x <- seq(0, 5, length = dim(a)[1])
y <- seq(0, 2, length = dim(a)[2])
c3 <- seq(0, 1, length = dim(a)[3])
c4 <- seq(0, 1, length = dim(a)[4])

## dimensions
dim.def.nc(nc, "x", length(x))
dim.def.nc(nc, "y", length(y))
dim.def.nc(nc, "c3", length(c3))
dim.def.nc(nc, "c4", length(c4))

## variables
var.def.nc(nc, "a", "NC_DOUBLE", c(0, 1, 2, 3))
var.def.nc(nc, "x", "NC_DOUBLE", 0)
var.def.nc(nc, "y", "NC_DOUBLE", 1)
var.def.nc(nc, "c3", "NC_DOUBLE", 2)
var.def.nc(nc, "c4", "NC_DOUBLE", 3)
## write the data (all at once, no start/count)
var.put.nc(nc, "a", a)
var.put.nc(nc, "x", x)
var.put.nc(nc, "y", y)
var.put.nc(nc, "c3", c3)
var.put.nc(nc, "c4", c4)
## finish up
close.nc(nc)

library(raster)
b <- brick("test-1.nc", level = 2)
nlevels(b)
dropLayer(b, 1)
class       : RasterLayer
band        : 2  (of  2  bands)
dimensions  : 3, 2, 6  (nrow, ncol, ncell)
resolution  : 5, 1  (x, y)
extent      : -2.5, 7.5, -0.5, 2.5  (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : C:\temp\test-1.nc
names       : X1
z-value     : 1
zvar        : a
level       : 1

Warning message:
In .rasterObjectFromCDF(x, type = objecttype, band = band, ...) :
  "level" set to 1 (there are 2 levels)
On Tue, Jul 29, 2014 at 7:43 PM, Mark Payne <markpayneatwork at gmail.com> wrote: