[R-sig-Geo] Raster package writeRaster format="NCF"

Robert J. Hijmans r.hijmans at gmail.com
Wed Oct 6 10:16:20 CEST 2010


Elizabeth,

Creating a raster object from a nc file is probably easiest done like this:

r <- raster(filename, band=1)

Or to get the all:

b <- brick(filename)

then:

ba <- aggregate(b, ....)

Normally, you should be able to do this to save it as ncdf:

ba <- writeRaster(ba, filename='out.nc')

Or in one step:

ba <- aggregate(b, filename='out.nc')

However, you have uncovered a bug in writing to a ncdf file when
values are not in memory, I'll think I have fixed that  in the
development version (available from R-Forge in 24 hrs). For now, a
work around is to do 'readAll' before you use writeRaster (this will
fail if the file is too large, but that is probably not the case)

r <- readAll(r)
r <- writeRaster(...


Robert


On Tue, Oct 5, 2010 at 6:20 AM, Elizabeth Crisfield <eac16 at psu.edu> wrote:
> I have read a 10 band .nc file into R using *open.ncdf* and
> *get.var.ncdf*and I've read each band into a raster format e.g.
> *raster(data[,,1])*. I used *aggregate* to upscale the data, and now I want
> to write it back out to netcdf format. For some reason, I can write each
> band out separately (e.g. *writeRaster(newdataband1, "newdataband1",
> format="CDF")* but if I *stack* the data first, I get an error when I try to
> write the stack out to netcdf. The error says "*CDF is not a supported file
> format. See writeFormats()"*. Sure enough, *writeFormats()* gives me a long
> list of formats that are enabled, and CDF is not in the list.
>
> ?
>
> I am running R (R 2.11.1 GUI 1.34 Leopard build 64-bit (5589)) on Mac OS X
> (10.6.4) with up-to-date packages of ncdf and Raster. I've also installed
> netcdf through MacPorts and the libraries are in /opt/local rather than
> /usr/local (I'm wondering if I have a library or path problem?)
>
> How can I get the writeRaster to add CDF to the list of writeFormats?
>
> Is there another way to reconstruct this netcdf file from the 10 raster
> layers in R? (*create.ncdf*?) Alternatively, since I think I have them
> written out to separate netcdfs I could maybe bind them back together using
> another utility outside of R?
>
> Thanks in advance.
>
> Elizabeth
> ____________________
> Elizabeth Crisfield
> Research Assistant
> Geography Department
> Penn State University
> phone 814-777-3395
> eac16 at psu.edu
> www.elizabethcrisfield.com
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list