[GRASS-user] Trying python scripts.

Dear all,

This is kind of my first e-mail ever to ANY mailing list, so please forgive
me if I don't express myself in the best way. I will improve eventually.
I am a total beginner in python and GRASS (and any other programming
language), so I am trying to run the first examples found in
http://grass.osgeo.org/wiki/GRASS_and_Python.
I managed to run the shaded_relief one OK, but the one with respect to
mapcalc, I couldn't.
I copied a script found in
http://www.mail-archive.com/grass-dev@lists.osgeo.org/msg14669.html
(transcribed below) and tried to run it:

#!/usr/bin/env python

# g.parser demo script for python programing

#%module
#% description: g.parser test script (python) TUN
#%end
#%flag
#% key: f
#% description: A flag
#%end
#%flag
#% key: n
#% description: Another flag
#%end
#%option
#% key: raster1
#% type: string
#% gisprompt: old,cell,raster
#% description: Raster input map
#% required : yes
#%end
#%option
#% key: raster2
#% type: string
#% gisprompt: old,cell,raster
#% description: Raster2 input map
#% required : yes
#%end
#%option
#% key: output
#% type: string
#% gisprompt: new,cell,raster
#% description: Output
#% required : yes
#%end

import os
import sys

import grass.script as grass

def main():
    output = options['output']
    raster1 = options['raster1']
    raster2 = options['raster2']
    grass.mapcalc("$out = $rast1 + $rast2", out = output, rast1 = raster1,
rast2 = raster2)
    if flags['f']:
        print "Flag -f set"
    else:
        print "Flag -f not set"

    if flags['n']:
        print "Flag -n foi definida mas nao devia"
    else:
        print "Flag -n not set- GOOD"
    print options
    print flags
    return 0

if __name__ == "__main__":
    options, flags = grass.parser()
    sys.exit(main())

It seems to run OK and “100%” is displayed in the GRASS prompt, but when I
tried to display the map, I found out that it wasn't created at all.
Just for you to know, to run the script I simply create it (say
“myscript.py”), give permissions and run it in a GRASS session with the
necessary options: > ./myscript.py output=outmap raster1=map1 raster2=map2
I am using Ubuntu 9.04 64bits and GRASS 6.4.ORC5.
Can anyone help me?

Best regards,
Marcello Gorini.
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Trying-python-scripts-tp5109105p5109105.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Hi Marcello,

Marcello Gorini:

This is kind of my first e-mail ever to ANY mailing list, so please forgive
me if I don't express myself in the best way. I will improve eventually.
I am a total beginner in python and GRASS (and any other programming
language), so I am trying to run the first examples found in
http://grass.osgeo.org/wiki/GRASS_and_Python.
I managed to run the shaded_relief one OK, but the one with respect to
mapcalc, I couldn't.
I copied a script found in
http://www.mail-archive.com/grass-dev@lists.osgeo.org/msg14669.html
(transcribed below) and tried to run it:

#!/usr/bin/env python

# g.parser demo script for python programing

#%module
#% description: g.parser test script (python) TUN
#%end
#%flag
#% key: f
#% description: A flag
#%end
#%flag
#% key: n
#% description: Another flag
#%end
#%option
#% key: raster1
#% type: string
#% gisprompt: old,cell,raster
#% description: Raster input map
#% required : yes
#%end
#%option
#% key: raster2
#% type: string
#% gisprompt: old,cell,raster
#% description: Raster2 input map
#% required : yes
#%end
#%option
#% key: output
#% type: string
#% gisprompt: new,cell,raster
#% description: Output
#% required : yes
#%end

import os
import sys

import grass.script as grass

def main():
    output = options['output']
    raster1 = options['raster1']
    raster2 = options['raster2']
    grass.mapcalc("$out = $rast1 + $rast2", out = output, rast1 = raster1,
rast2 = raster2)
    if flags['f']:
        print "Flag -f set"
    else:
        print "Flag -f not set"

    if flags['n']:
        print "Flag -n foi definida mas nao devia"
    else:
        print "Flag -n not set- GOOD"
    print options
    print flags
    return 0

if __name__ == "__main__":
    options, flags = grass.parser()
    sys.exit(main())

It seems to run OK and “100%” is displayed in the GRASS prompt, but when I
tried to display the map, I found out that it wasn't created at all.

If there are no errors and the script ran fine, maybe you just need to check
the region settings?

Just for you to know, to run the script I simply create it (say
“myscript.py”), give permissions and run it in a GRASS session with the
necessary options: > ./myscript.py output=outmap raster1=map1 raster2=map2
I am using Ubuntu 9.04 64bits and GRASS 6.4.ORC5.

For example:

# check region extent, etc.
g.region -p

# set region extent and resolution to the map of your interest
g.region rast=outmap -pa

# give color
r.colors rast=outmap color=rainbow

# try to display
d.mon x0 && d.rast outmap

# is it there?

Regards, Nikos

Hello Nikos,

Marcello Gorini.

Nikos Alexandris wrote:

Hi Marcello,

Marcello Gorini:

This is kind of my first e-mail ever to ANY mailing list, so please
forgive
me if I don't express myself in the best way. I will improve eventually.
I am a total beginner in python and GRASS (and any other programming
language), so I am trying to run the first examples found in
http://grass.osgeo.org/wiki/GRASS_and_Python.
I managed to run the shaded_relief one OK, but the one with respect to
mapcalc, I couldn't.
I copied a script found in
http://www.mail-archive.com/grass-dev@lists.osgeo.org/msg14669.html
(transcribed below) and tried to run it:

#!/usr/bin/env python

# g.parser demo script for python programing

#%module
#% description: g.parser test script (python) TUN
#%end
#%flag
#% key: f
#% description: A flag
#%end
#%flag
#% key: n
#% description: Another flag
#%end
#%option
#% key: raster1
#% type: string
#% gisprompt: old,cell,raster
#% description: Raster input map
#% required : yes
#%end
#%option
#% key: raster2
#% type: string
#% gisprompt: old,cell,raster
#% description: Raster2 input map
#% required : yes
#%end
#%option
#% key: output
#% type: string
#% gisprompt: new,cell,raster
#% description: Output
#% required : yes
#%end

import os
import sys

import grass.script as grass

def main():
    output = options['output']
    raster1 = options['raster1']
    raster2 = options['raster2']
    grass.mapcalc("$out = $rast1 + $rast2", out = output, rast1 =
raster1,
rast2 = raster2)
    if flags['f']:
        print "Flag -f set"
    else:
        print "Flag -f not set"

    if flags['n']:
        print "Flag -n foi definida mas nao devia"
    else:
        print "Flag -n not set- GOOD"
    print options
    print flags
    return 0

if __name__ == "__main__":
    options, flags = grass.parser()
    sys.exit(main())

It seems to run OK and “100%” is displayed in the GRASS prompt, but when
I
tried to display the map, I found out that it wasn't created at all.

If there are no errors and the script ran fine, maybe you just need to
check
the region settings?

Just for you to know, to run the script I simply create it (say
“myscript.py”), give permissions and run it in a GRASS session with the
necessary options: > ./myscript.py output=outmap raster1=map1
raster2=map2
I am using Ubuntu 9.04 64bits and GRASS 6.4.ORC5.

For example:

# check region extent, etc.
g.region -p

# set region extent and resolution to the map of your interest
g.region rast=outmap -pa

# give color
r.colors rast=outmap color=rainbow

# try to display
d.mon x0 && d.rast outmap

# is it there?

Regards, Nikos

No, it is not there. To make sure I set the region to the input map:

g.region rast=map1 -pa
d.mon x0 && d.rast map1

OK, it is there. Then I ran the script:

./myscript.py output=outmap raster1=map1 raster2=map2

After processing, I tried:

d.rast outmap

But then:

ERROR: Raster map <outmap> not found

What could be wrong?

Best regards,

Marcello.

P.S.: By the way, is this the right way to quote answers?

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

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Trying-python-scripts-tp5109105p5109431.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Oops, I guess I quoted the text in the wrong way. Sorry for that.

Marcello Gorini wrote:

Hello Nikos,

Marcello Gorini.

Nikos Alexandris wrote:

Hi Marcello,

Marcello Gorini:

This is kind of my first e-mail ever to ANY mailing list, so please
forgive
me if I don't express myself in the best way. I will improve eventually.
I am a total beginner in python and GRASS (and any other programming
language), so I am trying to run the first examples found in
http://grass.osgeo.org/wiki/GRASS_and_Python.
I managed to run the shaded_relief one OK, but the one with respect to
mapcalc, I couldn't.
I copied a script found in
http://www.mail-archive.com/grass-dev@lists.osgeo.org/msg14669.html
(transcribed below) and tried to run it:

#!/usr/bin/env python

# g.parser demo script for python programing

#%module
#% description: g.parser test script (python) TUN
#%end
#%flag
#% key: f
#% description: A flag
#%end
#%flag
#% key: n
#% description: Another flag
#%end
#%option
#% key: raster1
#% type: string
#% gisprompt: old,cell,raster
#% description: Raster input map
#% required : yes
#%end
#%option
#% key: raster2
#% type: string
#% gisprompt: old,cell,raster
#% description: Raster2 input map
#% required : yes
#%end
#%option
#% key: output
#% type: string
#% gisprompt: new,cell,raster
#% description: Output
#% required : yes
#%end

import os
import sys

import grass.script as grass

def main():
    output = options['output']
    raster1 = options['raster1']
    raster2 = options['raster2']
    grass.mapcalc("$out = $rast1 + $rast2", out = output, rast1 =
raster1,
rast2 = raster2)
    if flags['f']:
        print "Flag -f set"
    else:
        print "Flag -f not set"

    if flags['n']:
        print "Flag -n foi definida mas nao devia"
    else:
        print "Flag -n not set- GOOD"
    print options
    print flags
    return 0

if __name__ == "__main__":
    options, flags = grass.parser()
    sys.exit(main())

It seems to run OK and “100%” is displayed in the GRASS prompt, but when
I
tried to display the map, I found out that it wasn't created at all.

If there are no errors and the script ran fine, maybe you just need to
check
the region settings?

Just for you to know, to run the script I simply create it (say
“myscript.py”), give permissions and run it in a GRASS session with the
necessary options: > ./myscript.py output=outmap raster1=map1
raster2=map2
I am using Ubuntu 9.04 64bits and GRASS 6.4.ORC5.

For example:

# check region extent, etc.
g.region -p

# set region extent and resolution to the map of your interest
g.region rast=outmap -pa

# give color
r.colors rast=outmap color=rainbow

# try to display
d.mon x0 && d.rast outmap

# is it there?

Regards, Nikos

No, it is not there. To make sure I set the region to the input map:

g.region rast=map1 -pa
d.mon x0 && d.rast map1

OK, it is there. Then I ran the script:

./myscript.py output=outmap raster1=map1 raster2=map2

After processing, I tried:

d.rast outmap

But then:

ERROR: Raster map <outmap> not found

What could be wrong?

Best regards,

Marcello.

P.S.: By the way, is this the right way to quote answers?

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

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Trying-python-scripts-tp5109105p5109444.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Marcello Gorini wrote:
[...]

>>> ... I am trying to run the first examples found in
>>> http://grass.osgeo.org/wiki/GRASS_and_Python.

--%<---
a script was here - look first post !
--->%--

>>> It seems to run OK and “100%” is displayed in the GRASS prompt, but
>>> when I tried to display the map, I found out that it wasn't created at
>>> all.

Nikos Alexandris:

>> If there are no errors and the script ran fine, maybe you just need to
>> check the region settings?

>>> Just for you to know, to run the script I simply create it (say
>>> "myscript.py"), give permissions and run it in a GRASS session with the
>>> necessary options:
>>> > ./myscript.py output=outmap raster1=map1 raster2=map2

>>> I am using Ubuntu 9.04 64bits and GRASS 6.4.ORC5.

>> For example:
>> # check region extent, etc.
>> g.region -p
>>
>> # set region extent and resolution to the map of your interest
>> g.region rast=outmap -pa
>>
>> # give color
>> r.colors rast=outmap color=rainbow
>>
>> # try to display
>> d.mon x0 && d.rast outmap
>>
>> # is it there?

Marcello Gorini:

>> No, it is not there. To make sure I set the region to the input map:
>>
>> g.region rast=map1 -pa
>> d.mon x0 && d.rast map1

>> OK, it is there. Then I ran the script:
>> ./myscript.py output=outmap raster1=map1 raster2=map2

>> After processing, I tried:
>> d.rast outmap

>> But then:
>> ERROR: Raster map <outmap> not found

>> What could be wrong?

Marcello, not sure why it does not work for you. I (copied over the script
from the 1st post in this thread as test_mapcalc.py, added +x to make it
executable) and it works fine for me:

# test run
python test_mapcalc.py raster1=mod_b2 raster2=mod_b2_new
output=testing_mapcalc_python_script

# r.info on inputs and the output

# mod_b2
r.info -r mod_b2

min=0
max=5504

# mod_b2_new
r.info -r mod_b2_new

min=0
max=0.5504

# output
r.info -r testing_mapcalc_python_script

min=0
max=5504.5504

>> P.S.: By the way, is this the right way to quote answers?

Like (almost) always, it depends whom you are asking ;-). There are so many
ways and opinions and habits and... (you can scan the archive or other mailing
lists and you'll find that people even argue seriously about the "correct" way
to reply).

Personally I got stuck with _clean_ in-line bottom posting because I find it
so useful when I read older threads and understand what has been discussed and
get to the point right away.

I prefer to invest time and clean-out un-necessary stuff (taking though
sometimes the risk to reject parts that are important?) and give a logical
flow to the discussion (first the question, then the answer) in order to make
it easier for any (next) reader.

Regards, Nikos

Marcello Gorini wrote:
[...]

>>> ... I am trying to run the first examples found in
>>> http://grass.osgeo.org/wiki/GRASS_and_Python.

--%<---
a script was here - look first post !
--->%--

>>> It seems to run OK and “100%” is displayed in the GRASS prompt, but
>>> when I tried to display the map, I found out that it wasn't created at
>>> all.

Nikos Alexandris:

>> If there are no errors and the script ran fine, maybe you just need to
>> check the region settings?

>>> Just for you to know, to run the script I simply create it (say
>>> "myscript.py"), give permissions and run it in a GRASS session with
the
>>> necessary options:
>>> > ./myscript.py output=outmap raster1=map1 raster2=map2

>>> I am using Ubuntu 9.04 64bits and GRASS 6.4.ORC5.

>> For example:
>> # check region extent, etc.
>> g.region -p
>>
>> # set region extent and resolution to the map of your interest
>> g.region rast=outmap -pa
>>
>> # give color
>> r.colors rast=outmap color=rainbow
>>
>> # try to display
>> d.mon x0 && d.rast outmap
>>
>> # is it there?

Marcello Gorini:

>> No, it is not there. To make sure I set the region to the input map:
>>
>> g.region rast=map1 -pa
>> d.mon x0 && d.rast map1

>> OK, it is there. Then I ran the script:
>> ./myscript.py output=outmap raster1=map1 raster2=map2

>> After processing, I tried:
>> d.rast outmap

>> But then:
>> ERROR: Raster map <outmap> not found

>> What could be wrong?

Nikos Alexandris:

Marcello, not sure why it does not work for you. I (copied over the script
from the 1st post in this thread as test_mapcalc.py, added +x to make it
executable) and it works fine for me:

# test run
python test_mapcalc.py raster1=mod_b2 raster2=mod_b2_new
output=testing_mapcalc_python_script

# r.info on inputs and the output

# mod_b2
r.info -r mod_b2

min=0
max=5504

# mod_b2_new
r.info -r mod_b2_new

min=0
max=0.5504

# output
r.info -r testing_mapcalc_python_script

min=0
max=5504.5504

>> P.S.: By the way, is this the right way to quote answers?

Like (almost) always, it depends whom you are asking ;-). There are so

many

ways and opinions and habits and... (you can scan the archive or other

mailing

lists and you'll find that people even argue seriously about the "correct"

way

to reply).

Personally I got stuck with _clean_ in-line bottom posting because I find

it.

so useful when I read older threads and understand what has been discussed

and

get to the point right away.

I prefer to invest time and clean-out un-necessary stuff (taking though
sometimes the risk to reject parts that are important?) and give a logical
flow to the discussion (first the question, then the answer) in order to

make

it easier for any (next) reader.

Regards, Nikos

Hello Nikos,

Thanks for the try. I tested it again with other rasters, but I got the same
error.
Could it possibly be something related to the 64bits architecture? Or could
GRASS be sending the outcome somewhere else?
Well, I guess that eventually I will go around this problem or even bump
into the solution, but the most important thing to say is that I appreciated
the effort and that it was a very good reception to this list.
Thank you very much Nikos. And thank you also for the explanations with
respect to the right way to answer posts. I will do my best to make my posts
as clean and explanatory as possible.

Best regards,
Marcello.
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Trying-python-scripts-tp5109105p5110760.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Marcello Gorini wrote:

Thanks for the try. I tested it again with other rasters, but I got the
same error. Could it possibly be something related to the 64bits
architecture?

I don't think so. I work with Kubuntu Lucid 64 bit :slight_smile:

Or could GRASS be sending the outcome somewhere else?

Negative. You are running the script from within a grass-session (as you wrote
in the first post). Let's try to take this step by step. What is the result of
each of the following commands:

ls -l myscript.py
g.gisenv
g.region -p
r.info map1
r.info map2

Well, I guess that eventually I will go around this problem or even bump
into the solution, but the most important thing to say is that I
appreciated the effort and that it was a very good reception to this list.

Bah... it was only me trying to not forget the basics with python scripts.
You will get better and more precise replies from other advanced users and
developers.

Thank you very much Nikos. And thank you also for the explanations with
respect to the right way to answer posts. I will do my best to make my
posts as clean and explanatory as possible.

You are warned, you might get yourself into trouble ;-p
Cheers, Nikos

Nikos Alexandris wrote:

Marcello Gorini wrote:

Thanks for the try. I tested it again with other rasters, but I got the
same error. Could it possibly be something related to the 64bits
architecture?

I don't think so. I work with Kubuntu Lucid 64 bit :slight_smile:

Or could GRASS be sending the outcome somewhere else?

Negative. You are running the script from within a grass-session (as you
wrote
in the first post). Let's try to take this step by step. What is the
result of
each of the following commands:

ls -l myscript.py
g.gisenv
g.region -p
r.info map1
r.info map2

Well, I guess that eventually I will go around this problem or even bump
into the solution, but the most important thing to say is that I
appreciated the effort and that it was a very good reception to this
list.

Bah... it was only me trying to not forget the basics with python scripts.
You will get better and more precise replies from other advanced users and
developers.

Thank you very much Nikos. And thank you also for the explanations with
respect to the right way to answer posts. I will do my best to make my
posts as clean and explanatory as possible.

You are warned, you might get yourself into trouble ;-p
Cheers, Nikos
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

OK, thank you, so let's go step by step:
ls -l myscript.py
result:
-rwxr-xr-x 1 marcello marcello 1227 2010-05-28 08:13 myscript.py

g.gisenv
result:
GISDBASE=/home/marcello/grassdata
LOCATION_NAME=Ocean_floor
MAPSET=level4
MONITOR=x0
GRASS_GUI=tcltk

g.region -p
result:
projection: 99 (Mercator)
zone: 0
datum: wgs84
ellipsoid: wgs84
north: 3947900
south: 3633400
west: -7366700
east: -7022600
nsres: 3700
ewres: 3700
rows: 85
cols: 93
cells: 7905

r.info map1
result:
+----------------------------------------------------------------------------+
| Layer: map1 Date: Thu May 27 18:48:30 2010
|
| Mapset: level4 Login of Creator: marcello
|
| Location: Ocean_floor
|
| DataBase: /home/marcello/grassdata
|
| Title: ( topmod )
|
| Timestamp: none
|

|----------------------------------------------------------------------------|
|
|
| Type of Map: raster Number of Categories: 255
|
| Data Type: FCELL
|
| Rows: 1177
|
| Columns: 1388
|
| Total Cells: 1633676
|
| Projection: Mercator
|
| N: 6358450 S: 2003550 Res: 3700
|
| E: -3920150 W: -9055750 Res: 3700
|
| Range of data: min = 0.000000 max = 10000.000000
|
|
|
| Data Description:
|
| generated by r.mapcalc
|
|
|
| Comments:
|
| if(topmod > 0, 0, -topmod)
|
|
|

+----------------------------------------------------------------------------+

r.info map2
result:
+----------------------------------------------------------------------------+
| Layer: map2 Date: Thu May 27 18:49:29 2010
|
| Mapset: level4 Login of Creator: marcello
|
| Location: Ocean_floor
|
| DataBase: /home/marcello/grassdata
|
| Title: ( topmod2 )
|
| Timestamp: none
|

|----------------------------------------------------------------------------|
|
|
| Type of Map: raster Number of Categories: 255
|
| Data Type: FCELL
|
| Rows: 1177
|
| Columns: 1388
|
| Total Cells: 1633676
|
| Projection: Mercator
|
| N: 6358450 S: 2003550 Res: 3700
|
| E: -3920150 W: -9055750 Res: 3700
|
| Range of data: min = 1000.000000 max = 1000.000000
|
|
|
| Data Description:
|
| generated by r.mapcalc
|
|
|
| Comments:
|
| if(topmod > -2, 1000, topmod)
|
|
|

+----------------------------------------------------------------------------+

Now just to emphasize :slight_smile:
Result of "r.info outmap":
ERROR: Raster map <outmap> not found

Can you make anything out of it?

Best regards,
Marcello.
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Trying-python-scripts-tp5109105p5112658.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Marcello Gorini:

...let's go step by step:
ls -l myscript.py

-rwxr-xr-x 1 marcello marcello 1227 2010-05-28 08:13 myscript.py

looks fine

g.gisenv

GISDBASE=/home/marcello/grassdata
LOCATION_NAME=Ocean_floor
MAPSET=level4
MONITOR=x0
GRASS_GUI=tcltk

just out of curiosity (and not that, I think, it makes any differene): are you
running (grass and) the script via the gui's command line or in text-mode?

g.region -p

projection: 99 (Mercator)
zone: 0
datum: wgs84
ellipsoid: wgs84
north: 3947900
south: 3633400
west: -7366700
east: -7022600
nsres: 3700
ewres: 3700
rows: 85
cols: 93
cells: 7905

looks ok (below also)

r.info map1

...

| Range of data: min = 0.000000 max = 10000.000000

...

r.info map2

...

| Range of data: min = 1000.000000 max = 1000.000000

...

Result of "r.info outmap":
ERROR: Raster map <outmap> not found

Hmmm... I don't know what could be wrong here. I guess running directly in-
grass what the python script is expected to do ( r.mapcalc "outmap = map1 +
map2" ) is ok, isn't it?

Could you also please copy-paste the exact (python) command you use within the
grass-session to get the desired "outmap"?

Nikos

Hello Marcello.

It's good to _finally_ see you here :slight_smile:

I tried your script and it worked for me, but I had to change the last line from

sys.exit(main())

to:

main()

could you try that?

best

Carlos

On Fri, May 28, 2010 at 08:23, Marcello Gorini <gorini@gmail.com> wrote:

Nikos Alexandris wrote:

Marcello Gorini wrote:

Thanks for the try. I tested it again with other rasters, but I got the
same error. Could it possibly be something related to the 64bits
architecture?

I don't think so. I work with Kubuntu Lucid 64 bit :slight_smile:

Or could GRASS be sending the outcome somewhere else?

Negative. You are running the script from within a grass-session (as you
wrote
in the first post). Let's try to take this step by step. What is the
result of
each of the following commands:

ls -l myscript.py
g.gisenv
g.region -p
r.info map1
r.info map2

Well, I guess that eventually I will go around this problem or even bump
into the solution, but the most important thing to say is that I
appreciated the effort and that it was a very good reception to this
list.

Bah... it was only me trying to not forget the basics with python scripts.
You will get better and more precise replies from other advanced users and
developers.

Thank you very much Nikos. And thank you also for the explanations with
respect to the right way to answer posts. I will do my best to make my
posts as clean and explanatory as possible.

You are warned, you might get yourself into trouble ;-p
Cheers, Nikos
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

OK, thank you, so let's go step by step:
ls -l myscript.py
result:
-rwxr-xr-x 1 marcello marcello 1227 2010-05-28 08:13 myscript.py

g.gisenv
result:
GISDBASE=/home/marcello/grassdata
LOCATION_NAME=Ocean_floor
MAPSET=level4
MONITOR=x0
GRASS_GUI=tcltk

g.region -p
result:
projection: 99 (Mercator)
zone: 0
datum: wgs84
ellipsoid: wgs84
north: 3947900
south: 3633400
west: -7366700
east: -7022600
nsres: 3700
ewres: 3700
rows: 85
cols: 93
cells: 7905

r.info map1
result:
+----------------------------------------------------------------------------+
| Layer: map1 Date: Thu May 27 18:48:30 2010
|
| Mapset: level4 Login of Creator: marcello
|
| Location: Ocean_floor
|
| DataBase: /home/marcello/grassdata
|
| Title: ( topmod )
|
| Timestamp: none
|

|----------------------------------------------------------------------------|
|
|
| Type of Map: raster Number of Categories: 255
|
| Data Type: FCELL
|
| Rows: 1177
|
| Columns: 1388
|
| Total Cells: 1633676
|
| Projection: Mercator
|
| N: 6358450 S: 2003550 Res: 3700
|
| E: -3920150 W: -9055750 Res: 3700
|
| Range of data: min = 0.000000 max = 10000.000000
|
|
|
| Data Description:
|
| generated by r.mapcalc
|
|
|
| Comments:
|
| if(topmod > 0, 0, -topmod)
|
|
|

+----------------------------------------------------------------------------+

r.info map2
result:
+----------------------------------------------------------------------------+
| Layer: map2 Date: Thu May 27 18:49:29 2010
|
| Mapset: level4 Login of Creator: marcello
|
| Location: Ocean_floor
|
| DataBase: /home/marcello/grassdata
|
| Title: ( topmod2 )
|
| Timestamp: none
|

|----------------------------------------------------------------------------|
|
|
| Type of Map: raster Number of Categories: 255
|
| Data Type: FCELL
|
| Rows: 1177
|
| Columns: 1388
|
| Total Cells: 1633676
|
| Projection: Mercator
|
| N: 6358450 S: 2003550 Res: 3700
|
| E: -3920150 W: -9055750 Res: 3700
|
| Range of data: min = 1000.000000 max = 1000.000000
|
|
|
| Data Description:
|
| generated by r.mapcalc
|
|
|
| Comments:
|
| if(topmod > -2, 1000, topmod)
|
|
|

+----------------------------------------------------------------------------+

Now just to emphasize :slight_smile:
Result of "r.info outmap":
ERROR: Raster map <outmap> not found

Can you make anything out of it?

Best regards,
Marcello.
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Trying-python-scripts-tp5109105p5112658.html
Sent from the Grass - Users mailing list archive at Nabble.com.
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

--
Prof. Carlos Henrique Grohmann - Geologist D.Sc.
Institute of Geosciences - Univ. of São Paulo, Brazil
http://www.igc.usp.br/pessoais/guano
http://lattes.cnpq.br/5846052449613692
Linux User #89721
________________
Can’t stop the signal.

Nikos Alexandris wrote:

Marcello Gorini:

...let's go step by step:
ls -l myscript.py

-rwxr-xr-x 1 marcello marcello 1227 2010-05-28 08:13 myscript.py

looks fine

g.gisenv

GISDBASE=/home/marcello/grassdata
LOCATION_NAME=Ocean_floor
MAPSET=level4
MONITOR=x0
GRASS_GUI=tcltk

just out of curiosity (and not that, I think, it makes any differene): are
you
running (grass and) the script via the gui's command line or in text-mode?

g.region -p

projection: 99 (Mercator)
zone: 0
datum: wgs84
ellipsoid: wgs84
north: 3947900
south: 3633400
west: -7366700
east: -7022600
nsres: 3700
ewres: 3700
rows: 85
cols: 93
cells: 7905

looks ok (below also)

r.info map1

...

| Range of data: min = 0.000000 max = 10000.000000

...

r.info map2

...

| Range of data: min = 1000.000000 max = 1000.000000

...

Result of "r.info outmap":
ERROR: Raster map <outmap> not found

Hmmm... I don't know what could be wrong here. I guess running directly
in-
grass what the python script is expected to do ( r.mapcalc "outmap = map1
+
map2" ) is ok, isn't it?

Could you also please copy-paste the exact (python) command you use within
the
grass-session to get the desired "outmap"?

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

Ok, so answering your questions:

just out of curiosity (and not that, I think, it makes any differene): are

you

running (grass and) the script via the gui's command line or in text-mode?

No gui, everything using the terminal.

Hmmm... I don't know what could be wrong here. I guess running directly

in-

grass what the python script is expected to do ( r.mapcalc "outmap = map1

+

map2" ) is ok, isn't it?

Yep!

Could you also please copy-paste the exact (python) command you use within

the

grass-session to get the desired "outmap"?

Sure:
python myscript.py raster1=map1 raster2=map2 output=outmap

Well, I think that eventually we will find out that the problem lies between
my chair and my computer, and you are going to be mad with me :slight_smile:

But for now, let's keep going.

Cheers,
Marcello.

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Trying-python-scripts-tp5109105p5113299.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Hi,

2010/5/28 Carlos Grohmann <carlos.grohmann@gmail.com>:

I tried your script and it worked for me, but I had to change the last line from

sys.exit(main())

to:

main()

why, `sys.exit(main())` should be OK.

Martin

--
Martin Landa <landa.martin gmail.com> * http://gama.fsv.cvut.cz/~landa

Carlos Grohmann-2 wrote:

Hello Marcello.

It's good to _finally_ see you here :slight_smile:

I tried your script and it worked for me, but I had to change the last
line from

sys.exit(main())

to:

main()

could you try that?

best

Carlos

On Fri, May 28, 2010 at 08:23, Marcello Gorini <gorini@gmail.com> wrote:

Nikos Alexandris wrote:

Marcello Gorini wrote:

Thanks for the try. I tested it again with other rasters, but I got the
same error. Could it possibly be something related to the 64bits
architecture?

I don't think so. I work with Kubuntu Lucid 64 bit :slight_smile:

Or could GRASS be sending the outcome somewhere else?

Negative. You are running the script from within a grass-session (as you
wrote
in the first post). Let's try to take this step by step. What is the
result of
each of the following commands:

ls -l myscript.py
g.gisenv
g.region -p
r.info map1
r.info map2

Well, I guess that eventually I will go around this problem or even
bump
into the solution, but the most important thing to say is that I
appreciated the effort and that it was a very good reception to this
list.

Bah... it was only me trying to not forget the basics with python
scripts.
You will get better and more precise replies from other advanced users
and
developers.

Thank you very much Nikos. And thank you also for the explanations with
respect to the right way to answer posts. I will do my best to make my
posts as clean and explanatory as possible.

You are warned, you might get yourself into trouble ;-p
Cheers, Nikos
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

OK, thank you, so let's go step by step:
ls -l myscript.py
result:
-rwxr-xr-x 1 marcello marcello 1227 2010-05-28 08:13 myscript.py

g.gisenv
result:
GISDBASE=/home/marcello/grassdata
LOCATION_NAME=Ocean_floor
MAPSET=level4
MONITOR=x0
GRASS_GUI=tcltk

g.region -p
result:
projection: 99 (Mercator)
zone: 0
datum: wgs84
ellipsoid: wgs84
north: 3947900
south: 3633400
west: -7366700
east: -7022600
nsres: 3700
ewres: 3700
rows: 85
cols: 93
cells: 7905

r.info map1
result:
+----------------------------------------------------------------------------+
| Layer: map1 Date: Thu May 27 18:48:30
2010
|
| Mapset: level4 Login of Creator: marcello
|
| Location: Ocean_floor
|
| DataBase: /home/marcello/grassdata
|
| Title: ( topmod )
|
| Timestamp: none
|

|----------------------------------------------------------------------------|
|
|
| Type of Map: raster Number of Categories: 255
|
| Data Type: FCELL
|
| Rows: 1177
|
| Columns: 1388
|
| Total Cells: 1633676
|
| Projection: Mercator
|
| N: 6358450 S: 2003550 Res: 3700
|
| E: -3920150 W: -9055750 Res: 3700
|
| Range of data: min = 0.000000 max = 10000.000000
|
|
|
| Data Description:
|
| generated by r.mapcalc
|
|
|
| Comments:
|
| if(topmod > 0, 0, -topmod)
|
|
|

+----------------------------------------------------------------------------+

r.info map2
result:
+----------------------------------------------------------------------------+
| Layer: map2 Date: Thu May 27 18:49:29
2010
|
| Mapset: level4 Login of Creator: marcello
|
| Location: Ocean_floor
|
| DataBase: /home/marcello/grassdata
|
| Title: ( topmod2 )
|
| Timestamp: none
|

|----------------------------------------------------------------------------|
|
|
| Type of Map: raster Number of Categories: 255
|
| Data Type: FCELL
|
| Rows: 1177
|
| Columns: 1388
|
| Total Cells: 1633676
|
| Projection: Mercator
|
| N: 6358450 S: 2003550 Res: 3700
|
| E: -3920150 W: -9055750 Res: 3700
|
| Range of data: min = 1000.000000 max = 1000.000000
|
|
|
| Data Description:
|
| generated by r.mapcalc
|
|
|
| Comments:
|
| if(topmod > -2, 1000, topmod)
|
|
|

+----------------------------------------------------------------------------+

Now just to emphasize :slight_smile:
Result of "r.info outmap":
ERROR: Raster map <outmap> not found

Can you make anything out of it?

Best regards,
Marcello.
--
View this message in context:
http://osgeo-org.1803224.n2.nabble.com/Trying-python-scripts-tp5109105p5112658.html
Sent from the Grass - Users mailing list archive at Nabble.com.
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

--
Prof. Carlos Henrique Grohmann - Geologist D.Sc.
Institute of Geosciences - Univ. of São Paulo, Brazil
http://www.igc.usp.br/pessoais/guano
http://lattes.cnpq.br/5846052449613692
Linux User #89721
________________
Can’t stop the signal.
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

Hey Carlos, how are you my friend?

I used to understand only 10% of the posts, but I raised it to 20%, so I
took the courage to come in :slight_smile:

Thanks for the hint, but I tried it and it still doesn't work. That is a
very weird problem!

Cheers,

Marcello.
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Trying-python-scripts-tp5109105p5113324.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Marcello:
...

I used to understand only 10% of the posts, but I raised it to 20%, so I
took the courage to come in :slight_smile:

Just stick on Marcello. Get yourself the GRASS-book, look at the GRASS-wiki,
invest some time generally and specifically, and you will be addicted :wink: It
is worthwhile.

It seems strange though. We might need the guru's to solve this... (?).

Nikos:

Just stick on Marcello. Get yourself the GRASS-book, look at the

GRASS-wiki,

invest some time generally and specifically, and you will be addicted :wink:

It

is worthwhile.

I will, don't worry. And I already bought the GRASS book. That is what
really got me going (after Carlos got me interested in the first place). I
must say that it is the best technical book I have ever bought. Almost
everything I needed to achieve my firsts goals with GRASS, I found in the
book only by turning the page.

It seems strange though. We might need the guru's to solve this... (?).

This one is probably a subliminal message somewhere in the book. I need to
look closer :slight_smile:

Cheers,
Marcello.
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Trying-python-scripts-tp5109105p5113440.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Marcello Gorini:
...

I am a total beginner in python and GRASS (and any other programming
language), so I am trying to run the first examples found in
http://grass.osgeo.org/wiki/GRASS_and_Python.

Marcello,

just fyi: I've added another simple example script in
<http://grass.osgeo.org/wiki/GRASS_and_Python&gt;\.

Nikos

Nikos Alexandris wrote:

Marcello Gorini:
...

I am a total beginner in python and GRASS (and any other programming
language), so I am trying to run the first examples found in
http://grass.osgeo.org/wiki/GRASS_and_Python.

Marcello,

just fyi: I've added another simple example script in
<http://grass.osgeo.org/wiki/GRASS_and_Python&gt;\.

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

Thanks Nikos, it worked fine for me. The grass.mapcalc code is the only one
that is giving me trouble though. I guess I will have to stick with shell
scripts for now. But won´t give up python, of course.

Best regards,

Marcello.
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Trying-python-scripts-tp5109105p5118744.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Hi,

2010/5/30 Marcello Gorini <gorini@gmail.com>:

I am a total beginner in python and GRASS (and any other programming
language), so I am trying to run the first examples found in
http://grass.osgeo.org/wiki/GRASS_and_Python.

GRASS 6.4.0RC5 is quite old. Try RC6 or better download code from SVN

http://trac.osgeo.org/grass/wiki/DownloadSource#GRASS6.4

Martin

--
Martin Landa <landa.martin gmail.com> * http://gama.fsv.cvut.cz/~landa

Martin Landa wrote:

Hi,

2010/5/30 Marcello Gorini <gorini@gmail.com>:

I am a total beginner in python and GRASS (and any other programming
language), so I am trying to run the first examples found in
http://grass.osgeo.org/wiki/GRASS_and_Python.

GRASS 6.4.0RC5 is quite old. Try RC6 or better download code from SVN

http://trac.osgeo.org/grass/wiki/DownloadSource#GRASS6.4

Martin

--
Martin Landa <landa.martin gmail.com> * http://gama.fsv.cvut.cz/~landa
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

Hello Martin,

Yes, I think I need to move on and download a newer version.
I will probably try to install GRASS 7.

Thanks for the suggestion.

Best regards,

Marcello.

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Trying-python-scripts-tp5109105p5119718.html
Sent from the Grass - Users mailing list archive at Nabble.com.

[...]

Martin Landa wrote:

> GRASS 6.4.0RC5 is quite old. Try RC6 or better download code from SVN
> http://trac.osgeo.org/grass/wiki/DownloadSource#GRASS6.4

Marcello Gorini:

Yes, I think I need to move on and download a newer version.
I will probably try to install GRASS 7.

Gr7 is under continuous development. For real work better to use latest Gr64.

Nikos