<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>We really need a cookbook way to set up GRASS for Windows so that Python scripts run.&nbsp;</div><div><br></div><div>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.&nbsp;</div><div><br class="webkit-block-placeholder"></div><div>Michael</div>
<div><br><div>Begin forwarded message:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span class="Apple-style-span" style="font-family: monospace; ">Date: Tue, 4 May 2010 22:17:16 -0700 (PDT)<br>From: LeeDaniel &lt;<a href="mailto:Lee.Daniel.1986@gmail.com">Lee.Daniel.1986@gmail.com</a>&gt;<br>Subject: [GRASS-user] Problem with running Python script in GRASS<br>To:<span class="Apple-converted-space">&nbsp;</span><a href="mailto:grass-user@lists.osgeo.org">grass-user@lists.osgeo.org</a><br>Message-ID: &lt;<a href="mailto:1273036636062-5007296.post@n2.nabble.com">1273036636062-5007296.post@n2.nabble.com</a>&gt;<br>Content-Type: text/plain; charset=us-ascii<br><br><br>Hello fellow GRASS users!<br><br>I'm sure this is a very simple problem but I'm having a really difficult<br>time with it... After searching for the solution for several days I'm on the<br>end of my whits and am really needing this script to get working. This is<br>the problem:<br><br>I've written a Python script, doing my best to use the Python I know and<br>reverse engineer the python scripts I found in the Internet. As far as I can<br>tell, the script should be fine, although I naturally can't execute it<br>independently. My goal is to run it as a command from inside GRASS so that<br>the user can input the parameters through the GUI. I think GRASS recognizes<br>that the script is there but isn't able to generate the GUI.<br><br>Here's the script:<br><br>__________________________________<br><br>############################################################################<br>#<br># MODULE: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.solar<br># AUTHOR(S): &nbsp;&nbsp;&nbsp;Daniel Lee<br># PURPOSE: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Runs r.sun for a year using different inputs for each month<br># COPYRIGHT: &nbsp;&nbsp;&nbsp;(C) 2010 by Daniel Lee<br>#<br># &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This program is free software under the GNU General Public<br># &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;License (&gt;=v2). Read the file COPYING that comes with GRASS<br># &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for details.<br>#<br>#############################################################################<br><br>#%Module<br>#% label: Solar modeling tool.<br>#% description: Conducts a solar analysis for a year using empirical inputs<br>for each month.<br>#% keywords: raster<br>#%End<br>#%Option<br>#% key: elevin<br>#% type: string<br>#% gisprompt: old,cell,raster<br>#% description: Name of input elevation map (unit = meters)<br>#% required : yes<br>#% guisection: Required inputs<br>#%End<br>#%Option<br>#% key: aspect<br>#% type: string<br>#% gisprompt: old,cell,raster<br>#% description: Name of input aspect map (decimal degrees)<br>#% required : yes<br>#% guisection: Required inputs<br>#%End<br>#%Option<br>#% key: slopein<br>#% type: string<br>#% gisprompt: old,cell,raster<br>#% description: Name of input slope map (decimal degrees)<br>#% required : yes<br>#% guisection: Required inputs<br>#%End<br>#%Option<br>#% key: linkein<br>#% type: string<br>#% gisprompt: old,cell,raster<br>#% description: Name of input Linke atmospheric turbidity coefficient map<br>#% required : no<br>#% guisection: Optional inputs<br>#%End<br>#%Option<br>#% key: albedo<br>#% type: string<br>#% gisprompt: old,cell,raster<br>#% description: Name of input albedo coefficient map<br>#% required : no<br>#% guisection: Optional inputs<br>#%End<br>#%Option<br>#% key: mapset<br>#% type: string<br>#% description: Name of the mapset containing solar data<br>#% required : yes<br>#% guisection: Required inputs<br>#%End<br>#%Flag<br>#% key: z<br>#% description: Generate map of sunlight insolation time (h)<br>#% guisection: Output options<br>#%End<br>#%Flag<br>#% key: y<br>#% description: Generate map of reflected radiation (Wh/m2)<br>#% guisection: Output options<br>#%End<br><br>import sys<br>import os<br>import string<br>import grass.script as grass<br><br>def main():<br>&nbsp;elevin = options['elevin']<br>&nbsp;aspect = options['aspect']<br>&nbsp;slopein = options['slopein']<br>&nbsp;linkein = options['linkein']<br>&nbsp;albedo = options['albedo']<br>&nbsp;mapset = "@" + options['mapset']<br>&nbsp;reflected = flags['y']<br>&nbsp;duration = flags['z']<br>&nbsp;step = 0.16<br><br>&nbsp;for day in range(365):<br>&nbsp;&nbsp;&nbsp;day += 1<br>&nbsp;&nbsp;&nbsp;# Define month<br>&nbsp;&nbsp;&nbsp;if day == 1:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month = "01" + mapset<br>&nbsp;&nbsp;&nbsp;elif day == 32:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month = "02" + mapset<br>&nbsp;&nbsp;&nbsp;elif day == 60:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month = "03" + mapset<br>&nbsp;&nbsp;&nbsp;elif day == 91:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month = "04" + mapset<br>&nbsp;&nbsp;&nbsp;elif day == 121:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month = "05" + mapset<br>&nbsp;&nbsp;&nbsp;elif day == 152:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month = "06" + mapset<br>&nbsp;&nbsp;&nbsp;elif day == 182:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month = "07" + mapset<br>&nbsp;&nbsp;&nbsp;elif day == 213:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month = "08" + mapset<br>&nbsp;&nbsp;&nbsp;elif day == 244:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month = "09" + mapset<br>&nbsp;&nbsp;&nbsp;elif day == 274:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month = "10" + mapset<br>&nbsp;&nbsp;&nbsp;elif day == 305:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month = "11" + mapset<br>&nbsp;&nbsp;&nbsp;elif day == 335:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month = "12" + mapset<br><br>&nbsp;&nbsp;&nbsp;# Define coefficients!<br>&nbsp;&nbsp;&nbsp;coefbh = 'coefbh' + month<br>&nbsp;&nbsp;&nbsp;coefdh = 'coefdh' + month<br>&nbsp;&nbsp;&nbsp;if not grass.find_file(elevin)['file'] or not<br>grass.find_file(aspect)['file'] or not grass.find_file(slopein)['file'] or<br>not grass.find_file(coefbh)['file'] or not grass.find_file(coefdh)['file']:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grass.fatal(_("Raster map &lt;%a&gt; not found.") % input<br><br>&nbsp;&nbsp;&nbsp;# Define outputs!<br>&nbsp;&nbsp;&nbsp;beam_rad = 'beam' + str(day)<br>&nbsp;&nbsp;&nbsp;diff_rad = 'diffuse' + str(day)<br>&nbsp;&nbsp;&nbsp;glob_rad = 'global' + str(day)<br>&nbsp;&nbsp;&nbsp;if duration and reflected:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insol_time = 'insol_time' + str(day)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;refl_rad = 'reflected' + str(day)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grass.run_command('r.sun', flags = 's', elevin = elevin, aspin =<br>aspin, slopein = slopein, linkein = linkein, albedo = albedo, coefbh =<br>coefbh, coefdh = coefdh, beam_rad = beam_rad, insol_time = insol_time,<br>diff_rad = diff_rad, refl_rad = refl_rad, glob_rad = glob_rad, day = day,<br>step = step)<br>&nbsp;&nbsp;&nbsp;elif duration and not reflected:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insol_time = 'insol_time' + str(day)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grass.run_command('r.sun', flags = 's', elevin = elevin, aspin =<br>aspin, slopein = slopein, linkein = linkein, albedo = albedo, coefbh =<br>coefbh, coefdh = coefdh, beam_rad = beam_rad, insol_time = insol_time,<br>diff_rad = diff_rad, glob_rad = glob_rad, day = day, step = step)<br>&nbsp;&nbsp;&nbsp;elif reflected and not duration:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;refl_rad = 'reflected' + str(day)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grass.run_command('r.sun', flags = 's', elevin = elevin, aspin =<br>aspin, slopein = slopein, linkein = linkein, albedo = albedo, coefbh =<br>coefbh, coefdh = coefdh, beam_rad = beam_rad, diff_rad = diff_rad, refl_rad<br>= refl_rad, glob_rad = glob_rad, day = day, step = step)<br>&nbsp;&nbsp;&nbsp;else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grass.run_command('r.sun', flags = 's', elevin = elevin, aspin =<br>aspin, slopein = slopein, linkein = linkein, albedo = albedo, coefbh =<br>coefbh, coefdh = coefdh, beam_rad = beam_rad, diff_rad = diff_rad, glob_rad<br>= glob_rad, day = day, step = step)<br><br>if __name__ == "__main__":<br>&nbsp;options, flags = grass.parser()<br>&nbsp;main()<br><br>__________________________________<br><br>After loading that into the appropriate directory:<br>C:\GRASS-64\scripts<br>and restarting GRASS, I enter the command "r.solar" into the command line.<br><br>The result is the following message:<br><br>&lt;<br><br>Traceback (most recent call last):<br>&nbsp;File "C:/GRASS-64/etc/wxpython/wxgui.py", line 473, in<br>OnRunCmd<br><br>self.goutput.RunCmd(cmd, switchPage=False)<br>&nbsp;File "C:\GRASS-64\etc\wxpython\gui_modules\goutput.py",<br>line 354, in RunCmd<br><br>menuform.GUI().ParseCommand(cmdlist, parentframe=self)<br>&nbsp;File "C:\GRASS-64\etc\wxpython\gui_modules\menuform.py",<br>line 1825, in ParseCommand<br><br>xml.sax.parseString(getInterfaceDescription(cmd[0]).decode(e<br>nc).split('\n',1)[1].replace('', '&lt;?xml version="1.0"<br>encoding="utf-8"?&gt;\n', 1).encode("utf-8"),<br>&nbsp;File "C:\GRASS-64\etc\wxpython\gui_modules\menuform.py",<br>line 1764, in getInterfaceDescription<br><br>raise IOError, _("Unable to fetch interface description for<br>command '%s'.") % cmd<br>IOError<br>:<br>Unable to fetch interface description for command 'r.solar'.<br><br>__________________________________<br><br>I've tried this on two different computers, both with Windows 7. My belief<br>as to what the problem could be:<br>- I've put the file in the wrong directory<br>- I've got to somehow compile it before I can run it<br>- It's got to be imported somehow into the GRASS interface first<br>- My syntax is somehow all wrong<br>- I'm missing some component needed to initiate the GUI<br><br>Can somebody help me? I really need this for my thesis and, as always,<br>there's a lot of time pressure. I'd be immensely grateful to anyone who can<br>help me further. Thanks a lot!<br><br>Best regards,<br>Daniel Lee<br>--<br>View this message in context:<span class="Apple-converted-space">&nbsp;</span><a href="http://osgeo-org.1803224.n2.nabble.com/Problem-with-running-Python-script-in-GRASS-tp5007296p5007296.html">http://osgeo-org.1803224.n2.nabble.com/Problem-with-running-Python-script-in-GRASS-tp5007296p5007296.html</a><br>Sent from the Grass - Users mailing list archive at<span class="Apple-converted-space">&nbsp;</span><a href="http://Nabble.com/">Nabble.com</a>.<br></span></span></blockquote></div><br></body></html>