[GRASS-dev] Python script problem

Hello everybody,
I'm new to python and I'm trying to build up a script...

When I run the script the only commands that are executed are

    cmdargs1= ["elevation=%s" %dtm]
    cmdargs2=["slope=slope1", "format=percent", "--overwrite"]
    os.execvp("r.slope.aspect", ["r.slope.aspect"] + cmdargs1 + cmdargs2)

The other commands are ignored...What can be the issue?

thanks,
Monia
------------------------
Here is my script:
-----------------------

#!/usr/bin/python
# -*- coding:utf-8 -*-

#%Module
#% description:
#% keywords: keyword1, keyword2
#%End
#%option
#% key: dtm
#% type: string
#% gisprompt: old,cell,raster
#% description: Modello Digitale del Terreno
#% required : yes
#%end
#%option
#% key: h
#% type: double
#% description: Profondità del mare in prossimità della costa h
#% required : yes
#%end
#%option
#% key: ho
#% type: double
#% description: Profondità del mare in corrispondenza dell'epicentro
#% required : yes
#%end
#%option
#% key: Ho
#% type: double
#% description: Altezza d'onda nel punto di generazione dello tsunami
#% required : yes
#%end

import os,sys,subprocess,string,random,math

def main():

    #### add your code here ####
    print ""

    print "Value of GIS_OPT_h: %s" % os.getenv("GIS_OPT_h")

    print "Value of GIS_OPT_ho: %s" % os.getenv("GIS_OPT_ho")

    print "Value of GIS_OPT_Ho: %s" % os.getenv("GIS_OPT_Ho")
    print "Name of GIS_OPT_dtm: %s" % os.getenv("GIS_OPT_dtm")

    h = float(os.getenv('GIS_OPT_h'))

    ho = float(os.getenv('GIS_OPT_ho'))

    Ho = float(os.getenv('GIS_OPT_Ho'))
    dtm = os.getenv('GIS_OPT_dtm')

    Co=math.sqrt(9.8 * h)

    print "Celerità dell'onda=",Co

    H= Ho * ((ho/h)**(0.25))

    print "Altezza d'onda a riva=",H

    U=Co * math.sqrt((1+H/h))

    print "Velocità d'onda a riva=",U

    L= 4 * 2.415 *h /(math.sqrt(3*H/h))

    print "Lunghezza di un'onda solitaria=",L

    k1=U**2 * L/(19.6)

    print "k1=",k1

    k2= U**2 * H/(39.2)

    print "k2=",k2
    
    cmdargs1= ["elevation=%s" %dtm]
    cmdargs2=["slope=slope1", "format=percent", "--overwrite"]
    os.execvp("r.slope.aspect", ["r.slope.aspect"] + cmdargs1 + cmdargs2)

    cmdargs3=["input=slope1", "output=slope_prova1", "method=average", "size=9"]
    os.execvp("r.neighbors",["r.neighbors"] + cmdargs3)

    #### end of your code ####
    return

if __name__ == "__main__":
    if ( len(sys.argv) <= 1 or sys.argv[1] != "@ARGS_PARSED@" ):
        os.execvp("g.parser", [sys.argv[0]] + sys.argv)
    else:
  main();

Try the following...

On 10/25/07 1:26 AM, "monik1982@libero.it" <monik1982@libero.it> wrote:

    cmdargs1= ["elevation=%s" %dtm]
    cmdargs2=["slope=slope1", "format=percent", "--overwrite"]
    os.execvp("r.slope.aspect", ["r.slope.aspect"] + cmdargs1 + cmdargs2)

    cmdargs3=["input=slope1", "output=slope_prova1", "method=average",
"size=9"]
    os.execvp("r.neighbors",["r.neighbors"] + cmdargs3)

cmdargs1= ["r.slope.aspect","elevation=%s" %dtm,"slope=slope1",
"format=percent", "--overwrite"]

cmdargs2=["r.neighbors", "input=slope1", "output=slope_prova1",
"method=average","size=9"]

p1 = subprocess.Popen(cmdargs1)

# NOTE: you may have to insert a wait or something here to make sure that
# slope1 is created before running the next process

p2 = subprocess.Popen(cmdargs2)

# you can do more sophisticated things with Popen if you want
# and can check return values by parsing p1 and p2

Michael

__________________________________________
Michael Barton, Professor of Anthropology
Director of Graduate Studies
School of Human Evolution & Social Change
Center for Social Dynamics and Complexity
Arizona State University

phone: 480-965-6213
fax: 480-965-7671
www: http://www.public.asu.edu/~cmbarton

monik1982\@libero\.it wrote:

Hello everybody,
I'm new to python and I'm trying to build up a script...

When I run the script the only commands that are executed are

    cmdargs1= ["elevation=%s" %dtm]
    cmdargs2=["slope=slope1", "format=percent", "--overwrite"]
    os.execvp("r.slope.aspect", ["r.slope.aspect"] + cmdargs1 + cmdargs2)

The other commands are ignored...What can be the issue?

os.execvp() doesn't return (unless it fails); it executes the
specified command *in place of* the script which calls it.

If you want to run a command without terminating the script, use
subprocess.Popen as suggested by Michael. For more details, see §17.1
of the Python library reference:

  http://docs.python.org/lib/module-subprocess.html

In most scripts, the only place where os.execvp() should be used is
for invoking g.parser.

--
Glynn Clements <glynn@gclements.plus.com>