-
Notifications
You must be signed in to change notification settings - Fork 147
/
r.mapcalc.tiled.py
executable file
·224 lines (207 loc) · 5.86 KB
/
r.mapcalc.tiled.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
############################################################################
#
# MODULE: r.mapcalc.tiled
# AUTHOR(S): Moritz Lennert
#
# PURPOSE: Run r.mapcalc over tiles of the input map
# to allow parallel processing
# COPYRIGHT: (C) 2019 by the GRASS Development Team
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#############################################################################
# %Module
# % description: Runs r.mapcalc in parallel over tiles.
# % keyword: raster
# % keyword: algebra
# % keyword: tiling
# %end
#
# %option
# % key: expression
# % type: string
# % description: Expression to send to r.mapcalc
# % required: yes
# %end
#
# %option G_OPT_R_OUTPUT
# % description: Name of raster output map resulting from expression
# % key: output
# % required : no
# %end
#
# %option
# % key: width
# % type: integer
# % description: Width of tiles (columns)
# % required: no
# %end
#
# %option
# % key: height
# % type: integer
# % description: Height of tiles (rows)
# % required: no
# %end
#
# %option
# % key: overlap
# % type: integer
# % description: Overlap of tiles
# % answer: 0
# % required: yes
# %end
#
# %option
# % key: nprocs
# % type: integer
# % description: Number of r.mapcalc processes to run in parallel
# % required: no
# % options: 1-
# %end
#
# %option
# % key: processes
# % type: integer
# % description: Number of r.mapcalc processes to run in parallel, use nprocs option instead
# % label: This option is obsolete and replaced by nprocs
# % required: no
# % options: 1-
# %end
#
# %option
# % key: mapset_prefix
# % type: string
# % description: Mapset prefix
# % required: no
# %end
#
# %option
# % key: patch_backend
# % type: string
# % label: Backend for patching computed tiles
# % description: If backend is not specified, original serial implementation with RasterRow is used
# % options: RasterRow,r.patch
# % descriptions: RasterRow; serial patching with PyGRASS RasterRow; r.patch; parallelized r.patch (with zero overlap only)
# % required: no
# %end
#
# %rules
# % exclusive: processes,nprocs
# %end
import grass.script as gscript
from grass.pygrass.modules.grid.grid import (
GridModule,
Location,
split_region_tiles,
rpatch_map,
)
try:
parallel_rpatch_available = True
from grass.pygrass.modules.grid.grid import rpatch_map_r_patch_backend
except ImportError:
parallel_rpatch_available = False
class MyGridModule(GridModule):
"""inherit GridModule, but handle the fact that the output name is in the expression"""
def patch(self):
"""Patch the final results."""
bboxes = split_region_tiles(width=self.width, height=self.height)
output_map = self.out_prefix[:]
self.out_prefix = ""
if self.patch_backend == "RasterRow":
rpatch_map(
raster=output_map,
mapset=self.mset.name,
mset_str=self.msetstr,
bbox_list=bboxes,
overwrite=self.module.flags.overwrite,
start_row=self.start_row,
start_col=self.start_col,
prefix=self.out_prefix,
)
else:
rpatch_map_r_patch_backend(
raster=output_map,
mset_str=self.msetstr,
bbox_list=bboxes,
overwrite=self.module.flags.overwrite,
start_row=self.start_row,
start_col=self.start_col,
prefix=self.out_prefix,
processes=self.processes,
)
def main():
expression = options["expression"]
width = options["width"]
height = options["height"]
# v8.2 GridModule doesn't require tile size anymore
# this is proxy for v8.2
# can be removed in v9.0
if width:
width = int(width)
if height:
height = int(height)
if not parallel_rpatch_available:
warning = False
if not width:
width = 1000
warning = True
if not height:
height = 1000
warning = True
if warning:
# square tiles tend to be slower than horizontal slices
gscript.warning(
_(
"No tile width or height provided, default tile size set: {h} rows x {w} cols."
).format(h=height, w=width)
)
overlap = int(options["overlap"])
processes = options["nprocs"]
patch_backend = options["patch_backend"]
if not processes:
processes = options["processes"]
if processes:
gscript.warning(_("Option processes is obsolete, use nprocs instead."))
try:
processes = int(processes)
except ValueError:
processes = 1
output = None
if options["output"]:
output = options["output"]
mapset_prefix = None
if options["mapset_prefix"]:
mapset_prefix = options["mapset_prefix"]
kwargs = {"expression": expression, "quiet": True}
if not parallel_rpatch_available and patch_backend == "r.patch":
gs.warning(
_(
"r.patch backend is not available in this version of GRASS GIS, using RasterRow"
)
)
if patch_backend == "r.patch" and overlap > 0:
gs.fatal(_("Patching backend 'r.patch' doesn't work for overlap > 0"))
if parallel_rpatch_available:
kwargs["patch_backend"] = patch_backend
if output:
output_mapname = output
else:
output_mapname = expression.split("=")[0].strip()
grd = MyGridModule(
"r.mapcalc",
width=width,
height=height,
overlap=overlap,
processes=processes,
split=False,
mapset_prefix=mapset_prefix,
out_prefix=output_mapname,
**kwargs,
)
grd.run()
if __name__ == "__main__":
options, flags = gscript.parser()
main()