[GRASS-dev] python script problem...

Michael Barton michael.barton at asu.edu
Tue Oct 16 13:21:32 EDT 2007




On 10/16/07 8:18 AM, "Massimiliano Cannata" <massimiliano.cannata at supsi.ch>
wrote:

> Hi all,
> I'm new to python and I'm trying to build up a script...
> My question is:
> After I setted up an option of type double, How can I get it in a python
> variable?
> 
> h = float(os.getenv('GIS_OPT_h')) it doesn't work, it require a string
> or numeric attribute !?!?
> 
> how to do?
> 
> thanks,
> Maxi
> ------------------------
> Here is my script:
> -----------------------
> #!/usr/bin/python
> # -*- coding:utf-8 -*-
> import math
> 
> #%Module
> #%  description: g.parser test script
> #%  keywords: keyword1, keyword2
> #%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 sys
> import os
> 
> 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")

You need to indent the stuff below here too (down to your if __name...
clause), to keep it in main()
> 
> #### it works and print the user input ####
> 
> h = float(os.getenv('GIS_OPT_h'))
> ho = os.getenv('GIS_OPT_ho')
> Ho = os.getenv('GIS_OPT_Ho')
> 
> #### NOT WORKING ! i guess i need something similar to atof in C ANSI ####
> 
> 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
> 
> #### end of your code ####
> 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();
> 
> 
> 
> 

__________________________________________
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 





More information about the grass-dev mailing list