[GRASS-user] some issues with python scripts

Hello all.

I've been trying to convert some of my bash scripts to python, but
some issues appeared:

1 - it seems that http://grass.osgeo.org/programming6/pythonlib.html is offline

2 - dealing with maps names. if I use find_file, I end up with the
full name of the map (that is, map name plus mapset) in the
dictionary. The wx interface created by g.parser includes the mapset
in the map name, and all that goes as a string to find_file:

import grass.script as grass

# this is what should happen

elevmap = 'cass'
find_elev = grass.find_file(elevmap, element = 'cell')
find_elev

{'fullname': 'cass@brasil', 'mapset': 'brasil', 'name': 'cass',
'file': '/home/guano/grassdata/amsul_wgs84/brasil/cell/cass'}

# this is what actually happens when using the wx interface

elevmap = 'cass@brasil'
find_elev = grass.find_file(elevmap, element = 'cell')
find_elev

{'fullname': 'cass@brasil', 'mapset': 'brasil', 'name': 'cass@brasil',
'file': '/home/guano/grassdata/amsul_wgs84/brasil/cell/cass'}

3 - temp files.
In my bash scripts I used temp files. I found out that using
g.tempfile() gives me a filename with a full path, which cannot be
used in r.mapcalc, for instance.
So I just used normal files that I delete in the end of the script. Is
there a better way?

4 - error handling + user break
In my bash scripts I have a few lines that deal with user break:

#######################################################################
# what to do in case of user break:
exitprocedure()
{
g.message -e 'User break!'
#delete any TMP files:
g.remove rast=$TMP1,$TMP2,$TMP3,$TMP4 > /dev/null

}
# shell check for user break (signal list: trap -l)
trap "exitprocedure" 2 3 15

what would be an equivalent for python?

many thanks!

Carlos

--
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.

Hi,

2011/1/16 Carlos Grohmann <carlos.grohmann@gmail.com>:

1 - it seems that http://grass.osgeo.org/programming6/pythonlib.html is offline

I can see, it's back.

[...]

4 - error handling + user break
In my bash scripts I have a few lines that deal with user break:

#######################################################################
# what to do in case of user break:
exitprocedure()
{
g.message -e 'User break!'
#delete any TMP files:
g.remove rast=$TMP1,$TMP2,$TMP3,$TMP4 > /dev/null

}
# shell check for user break (signal list: trap -l)
trap "exitprocedure" 2 3 15

what would be an equivalent for python?

I created a wiki page for these issue, feel free to improve...

http://grass.osgeo.org/wiki/Converting_Bash_scripts_to_Python

Martin

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

2011/1/16 Carlos Grohmann <carlos.grohmann@gmail.com>:

2 - dealing with maps names. if I use find_file, I end up with the
full name of the map (that is, map name plus mapset) in the
dictionary. The wx interface created by g.parser includes the mapset
in the map name, and all that goes as a string to find_file:

import grass.script as grass

# this is what should happen

elevmap = 'cass'
find_elev = grass.find_file(elevmap, element = 'cell')
find_elev

{'fullname': 'cass@brasil', 'mapset': 'brasil', 'name': 'cass',
'file': '/home/guano/grassdata/amsul_wgs84/brasil/cell/cass'}

# this is what actually happens when using the wx interface

elevmap = 'cass@brasil'
find_elev = grass.find_file(elevmap, element = 'cell')
find_elev

{'fullname': 'cass@brasil', 'mapset': 'brasil', 'name': 'cass@brasil',
'file': '/home/guano/grassdata/amsul_wgs84/brasil/cell/cass'}

this corresponds with behaviour of g.findfile

GRASS 7.0.svn (nc_spm_08):~/src/grass_trunk > g.findfile element=cell file=dummy
name='dummy'
mapset='landa'
fullname='dummy@landa'
file='/home/martin/grassdata/nc_spm_08/landa/cell/dummy'
GRASS 7.0.svn (nc_spm_08):~/src/grass_trunk > g.findfile element=cell
file=dummy@landa
name='dummy@landa'
mapset='landa'
fullname='dummy@landa'
file='/home/martin/grassdata/nc_spm_08/landa/cell/dummy'

3 - temp files.
In my bash scripts I used temp files. I found out that using
g.tempfile() gives me a filename with a full path, which cannot be
used in r.mapcalc, for instance.
So I just used normal files that I delete in the end of the script. Is
there a better way?

Sample code?

Martin

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

Hello Martin. Thanks for your answers

this corresponds with behaviour of g.findfile

Well, yes. I just thought that once the wx interface will always
provide the full name when a map is selected,
then the name should be parsed so that 'name' and 'full_name' don't be the same.

3 - temp files.

Sample code?

here. r.mapcalc can't deal with the slashes in the path.

import grass.script as grass

a = grass.tempfile()
a

'/home/guano/grassdata/amsul_wgs84/brasil/.tmp/guanodesk/16665.0'

grass.mapcalc("${out} = cos(${rast1})", out = 'cosine', rast1 = a)

syntax error, unexpected '/'
Parse error
ERROR: parse error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/grass-7.0.svn/etc/python/grass/script/raster.py",
line 105, in mapcalc
    fatal(_("An error occurred while running r.mapcalc"))
TypeError: 'str' object is not callable

best,
Carlos

--
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.

Hi,

2011/1/17 Carlos Grohmann <carlos.grohmann@gmail.com>:

this corresponds with behaviour of g.findfile

Well, yes. I just thought that once the wx interface will always
provide the full name when a map is selected,
then the name should be parsed so that 'name' and 'full_name' don't be the same.

Probably g.findfile should be modified to return name as not qualified name(?)

import grass.script as grass

a = grass.tempfile()
a

'/home/guano/grassdata/amsul_wgs84/brasil/.tmp/guanodesk/16665.0'

grass.mapcalc("${out} = cos(${rast1})", out = 'cosine', rast1 = a)

os.path.basename(grass.tempfile())

Anyway grass.tempfile() is not dedicated for this usage, this name is
not a valid raster map name....

Martin

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

Probably g.findfile should be modified to return name as not qualified name(?)

That would be really helpfull.
in the meantime, I came up with this:

    if '@' in find_elev['name']: # when g.findfile returns fullname as name
        i= find_elev['name'].index('@')
        name = find_elev['name'][0:i]
    else:
        name = find_elev['name']

Anyway grass.tempfile() is not dedicated for this usage, this name is
not a valid raster map name....

OK, i will keep using 'real' names and then deleting the files in the end.

thanks

--
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.

2011/1/17 Carlos Grohmann <carlos.grohmann@gmail.com>:

Probably g.findfile should be modified to return name as not qualified name(?)

That would be really helpfull.

done in r45068 (backported to devbr6). After some testing it could go
into 6.4.2.

Martin

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

Works perfectly. many thanks

C

On Mon, Jan 17, 2011 at 15:43, Martin Landa <landa.martin@gmail.com> wrote:

2011/1/17 Carlos Grohmann <carlos.grohmann@gmail.com>:

Probably g.findfile should be modified to return name as not qualified name(?)

That would be really helpfull.

done in r45068 (backported to devbr6). After some testing it could go
into 6.4.2.

Martin

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

--
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.

[sorry, I read this thread backwards so missed vital history..]

Carlos:

# this is what actually happens when using the wx interface
>>>> elevmap = 'cass@brasil'
>>>> find_elev = grass.find_file(elevmap, element = 'cell')

No, you are using .find_file wrong. it finds *files* not maps.

if something is qualifying the name, and you don't want it
qualified, try using .split('@')[0] before treating the map
name as a map component file which happens to share the map's
name.

3 - temp files.
In my bash scripts I used temp files. I found out that using
g.tempfile() gives me a filename with a full path,
which cannot be used in r.mapcalc, for instance.

again, that is giving you a *file* name, not a map name to work
with. if you want a temp *map* for r.mapcalc, a convention is to
call it something like tmp_modulename.$$, then remove later with
g.remove.

Hamish