test_a_b_coeff.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: test_small_data
  5. # AUTHOR: Vaclav Petras
  6. # PURPOSE: Fast test using a small example
  7. # COPYRIGHT: (C) 2016 by Vaclav Petras and the GRASS Development Team
  8. #
  9. # This program is free software under the GNU General Public
  10. # License (>=v2). Read the file COPYING that comes with GRASS
  11. # for details.
  12. #
  13. #############################################################################
  14. from grass.gunittest.case import TestCase
  15. from grass.gunittest.main import test
  16. from grass.script import list_strings
  17. # generated by
  18. # g.region n=12 s=9 e=21 w=18 t=8 b=4 res=1 res3=1 -p3
  19. # r3.mapcalc "x = rand(0,10)" seed=100 && r3.out.ascii x prec=0
  20. INPUT = """\
  21. version: grass7
  22. order: nsbt
  23. north: 12
  24. south: 9
  25. east: 21
  26. west: 18
  27. top: 8
  28. bottom: 4
  29. rows: 3
  30. cols: 3
  31. levels: 4
  32. 6 5 1
  33. 0 7 5
  34. 1 7 1
  35. 8 2 1
  36. 3 4 2
  37. 8 5 6
  38. 1 2 8
  39. 1 5 5
  40. 1 1 3
  41. 1 8 3
  42. 6 5 1
  43. 5 1 7
  44. """
  45. # created from the above and template from
  46. # r.mapcalc "x = rand(0,10)" seed=100 && r.out.ascii x prec=0
  47. OUTPUTS = [
  48. """\
  49. north: 12
  50. south: 9
  51. east: 21
  52. west: 18
  53. rows: 3
  54. cols: 3
  55. 12.5 10.5 2.5
  56. 0.5 14.5 10.5
  57. 2.5 14.5 2.5
  58. """,
  59. """\
  60. north: 12
  61. south: 9
  62. east: 21
  63. west: 18
  64. rows: 3
  65. cols: 3
  66. 16.5 4.5 2.5
  67. 6.5 8.5 4.5
  68. 16.5 10.5 12.5
  69. """,
  70. """\
  71. north: 12
  72. south: 9
  73. east: 21
  74. west: 18
  75. rows: 3
  76. cols: 3
  77. 2.5 4.5 16.5
  78. 2.5 10.5 10.5
  79. 2.5 2.5 6.5
  80. """,
  81. """\
  82. north: 12
  83. south: 9
  84. east: 21
  85. west: 18
  86. rows: 3
  87. cols: 3
  88. 2.5 16.5 6.5
  89. 12.5 10.5 2.5
  90. 10.5 2.5 14.5
  91. """,
  92. ]
  93. class TestR3ToRast(TestCase):
  94. # TODO: replace by unified handing of maps
  95. # mixing class and object attributes
  96. to_remove_3d = []
  97. to_remove_2d = []
  98. rast3d = "r3_to_rast_test_a_b_coeff"
  99. rast2d = "r3_to_rast_test_a_b_coeff"
  100. rast2d_ref = "r3_to_rast_test_a_b_coeff_ref"
  101. rast2d_refs = []
  102. def setUp(self):
  103. self.use_temp_region()
  104. self.runModule("r3.in.ascii", input="-", stdin_=INPUT, output=self.rast3d)
  105. self.to_remove_3d.append(self.rast3d)
  106. self.runModule("g.region", raster_3d=self.rast3d)
  107. for i, data in enumerate(OUTPUTS):
  108. rast = "%s_%d" % (self.rast2d_ref, i)
  109. self.runModule("r.in.ascii", input="-", stdin_=data, output=rast)
  110. self.to_remove_2d.append(rast)
  111. self.rast2d_refs.append(rast)
  112. def tearDown(self):
  113. if self.to_remove_3d:
  114. self.runModule(
  115. "g.remove",
  116. flags="f",
  117. type="raster_3d",
  118. name=",".join(self.to_remove_3d),
  119. verbose=True,
  120. )
  121. if self.to_remove_2d:
  122. self.runModule(
  123. "g.remove",
  124. flags="f",
  125. type="raster",
  126. name=",".join(self.to_remove_2d),
  127. verbose=True,
  128. )
  129. self.del_temp_region()
  130. def test_a_b_coeff(self):
  131. self.assertModule(
  132. "r3.to.rast", input=self.rast3d, output=self.rast2d, multiply=2, add=0.5
  133. )
  134. rasts = list_strings(
  135. "raster",
  136. mapset=".",
  137. pattern="%s_*" % self.rast2d,
  138. exclude="%s_*" % self.rast2d_ref,
  139. )
  140. self.assertEquals(
  141. len(rasts), 4, msg="Wrong number of 2D rasters present" " in the mapset"
  142. )
  143. ref_info = dict(cells=9)
  144. ref_univar = dict(cells=9, null_cells=0)
  145. for rast in rasts:
  146. self.assertRasterExists(rast)
  147. # the following doesn't make much sense because we just listed them
  148. self.to_remove_2d.append(rast)
  149. self.assertRasterFitsInfo(raster=rast, reference=ref_info, precision=0)
  150. self.assertRasterFitsUnivar(raster=rast, reference=ref_univar, precision=0)
  151. # check the actual values
  152. for rast_ref, rast in zip(self.rast2d_refs, rasts):
  153. self.assertRastersNoDifference(
  154. actual=rast, reference=rast_ref, precision=0.1
  155. )
  156. if __name__ == "__main__":
  157. test()