1
"""!@package grass.temporal
3
Temporal raster algebra
5
(C) 2013 by the GRASS Development Team
6
This program is free software under the GNU General Public
7
License (>=v2). Read the file COPYING that comes with GRASS
10
:authors: Thomas Leppelt and Soeren Gebbert
12
.. code-block:: python
14
>>> p = TemporalRasterAlgebraLexer()
17
>>> expression = 'R = A[0,1,0] / B[0,0,1] * 20 + C[0,1,1] - 2.45'
18
>>> p.test(expression)
19
R = A[0,1,0] / B[0,0,1] * 20 + C[0,1,1] - 2.45
20
LexToken(NAME,'R',1,0)
21
LexToken(EQUALS,'=',1,2)
22
LexToken(NAME,'A',1,4)
23
LexToken(L_SPAREN,'[',1,5)
25
LexToken(COMMA,',',1,7)
27
LexToken(COMMA,',',1,9)
29
LexToken(R_SPAREN,']',1,11)
30
LexToken(DIV,'/',1,13)
31
LexToken(NAME,'B',1,15)
32
LexToken(L_SPAREN,'[',1,16)
34
LexToken(COMMA,',',1,18)
36
LexToken(COMMA,',',1,20)
38
LexToken(R_SPAREN,']',1,22)
39
LexToken(MULT,'*',1,24)
41
LexToken(ADD,'+',1,29)
42
LexToken(NAME,'C',1,31)
43
LexToken(L_SPAREN,'[',1,32)
45
LexToken(COMMA,',',1,34)
47
LexToken(COMMA,',',1,36)
49
LexToken(R_SPAREN,']',1,38)
50
LexToken(SUB,'-',1,40)
51
LexToken(FLOAT,2.45,1,42)
55
from temporal_raster_base_algebra import *
57
###############################################################################
59
class TemporalRasterAlgebraParser(TemporalRasterBaseAlgebraParser):
60
"""The temporal raster algebra class"""
62
def __init__(self, pid=None, run=False, debug=True, spatial = False, nprocs = 1, register_null = False):
63
TemporalRasterBaseAlgebraParser.__init__(self, pid, run, debug, spatial, nprocs, register_null)
65
self.m_mapcalc = pymod.Module('r.mapcalc')
66
self.m_mremove = pymod.Module('g.remove')
68
def parse(self, expression, basename = None, overwrite=False):
69
# Check for space time dataset type definitions from temporal algebra
70
l = TemporalRasterAlgebraLexer()
72
l.lexer.input(expression)
78
if tok.type == "STVDS" or tok.type == "STRDS" or tok.type == "STR3DS":
79
raise SyntaxError("Syntax error near '%s'" %(tok.type))
81
self.lexer = TemporalRasterAlgebraLexer()
83
self.parser = yacc.yacc(module=self, debug=self.debug)
85
self.overwrite = overwrite
87
self.stdstype = "strds"
88
self.maptype = "raster"
89
self.mapclass = RasterDataset
90
self.basename = basename
91
self.expression = expression
92
self.parser.parse(expression)
94
######################### Temporal functions ##############################
96
def p_statement_assign(self, t):
97
# The expression should always return a list of maps.
99
statement : stds EQUALS expr
101
TemporalRasterBaseAlgebraParser.p_statement_assign(self, t)
103
def p_ts_neighbour_operation(self, t):
109
expr : stds L_SPAREN number COMMA number R_SPAREN
110
| stds L_SPAREN number R_SPAREN
111
| stds L_SPAREN number COMMA number COMMA number R_SPAREN
114
maplist = self.check_stds(t[1])
129
max_index = len(maplist)
130
for map_i in maplist:
131
# Get map index and temporal extent.
132
map_index = maplist.index(map_i)
133
new_index = map_index + t_neighbour
134
if new_index < max_index and new_index >= 0:
135
map_i_t_extent = map_i.get_temporal_extent()
136
# Get neighbouring map and set temporal extent.
137
map_n = maplist[new_index]
138
# Generate an intermediate map for the result map list.
139
map_new = self.generate_new_map(map_n, bool_op = 'and', copy = True)
140
map_new.set_temporal_extent(map_i_t_extent)
141
# Create r.mapcalc expression string for the operation.
142
if "cmd_list" in dir(map_new) and len(t) == 5:
143
cmdstring = "%s" %(map_new.cmd_list)
144
elif "cmd_list" not in dir(map_new) and len(t) == 5:
145
cmdstring = "%s" %(map_n.get_id())
146
elif "cmd_list" in dir(map_new) and len(t) in (7, 9):
147
cmdstring = "%s[%s,%s]" %(map_new.cmd_list, row_neigbour, col_neigbour)
148
elif "cmd_list" not in dir(map_new) and len(t) in (7, 9):
149
cmdstring = "%s[%s,%s]" %(map_n.get_id(), row_neigbour, col_neigbour)
150
# Set new command list for map.
151
map_new.cmd_list = cmdstring
152
# Append map with updated command list to result list.
153
resultlist.append(map_new)
157
###############################################################################
159
if __name__ == "__main__":