[GRASS-user] Getting statistical values (r.univar) via python

Hi,

I am trying to get statistical values for a raster map in a python script.
What I am doing so far is using r.univar (e.g. for the nc-dataset):

grass.read_command("r.univar", map ="elevation")

but how to get e.g the mean value as float? I thought of two
options:

1) either indexing like
float(grass.read_command("r.univar", map ="elevation")[x:y])

2) or splitting the string into lines and then at the ":"

The problem with the first method, although quite easy, is that
the indexing numbers [x:y] actually depend on the map (different
decimal value lengths). Thus it is probably not possible to use
the first approach in a automatized way e.g. loop.
I haven't tried the second approach as I am not that familiar with
string splitting yet, but any hint is appreciated.

In the Wiki (http://grass.osgeo.org/wiki/GRASS_Python_Scripting_Library#Using_output_from_GRASS_modules_in_the_script) I found an example for grass.raster_info
but that does not provide e.g. a mean value.

Any suggestions or is there even an other method?

/Johannes

--
NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone!
Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a

Hi Johannes,

On Fri, May 25, 2012 at 1:12 PM, Johannes Radinger <JRadinger@gmx.at> wrote:

Hi,

I am trying to get statistical values for a raster map in a python script.
What I am doing so far is using r.univar (e.g. for the nc-dataset):

grass.read_command(“r.univar”, map =“elevation”)

but how to get e.g the mean value as float? I thought of two
options:

  1. either indexing like
    float(grass.read_command(“r.univar”, map =“elevation”)[x:y])

  2. or splitting the string into lines and then at the “:”

You can use the following:

import sys
import os
import grass.script as grass

univar = grass.read_command(‘r.univar’, map=‘elevation’)

print univar #so that you can see how the output looks like

The output is:
total null and non-null cells: 2025000
total null cells: 0

Of the non-null cells:

n: 2025000
minimum: 55.5788
maximum: 156.33
range: 100.751
mean: 110.375
mean of absolute values: 110.375
standard deviation: 20.3153
variance: 412.712
variation coefficient: 18.4057 %
sum: 223510266.5581016541

Now you want to split every line of the output with .split(‘\n’), you are interested in the line [10], then you split against split(‘:’) and take the argument [1].

mean = float(univar.split(‘\n’)[10].split(‘:’)[1])

Hope this helps,

madi


Ing. Margherita Di Leo, Ph.D.

Hi Madi,

thank you, that's just what I thought of...
anyway do you know does r.univar consider MASK if it is existing?

/johannes

-------- Original-Nachricht --------

Datum: Fri, 25 May 2012 13:38:05 +0200
Von: Margherita Di Leo <diregola@gmail.com>
An: Johannes Radinger <JRadinger@gmx.at>
CC: grass user list <grass-user@lists.osgeo.org>
Betreff: Re: [GRASS-user] Getting statistical values (r.univar) via python

Hi Johannes,

On Fri, May 25, 2012 at 1:12 PM, Johannes Radinger <JRadinger@gmx.at>
wrote:

> Hi,
>
> I am trying to get statistical values for a raster map in a python
script.
> What I am doing so far is using r.univar (e.g. for the nc-dataset):
>
> grass.read_command("r.univar", map ="elevation")
>
> but how to get e.g the mean value as float? I thought of two
> options:
>
> 1) either indexing like
> float(grass.read_command("r.univar", map ="elevation")[x:y])
>
> 2) or splitting the string into lines and then at the ":"
>
>
You can use the following:

import sys
import os
import grass.script as grass

univar = grass.read_command('r.univar', map='elevation')

print univar #so that you can see how the output looks like

The output is:
total null and non-null cells: 2025000
total null cells: 0

Of the non-null cells:
----------------------
n: 2025000
minimum: 55.5788
maximum: 156.33
range: 100.751
mean: 110.375
mean of absolute values: 110.375
standard deviation: 20.3153
variance: 412.712
variation coefficient: 18.4057 %
sum: 223510266.5581016541

Now you want to split every line of the output with .split('\n'), you are
interested in the line [10], then you split against split(':') and take
the
argument [1].

mean = float(univar.split('\n')[10].split(':')[1])

Hope this helps,

madi
--
Ing. Margherita Di Leo, Ph.D.

--
NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone!
Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a

On Fri, May 25, 2012 at 1:46 PM, Johannes Radinger <JRadinger@gmx.at> wrote:

Hi Madi,

thank you, that’s just what I thought of…
anyway do you know does r.univar consider MASK if it is existing?

Yes it does.

madi

/johannes

-------- Original-Nachricht --------

Datum: Fri, 25 May 2012 13:38:05 +0200
Von: Margherita Di Leo <diregola@gmail.com>
An: Johannes Radinger <JRadinger@gmx.at>
CC: grass user list <grass-user@lists.osgeo.org>
Betreff: Re: [GRASS-user] Getting statistical values (r.univar) via python

Hi Johannes,

On Fri, May 25, 2012 at 1:12 PM, Johannes Radinger <JRadinger@gmx.at>
wrote:

Hi,

I am trying to get statistical values for a raster map in a python
script.
What I am doing so far is using r.univar (e.g. for the nc-dataset):

grass.read_command(“r.univar”, map =“elevation”)

but how to get e.g the mean value as float? I thought of two
options:

  1. either indexing like
    float(grass.read_command(“r.univar”, map =“elevation”)[x:y])

  2. or splitting the string into lines and then at the “:”

You can use the following:

import sys
import os
import grass.script as grass

univar = grass.read_command(‘r.univar’, map=‘elevation’)

print univar #so that you can see how the output looks like

The output is:
total null and non-null cells: 2025000
total null cells: 0

Of the non-null cells:

n: 2025000
minimum: 55.5788
maximum: 156.33
range: 100.751
mean: 110.375
mean of absolute values: 110.375
standard deviation: 20.3153
variance: 412.712
variation coefficient: 18.4057 %
sum: 223510266.5581016541

Now you want to split every line of the output with .split(‘\n’), you are
interested in the line [10], then you split against split(‘:’) and take
the
argument [1].

mean = float(univar.split(‘\n’)[10].split(‘:’)[1])

Hope this helps,

madi

Ing. Margherita Di Leo, Ph.D.


NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone!
Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a


Ing. Margherita Di Leo, Ph.D.

On 25/05/12 13:12, Johannes Radinger wrote:

Hi,

I am trying to get statistical values for a raster map in a python script.
What I am doing so far is using r.univar (e.g. for the nc-dataset):

grass.read_command("r.univar", map ="elevation")

but how to get e.g the mean value as float?

There are probably more elegant ways, but this should work:

float(grass.read_command("r.univar", map="elevation").splitlines()[10].split()[4])

Moritz