~ubuntu-branches/ubuntu/wily/grass/wily

« back to all changes in this revision

Viewing changes to lib/python/temporal/temporal_raster_algebra.py

Tags: 7.0.0~rc1+ds1-1~exp1
* New upstream release candidate.
* Repack upstream tarball, remove precompiled Python objects.
* Add upstream metadata.
* Update gbp.conf and Vcs-Git URL to use the experimental branch.
* Update watch file for GRASS 7.0.
* Drop build dependencies for Tcl/Tk, add build dependencies:
  python-numpy, libnetcdf-dev, netcdf-bin, libblas-dev, liblapack-dev
* Update Vcs-Browser URL to use cgit instead of gitweb.
* Update paths to use grass70.
* Add configure options: --with-netcdf, --with-blas, --with-lapack,
  remove --with-tcltk-includes.
* Update patches for GRASS 7.
* Update copyright file, changes:
  - Update copyright years
  - Group files by license
  - Remove unused license sections
* Add patches for various typos.
* Fix desktop file with patch instead of d/rules.
* Use minimal dh rules.
* Bump Standards-Version to 3.9.6, no changes.
* Use dpkg-maintscript-helper to replace directories with symlinks.
  (closes: #776349)
* Update my email to use @debian.org address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""!@package grass.temporal
 
2
 
 
3
Temporal raster algebra
 
4
 
 
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
 
8
for details.
 
9
 
 
10
:authors: Thomas Leppelt and Soeren Gebbert
 
11
 
 
12
.. code-block:: python
 
13
 
 
14
    >>> p = TemporalRasterAlgebraLexer()
 
15
    >>> p.build()
 
16
    >>> p.debug = True
 
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)
 
24
    LexToken(INT,0,1,6)
 
25
    LexToken(COMMA,',',1,7)
 
26
    LexToken(INT,1,1,8)
 
27
    LexToken(COMMA,',',1,9)
 
28
    LexToken(INT,0,1,10)
 
29
    LexToken(R_SPAREN,']',1,11)
 
30
    LexToken(DIV,'/',1,13)
 
31
    LexToken(NAME,'B',1,15)
 
32
    LexToken(L_SPAREN,'[',1,16)
 
33
    LexToken(INT,0,1,17)
 
34
    LexToken(COMMA,',',1,18)
 
35
    LexToken(INT,0,1,19)
 
36
    LexToken(COMMA,',',1,20)
 
37
    LexToken(INT,1,1,21)
 
38
    LexToken(R_SPAREN,']',1,22)
 
39
    LexToken(MULT,'*',1,24)
 
40
    LexToken(INT,20,1,26)
 
41
    LexToken(ADD,'+',1,29)
 
42
    LexToken(NAME,'C',1,31)
 
43
    LexToken(L_SPAREN,'[',1,32)
 
44
    LexToken(INT,0,1,33)
 
45
    LexToken(COMMA,',',1,34)
 
46
    LexToken(INT,1,1,35)
 
47
    LexToken(COMMA,',',1,36)
 
48
    LexToken(INT,1,1,37)
 
49
    LexToken(R_SPAREN,']',1,38)
 
50
    LexToken(SUB,'-',1,40)
 
51
    LexToken(FLOAT,2.45,1,42)
 
52
 
 
53
"""
 
54
 
 
55
from temporal_raster_base_algebra import *
 
56
 
 
57
###############################################################################
 
58
 
 
59
class TemporalRasterAlgebraParser(TemporalRasterBaseAlgebraParser):
 
60
    """The temporal raster algebra class"""
 
61
 
 
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)
 
64
 
 
65
        self.m_mapcalc = pymod.Module('r.mapcalc')
 
66
        self.m_mremove = pymod.Module('g.remove')
 
67
 
 
68
    def parse(self, expression, basename = None, overwrite=False):
 
69
        # Check for space time dataset type definitions from temporal algebra
 
70
        l = TemporalRasterAlgebraLexer()
 
71
        l.build()
 
72
        l.lexer.input(expression)
 
73
 
 
74
        while True:
 
75
            tok = l.lexer.token()
 
76
            if not tok: break
 
77
            
 
78
            if tok.type == "STVDS" or tok.type == "STRDS" or tok.type == "STR3DS":
 
79
                raise SyntaxError("Syntax error near '%s'" %(tok.type))
 
80
        
 
81
        self.lexer = TemporalRasterAlgebraLexer()
 
82
        self.lexer.build()
 
83
        self.parser = yacc.yacc(module=self, debug=self.debug)
 
84
 
 
85
        self.overwrite = overwrite
 
86
        self.count = 0
 
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)
 
93
 
 
94
    ######################### Temporal functions ##############################
 
95
 
 
96
    def p_statement_assign(self, t):
 
97
        # The expression should always return a list of maps.
 
98
        """
 
99
        statement : stds EQUALS expr
 
100
        """
 
101
        TemporalRasterBaseAlgebraParser.p_statement_assign(self, t)
 
102
 
 
103
    def p_ts_neighbour_operation(self, t):
 
104
        # Examples:
 
105
        # A[1,0]
 
106
        # B[-2]
 
107
        # C[-2,1,3]
 
108
        """
 
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
 
112
        """
 
113
        # Check input stds.
 
114
        maplist = self.check_stds(t[1])
 
115
        row_neigbour = None
 
116
        col_neigbour = None
 
117
        if len(t) == 5:
 
118
            t_neighbour = t[3]
 
119
        elif len(t) == 7:
 
120
            t_neighbour = 0
 
121
            row_neigbour = t[3]
 
122
            col_neigbour = t[5]
 
123
        elif len(t) == 9:
 
124
            t_neighbour = t[7]
 
125
            row_neigbour = t[3]
 
126
            col_neigbour = t[5]
 
127
        if self.run:
 
128
            resultlist = []
 
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)
 
154
 
 
155
            t[0] = resultlist
 
156
 
 
157
###############################################################################
 
158
 
 
159
if __name__ == "__main__":
 
160
    import doctest
 
161
    doctest.testmod()
 
162
 
 
163