[GRASS-dev] [GRASS GIS] #2659: replace function bug in t.rast.mapcalc.py

#2659: replace function bug in t.rast.mapcalc.py
----------------------------+-----------------------------------------------
Reporter: eosduero | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone: 7.0.1
Component: Python | Version: 7.0.0
Keywords: t.rast.mapcalc | Platform: Unspecified
      Cpu: x86-32 |
----------------------------+-----------------------------------------------
There is a bug in the ''t.rast.mapcalc.py'' function. The problem occurs
when we have 2 temporal datasets with similar names, for example:
* map1
* ecmmap1

When we try:
{{{
t.rast.mapcalc.py" --o input=map1,ecmmap1 expr="map1 - ecmmap1" out=sal1
basename=sal1 method=start
}}}

It cause an error. I think that the problem is in the line 217 (file
''etc\python\grass\temporal\mapcalc.py''):
{{{
                 expr = expr.replace(id_list[j], map_matrix[j][i])
}}}
The function can not replace correctly the raster dataset when they have
equal substrings.

Thanks and sorry for my english.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/2659&gt;
GRASS GIS <http://grass.osgeo.org>

#2659: replace function bug in t.rast.mapcalc.py
----------------------------+-----------------------------------------------
Reporter: eosduero | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone: 7.0.1
Component: Python | Version: 7.0.0
Keywords: t.rast.mapcalc | Platform: Unspecified
      Cpu: x86-32 |
----------------------------+-----------------------------------------------

Comment(by eosduero):

NOTE: In the example the ''basename'' of map1 and ecmmap1 ''datasets''
were created with the same name (map1 and ecmmap1).

--
Ticket URL: <http://trac.osgeo.org/grass/ticket/2659#comment:1&gt;
GRASS GIS <http://grass.osgeo.org>

#2659: replace function bug in t.rast.mapcalc.py
----------------------------+-----------------------------------------------
Reporter: eosduero | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone: 7.0.1
Component: Python | Version: 7.0.0
Keywords: t.rast.mapcalc | Platform: Unspecified
      Cpu: x86-32 |
----------------------------+-----------------------------------------------

Comment(by eosduero):

A possible solution:

{{{
             # current time step
             for j in range(len(map_matrix)):
                 if map_matrix[j][i] is None:
                     valid_maps = False
                     break
                 # Substitute the dataset name with the map name
                 expr = expr.replace(id_list[j], "aux__%00002d__" % j)

             # EOS
             for j in range(len(map_matrix)):
                 if map_matrix[j][i] is None:
                     valid_maps = False
                     break
                 # Substitute the dataset name with the map name
                 expr = expr.replace("aux__%00002d__" % j,
map_matrix[j][i])
}}}

--
Ticket URL: <http://trac.osgeo.org/grass/ticket/2659#comment:2&gt;
GRASS GIS <http://grass.osgeo.org>

#2659: replace function bug in t.rast.mapcalc.py
----------------------------+-----------------------------------------------
Reporter: eosduero | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone: 7.0.1
Component: Python | Version: 7.0.0
Keywords: t.rast.mapcalc | Platform: Unspecified
      Cpu: x86-32 |
----------------------------+-----------------------------------------------

Comment(by eosduero):

Other better posibility:

{{{
...............
from grass.exceptions import CalledModuleError
import re

############################################################################

def multiple_replace(string, rep_dict):
     pattern = re.compile("|".join([re.escape(k) for k in
rep_dict.keys()]), re.M)
     return pattern.sub(lambda x: rep_dict[x.group(0)], string)

...................

             # Replace all dataset names with their map names of the
             # current time step
             plants = {}

             for j in range(len(map_matrix)):
                 if map_matrix[j][i] is None:
                     valid_maps = False
                     break
                 # Substitute the dataset name with the map name
                 #expr = expr.replace(id_list[j], map_matrix[j][i])
                 plants[id_list[j]] = map_matrix[j][i]
                 #msgr.message()

             expr = multiple_replace(expr, plants)

}}}

--
Ticket URL: <http://trac.osgeo.org/grass/ticket/2659#comment:3&gt;
GRASS GIS <http://grass.osgeo.org>