[GRASS-dev] GRASS to Numpy [was: Re: List of new features in GRASS
7 (in trac)]
Glynn Clements
glynn at gclements.plus.com
Tue Jan 26 04:45:19 EST 2010
Tim Michelsen wrote:
> O question on this here:
> > Improved Python API: * read/write GRASS rasters to/from NumPy
> How do I trigger this?
>
> Will a command r.out.numpy save the raster in a pickled array?
> How will the geographic information be retrained?
The grass.script.array module defines a class "array" which is a
subclass of numpy.memmap with .read() and .write() methods to
read/write the underlying file via r.out.bin/r.in.bin.
Example:
import grass.script.array as garray
a = garray.array()
a.read("elevation.dem")
b = garray.array()
b[...] = (a / 50).astype(int) * 50 # or whatever
b.write("elev.50m")
The size of the array is taken from the current region.
The main drawback of using numpy is that you're limited by available
memory. Using a subclass of numpy.memmap lets you use files which may
be much larger, but processing the entire array in one go is likely to
produce in-memory results of a similar size.
Unfortunately, I don't think that it's possible to define an array
subclass which provides the data in chunks.
--
Glynn Clements <glynn at gclements.plus.com>
More information about the grass-dev
mailing list