We really need a cookbook way to set up GRASS for Windows so that Python scripts run.
A couple students and I have struggled with this for the past 9 months. Although we’ve managed to get Python recognized by WinGRASS by running in a Windows terminal rather than Msys, we still can’t run scripts that call GRASS libraries or the parser.
Michael
Begin forwarded message:
Date: Tue, 4 May 2010 22:17:16 -0700 (PDT)
From: LeeDaniel <Lee.Daniel.1986@gmail.com>
Subject: [GRASS-user] Problem with running Python script in GRASS
To: grass-user@lists.osgeo.org
Message-ID: <1273036636062-5007296.post@n2.nabble.com>
Content-Type: text/plain; charset=us-asciiHello fellow GRASS users!
I’m sure this is a very simple problem but I’m having a really difficult
time with it… After searching for the solution for several days I’m on the
end of my whits and am really needing this script to get working. This is
the problem:I’ve written a Python script, doing my best to use the Python I know and
reverse engineer the python scripts I found in the Internet. As far as I can
tell, the script should be fine, although I naturally can’t execute it
independently. My goal is to run it as a command from inside GRASS so that
the user can input the parameters through the GUI. I think GRASS recognizes
that the script is there but isn’t able to generate the GUI.Here’s the script:
############################################################################
MODULE: r.solar
AUTHOR(S): Daniel Lee
PURPOSE: Runs r.sun for a year using different inputs for each month
COPYRIGHT: (C) 2010 by Daniel Lee
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
#############################################################################
#%Module
#% label: Solar modeling tool.
#% description: Conducts a solar analysis for a year using empirical inputs
for each month.
#% keywords: raster
#%End
#%Option
#% key: elevin
#% type: string
#% gisprompt: old,cell,raster
#% description: Name of input elevation map (unit = meters)
#% required : yes
#% guisection: Required inputs
#%End
#%Option
#% key: aspect
#% type: string
#% gisprompt: old,cell,raster
#% description: Name of input aspect map (decimal degrees)
#% required : yes
#% guisection: Required inputs
#%End
#%Option
#% key: slopein
#% type: string
#% gisprompt: old,cell,raster
#% description: Name of input slope map (decimal degrees)
#% required : yes
#% guisection: Required inputs
#%End
#%Option
#% key: linkein
#% type: string
#% gisprompt: old,cell,raster
#% description: Name of input Linke atmospheric turbidity coefficient map
#% required : no
#% guisection: Optional inputs
#%End
#%Option
#% key: albedo
#% type: string
#% gisprompt: old,cell,raster
#% description: Name of input albedo coefficient map
#% required : no
#% guisection: Optional inputs
#%End
#%Option
#% key: mapset
#% type: string
#% description: Name of the mapset containing solar data
#% required : yes
#% guisection: Required inputs
#%End
#%Flag
#% key: z
#% description: Generate map of sunlight insolation time (h)
#% guisection: Output options
#%End
#%Flag
#% key: y
#% description: Generate map of reflected radiation (Wh/m2)
#% guisection: Output options
#%Endimport sys
import os
import string
import grass.script as grassdef main():
elevin = options[‘elevin’]
aspect = options[‘aspect’]
slopein = options[‘slopein’]
linkein = options[‘linkein’]
albedo = options[‘albedo’]
mapset = “@” + options[‘mapset’]
reflected = flags[‘y’]
duration = flags[‘z’]
step = 0.16for day in range(365):
day += 1Define month
if day == 1:
month = “01” + mapset
elif day == 32:
month = “02” + mapset
elif day == 60:
month = “03” + mapset
elif day == 91:
month = “04” + mapset
elif day == 121:
month = “05” + mapset
elif day == 152:
month = “06” + mapset
elif day == 182:
month = “07” + mapset
elif day == 213:
month = “08” + mapset
elif day == 244:
month = “09” + mapset
elif day == 274:
month = “10” + mapset
elif day == 305:
month = “11” + mapset
elif day == 335:
month = “12” + mapsetDefine coefficients!
coefbh = ‘coefbh’ + month
coefdh = ‘coefdh’ + month
if not grass.find_file(elevin)[‘file’] or not
grass.find_file(aspect)[‘file’] or not grass.find_file(slopein)[‘file’] or
not grass.find_file(coefbh)[‘file’] or not grass.find_file(coefdh)[‘file’]:
grass.fatal(_(“Raster map <%a> not found.”) % inputDefine outputs!
beam_rad = ‘beam’ + str(day)
diff_rad = ‘diffuse’ + str(day)
glob_rad = ‘global’ + str(day)
if duration and reflected:
insol_time = ‘insol_time’ + str(day)
refl_rad = ‘reflected’ + str(day)
grass.run_command(‘r.sun’, flags = ‘s’, elevin = elevin, aspin =
aspin, slopein = slopein, linkein = linkein, albedo = albedo, coefbh =
coefbh, coefdh = coefdh, beam_rad = beam_rad, insol_time = insol_time,
diff_rad = diff_rad, refl_rad = refl_rad, glob_rad = glob_rad, day = day,
step = step)
elif duration and not reflected:
insol_time = ‘insol_time’ + str(day)
grass.run_command(‘r.sun’, flags = ‘s’, elevin = elevin, aspin =
aspin, slopein = slopein, linkein = linkein, albedo = albedo, coefbh =
coefbh, coefdh = coefdh, beam_rad = beam_rad, insol_time = insol_time,
diff_rad = diff_rad, glob_rad = glob_rad, day = day, step = step)
elif reflected and not duration:
refl_rad = ‘reflected’ + str(day)
grass.run_command(‘r.sun’, flags = ‘s’, elevin = elevin, aspin =
aspin, slopein = slopein, linkein = linkein, albedo = albedo, coefbh =
coefbh, coefdh = coefdh, beam_rad = beam_rad, diff_rad = diff_rad, refl_rad
= refl_rad, glob_rad = glob_rad, day = day, step = step)
else:
grass.run_command(‘r.sun’, flags = ‘s’, elevin = elevin, aspin =
aspin, slopein = slopein, linkein = linkein, albedo = albedo, coefbh =
coefbh, coefdh = coefdh, beam_rad = beam_rad, diff_rad = diff_rad, glob_rad
= glob_rad, day = day, step = step)if name == “main”:
options, flags = grass.parser()
main()
After loading that into the appropriate directory:
C:\GRASS-64\scripts
and restarting GRASS, I enter the command “r.solar” into the command line.The result is the following message:
<
Traceback (most recent call last):
File “C:/GRASS-64/etc/wxpython/wxgui.py”, line 473, in
OnRunCmdself.goutput.RunCmd(cmd, switchPage=False)
File “C:\GRASS-64\etc\wxpython\gui_modules\goutput.py”,
line 354, in RunCmdmenuform.GUI().ParseCommand(cmdlist, parentframe=self)
File “C:\GRASS-64\etc\wxpython\gui_modules\menuform.py”,
line 1825, in ParseCommandxml.sax.parseString(getInterfaceDescription(cmd[0]).decode(e
nc).split(‘\n’,1)[1].replace(‘’, ‘<?xml version="1.0" encoding="utf-8"?>\n’, 1).encode(“utf-8”),
File “C:\GRASS-64\etc\wxpython\gui_modules\menuform.py”,
line 1764, in getInterfaceDescriptionraise IOError, _(“Unable to fetch interface description for
command ‘%s’.”) % cmd
IOError
:
Unable to fetch interface description for command ‘r.solar’.
I’ve tried this on two different computers, both with Windows 7. My belief
as to what the problem could be:
- I’ve put the file in the wrong directory
- I’ve got to somehow compile it before I can run it
- It’s got to be imported somehow into the GRASS interface first
- My syntax is somehow all wrong
- I’m missing some component needed to initiate the GUI
Can somebody help me? I really need this for my thesis and, as always,
there’s a lot of time pressure. I’d be immensely grateful to anyone who can
help me further. Thanks a lot!Best regards,
Daniel LeeView this message in context: http://osgeo-org.1803224.n2.nabble.com/Problem-with-running-Python-script-in-GRASS-tp5007296p5007296.html
Sent from the Grass - Users mailing list archive at Nabble.com.