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

Margherita Di Leo diregola at gmail.com
Fri May 25 07:38:05 EDT 2012


Hi Johannes,

On Fri, May 25, 2012 at 1:12 PM, Johannes Radinger <JRadinger at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/grass-user/attachments/20120525/e202853f/attachment.html


More information about the grass-user mailing list