[GRASS-dev] python script problem...

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")

#### 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();

--
-----------------------------------------------------
Dr. Massimiliano Cannata
Environmental & Geomatic Engineer

Institute of Earth Scences - SUPSI
Trevano, C.P. 72, CH-6952 Canobbio, SWITZERLAND

Tel: +41 (0)58 / 666 62 14
Fax: +41 (0)58 / 666 62 09
E-mail: massimiliano.cannata@supsi.ch

Web:
http://www.ist.supsi.ch
http://istgis.ist.supsi.ch:8001/geomatica/
-------------------------------------------------------

Thanks for the help.
The problem was the correct indentation into the main()....
Now it works perfectly.

Maxi

Massimiliano Cannata 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")

#### 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();

--
-----------------------------------------------------
Dr. Massimiliano Cannata
Environmental & Geomatic Engineer

Institute of Earth Scences - SUPSI
Trevano, C.P. 72, CH-6952 Canobbio, SWITZERLAND

Tel: +41 (0)58 / 666 62 14 Fax: +41 (0)58 / 666 62 09
E-mail: massimiliano.cannata@supsi.ch

Web: http://www.ist.supsi.ch
http://istgis.ist.supsi.ch:8001/geomatica/
-------------------------------------------------------