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

Hi,

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?

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.

Best regards,
Johannes

[1]http://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library#Interfacing_with_NumPy
[2]http://grass.osgeo.org/programming6/classpython_1_1array_1_1array.html#a493c541122108eca3fc4b441fe1a5487

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@gclements.plus.com>