Debian Codegraph @

raster/r.horizon/testsuite/test_r_horizon_parallel.py

raw
1 """
2 TEST: test_r_horizon.py
3
4 AUTHOR(S): Anna Petrasova <kratochanna at gmail>
5
6 PURPOSE: Test r.horizon
7
8 COPYRIGHT: (C) 2015-2024 Anna Petrasova
9
10 This program is free software under the GNU General Public
11 License (>=v2). Read the file COPYING that comes with GRASS
12 for details.
13 """
14
15 import json
16
17 from grass.gunittest.case import TestCase
18 from grass.gunittest.main import test
19 from grass.gunittest.gmodules import SimpleModule
20 from grass.script import raster_what
21
22
23 ref1 = """azimuth,horizon_height
24 180.000000,0.023101
25 """
26
27 ref2 = """azimuth,horizon_height
28 180.000000,0.023101
29 200.000000,0.034850
30 220.000000,0.050549
31 240.000000,0.048211
32 260.000000,0.053101
33 280.000000,0.039774
34 300.000000,0.032360
35 320.000000,0.014804
36 340.000000,0.000000
37 360.000000,0.004724
38 20.000000,0.012612
39 40.000000,0.015207
40 60.000000,0.014344
41 80.000000,0.011044
42 100.000000,0.012192
43 120.000000,0.007462
44 140.000000,0.004071
45 160.000000,0.015356
46 """
47
48 ref3 = """azimuth,horizon_height
49 180.000000,0.023101
50 200.000000,0.034850
51 220.000000,0.050549
52 240.000000,0.048211
53 260.000000,0.053101
54 280.000000,0.039774
55 300.000000,0.032360
56 320.000000,0.014804
57 340.000000,0.000000
58 360.000000,0.004724
59 20.000000,0.012612
60 40.000000,0.015207
61 60.000000,0.014344
62 80.000000,0.011044
63 100.000000,0.012192
64 120.000000,0.007462
65 140.000000,0.004071
66 160.000000,0.015356
67 """
68
69 ref4 = """azimuth,horizon_height
70 0.000000,0.197017
71 20.000000,0.196832
72 40.000000,0.196875
73 60.000000,0.196689
74 80.000000,0.196847
75 100.000000,0.196645
76 120.000000,0.196969
77 140.000000,0.196778
78 160.000000,0.196863
79 180.000000,0.197017
80 200.000000,0.196832
81 220.000000,0.196875
82 240.000000,0.196689
83 260.000000,0.196847
84 280.000000,0.196645
85 300.000000,0.196969
86 320.000000,0.196778
87 340.000000,0.196863
88 """
89
90 ref5 = """azimuth,horizon_height,horizon_distance
91 0.000000,0.197017,5000.040000
92 20.000000,0.196832,5004.837660
93 40.000000,0.196875,5003.728610
94 60.000000,0.196689,5008.552685
95 80.000000,0.196847,5004.448022
96 100.000000,0.196645,5009.690609
97 120.000000,0.196969,5001.279836
98 140.000000,0.196778,5006.246099
99 160.000000,0.196863,5004.018385
100 180.000000,0.197017,5000.040000
101 200.000000,0.196832,5004.837660
102 220.000000,0.196875,5003.728610
103 240.000000,0.196689,5008.552685
104 260.000000,0.196847,5004.448022
105 280.000000,0.196645,5009.690609
106 300.000000,0.196969,5001.279836
107 320.000000,0.196778,5006.246099
108 340.000000,0.196863,5004.018385
109 """
110
111 ref6 = """azimuth,horizon_height,horizon_distance
112 180.000000,0.023101,420.000000
113 200.000000,0.034850,436.577599
114 220.000000,0.050549,184.390889
115 240.000000,0.048211,197.230829
116 260.000000,0.053101,162.788206
117 280.000000,0.039774,253.179778
118 300.000000,0.032360,277.848880
119 320.000000,0.014804,262.488095
120 340.000000,0.000000,0.000000
121 360.000000,0.004724,2780.017986
122 20.000000,0.012612,1148.259553
123 40.000000,0.015207,1334.166406
124 60.000000,0.014344,1867.966809
125 80.000000,0.011044,2964.203097
126 100.000000,0.012192,1828.223181
127 120.000000,0.007462,4270.667395
128 140.000000,0.004071,5659.231397
129 160.000000,0.015356,1666.883319
130 """
131
132
133 class TestHorizon(TestCase):
134 circle = "circle"
135 horizon = "test_horizon_from_elevation"
136 horizon_output = "test_horizon_output_from_elevation"
137
138 @classmethod
139 def setUpClass(cls):
140 cls.use_temp_region()
141 cls.runModule("g.region", raster="elevation")
142 cls.runModule(
143 "r.circle",
144 flags="b",
145 output=cls.circle,
146 coordinates=(637505, 221755),
147 min=5000,
148 multiplier=1000,
149 )
150 cls.runModule("r.null", map=cls.circle, null=0)
151
152 @classmethod
153 def tearDownClass(cls):
154 cls.runModule("g.remove", flags="f", type="raster", name=cls.circle)
155 cls.del_temp_region()
156
157 def setUp(self):
158 self.runModule("g.region", raster="elevation")
159
160 def tearDown(self):
161 """Remove horizon map after each test method"""
162 self.runModule("g.remove", flags="f", type="raster", name=self.horizon)
163 self.runModule(
164 "g.remove", flags="f", type="raster", pattern=self.horizon_output + "*"
165 )
166
167 def test_point_mode_one_direction(self):
168 """Test mode with 1 point and 1 direction"""
169 module = SimpleModule(
170 "r.horizon",
171 elevation="elevation",
172 coordinates=(634720, 216180),
173 output=self.horizon,
174 direction=180,
175 step=0,
176 nprocs=4,
177 )
178 self.assertModule(module)
179 stdout = module.outputs.stdout
180 self.assertMultiLineEqual(first=ref1, second=stdout)
181
182 def test_point_mode_multiple_direction(self):
183 """Test mode with 1 point and multiple directions"""
184 module = SimpleModule(
185 "r.horizon",
186 elevation="elevation",
187 coordinates=(634720, 216180),
188 output=self.horizon,
189 direction=180,
190 step=20,
191 nprocs=4,
192 )
193 self.assertModule(module)
194 stdout = module.outputs.stdout
195 self.assertMultiLineEqual(first=ref2, second=stdout)
196
197 # include nulls along the edge
198 self.runModule("g.region", raster="elevation", w="w-100")
199 self.assertModule(module)
200 stdout = module.outputs.stdout
201 self.assertMultiLineEqual(first=ref2, second=stdout)
202
203 def test_point_mode_multiple_points_and_directions(self):
204 """Test mode with 2 identical points and multiple directions"""
205 module = SimpleModule(
206 "r.horizon",
207 elevation="elevation",
208 coordinates=(634720, 216180, 634720, 216180),
209 output=self.horizon,
210 direction=180,
211 step=20,
212 nprocs=4,
213 )
214 self.assertModule(module)
215 stdout = module.outputs.stdout
216 self.assertMultiLineEqual(first=ref2 + ref2, second=stdout)
217
218 def test_point_mode_multiple_direction_json(self):
219 """Test mode with 1 point and multiple directions with JSON"""
220 module = SimpleModule(
221 "r.horizon",
222 elevation="elevation",
223 coordinates=(634720, 216180),
224 output=self.horizon,
225 direction=180,
226 step=20,
227 nprocs=4,
228 format="json",
229 )
230 self.assertModule(module)
231 stdout = json.loads(module.outputs.stdout)
232 horizons = []
233 reference = {}
234 for line in ref6.splitlines()[1:]:
235 azimuth, horizon, distance = line.split(",")
236 horizons.append(
237 {
238 "azimuth": float(azimuth),
239 "angle": float(horizon),
240 "distance": float(distance),
241 }
242 )
243 reference["x"] = 634720.0
244 reference["y"] = 216180.0
245 reference["horizons"] = horizons
246
247 self.assertListEqual([reference], stdout)
248
249 def test_point_mode_multiple_points_and_directions_json(self):
250 """Test mode with 2 identical points and multiple directions with JSON"""
251 module = SimpleModule(
252 "r.horizon",
253 elevation="elevation",
254 coordinates=(634720, 216180, 634720, 216180),
255 output=self.horizon,
256 direction=180,
257 step=20,
258 nprocs=4,
259 format="json",
260 )
261 self.assertModule(module)
262 stdout = json.loads(module.outputs.stdout)
263 horizons = []
264 reference = {}
265 for line in ref6.splitlines()[1:]:
266 azimuth, horizon, distance = line.split(",")
267 horizons.append(
268 {
269 "azimuth": float(azimuth),
270 "angle": float(horizon),
271 "distance": float(distance),
272 }
273 )
274 reference["x"] = 634720.0
275 reference["y"] = 216180.0
276 reference["horizons"] = horizons
277
278 self.assertListEqual([reference, reference], stdout)
279
280 def test_point_mode_multiple_direction_artificial(self):
281 """Test mode with 1 point and multiple directions with artificial surface"""
282 module = SimpleModule(
283 "r.horizon",
284 elevation=self.circle,
285 coordinates=(637505, 221755),
286 output=self.horizon,
287 direction=0,
288 step=20,
289 nprocs=4,
290 )
291 self.assertModule(module)
292 stdout = module.outputs.stdout
293 self.assertMultiLineEqual(first=ref4, second=stdout)
294
295 def test_point_mode_multiple_direction_artificial_distance(self):
296 """With 1 point, more directions on artificial surface, distance in output"""
297 module = SimpleModule(
298 "r.horizon",
299 elevation=self.circle,
300 coordinates=(637505, 221755),
301 output=self.horizon,
302 direction=0,
303 step=20,
304 nprocs=4,
305 flags="l",
306 )
307 self.assertModule(module)
308 stdout = module.outputs.stdout
309 self.assertMultiLineEqual(first=ref5, second=stdout)
310
311 module = SimpleModule(
312 "r.horizon",
313 elevation=self.circle,
314 coordinates=(637505, 221755),
315 output=self.horizon,
316 direction=0,
317 step=20,
318 nprocs=4,
319 flags="l",
320 format="json",
321 )
322 self.assertModule(module)
323 stdout = json.loads(module.outputs.stdout)
324 horizons = []
325 reference = {}
326 for line in ref5.splitlines()[1:]:
327 azimuth, horizon, distance = line.split(",")
328 horizons.append(
329 {
330 "azimuth": float(azimuth),
331 "angle": float(horizon),
332 "distance": float(distance),
333 }
334 )
335 reference["x"] = 637505.0
336 reference["y"] = 221755.0
337 reference["horizons"] = horizons
338
339 self.assertListEqual([reference], stdout)
340
341 def test_raster_mode_one_direction(self):
342 """Test mode with one direction and against point mode"""
343 module = SimpleModule(
344 "r.horizon",
345 elevation="elevation",
346 output=self.horizon_output,
347 direction=50,
348 nprocs=4,
349 )
350 self.assertModule(module)
351 ref = {
352 "min": 0,
353 "max": 0.70678365230560,
354 "stddev": 0.0360724286360789,
355 }
356 output = "test_horizon_output_from_elevation_050"
357 self.assertRasterFitsUnivar(
358 raster=output,
359 reference=ref,
360 precision=1e6,
361 )
362
363 # test if point mode matches raster mode
364 coordinates = [
365 (634725, 216185),
366 (633315, 217595),
367 (633555, 223405),
368 (639955, 220605),
369 (637505, 219705),
370 (641105, 222225),
371 ]
372 for coordinate in coordinates:
373 module = SimpleModule(
374 "r.horizon",
375 elevation="elevation",
376 coordinates=coordinate,
377 output=self.horizon,
378 direction=50,
379 step=0,
380 )
381 self.assertModule(module)
382 stdout = module.outputs.stdout
383 first = float(stdout.splitlines()[-1].split(",")[-1])
384 what = raster_what(output, coord=coordinate)
385 second = float(what[0][output]["value"])
386 self.assertAlmostEqual(first=first, second=second, delta=0.000001)
387
388 def test_raster_mode_multiple_direction(self):
389 self.runModule("g.region", raster="elevation", res=100)
390 module = SimpleModule(
391 "r.horizon",
392 elevation="elevation",
393 output=self.horizon_output,
394 start=10,
395 end=50,
396 step=15.512,
397 nprocs=4,
398 )
399 self.assertModule(module)
400 module_list = SimpleModule(
401 "g.list", type="raster", pattern=self.horizon_output + "*"
402 )
403 self.runModule(module_list)
404 stdout = module_list.outputs.stdout.strip()
405 self.assertMultiLineEqual(
406 first=(
407 "test_horizon_output_from_elevation_010_000\n"
408 "test_horizon_output_from_elevation_025_512\n"
409 "test_horizon_output_from_elevation_041_024"
410 ),
411 second=stdout,
412 )
413
414 def test_raster_mode_multiple_direction_offset(self):
415 self.runModule("g.region", raster="elevation", res=100)
416 module = SimpleModule(
417 "r.horizon",
418 elevation="elevation",
419 output=self.horizon_output,
420 start=10,
421 end=50,
422 step=15.512,
423 nprocs=4,
424 direction=80,
425 )
426 self.assertModule(module)
427 module_list = SimpleModule(
428 "g.list", type="raster", pattern=self.horizon_output + "*"
429 )
430 self.runModule(module_list)
431 stdout = module_list.outputs.stdout.strip()
432 self.assertMultiLineEqual(
433 first=(
434 "test_horizon_output_from_elevation_090_000\n"
435 "test_horizon_output_from_elevation_105_512\n"
436 "test_horizon_output_from_elevation_121_024"
437 ),
438 second=stdout,
439 )
440
441 def test_raster_mode_bufferzone(self):
442 """Test buffer 100 m and 109 m with resolution 10 gives the same result"""
443 self.runModule(
444 "g.region",
445 raster="elevation",
446 n="n-5000",
447 s="s+5000",
448 e="e-5000",
449 w="w+5000",
450 )
451 # raises ValueError from pygrass parameter check
452 self.assertRaises(
453 ValueError,
454 SimpleModule,
455 "r.horizon",
456 elevation="elevation",
457 output=self.horizon_output,
458 direction=50,
459 bufferzone=-100,
460 nprocs=4,
461 )
462 self.assertRaises(
463 ValueError,
464 SimpleModule,
465 "r.horizon",
466 elevation="elevation",
467 output=self.horizon_output,
468 direction=50,
469 e_buff=100,
470 n_buff=0,
471 s_buff=-100,
472 w_buff=-100,
473 nprocs=4,
474 )
475 module = SimpleModule(
476 "r.horizon",
477 elevation="elevation",
478 output=self.horizon_output,
479 direction=50,
480 bufferzone=100,
481 nprocs=4,
482 )
483 self.assertModule(module)
484 ref = {
485 "mean": 0.0344791,
486 }
487 output = "test_horizon_output_from_elevation_050"
488 self.assertRasterFitsUnivar(
489 raster=output,
490 reference=ref,
491 precision=1e-6,
492 )
493 module = SimpleModule(
494 "r.horizon",
495 elevation="elevation",
496 output=self.horizon_output,
497 direction=50,
498 bufferzone=103,
499 nprocs=4,
500 )
501 self.assertModule(module)
502 self.assertRasterFitsUnivar(
503 raster=output,
504 reference=ref,
505 precision=1e-6,
506 )
507 module = SimpleModule(
508 "r.horizon",
509 elevation="elevation",
510 output=self.horizon_output,
511 direction=50,
512 bufferzone=95,
513 nprocs=4,
514 )
515 self.assertModule(module)
516 ref = {
517 "mean": 0.0344624,
518 }
519 self.assertRasterFitsUnivar(
520 raster=output,
521 reference=ref,
522 precision=1e-6,
523 )
524 self.runModule("g.region", raster="elevation")
525
526
527 if __name__ == "__main__":
528 test()