Debian Codegraph @

raster/r.slope.aspect/benchmark/benchmark_r_slope_aspect.py

raw
1 """Benchmarking of r.slope aspect
2
3 @author Aaron Saw Min Sern
4 """
5
6 from grass.exceptions import CalledModuleError
7 from grass.pygrass.modules import Module
8 from subprocess import DEVNULL
9
10 import grass.benchmark as bm
11
12
13 def main():
14 results = []
15
16 # Users can add more or modify existing reference maps
17 benchmark(7071, "r.slope.aspect_50M", results)
18 benchmark(10000, "r.slope.aspect_100M", results)
19 benchmark(14142, "r.slope.aspect_200M", results)
20 benchmark(20000, "r.slope.aspect_400M", results)
21
22 bm.nprocs_plot(results, filename="r_slope_aspect_benchmark_size.svg")
23
24
25 def benchmark(size, label, results):
26 reference = "r_slope_aspect_reference_map"
27 slope = "benchmark_slope"
28 aspect = "benchmark_aspect"
29 pcurv = "benchmark_pcurv"
30 tcurv = "benchmark_tcurv"
31
32 generate_map(rows=size, cols=size, fname=reference)
33 module = Module(
34 "r.slope.aspect",
35 elevation=reference,
36 slope=slope,
37 aspect=aspect,
38 pcurvature=pcurv,
39 tcurvature=tcurv,
40 run_=False,
41 stdout_=DEVNULL,
42 overwrite=True,
43 )
44 results.append(bm.benchmark_nprocs(module, label=label, max_nprocs=16, repeat=3))
45 Module(
46 "g.remove",
47 quiet=True,
48 flags="f",
49 type="raster",
50 name=(reference, slope, aspect, pcurv, tcurv),
51 )
52
53
54 def generate_map(rows, cols, fname):
55 Module("g.region", flags="p", s=0, n=rows, w=0, e=cols, res=1)
56 # Generate using r.random.surface if r.surf.fractal fails
57 try:
58 print("Generating reference map using r.surf.fractal...")
59 Module("r.surf.fractal", output=fname)
60 except CalledModuleError:
61 print("r.surf.fractal fails, using r.random.surface instead...")
62 Module("r.random.surface", output=fname)
63
64
65 if __name__ == "__main__":
66 main()