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.
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])
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:
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])
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:
either indexing like
float(grass.read_command(“r.univar”, map =“elevation”)[x:y])
or splitting the string into lines and then at the “:”
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])