1#!/usr/local/bin/python3.8
2# -*- coding: utf-8 -*-
3############################################################################
4#
5# MODULE:       t.rast.algebra
6# AUTHOR(S):    Thomas Leppelt, Soeren Gebbert
7#
8# PURPOSE:      Provide temporal raster algebra to perform spatial an temporal operations
9#               for space time datasets by topological relationships to other space time
10#               datasets.
11# COPYRIGHT:    (C) 2014-2017 by the GRASS Development Team
12#
13#  This program is free software; you can redistribute it and/or modify
14#  it under the terms of the GNU General Public License as published by
15#  the Free Software Foundation; either version 2 of the License, or
16#  (at your option) any later version.
17#
18#  This program is distributed in the hope that it will be useful,
19#  but WITHOUT ANY WARRANTY; without even the implied warranty of
20#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21#  GNU General Public License for more details.
22#
23#############################################################################
24
25#%module
26#% description: Apply temporal and spatial operations on space time raster datasets using temporal raster algebra.
27#% keyword: temporal
28#% keyword: algebra
29#% keyword: raster
30#% keyword: time
31#%end
32
33#%option
34#% key: expression
35#% type: string
36#% description: r.mapcalc expression for temporal and spatial analysis of space time raster datasets
37#% required : yes
38#%end
39
40#%option
41#% key: basename
42#% type: string
43#% label: Basename of the new generated output maps
44#% description: A numerical suffix separated by an underscore will be attached to create a unique identifier
45#% required: yes
46#%end
47
48#%option
49#% key: suffix
50#% type: string
51#% description: Suffix to add at basename: set 'gran' for granularity, 'time' for the full time format, 'num' for numerical suffix with a specific number of digits (default %05)
52#% answer: num
53#% required: no
54#% multiple: no
55#%end
56
57#%option
58#% key: nprocs
59#% type: integer
60#% description: Number of r.mapcalc processes to run in parallel
61#% required: no
62#% multiple: no
63#% answer: 1
64#%end
65
66#%flag
67#% key: s
68#% description: Check the spatial topology of temporally related maps and process only spatially related maps
69#%end
70
71#%flag
72#% key: n
73#% description: Register Null maps
74#%end
75
76#%flag
77#% key: g
78#% description: Use granularity sampling instead of the temporal topology approach
79#%end
80
81#%flag
82#% key: d
83#% description: Perform a dry run, compute all dependencies and module calls but don't run them
84#%end
85
86import grass.script
87import sys
88
89
90def main():
91    # lazy imports
92    import grass.temporal as tgis
93
94    expression = options['expression']
95    basename = options['basename']
96    nprocs = options["nprocs"]
97    time_suffix = options["suffix"]
98    spatial = flags["s"]
99    register_null = flags["n"]
100    granularity = flags["g"]
101    dry_run = flags["d"]
102
103    # Check for PLY istallation
104    try:
105        import ply.lex as lex
106        import ply.yacc as yacc
107    except:
108        grass.script.fatal(_("Please install PLY (Lex and Yacc Python implementation) to use the temporal algebra modules. "
109                             "You can use t.rast.mapcalc that provides a limited but useful alternative to "
110                             "t.rast.algebra without PLY requirement."))
111
112    tgis.init(True)
113    p = tgis.TemporalRasterAlgebraParser(run = True,
114                                         debug=False,
115                                         spatial=spatial,
116                                         nprocs=nprocs,
117                                         register_null=register_null,
118                                         dry_run=dry_run, time_suffix=time_suffix)
119
120    if granularity:
121        if not p.setup_common_granularity(expression=expression,  lexer = tgis.TemporalRasterAlgebraLexer()):
122            grass.script.fatal(_("Unable to process the expression in granularity algebra mode"))
123
124    pc = p.parse(expression, basename, grass.script.overwrite())
125
126    if dry_run is True:
127        import pprint
128        pprint.pprint(pc)
129
130if __name__ == "__main__":
131    options, flags = grass.script.parser()
132    sys.exit(main())
133
134