[R-sig-Geo] raster - 4D Bricks

Michael Sumner mdsumner at gmail.com
Tue Jul 29 13:24:45 CEST 2014


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:
> Hi,
>
> I have a 4D climate model output that I am trying to work with via raster,
> and that is unfortunately giving problems. I create the object as a brick:
>
>> b
> class       : RasterBrick
> dimensions  : 220, 254, 55880, 756  (nrow, ncol, ncell, nlayers)
> resolution  : 1, 1  (x, y)
> extent      : 0.5, 254.5, 0.5, 220.5  (xmin, xmax, ymin, ymax)
> coord. ref. : +proj=longlat +datum=WGS84
> data source : /home/mpayne/Documents/NACLIM/
> ncep_07c_tho_all_1948-2010.mon.nc
> names       : X19480131, X19480229, X19480331, X19480430, X19480531,
> X19480630, X19480731, X19480831, X19480930, X19481031, X19481130,
> X19481231, X19490131, X19490228, X19490331, ...
> z-value     : 19480131, 20101231 (min, max)
> varname     : var2
> level       : 3
>
> But I would like to drop a lot of the layers, so I try:
>
>> dropLayer(b,1:5)
> Error in (function (classes, fdef, mtable)  :
>   unable to find an inherited method for function ‘extent’ for signature
> ‘"character"’
>>
>
> And get a rather strange error.... Trying subset instead:
>
>> subset(b,1:5)
> Error in (function (classes, fdef, mtable)  :
>   unable to find an inherited method for function ‘extent’ for signature
> ‘"character"’
>>
> Gives the same error. I'm assuming that this is related to the 4D nature of
> the data, as I can't reproduce it elsewise, e.g. using the examples... Any
> ideas?
>
> Best wishes,
>
> Mark
>
>         [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-sig-Geo mailing list