[GRASS-user] Python - GRASS module output, r.univar

Hi,

I try to write a python script for GRASS6.5 including a
WHILE loop. A certain calculation step (iterative) should
be performed until there are only NULL values in a raster.

I am not sure what is the best way to find out if there are
only NULL cells. I tried it with r.univar which gives no result
when there are only NULL cells in a raster (and no result is acutally
also a result :)). But how can I get that value to variable
in the python script? In the wiki there is something stated about
the grass.script.raster_info (e.g. for the max value):
max = grass.raster_info(inmap)['max']

Is there a special function also for the counts of cells? Or how
can I parse the result of r.univar to a python variable?

My loop:

while (count > 0):
    ...
    ...
    count = number of !isnull of raster A

What would you do? Suggestions are mostly welcome :slight_smile:

cheers,
Johannes

--
NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone!
Jetzt informieren: http://www.gmx.net/de/go/freephone/

Johannes Radinger wrote:

I am not sure what is the best way to find out if there are
only NULL cells. I tried it with r.univar which gives no result
when there are only NULL cells in a raster (and no result is acutally
also a result :)). But how can I get that value to variable
in the python script? In the wiki there is something stated about
the grass.script.raster_info (e.g. for the max value):
max = grass.raster_info(inmap)['max']

If a map contains only null cells, its minimum and maximum will be
"NULL":

  $ r.mapcalc 'foo = null()'
  $ r.info -r foo
  min=NULL
  max=NULL

Using the Python API, The 'min' and 'max' values in the result of the
raster_info() function will be None.

Is there a special function also for the counts of cells? Or how
can I parse the result of r.univar to a python variable?

Counting cells is far more expensive than simply determining whether
there are any non-null cells. Counting cells requires reading the
entire map, while the r.info approach only needs to read the metadata
files.

If you do need to count cells, r.stats is likely to be more efficient
than r.univar.

My loop:

while (count > 0):
    ...
    ...
    count = number of !isnull of raster A

What would you do? Suggestions are mostly welcome :slight_smile:

  while grass.raster_info(inmap)['max'] is not None:
      ...

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

Hi,

I took liberty to add the notes here:
http://grass.osgeo.org/wiki/GRASS_and_Python#NULL_data_management

Markus