Re: [GRASS-windows] Re: [GRASS-user] Problem with running Python script in GRASS
Luigi Ponti wrote: > > Windows, Python and 6.4 isn't the most reliable combination. > > > > At a minimum, you need a fairly recent version of 6.5 to be able to > > use Python scripts on Windows (the hacks required to get shell scripts > > to work on Windows get in the way of running other scripts). 6.4.0-RC6 > > won't work (g.parser was updated, but lib/python wasn't). > > Is this true also for the daily WinGRASS-6.4.SVN at > http://josef.fsv.cvut.cz/wingrass/ and will this hold true for 6.4 in > general? The current 6.4 SVN head has been updated to use "g.parser -s", which eliminates the main issue with Python scripts (on Windows, you can't "execute" a script based upon the "#!" line, so g.parser explicitly invoked the script via %GRASS_SH%, which didn't work for Python scripts). > > You also need a "clean" Python install with the correct registry > > entries, > > From what you say, one needs two Python installations: the one that > comes with GRASS and an additional "fresh" one -- is it ok to install > the current Python 2.6.5 version or 2.5 is better (2.6 seems to be ok > from http://grass.osgeo.org/wiki/GRASS_and_Python)? From the > GRASS/Python wiki one also infers that on Windows the Python binaries > that come with the GRASS installation are no longer used (?). You don't need the version which comes with GRASS, and you're probably better off not using it. You need Python 2.4 or later, with wxWidgets 2.8, NumPy, and PyWin32. [The main reason for the attempt at "bundling" Python in the installer is to eliminate the need to install wxWidgets, NumPy and PyWin32 to match the installed version of Python.] You may need to set PYTHONPATH to %WINGISBASE%\etc\python in order for scripts to find the grass.script module. You may also need to add %WINGISBASE%\scripts to %PATH% (Init.bat doesn't do this on Windows, as shell scripts in the scripts directory are invoked via a matching batch file in the bin directory). > (Currently I am using WinGRASS-6.4.SVN-r42046-1 from the installer > downloaded at the link above; OS is windows 7.) > > > and you may need to hack the GRASS startup scripts (the > > WinGRASS installer fails quite badly with regard to Python, and the > > OSGeo4W installer fails even harder). > > > > But my question is actually: if a user wants to take a (Windows) Python > scripting approach to GRASS, what version should she/he target? My > impression is that trying to build functionality using GRASS 7 may have > the best returns, although I am not sure how feasible is that right now, > i.e. what GRASS 6 functionality can currently be ported to GRASS 6 (I am > thinking for example to a way around the display issue that in 6.4 is > solved using appropriate settings for e.g. GRASS_RENDER_IMMEDIATE). A lot of the fundamental issues have been fixed in 7.0, although it isn't as widely deployed and hasn't been as widely tested as 6.x. The need for backwards compatibility in 6.x makes it harder to deal with some fundamental portability issues, while 7.0 doesn't have that problem (if existing mechanisms aren't portable, they get replaced with something that is, even if it breaks compatibility). -- Glynn Clements ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
Re: [GRASS-user] Problem with running Python script in GRASS
Dear Glynn, I am always interested in GRASS scripting-related questions. Thanks for your support on Python/Windows issues. From: Glynn Clements Subject: Re: [GRASS-user] Problem with running Python script in GRASS To: LeeDaniel Cc: grass-user@lists.osgeo.org Message-ID: <19426.59411.559013.36...@cerise.gclements.plus.com> Content-Type: text/plain; charset=us-ascii LeeDaniel wrote: > 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. [...] Windows, Python and 6.4 isn't the most reliable combination. At a minimum, you need a fairly recent version of 6.5 to be able to use Python scripts on Windows (the hacks required to get shell scripts to work on Windows get in the way of running other scripts). 6.4.0-RC6 won't work (g.parser was updated, but lib/python wasn't). Is this true also for the daily WinGRASS-6.4.SVN at http://josef.fsv.cvut.cz/wingrass/ and will this hold true for 6.4 in general? You also need a "clean" Python install with the correct registry entries, From what you say, one needs two Python installations: the one that comes with GRASS and an additional "fresh" one -- is it ok to install the current Python 2.6.5 version or 2.5 is better (2.6 seems to be ok from http://grass.osgeo.org/wiki/GRASS_and_Python)? From the GRASS/Python wiki one also infers that on Windows the Python binaries that come with the GRASS installation are no longer used (?). (Currently I am using WinGRASS-6.4.SVN-r42046-1 from the installer downloaded at the link above; OS is windows 7.) and you may need to hack the GRASS startup scripts (the WinGRASS installer fails quite badly with regard to Python, and the OSGeo4W installer fails even harder). But my question is actually: if a user wants to take a (Windows) Python scripting approach to GRASS, what version should she/he target? My impression is that trying to build functionality using GRASS 7 may have the best returns, although I am not sure how feasible is that right now, i.e. what GRASS 6 functionality can currently be ported to GRASS 6 (I am thinking for example to a way around the display issue that in 6.4 is solved using appropriate settings for e.g. GRASS_RENDER_IMMEDIATE). Given that WinGRASS users have the extreme luxury of 7.0.svn snapshot installers being built almost daily thanks to Martin Landa: http://josef.fsv.cvut.cz/wingrass/grass70/ it would be very interesting to know your opinion. Kind regards and thank you in advance, Luigi -- Glynn Clements ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
Re: [GRASS-user] Problem with running Python script in GRASS
LeeDaniel wrote: > 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. > 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: > Unable to fetch interface description for command 'r.solar'. Windows, Python and 6.4 isn't the most reliable combination. At a minimum, you need a fairly recent version of 6.5 to be able to use Python scripts on Windows (the hacks required to get shell scripts to work on Windows get in the way of running other scripts). 6.4.0-RC6 won't work (g.parser was updated, but lib/python wasn't). You also need a "clean" Python install with the correct registry entries, and you may need to hack the GRASS startup scripts (the WinGRASS installer fails quite badly with regard to Python, and the OSGeo4W installer fails even harder). -- Glynn Clements ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
Re: [GRASS-user] Problem with running Python script in GRASS
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 > Subject: [GRASS-user] Problem with running Python script in GRASS > To: grass-user@lists.osgeo.org > Message-ID: <1273036636062-5007296.p...@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 = &q
[GRASS-user] Problem with running Python script in GRASS
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_r