1# -*- coding: utf-8 -*-
2"""
3Test of r3.flow
4
5@author Anna Petrasova
6"""
7import os
8from grass.gunittest.case import TestCase
9from grass.gunittest.main import test
10
11seeds = """
1284.80609404|35.19733594|39.43321996
1314.42915927|56.86951467|22.42338987
1429.06094033|78.06029074|39.31707858
1564.95524796|50.76614609|12.02301418
1675.47677891|18.36008965|29.362122
1788.84231714|31.23108675|34.33555293
1841.13822083|64.70413255|25.85158957
1914.15768282|45.26556161|10.63049231
2069.36315244|2.92994235|39.63663467
2195.88028571|0.80210167|28.57206445
22"""
23
24flowaccum = """
25n=480
26null_cells=0
27cells=480
28min=0
29max=89
30range=89
31mean=6.38333333333333
32mean_of_abs=6.38333333333333
33stddev=11.3061070026581
34variance=127.828055555556
35coeff_var=177.119169754436
36sum=3064
37"""
38
39class FlowlineTest(TestCase):
40
41    @classmethod
42    def setUpClass(cls):
43        """Use temporary region settings"""
44        cls.use_temp_region()
45        cls.runModule("g.region", res=10, res3=10, n=80, s=0, w=0, e=120, b=0, t=50)
46        cls.runModule("r3.mapcalc", expression="map_1 = 100")
47        cls.runModule("r3.mapcalc", expression="map_2 = -20")
48        cls.runModule("r3.mapcalc", expression="map_3 = 0.01")
49        cls.runModule("r3.mapcalc", expression="map_4 = col() + row() + depth()")
50        cls.runModule("r3.mapcalc", expression="map_5 = col() * col() + row() * row() + depth() * depth()")
51        cls.runModule('v.in.ascii', input='-', output='test_seeds', z=3, flags='zt',
52                      stdin=seeds)
53
54    @classmethod
55    def tearDownClass(cls):
56        """!Remove the temporary region"""
57        cls.del_temp_region()
58        cls.runModule('g.remove', flags='f', type='raster_3d', name=','.join(['map_1', 'map_2', 'map_3', 'map_4', 'map_5', 'test_flowaccum']))
59        cls.runModule('g.remove', flags='f', type='vector', name=','.join(['test_flowline', 'test_seeds']))
60        os.remove('./data/flowline_tmp.ascii')
61
62    def test_interpolation(self):
63        self.assertModuleKeyValue('test.r3flow', test='interpolation',
64                                  coordinates=[100, 55, 11], input=['map_1', 'map_2', 'map_3'],
65                                  reference={'return': 0, 'values': [100, -20, 0.01]},
66                                  precision=1e-10, sep='=')
67        self.assertModuleKeyValue('test.r3flow', test='interpolation',
68                                  coordinates=[5, 5, 5], input=['map_1', 'map_2', 'map_3'],
69                                  reference={'return': 0, 'values': [100, -20, 0.01]},
70                                  precision=1e-10, sep='=')
71        self.assertModuleKeyValue('test.r3flow', test='interpolation',
72                                  coordinates=[10, 10, 60], input=['map_1', 'map_2', 'map_3'],
73                                  reference={'return': -1},
74                                  precision=1e-10, sep='=')
75        self.assertModuleKeyValue('test.r3flow', test='interpolation',
76                                  coordinates=[25, 69, 17], input=['map_4', 'map_4', 'map_4'],
77                                  reference={'return': 0, 'values': [7.8, 7.8, 7.8]},
78                                  precision=1e-10, sep='=')
79        self.assertModuleKeyValue('test.r3flow', test='interpolation',
80                                  coordinates=[81, 30, 25], input=['map_4', 'map_4', 'map_4'],
81                                  reference={'return': 0, 'values': [18.1, 18.1, 18.1]},
82                                  precision=1e-10, sep='=')
83
84    def test_flowlines(self):
85        self.assertModule('r3.flow', input='map_5', flowline='test_flowline',
86                          seed_points='test_seeds', flowaccumulation='test_flowaccum',
87                          direction='down')
88        self.runModule('v.out.ascii', input='test_flowline',
89                       format='standard', output='./data/flowline_tmp.ascii',
90                       precision=6)
91        self.assertVectorAsciiEqualsVectorAscii(actual='./data/flowline_tmp.ascii',
92                                                reference='./data/flowline.ascii')
93        self.assertRaster3dFitsUnivar('test_flowaccum', reference=flowaccum, precision=1e-6)
94
95
96if __name__ == '__main__':
97    test()
98