[GRASS-user] Null-value in grass python array

Glynn Clements glynn at gclements.plus.com
Sat Oct 11 03:59:37 PDT 2014

Johannes Radinger wrote:

> I am using the GRASS-numpy functionality [1] to read a python numpy.array
> from a GRASS raster map (GRASS7):
> import grass.script.array as garray
> a = garray.array()
> a.read(map)
> Here [2] it says that also the null value can be specified. So what is the
> actual null value in GRASS so that the raster map is correctly saved as
> numpy.array with numpy.nan where appropriate?

For integer maps, the null value is -2^31 = -2147483648. For
floating-point maps, the null value is NaN.

Note that the null= parameter for read() and write() specifies the
value in the numpy array which is mapped to/from null values in the
GRASS raster.

If you're using floating-point numpy arrays, then use
null=numpy.nan[1]. For integer arrays, using null=-2147483648 will
ensure that valid values don't collide with nulls.

[1] This assumes that atof() and sscanf("%lf") recognise "nan"; this
is the case on Linux, but doesn't appear to work on Windows.

> And a second related question: Does grass.script.array.read() respect an
> existing MASK so that areas that are masked will get numpy.nan? Of course this can
> also be done within numpy like map[numpy.isnan(mask)] but this again needs
> to properly define the nan value when reading the raster mask into an
> numpy.array.

read() and write() use r.out.bin and r.in.bin respectively. r.out.bin
respects the MASK.

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-user mailing list