[GRASS-dev] grass7 - python script - moving window: how to simplify/accelerate r.mapcalc?

hi,

in a python script within a mapcalc-expression I have moving window with 6
cells in all directions and a if(x,a,b) for each cell in

r.mapcalc "elevation_percentile_step2 = (100.0 / 48.0) * \
(if(eudem_osttirol[3,3] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[2,2] <
eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[1,1] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[-3,3]
< eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[-2,2] < eudem_osttirol , 1, 0 ) +
if(eudem_osttirol[-1,1] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[3,0] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[3,-3]
< eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[2,0] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[2,-2]
< eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[1,0] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[1,-1]
< eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[0,3] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[0,2]
< eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[0,1] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[0,-1]
< eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[0,-2] < eudem_osttirol , 1, 0 ) +
if(eudem_osttirol[0,-3] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[-1,0] < eudem_osttirol , 1, 0 ) +
if(eudem_osttirol[-1,-1] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[-2,0] < eudem_osttirol , 1, 0 ) +
if(eudem_osttirol[-2,-2] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[-3,0] < eudem_osttirol , 1, 0 ) +
if(eudem_osttirol[-3,-3] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[-4,4] < eudem_osttirol , 1, 0 ) +
if(eudem_osttirol[-5,5] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[-6,6] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[0,4]
< eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[0,5] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[0,6]
< eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[4,4] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[5,5]
< eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[6,6] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[4,0]
< eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[5,0] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[6,0]
< eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[4,-4] < eudem_osttirol , 1, 0 ) +
if(eudem_osttirol[5,-5] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[6,-6] < eudem_osttirol , 1, 0 ) +
if(eudem_osttirol[0,-4] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[0,-5] < eudem_osttirol , 1, 0 ) +
if(eudem_osttirol[0,-6] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[-4,-4] < eudem_osttirol , 1, 0 ) +
if(eudem_osttirol[-5,-5] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[-6,-6] < eudem_osttirol , 1, 0 ) +
if(eudem_osttirol[-4,0] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[-5,0] < eudem_osttirol , 1, 0 ) +
if(eudem_osttirol[-6,0] < eudem_osttirol , 1, 0 ))"

any idea/hint to simplify and/or accelerate such a python script with such a
large moving window?

thanks

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/grass7-python-script-moving-window-how-to-simplify-accelerate-r-mapcalc-tp5143161.html
Sent from the Grass - Dev mailing list archive at Nabble.com.

Helmut Kudrnovsky wrote:

in a python script within a mapcalc-expression I have moving window with 6
cells in all directions and a if(x,a,b) for each cell in

r.mapcalc "elevation_percentile_step2 = (100.0 / 48.0) * \
(if(eudem_osttirol[3,3] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[2,2] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[1,1] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[-3,3] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[-2,2] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[-1,1] < eudem_osttirol , 1, 0 ) \

[snip]

+ if(eudem_osttirol[-4,-4] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[-5,-5] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[-6,-6] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[-4,0] < eudem_osttirol , 1, 0 ) \
+ if(eudem_osttirol[-5,0] < eudem_osttirol , 1, 0 ) + if(eudem_osttirol[-6,0] < eudem_osttirol , 1, 0 ))"

any idea/hint to simplify and/or accelerate such a python script with such a
large moving window?

The most straightforward change is to eliminate the if() functions.
Any boolean value (e.g. the result of a comparison) is an integer,
with 1 for true, 0 for false. So "if(a<b,1,0)" is equivalent to just
"(a<b)". This should also provide a minor increase in efficiency.

It may be desirable to generate the expression dynamically, e.g.

  offsets = [d
      for j in xrange(1,6+1)
      for i in [j,-j]
      for d in [(i,0),(0,i),(i,i),(i,-i)]]
  terms = ["(eudem_osttirol[%d,%d] < eudem_osttirol)" % d
           for d in offsets]
  expr = "elevation_percentile_step2 = (100.0 / 48.0) * \\\n(%s)" % " + \\\n".join(terms)

This makes it easier to change e.g. the size of the window or the name
of the map.

But if you want any significant gain in efficiency, the obvious
solution is to modify r.neighbors to add a new aggregate (similar to
interspersion, but using less-than rather than not-equal). The weight=
option can be used to specify the window shape (for an aggregate which
lacks a weighted version, the weights are converted to a mask, where
cells with a non-zero weight are used and those with a zero weight
ignored).

If the region is small, you may get away with using grass.script.array
and NumPy. E.g. (untested)

  import numpy
  import grass.script.array
  data = grass.script.array.array()
  data.read("eudem_osttirol")
  offsets = [d
      for j in xrange(1,6+1)
      for i in [j,-j]
      for d in [(i,0),(0,i),(i,i),(i,-i)]]
  rows,cols = data.shape
  center = data[6:,6:][:rows-13,:cols-13]
  count = numpy.zeros((rows-13,cols-13),dtype=int)
  for dy,dx in offsets:
      cmp = data[6-dy:,6-dx:][:rows-13,:cols-13] < center
      count += cmp
  perc = grass.script.array.array()
  perc[...] = 0
  perc[6:-7,6:-7] = count * 100.0 / 48
  perc.write("elevation_percentile_step2")

Realistically, this requires that the raw data is significantly
smaller than the available memory.

This type of problem comes up often enough that we really want an
aggregate (method) which evaluates an arbitrary expression with the
cell values and weights as inputs.

The language would need to support iterating over arrays (which rules
out r.mapcalc), reasonably easy to embed, and preferably efficient for
the case where you're evaluating the same expression many times with
different inputs (i.e. it shouldn't parse/compile/etc the expression
each time).

--
Glynn Clements <glynn@gclements.plus.com>

The most straightforward change is to eliminate the if() functions.
Any boolean value (e.g. the result of a comparison) is an integer,
with 1 for true, 0 for false. So "if(a<b,1,0)&quot; is equivalent to just
"(a<b)&quot;. This should also provide a minor increase in efficiency.

ah thanks. a quick diff between r.mapcalc with and without the if()
function:

| Range of data: min = 0 max = 0
| Data Description:
|
| generated by r.mapcalc
|
|
|
| Comments:
|
| elevation_percentile_step2@user1 - elevation_percentile_step2a@user1
|

yes, it works.

now a test in the python shell of the wxgui with nc sample dataset:

import grass.script as grass

offsets = [d

    for j in xrange(1,6+1)
    for i in [j,-j]
    for d in [(i,0),(0,i),(i,i),(i,-i)]]
  

offsets

[(1, 0), (0, 1), (1, 1), (1, -1), (-1, 0), (0, -1), (-1, -1), (-1, 1), (2,
0), (0, 2), (2, 2), (2, -2), (-2, 0), (0, -2), (-2, -2), (-2, 2), (3, 0),
(0, 3), (3, 3), (3, -3), (-3, 0), (0, -3), (-3, -3), (-3, 3), (4, 0), (0,
4), (4, 4), (4, -4), (-4, 0), (0, -4), (-4, -4), (-4, 4), (5, 0), (0, 5),
(5, 5), (5, -5), (-5, 0), (0, -5), (-5, -5), (-5, 5), (6, 0), (0, 6), (6,
6), (6, -6), (-6, 0), (0, -6), (-6, -6), (-6, 6)]

terms = ["(myelevnc[%d,%d] < myelevnc)" % d

         for d in offsets]

terms

['(myelevnc[1,0] < myelevnc)', '(myelevnc[0,1] < myelevnc)', '(myelevnc[1,1]
< myelevnc)', '(myelevnc[1,-1] < myelevnc)', '(myelevnc[-1,0] < myelevnc)',
'(myelevnc[0,-1] < myelevnc)', '(myelevnc[-1,-1] < myelevnc)',
'(myelevnc[-1,1] < myelevnc)', '(myelevnc[2,0] < myelevnc)', '(myelevnc[0,2]
< myelevnc)', '(myelevnc[2,2] < myelevnc)', '(myelevnc[2,-2] < myelevnc)',
'(myelevnc[-2,0] < myelevnc)', '(myelevnc[0,-2] < myelevnc)',
'(myelevnc[-2,-2] < myelevnc)', '(myelevnc[-2,2] < myelevnc)',
'(myelevnc[3,0] < myelevnc)', '(myelevnc[0,3] < myelevnc)', '(myelevnc[3,3]
< myelevnc)', '(myelevnc[3,-3] < myelevnc)', '(myelevnc[-3,0] < myelevnc)',
'(myelevnc[0,-3] < myelevnc)', '(myelevnc[-3,-3] < myelevnc)',
'(myelevnc[-3,3] < myelevnc)', '(myelevnc[4,0] < myelevnc)', '(myelevnc[0,4]
< myelevnc)', '(myelevnc[4,4] < myelevnc)', '(myelevnc[4,-4] < myelevnc)',
'(myelevnc[-4,0] < myelevnc)', '(myelevnc[0,-4] < myelevnc)',
'(myelevnc[-4,-4] < myelevnc)', '(myelevnc[-4,4] < myelevnc)',
'(myelevnc[5,0] < myelevnc)', '(myelevnc[0,5] < myelevnc)', '(myelevnc[5,5]
< myelevnc)', '(myelevnc[5,-5] < myelevnc)', '(myelevnc[-5,0] < myelevnc)',
'(myelevnc[0,-5] < myelevnc)', '(myelevnc[-5,-5] < myelevnc)',
'(myelevnc[-5,5] < myelevnc)', '(myelevnc[6,0] < myelevnc)', '(myelevnc[0,6]
< myelevnc)', '(myelevnc[6,6] < myelevnc)', '(myelevnc[6,-6] < myelevnc)',
'(myelevnc[-6,0] < myelevnc)', '(myelevnc[0,-6] < myelevnc)',
'(myelevnc[-6,-6] < myelevnc)', '(myelevnc[-6,6] < myelevnc)']

expr = "elevation_percentile = (100.0 / 48.0) * \\\n(%s)" % " +

\\\n".join(terms)

expr

elevation_percentile = (100.0 / 48.0) * \
((myelevnc[1,0] < myelevnc) + \
(myelevnc[0,1] < myelevnc) + \
(myelevnc[1,1] < myelevnc) + \
(myelevnc[1,-1] < myelevnc) + \
(myelevnc[-1,0] < myelevnc) + \
(myelevnc[0,-1] < myelevnc) + \
(myelevnc[-1,-1] < myelevnc) + \
(myelevnc[-1,1] < myelevnc) + \
(myelevnc[2,0] < myelevnc) + \
(myelevnc[0,2] < myelevnc) + \
(myelevnc[2,2] < myelevnc) + \
(myelevnc[2,-2] < myelevnc) + \
(myelevnc[-2,0] < myelevnc) + \
(myelevnc[0,-2] < myelevnc) + \
(myelevnc[-2,-2] < myelevnc) + \
(myelevnc[-2,2] < myelevnc) + \
(myelevnc[3,0] < myelevnc) + \
(myelevnc[0,3] < myelevnc) + \
(myelevnc[3,3] < myelevnc) + \
(myelevnc[3,-3] < myelevnc) + \
(myelevnc[-3,0] < myelevnc) + \
(myelevnc[0,-3] < myelevnc) + \
(myelevnc[-3,-3] < myelevnc) + \
(myelevnc[-3,3] < myelevnc) + \
(myelevnc[4,0] < myelevnc) + \
(myelevnc[0,4] < myelevnc) + \
(myelevnc[4,4] < myelevnc) + \
(myelevnc[4,-4] < myelevnc) + \
(myelevnc[-4,0] < myelevnc) + \
(myelevnc[0,-4] < myelevnc) + \
(myelevnc[-4,-4] < myelevnc) + \
(myelevnc[-4,4] < myelevnc) + \
(myelevnc[5,0] < myelevnc) + \
(myelevnc[0,5] < myelevnc) + \
(myelevnc[5,5] < myelevnc) + \
(myelevnc[5,-5] < myelevnc) + \
(myelevnc[-5,0] < myelevnc) + \
(myelevnc[0,-5] < myelevnc) + \
(myelevnc[-5,-5] < myelevnc) + \
(myelevnc[-5,5] < myelevnc) + \
(myelevnc[6,0] < myelevnc) + \
(myelevnc[0,6] < myelevnc) + \
(myelevnc[6,6] < myelevnc) + \
(myelevnc[6,-6] < myelevnc) + \
(myelevnc[-6,0] < myelevnc) + \
(myelevnc[0,-6] < myelevnc) + \
(myelevnc[-6,-6] < myelevnc) + \
(myelevnc[-6,6] < myelevnc))

the expression expr is build correct, thanks.

but then while do

grass.mapcalc("%s" % expr)

or

grass.mapcalc( expr )

an error raise up with following message with DEBUG=3

GRASS 7.1.svn> D2/3: G_file_name(): path =
C:\grassdata/nc_spm_08_grass7/user1
D1/3: G_find_raster2(): name=\ mapset=
D2/3: G_file_name(): path = C:\grassdata/nc_spm_08_grass7/user1/SEARCH_PATH
D2/3: G_file_name(): path = C:\grassdata/nc_spm_08_grass7/PERMANENT
D2/3: G_file_name(): path = C:\grassdata/nc_spm_08_grass7/user1/cell/\
D2/3: G_file_name(): path = C:\grassdata/nc_spm_08_grass7/PERMANENT/cell/\

GRASS_INFO_WARNING(8628,1): 'cell/\' wurde in mehreren Mapsets gefunden
(auch in
<PERMANENT>).
GRASS_INFO_END(8628,1)

GRASS_INFO_WARNING(8628,2): Verwende <\@user1>
GRASS_INFO_END(8628,2)
D1/3: G_find_raster2(): name=\ mapset=user1
D2/3: G_file_name(): path = C:\grassdata/nc_spm_08_grass7/user1/cell/\
D2/3: G_file_name(): path = C:\grassdata/nc_spm_08_grass7/user1/fcell/\
D1/3: G_find_raster2(): name=\ mapset=user1
D2/3: G_file_name(): path = C:\grassdata/nc_spm_08_grass7/user1/cell/\

GRASS_INFO_ERROR(8628,3): Kann
'C:\grassdata/nc_spm_08_grass7/user1/cell_misc/\/
f_format' nicht finden.
GRASS_INFO_END(8628,3)
D2/3: G_file_name(): path = C:\grassdata/nc_spm_08_grass7/user1

GRASS_INFO_ERROR(5412,1): An error occurred while running r.mapcalc
GRASS_INFO_END(5412,1)

? ... not a very informative error message

just a another quick test

mapinput = "myelevnc"

mapinput

myelevnc

mapoutput = "myelevnc_1"

mapoutput

myelevnc_1

expression = "%s = %s + 1.0" % (mapoutput, mapinput)

expression

myelevnc_1 = myelevnc + 1.0

grass.mapcalc( expression )

grass.mapcalc( expression )
None

grass.read_command("r.info", map = mapoutput)

+----------------------------------------------------------------------------+
| Layer: myelevnc_1 Date: Sat May 31 15:20:02 2014
|
| Mapset: user1 Login of Creator: myricaria
|
| Location: nc_spm_08_grass7
|
| DataBase: C:\grassdata
|
| Title: ( myelevnc_1 )
|
| Timestamp: none
|

|----------------------------------------------------------------------------|
|
|
| Type of Map: raster Number of Categories: 0
|
| Data Type: DCELL
|
| Rows: 1350
|
| Columns: 1500
|
| Total Cells: 2025000
|
| Projection: Lambert Conformal Conic
|
| N: 228500 S: 215000 Res: 10
|
| E: 645000 W: 630000 Res: 10
|
| Range of data: min = 56.5787925720215 max = 157.329864501953
|
|
|
| Data Description:
|
| generated by r.mapcalc
|
|
|
| Comments:
|
| myelevnc + 1
|
|
|

+----------------------------------------------------------------------------+

this works.

any idea?

But if you want any significant gain in efficiency, the obvious
solution is to modify r.neighbors to add a new aggregate (similar to
interspersion, but using less-than rather than not-equal). The weight=
option can be used to specify the window shape (for an aggregate which
lacks a weighted version, the weights are converted to a mask, where
cells with a non-zero weight are used and those with a zero weight
ignored).

[...]

This type of problem comes up often enough that we really want an
aggregate (method) which evaluates an arbitrary expression with the
cell values and weights as inputs.

that would be nice.

thanks.

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/grass7-python-script-moving-window-how-to-simplify-accelerate-r-mapcalc-tp5143161p5143415.html
Sent from the Grass - Dev mailing list archive at Nabble.com.

grass.mapcalc("%s" % expr)

or

grass.mapcalc( expr )

[...]

GRASS_INFO_ERROR(8628,3): Kann

'C:\grassdata/nc_spm_08_grass7/user1/cell_misc/\/

f_format' nicht finden.
GRASS_INFO_END(8628,3)
D2/3: G_file_name(): path = C:\grassdata/nc_spm_08_grass7/user1

GRASS_INFO_ERROR(5412,1): An error occurred while running r.mapcalc
GRASS_INFO_END(5412,1)

to answer my own question, new lines are not recognized correctly in the
expression,

so instead of

  expr = "elevation_percentile_step2 = (100.0 / 48.0) * \\\n(%s)" % " +
\\\n".join(terms)

it should be

  expr = "elevation_percentile3 = (100.0 / 48.0) * (%s)" % " + ".join(terms)

expr

elevation_percentile3 = (100.0 / 48.0) * ((myelevnc[1,0] < myelevnc) +
(myelevnc[0,1] < myelevnc) + (myelevnc[1,1] < myelevnc) + (myelevnc[1,-1] <
myelevnc) + (myelevnc[-1,0] < myelevnc) + (myelevnc[0,-1] < myelevnc) +
(myelevnc[-1,-1] < myelevnc) + (myelevnc[-1,1] < myelevnc) + (myelevnc[2,0]
< myelevnc) + (myelevnc[0,2] < myelevnc) + (myelevnc[2,2] < myelevnc) +
(myelevnc[2,-2] < myelevnc) + (myelevnc[-2,0] < myelevnc) + (myelevnc[0,-2]
< myelevnc) + (myelevnc[-2,-2] < myelevnc) + (myelevnc[-2,2] < myelevnc) +
(myelevnc[3,0] < myelevnc) + (myelevnc[0,3] < myelevnc) + (myelevnc[3,3] <
myelevnc) + (myelevnc[3,-3] < myelevnc) + (myelevnc[-3,0] < myelevnc) +
(myelevnc[0,-3] < myelevnc) + (myelevnc[-3,-3] < myelevnc) + (myelevnc[-3,3]
< myelevnc) + (myelevnc[4,0] < myelevnc) + (myelevnc[0,4] < myelevnc) +
(myelevnc[4,4] < myelevnc) + (myelevnc[4,-4] < myelevnc) + (myelevnc[-4,0] <
myelevnc) + (myelevnc[0,-4] < myelevnc) + (myelevnc[-4,-4] < myelevnc) +
(myelevnc[-4,4] < myelevnc) + (myelevnc[5,0] < myelevnc) + (myelevnc[0,5] <
myelevnc) + (myelevnc[5,5] < myelevnc) + (myelevnc[5,-5] < myelevnc) +
(myelevnc[-5,0] < myelevnc) + (myelevnc[0,-5] < myelevnc) + (myelevnc[-5,-5]
< myelevnc) + (myelevnc[-5,5] < myelevnc) + (myelevnc[6,0] < myelevnc) +
(myelevnc[0,6] < myelevnc) + (myelevnc[6,6] < myelevnc) + (myelevnc[6,-6] <
myelevnc) + (myelevnc[-6,0] < myelevnc) + (myelevnc[0,-6] < myelevnc) +
(myelevnc[-6,-6] < myelevnc) + (myelevnc[-6,6] < myelevnc))

grass.mapcalc( expr )

None

now the calculation works.

thanks

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/grass7-python-script-moving-window-how-to-simplify-accelerate-r-mapcalc-tp5143161p5143417.html
Sent from the Grass - Dev mailing list archive at Nabble.com.

grass.mapcalc( expr )

None

now the calculation works.

for the record, there is now a wiki entry for this.

http://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library#r.mapcalc_example:_defining_a_moving_window

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/grass7-python-script-moving-window-how-to-simplify-accelerate-r-mapcalc-tp5143161p5143425.html
Sent from the Grass - Dev mailing list archive at Nabble.com.

Helmut Kudrnovsky wrote:

but then while do

>>>grass.mapcalc("%s" % expr)

or

>>>grass.mapcalc( expr )

an error raise up with following message with DEBUG=3

GRASS 7.1.svn> D2/3: G_file_name(): path =
C:\grassdata/nc_spm_08_grass7/user1
D1/3: G_find_raster2(): name=\ mapset=

Ugh.

Can you try replacing the grass.mapcalc() call with:

  grass.write_command('r.mapcalc', file = '-', stdin = expr)

Or maybe it requires CR+LF on Windows:

  expr = "elevation_percentile = (100.0 / 48.0) * \\\r\n(%s)" % " + \\\r\n".join(terms)
  grass.mapcalc(expr)

Alternatively, just omit the line breaks from the expression:

  expr = "elevation_percentile = (100.0 / 48.0) * (%s)" % " + ".join(terms)
  grass.mapcalc(expr)

I'm not sure whether Windows even allows newlines in command-line
arguments. If it does, then it would suggest a bug in Python's
subprocess module, which handles the conversion of the argument list
to a string.

--
Glynn Clements <glynn@gclements.plus.com>

Helmut Kudrnovsky wrote:

to answer my own question, new lines are not recognized correctly in the
expression,

I should have read your follow-up before sending my reply.

But, for future reference, can you test the other two options (using
CR+LF, and using write_command())?

--
Glynn Clements <glynn@gclements.plus.com>

Glynn Clements wrote

Helmut Kudrnovsky wrote:

to answer my own question, new lines are not recognized correctly in the
expression,

I should have read your follow-up before sending my reply.

But, for future reference, can you test the other two options (using
CR+LF, and using write_command())?

--
Glynn Clements &lt;

glynn@.plus

&gt;
_______________________________________________
grass-dev mailing list

grass-dev@.osgeo

http://lists.osgeo.org/mailman/listinfo/grass-dev

Can you try replacing the grass.mapcalc() call with:
grass.write_command('r.mapcalc', file = '-', stdin = expr)

grass.write_command('r.mapcalc', file = '-', stdin = expr)
0

=> calculation done

Or maybe it requires CR+LF on Windows:
expr = "calc4 = (100.0 / 48.0) * \\\r\n(%s)" % " + \\\r\n".join(terms)
grass.mapcalc(expr)

=> wxpython GUI crash

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/grass7-python-script-moving-window-how-to-simplify-accelerate-r-mapcalc-tp5143161p5143452.html
Sent from the Grass - Dev mailing list archive at Nabble.com.

Helmut Kudrnovsky wrote:

> Can you try replacing the grass.mapcalc() call with:
> grass.write_command('r.mapcalc', file = '-', stdin = expr)

grass.write_command('r.mapcalc', file = '-', stdin = expr)
0

=> calculation done

r60662 modifies the mapcalc() and mapcalc_start() functions to pass
the expression on stdin rather than the command line.

--
Glynn Clements <glynn@gclements.plus.com>