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

Michael Sumner mdsumner at gmail.com
Wed Jul 30 10:13:08 CEST 2014


Hello,

Thanks for clarifying, I get that problem too.  If I get a chance I'll
have a closer look, but otherwise here's my notes for now.

Note that conversion to stack allows it to work:
library(raster)
Loading required package: sp
b <- brick('http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/godas/pottmp.2013.nc',
lvar=3, level=1, varname='pottmp')
dropLayer(stack(b), 1:2)

I presume you are getting this url read via the ncdf4 package? (And
not via the GDAL DODS driver, for example).

I get the same problem with subset() where i is longer than 1
(dropLayer is a wrapper around subset). Below is my subset traceback
and session info.

Cheers, Mike.

subset(x, 1:2)
Error in (function (classes, fdef, mtable)  :
  unable to find an inherited method for function ‘extent’ for
signature ‘"character"’
In addition: Warning message:
In .stackCDF(x, varname = varname, bands = bands) :
  pottmp has 4 dimensions, I am using the last one
> traceback()
8: stop(gettextf("unable to find an inherited method for function %s
for signature %s",
       sQuote(fdef at generic), sQuote(cnames)), domain = NA)
7: (function (classes, fdef, mtable)
   {
       methods <- .findInheritedMethods(classes, fdef, mtable)
       if (length(methods) == 1L)
           return(methods[[1L]])
       else if (length(methods) == 0L) {
           cnames <- paste0("\"", sapply(classes, as.character),
               "\"", collapse = ", ")
           stop(gettextf("unable to find an inherited method for
function %s for signature %s",
               sQuote(fdef at generic), sQuote(cnames)), domain = NA)
       }
       else stop("Internal error in finding inherited methods; didn't
return a unique method",
           domain = NA)
   })(list("character"), function (x, ...)
   standardGeneric("extent"), <environment>)
6: extent(x)
5: setExtent(x, value)
4: `extent<-`(`*tmp*`, value = <S4 object of class "Extent">)
3: .local(x, ...)
2: subset(x, 1:2)
1: subset(x, 1:2)

sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-redhat-linux-gnu (64-bit)

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] ncdf4_1.12    raster_2.2-31 sp_1.0-15

loaded via a namespace (and not attached):
[1] grid_3.1.0      lattice_0.20-29 tools_3.1.0






On Wed, Jul 30, 2014 at 5:45 PM, Mark Payne <markpayneatwork at gmail.com> wrote:
> No, apparently it is still a brick:
>
>> class(b)
> [1] "RasterBrick"
> attr(,"package")
> [1] "raster"
>
>
> Here is a minimum (not)-working example, based on this post, which seems to
> be having similar problems:
> http://r-sig-geo.2731867.n2.nabble.com/Subset-a-Raster-object-read-from-a-4D-NetCDF-file-td7586200.html
>
> Example follows:
>> library(raster)
> Loading required package: sp
>> b <-
>> brick('http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/godas/pottmp.2013.nc',
>> lvar=3, level=1, varname='pottmp')
>> b
> class       : RasterBrick
> dimensions  : 418, 360, 150480, 12  (nrow, ncol, ncell, nlayers)
> resolution  : 1, 0.3333309  (x, y)
> extent      : 0, 360, -74.66667, 64.66567  (xmin, xmax, ymin, ymax)
> coord. ref. : +proj=longlat +datum=WGS84
> data source :
> http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/godas/pottmp.2013.nc
> names       : X2013.01.01, X2013.02.01, X2013.03.01, X2013.04.01,
> X2013.05.01, X2013.06.01, X2013.07.01, X2013.08.01, X2013.09.01,
> X2013.10.01, X2013.11.01, X2013.12.01
> Date        : 2013-01-01, 2013-02-01, 2013-03-01, 2013-04-01, 2013-05-01,
> 2013-06-01, 2013-07-01, 2013-08-01, 2013-09-01, 2013-10-01, 2013-11-01,
> 2013-12-01
> varname     : pottmp
> level       : 1
>
>> dropLayer(b,1)
>
> Error in (function (classes, fdef, mtable)  :
>   unable to find an inherited method for function ‘extent’ for signature
> ‘"character"’
> In addition: Warning message:
> In .stackCDF(x, varname = varname, bands = bands) :
>   pottmp has 4 dimensions, I am using the last one
>>
>
>
>
> On 29 July 2014 13:24, Michael Sumner <mdsumner at gmail.com> wrote:
>>
>> 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
>
>



-- 
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