test_what.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. """Test t.rast.what
  2. (C) 2014 by the GRASS Development Team
  3. This program is free software under the GNU General Public
  4. License (>=v2). Read the file COPYING that comes with GRASS
  5. for details.
  6. @author Soeren Gebbert
  7. """
  8. from grass.gunittest.case import TestCase
  9. from grass.gunittest.gmodules import SimpleModule
  10. class TestRasterWhat(TestCase):
  11. @classmethod
  12. def setUpClass(cls):
  13. """Initiate the temporal GIS and set the region
  14. """
  15. cls.use_temp_region()
  16. cls.runModule("g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10,
  17. res3=10)
  18. cls.runModule("r.mapcalc", expression="a_1 = 100", overwrite=True)
  19. cls.runModule("r.mapcalc", expression="a_2 = 200", overwrite=True)
  20. cls.runModule("r.mapcalc", expression="a_3 = 300", overwrite=True)
  21. cls.runModule("r.mapcalc", expression="a_4 = 400", overwrite=True)
  22. cls.runModule("v.random", output="points", npoints=3, seed=1,
  23. overwrite=True)
  24. cls.runModule("t.create", type="strds", temporaltype="absolute",
  25. output="A", title="A test", description="A test",
  26. overwrite=True)
  27. cls.runModule("t.register", flags="i", type="raster", input="A",
  28. maps="a_1,a_2,a_3,a_4", start="2001-01-01",
  29. increment="3 months", overwrite=True)
  30. @classmethod
  31. def tearDownClass(cls):
  32. """Remove the temporary region
  33. """
  34. cls.runModule("t.remove", flags="rf", type="strds", inputs="A")
  35. cls.del_temp_region()
  36. def test_row_output(self):
  37. self.assertModule("t.rast.what", strds="A", output="out_row.txt",
  38. points="points", flags="n", layout="row",
  39. nprocs=1, overwrite=True, verbose=True)
  40. self.assertFileMd5("out_row.txt", "55209718566d70e1427bd1fecf844d53",
  41. text=True)
  42. def test_row_output_coords(self):
  43. self.assertModule("t.rast.what", strds="A", output="out_row_coords.txt",
  44. coordinates=(30, 30, 45, 45), flags="n", layout="row",
  45. nprocs=1, overwrite=True, verbose=True)
  46. self.assertFileMd5("out_row_coords.txt",
  47. "cd917ac4848786f1b944512eed1da5bc", text=True)
  48. def test_row_output_coords_stdin(self):
  49. self.assertModule("t.rast.what", strds="A", output="out_row_coords.txt",
  50. flags="ni", layout="row", stdin_="30 30\n45 45",
  51. nprocs=1, overwrite=True, verbose=True)
  52. self.assertFileMd5("out_row_coords.txt",
  53. "cd917ac4848786f1b944512eed1da5bc", text=True)
  54. def test_row_output_cat(self):
  55. self.assertModule("t.rast.what", strds="A", output="out_row_cat.txt",
  56. points="points", flags="nv", layout="row",
  57. nprocs=1, overwrite=True, verbose=True)
  58. self.assertFileMd5("out_row_cat.txt",
  59. "bddde54bf4f40c41a17305c3442980e5", text=True)
  60. def test_col_output(self):
  61. self.assertModule("t.rast.what", strds="A", output="out_col.txt",
  62. points="points", flags="n", layout="col",
  63. nprocs=1, overwrite=True, verbose=True)
  64. self.assertFileMd5("out_col.txt",
  65. "885b6f50405b08fa9fe9ae33ed50e29b", text=True)
  66. def test_col_output_coords(self):
  67. self.assertModule("t.rast.what", strds="A", output="out_col_coords.txt",
  68. coordinates=(30, 30, 45, 45), flags="n", layout="col",
  69. nprocs=1, overwrite=True, verbose=True)
  70. self.assertFileMd5("out_col_coords.txt",
  71. "ecdc79a6880a9e1f163cc92fa384b8a3", text=True)
  72. def test_timerow_output(self):
  73. self.assertModule("t.rast.what", strds="A", output="out_timerow.txt",
  74. points="points", flags="n", layout="timerow",
  75. nprocs=1, overwrite=True, verbose=True)
  76. self.assertFileMd5("out_timerow.txt",
  77. "129fe0b63019e505232efa20ad42c03a", text=True)
  78. def test_col_output_cat(self):
  79. self.assertModule("t.rast.what", strds="A", output="out_col_cat.txt",
  80. points="points", flags="nv", layout="col",
  81. nprocs=1, overwrite=True, verbose=True)
  82. self.assertFileMd5("out_col_cat.txt",
  83. "ac0a9b14e59920c3f8b5834282a24822", text=True)
  84. def test_timerow_output_cat(self):
  85. self.assertModule("t.rast.what", strds="A", output="out_col_trow.txt",
  86. points="points", flags="nv", layout="timerow",
  87. nprocs=1, overwrite=True, verbose=True)
  88. self.assertFileMd5("out_col_trow.txt",
  89. "1da3350f488df9c919b4009625956b3b", text=True)
  90. def test_timerow_output_coords(self):
  91. self.assertModule("t.rast.what", strds="A", output="out_timerow_coords.txt",
  92. coordinates=(30, 30, 45, 45), flags="n", layout="timerow",
  93. nprocs=1, overwrite=True, verbose=True)
  94. self.assertFileMd5("out_timerow_coords.txt",
  95. "ca4ee0e7e4aaca170d6034e0d57d292d", text=True)
  96. def test_row_stdout_where_parallel(self):
  97. t_rast_what = SimpleModule("t.rast.what", strds="A", output="-",
  98. points="points", flags="n",
  99. where="start_time > '2001-03-01'",
  100. nprocs=4, overwrite=True, verbose=True)
  101. self.assertModule(t_rast_what)
  102. text = """x|y|start|end|value
  103. 115.0043586274|36.3593955783|2001-04-01 00:00:00|2001-07-01 00:00:00|200
  104. 79.6816763826|45.2391522853|2001-04-01 00:00:00|2001-07-01 00:00:00|200
  105. 97.4892579600|79.2347263950|2001-04-01 00:00:00|2001-07-01 00:00:00|200
  106. 115.0043586274|36.3593955783|2001-07-01 00:00:00|2001-10-01 00:00:00|300
  107. 79.6816763826|45.2391522853|2001-07-01 00:00:00|2001-10-01 00:00:00|300
  108. 97.4892579600|79.2347263950|2001-07-01 00:00:00|2001-10-01 00:00:00|300
  109. 115.0043586274|36.3593955783|2001-10-01 00:00:00|2002-01-01 00:00:00|400
  110. 79.6816763826|45.2391522853|2001-10-01 00:00:00|2002-01-01 00:00:00|400
  111. 97.4892579600|79.2347263950|2001-10-01 00:00:00|2002-01-01 00:00:00|400
  112. """
  113. self.assertLooksLike(text, t_rast_what.outputs.stdout)
  114. def test_row_stdout_where_parallel_cat(self):
  115. t_rast_what = SimpleModule("t.rast.what", strds="A", output="-",
  116. points="points", flags="nv",
  117. where="start_time > '2001-03-01'",
  118. nprocs=4, overwrite=True, verbose=True)
  119. self.assertModule(t_rast_what)
  120. text = """cat|x|y|start|end|value
  121. 1|115.0043586274|36.3593955783|2001-04-01 00:00:00|2001-07-01 00:00:00|200
  122. 2|79.6816763826|45.2391522853|2001-04-01 00:00:00|2001-07-01 00:00:00|200
  123. 3|97.4892579600|79.2347263950|2001-04-01 00:00:00|2001-07-01 00:00:00|200
  124. 1|115.0043586274|36.3593955783|2001-07-01 00:00:00|2001-10-01 00:00:00|300
  125. 2|79.6816763826|45.2391522853|2001-07-01 00:00:00|2001-10-01 00:00:00|300
  126. 3|97.4892579600|79.2347263950|2001-07-01 00:00:00|2001-10-01 00:00:00|300
  127. 1|115.0043586274|36.3593955783|2001-10-01 00:00:00|2002-01-01 00:00:00|400
  128. 2|79.6816763826|45.2391522853|2001-10-01 00:00:00|2002-01-01 00:00:00|400
  129. 3|97.4892579600|79.2347263950|2001-10-01 00:00:00|2002-01-01 00:00:00|400
  130. """
  131. self.assertLooksLike(text, t_rast_what.outputs.stdout)
  132. def test_row_stdout_where_parallel2(self):
  133. """Here without output definition, the default is used then"""
  134. t_rast_what = SimpleModule("t.rast.what", strds="A", points="points",
  135. flags="n", where="start_time > '2001-03-01'",
  136. nprocs=4, overwrite=True, verbose=True)
  137. self.assertModule(t_rast_what)
  138. text = """x|y|start|end|value
  139. 115.0043586274|36.3593955783|2001-04-01 00:00:00|2001-07-01 00:00:00|200
  140. 79.6816763826|45.2391522853|2001-04-01 00:00:00|2001-07-01 00:00:00|200
  141. 97.4892579600|79.2347263950|2001-04-01 00:00:00|2001-07-01 00:00:00|200
  142. 115.0043586274|36.3593955783|2001-07-01 00:00:00|2001-10-01 00:00:00|300
  143. 79.6816763826|45.2391522853|2001-07-01 00:00:00|2001-10-01 00:00:00|300
  144. 97.4892579600|79.2347263950|2001-07-01 00:00:00|2001-10-01 00:00:00|300
  145. 115.0043586274|36.3593955783|2001-10-01 00:00:00|2002-01-01 00:00:00|400
  146. 79.6816763826|45.2391522853|2001-10-01 00:00:00|2002-01-01 00:00:00|400
  147. 97.4892579600|79.2347263950|2001-10-01 00:00:00|2002-01-01 00:00:00|400
  148. """
  149. self.assertLooksLike(text, t_rast_what.outputs.stdout)
  150. def test_row_output_where_parallel(self):
  151. self.assertModule("t.rast.what", strds="A", output="out_where.txt",
  152. points="points", flags="n",
  153. where="start_time > '2001-03-01'",
  154. nprocs=4, overwrite=True, verbose=True)
  155. self.assertFileMd5("out_where.txt",
  156. "af731bec01fedc262f4ac162fe420707", text=True)
  157. def test_empty_strds(self):
  158. self.assertModuleFail("t.rast.what", strds="A", output="out_error.txt",
  159. points="points", flags="n",
  160. where="start_time > '2002-03-01'",
  161. nprocs=4, overwrite=True, verbose=True)
  162. class TestRasterWhatFails(TestCase):
  163. def test_error_handling(self):
  164. # No vector map, no strds, no coordinates
  165. self.assertModuleFail("t.rast.what", output="out.txt")
  166. # No vector map, no coordinates
  167. self.assertModuleFail("t.rast.what", strds="A", output="out.txt")
  168. # Points and coordinates are mutually exclusive
  169. self.assertModuleFail("t.rast.what", points="points", strds="A",
  170. coordinates=(30, 30, 45, 45), output="out.txt")
  171. class TestRasterWhatNull(TestCase):
  172. @classmethod
  173. def setUpClass(cls):
  174. """Initiate the temporal GIS and set the region
  175. """
  176. cls.use_temp_region()
  177. cls.runModule("g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10)
  178. cls.runModule("r.mapcalc", expression="a_1 = 100", overwrite=True)
  179. cls.runModule("r.mapcalc", expression="a_null = null()", overwrite=True)
  180. cls.runModule("v.random", output="points", npoints=1, seed=1, overwrite=True)
  181. cls.runModule("t.create", type="strds", temporaltype="absolute",
  182. output="A", title="A test", description="A test",
  183. overwrite=True)
  184. cls.runModule("t.register", flags="i", type="raster", input="A",
  185. maps="a_1,a_null", start="2001-01-01",
  186. increment="3 months", overwrite=True)
  187. @classmethod
  188. def tearDownClass(cls):
  189. """Remove the temporary region
  190. """
  191. cls.runModule("t.remove", flags="rf", type="strds",
  192. inputs="A")
  193. cls.del_temp_region()
  194. def test_null_value(self):
  195. """Test setting the null value
  196. """
  197. t_rast_what = SimpleModule("t.rast.what", strds="A", output="-",
  198. points="points", flags="n",
  199. where="start_time >= '2001-01-01'",
  200. null_value="NULL",
  201. nprocs=4, overwrite=True, verbose=True)
  202. self.assertModule(t_rast_what)
  203. text="""x|y|start|end|value
  204. 115.0043586274|36.3593955783|2001-01-01 00:00:00|2001-04-01 00:00:00|100
  205. 115.0043586274|36.3593955783|2001-04-01 00:00:00|2001-07-01 00:00:00|NULL
  206. """
  207. self.assertLooksLike(text, t_rast_what.outputs.stdout)
  208. if __name__ == '__main__':
  209. from grass.gunittest.main import test
  210. test()