[GRASS-user] Problem with running Python script in GRASS

Michael Barton michael.barton at asu.edu
Wed May 5 11:35:05 EDT 2010


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 at gmail.com>
> Subject: [GRASS-user] Problem with running Python script in GRASS
> To: grass-user at lists.osgeo.org
> Message-ID: <1273036636062-5007296.post at n2.nabble.com>
> Content-Type: text/plain; charset=us-ascii
> 
> 
> Hello 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
> #%End
> 
> import sys
> import os
> import string
> import grass.script as grass
> 
> def 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.16
> 
>  for day in range(365):
>    day += 1
>    # Define 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" + mapset
> 
>    # Define 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.") % input
> 
>    # Define 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
> OnRunCmd
> 
> self.goutput.RunCmd(cmd, switchPage=False)
>  File "C:\GRASS-64\etc\wxpython\gui_modules\goutput.py",
> line 354, in RunCmd
> 
> menuform.GUI().ParseCommand(cmdlist, parentframe=self)
>  File "C:\GRASS-64\etc\wxpython\gui_modules\menuform.py",
> line 1825, in ParseCommand
> 
> xml.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 getInterfaceDescription
> 
> raise 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 Lee
> --
> View 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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/grass-user/attachments/20100505/41d3a1e3/attachment-0001.html


More information about the grass-user mailing list