[GRASS-dev] grass.script.array.read: reading multiple rasters

To read in a raster with GRASS Python Scripting Library, I can use

ref = garray.array()
ref.read(‘mymap’)

But is it also possible to read in multiple raster layers at once? Something like:

ref = garray.array()
ref.read((‘map1’,‘map2’))

If not, what would be the best way to get an array with data from multiple raster layers… using a loop?

What I am after is to get an array with multiple variables (raster layers) to be used as input in e…g. numpy.cov (http://docs.scipy.org/doc/numpy/reference/generated/numpy.cov.html).

Paulo van Breugel wrote:

To read in a raster with GRASS Python Scripting Library, I can use

ref = garray.array()
ref.read('mymap')

But is it also possible to read in multiple raster layers at once?
Something like:

ref = garray.array()
ref.read(('map1','map2'))

At present, no.

The grass.script.array module's "array" class is a fairly simple
subclass of numpy.memmap, which stores the data in a memory-mapped
file).

The only extensions provided by the subclass are that the constructor
automatically sets the array size from the current region, and the
addition of read() and write() methods which use r.out.bin/r.in.bin to
export/import GRASS raster maps to/from the mapped file.

If not, what would be the best way to get an array with data from
multiple raster layers.. using a loop?

Yes.

You can create a 3-dimensional array of the desired size, then read
the individual maps into the layers with e.g.

  import numpy as np
  import grass.script.array as garray

  data = None
  layer = garray.array()

  for i, map in enumerate(maps):
      layer.read(map)
      if data is None:
          s = len(maps)
          r, c = layer.shape
          data = np.empty((s, r, c), dtype=np.double)
      data[i,:,:] = layer

  del layer

--
Glynn Clements <glynn@gclements.plus.com>