[GRASS-user] how to store results from r.sum with script

Dear all,

I would like to know if there is a way to store results of a calculation done with r.sum on many raster with a bash script.

I need to reuse these results in later calculations.

Thanks

Lucien

Hi,

I might missed it but I don’t know about r.sum module. (I know only r.sun).

Saving standard output of module (if it prints something to standard output) is the same as for any other command line tool in bash. If the output is a map (in GRASS) just use it as input to some other module (using the map name).

Examples bellow show saving outputs of echo command to variables (echo command just print its parameters to standard output, i.e. to command line). Working with numbers (additions, multiplications) can be more difficult (requires different syntax) but there should be some online guides.

Vaclav

$ A=“some text”
$ echo $A
some text

$ B=echo "this is printed to stdout"
$ echo $B
this is printed to stdout

$ C=$(echo “this is printed to stdout”)
$ echo $C
this is printed to stdout

(lines beginning with $ are lines with command which are meant to by typed into the command line, the other lines are output)

···

On Sat, Jan 11, 2014 at 9:32 AM, BLANDENIER Lucien <lucien.blandenier@unine.ch> wrote:

Dear all,

I would like to know if there is a way to store results of a calculation done with r.sum on many raster with a bash script.

I need to reuse these results in later calculations.

Thanks

Lucien


grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

Hello Vaclav,

I tried with the following script:

$C=$(r.sum rast=map@mapset)
$echo $C
SUM = 16116.500006

But I think the variable C stores all the command (r.sum rast=map@mapset) and not only the result. Do you know how it would be possible to store the result "SUM = 16116.500006"

Regards

Lucien

________________________________
De : Vaclav Petras [wenzeslaus@gmail.com]
Envoyé : dimanche 12 janvier 2014 03:11
À : BLANDENIER Lucien
Cc : grass-user@lists.osgeo.org
Objet : Re: [GRASS-user] how to store results from r.sum with script

Hi,

I might missed it but I don't know about r.sum module. (I know only r.sun).

Saving standard output of module (if it prints something to standard output) is the same as for any other command line tool in bash. If the output is a map (in GRASS) just use it as input to some other module (using the map name).

Examples bellow show saving outputs of echo command to variables (echo command just print its parameters to standard output, i.e. to command line). Working with numbers (additions, multiplications) can be more difficult (requires different syntax) but there should be some online guides.

Vaclav

$ A="some text"
$ echo $A
some text

$ B=`echo "this is printed to stdout"`
$ echo $B
this is printed to stdout

$ C=$(echo "this is printed to stdout")
$ echo $C
this is printed to stdout

(lines beginning with $ are lines with command which are meant to by typed into the command line, the other lines are output)

On Sat, Jan 11, 2014 at 9:32 AM, BLANDENIER Lucien <lucien.blandenier@unine.ch<mailto:lucien.blandenier@unine.ch>> wrote:
Dear all,

I would like to know if there is a way to store results of a calculation done with r.sum on many raster with a bash script.

I need to reuse these results in later calculations.

Thanks

Lucien

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org<mailto:grass-user@lists.osgeo.org>
http://lists.osgeo.org/mailman/listinfo/grass-user

Try remove spaces from r.sum command output (e.g. using sed) and since the results is

SUM=16116.500006

which is valid bash variable definition, you can use eval command which will define the variable, so you can use it afterwards.

$ eval $(echo “SUM = 16116.500006” | sed -e “s/ //g”)
$ echo $SUM
16116.500006

I assume from what you have written that r.sum rast=map@mapset prints

SUM = 16116.500006

to standard output. Knowing what is the output of that module is crucial. What module is this, do you have documentation? GRASS modules (in GRASS distribution) use the convention that if you use -g flag they print the variables in “shell script style”, so you can use the output in eval function or parse it easily. They output would be

SUM = 16116.500006

···

On Mon, Jan 13, 2014 at 10:20 AM, BLANDENIER Lucien <lucien.blandenier@unine.ch> wrote:

Hello Vaclav,

I tried with the following script:

$C=$(r.sum rast=map@mapset)
$echo $C
SUM = 16116.500006

But I think the variable C stores all the command (r.sum rast=map@mapset) and not only the result. Do you know how it would be possible to store the result “SUM = 16116.500006”

Regards

Lucien


De : Vaclav Petras [wenzeslaus@gmail.com]
Envoyé : dimanche 12 janvier 2014 03:11
À : BLANDENIER Lucien
Cc : grass-user@lists.osgeo.org
Objet : Re: [GRASS-user] how to store results from r.sum with script

Hi,

I might missed it but I don’t know about r.sum module. (I know only r.sun).

Saving standard output of module (if it prints something to standard output) is the same as for any other command line tool in bash. If the output is a map (in GRASS) just use it as input to some other module (using the map name).

Examples bellow show saving outputs of echo command to variables (echo command just print its parameters to standard output, i.e. to command line). Working with numbers (additions, multiplications) can be more difficult (requires different syntax) but there should be some online guides.

Vaclav

$ A=“some text”
$ echo $A
some text

$ B=echo "this is printed to stdout"
$ echo $B
this is printed to stdout

$ C=$(echo “this is printed to stdout”)
$ echo $C
this is printed to stdout

(lines beginning with $ are lines with command which are meant to by typed into the command line, the other lines are output)

On Sat, Jan 11, 2014 at 9:32 AM, BLANDENIER Lucien <lucien.blandenier@unine.chmailto:[lucien.blandenier@unine.ch](mailto:lucien.blandenier@unine.ch)> wrote:
Dear all,

I would like to know if there is a way to store results of a calculation done with r.sum on many raster with a bash script.

I need to reuse these results in later calculations.

Thanks

Lucien


grass-user mailing list

grass-user@lists.osgeo.orgmailto:[grass-user@lists.osgeo.org](mailto:grass-user@lists.osgeo.org)
http://lists.osgeo.org/mailman/listinfo/grass-user

The -g flag is not valide for this module...

Do you know if there is others possibilities?

Thanks a lot

Lucien

________________________________
De : Vaclav Petras [wenzeslaus@gmail.com]
Envoyé : lundi 13 janvier 2014 16:34
À : BLANDENIER Lucien
Cc : grass-user@lists.osgeo.org
Objet : Re: [GRASS-user] how to store results from r.sum with script

Try remove spaces from r.sum command output (e.g. using `sed`) and since the results is

SUM=16116.500006

which is valid bash variable definition, you can use `eval` command which will define the variable, so you can use it afterwards.

$ eval $(echo "SUM = 16116.500006" | sed -e "s/ //g")
$ echo $SUM
16116.500006

I assume from what you have written that `r.sum rast=map@mapset` prints

SUM = 16116.500006

to standard output. Knowing what is the output of that module is crucial. What module is this, do you have documentation? GRASS modules (in GRASS distribution) use the convention that if you use `-g` flag they print the variables in "shell script style", so you can use the output in eval function or parse it easily. They output would be

SUM = 16116.500006

On Mon, Jan 13, 2014 at 10:20 AM, BLANDENIER Lucien <lucien.blandenier@unine.ch<mailto:lucien.blandenier@unine.ch>> wrote:
Hello Vaclav,

I tried with the following script:

$C=$(r.sum rast=map@mapset)
$echo $C
SUM = 16116.500006

But I think the variable C stores all the command (r.sum rast=map@mapset) and not only the result. Do you know how it would be possible to store the result "SUM = 16116.500006"

Regards

Lucien

________________________________
De : Vaclav Petras [wenzeslaus@gmail.com<mailto:wenzeslaus@gmail.com>]
Envoyé : dimanche 12 janvier 2014 03:11
À : BLANDENIER Lucien
Cc : grass-user@lists.osgeo.org<mailto:grass-user@lists.osgeo.org>
Objet : Re: [GRASS-user] how to store results from r.sum with script

Hi,

I might missed it but I don't know about r.sum module. (I know only r.sun).

Saving standard output of module (if it prints something to standard output) is the same as for any other command line tool in bash. If the output is a map (in GRASS) just use it as input to some other module (using the map name).

Examples bellow show saving outputs of echo command to variables (echo command just print its parameters to standard output, i.e. to command line). Working with numbers (additions, multiplications) can be more difficult (requires different syntax) but there should be some online guides.

Vaclav

$ A="some text"
$ echo $A
some text

$ B=`echo "this is printed to stdout"`
$ echo $B
this is printed to stdout

$ C=$(echo "this is printed to stdout")
$ echo $C
this is printed to stdout

(lines beginning with $ are lines with command which are meant to by typed into the command line, the other lines are output)

On Sat, Jan 11, 2014 at 9:32 AM, BLANDENIER Lucien <lucien.blandenier@unine.ch<mailto:lucien.blandenier@unine.ch><mailto:lucien.blandenier@unine.ch>> wrote:
Dear all,

I would like to know if there is a way to store results of a calculation done with r.sum on many raster with a bash script.

I need to reuse these results in later calculations.

Thanks

Lucien

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org<mailto:grass-user@lists.osgeo.org><mailto:grass-user@lists.osgeo.org>
http://lists.osgeo.org/mailman/listinfo/grass-user

On Mon, Jan 13, 2014 at 10:42 AM, BLANDENIER Lucien <
lucien.blandenier@unine.ch> wrote:

The -g flag is not valide for this module...

OK, now I understand, r.sum was merged to
r.statistics<http://grass.osgeo.org/grass70/manuals/r.statistics.html&gt;
, r.statistics2 <http://grass.osgeo.org/grass70/manuals/r.statistics2.html&gt;
, r.statistics3 <http://grass.osgeo.org/grass70/manuals/r.statistics3.html&gt; in
GRASS 7, it is available only in GRASS 6.

http://grass.osgeo.org/grass64/manuals/r.sum.html
https://trac.osgeo.org/grass/wiki/Grass7/NewFeatures

Do you know if there is others possibilities?

Yes, use what I wrote before (explained in that email):

$ eval $(echo "SUM = 16116.500006" | sed -e "s/ //g")
$ echo $SUM
16116.500006

For r.sum in GRASS 6 in NC sample location:

eval $(r.sum elevation | sed -e "s/ //g")
echo $SUM

27569640.087723

SUM_elevation=$SUM
echo $SUM_elevation

27569640.087723

or shorter

SUM_elevation=$(r.sum elevation | sed -e "s/SUM = //g")
echo $SUM_elevation

27569640.087723

Thanks a lot

Lucien

________________________________
De : Vaclav Petras [wenzeslaus@gmail.com]
Envoyé : lundi 13 janvier 2014 16:34
À : BLANDENIER Lucien
Cc : grass-user@lists.osgeo.org
Objet : Re: [GRASS-user] how to store results from r.sum with script

Try remove spaces from r.sum command output (e.g. using `sed`) and since
the results is

SUM=16116.500006

which is valid bash variable definition, you can use `eval` command which
will define the variable, so you can use it afterwards.

$ eval $(echo "SUM = 16116.500006" | sed -e "s/ //g")
$ echo $SUM
16116.500006

I assume from what you have written that `r.sum rast=map@mapset` prints

SUM = 16116.500006

to standard output. Knowing what is the output of that module is crucial.
What module is this, do you have documentation? GRASS modules (in GRASS
distribution) use the convention that if you use `-g` flag they print the
variables in "shell script style", so you can use the output in eval
function or parse it easily. They output would be

SUM = 16116.500006

On Mon, Jan 13, 2014 at 10:20 AM, BLANDENIER Lucien <
lucien.blandenier@unine.ch<mailto:lucien.blandenier@unine.ch>> wrote:
Hello Vaclav,

I tried with the following script:

$C=$(r.sum rast=map@mapset)
$echo $C
SUM = 16116.500006

But I think the variable C stores all the command (r.sum rast=map@mapset)
and not only the result. Do you know how it would be possible to store the
result "SUM = 16116.500006"

Regards

Lucien

________________________________
De : Vaclav Petras [wenzeslaus@gmail.com<mailto:wenzeslaus@gmail.com>]
Envoyé : dimanche 12 janvier 2014 03:11
À : BLANDENIER Lucien
Cc : grass-user@lists.osgeo.org<mailto:grass-user@lists.osgeo.org>
Objet : Re: [GRASS-user] how to store results from r.sum with script

Hi,

I might missed it but I don't know about r.sum module. (I know only r.sun).

Saving standard output of module (if it prints something to standard
output) is the same as for any other command line tool in bash. If the
output is a map (in GRASS) just use it as input to some other module (using
the map name).

Examples bellow show saving outputs of echo command to variables (echo
command just print its parameters to standard output, i.e. to command
line). Working with numbers (additions, multiplications) can be more
difficult (requires different syntax) but there should be some online
guides.

Vaclav

$ A="some text"
$ echo $A
some text

$ B=`echo "this is printed to stdout"`
$ echo $B
this is printed to stdout

$ C=$(echo "this is printed to stdout")
$ echo $C
this is printed to stdout

(lines beginning with $ are lines with command which are meant to by typed
into the command line, the other lines are output)

On Sat, Jan 11, 2014 at 9:32 AM, BLANDENIER Lucien <
lucien.blandenier@unine.ch<mailto:lucien.blandenier@unine.ch><mailto:
lucien.blandenier@unine.ch<mailto:lucien.blandenier@unine.ch>>> wrote:
Dear all,

I would like to know if there is a way to store results of a calculation
done with r.sum on many raster with a bash script.

I need to reuse these results in later calculations.

Thanks

Lucien

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org<mailto:grass-user@lists.osgeo.org><mailto:
grass-user@lists.osgeo.org<mailto:grass-user@lists.osgeo.org>>
http://lists.osgeo.org/mailman/listinfo/grass-user