test_univar.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. """Test t.rast.univar
  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 TestRasterUnivar(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=1, res3=1)
  17. cls.runModule("r.mapcalc", expression="a_1 = 100", overwrite=True)
  18. cls.runModule("r.mapcalc", expression="a_2 = 200", overwrite=True)
  19. cls.runModule("r.mapcalc", expression="a_3 = 300", overwrite=True)
  20. cls.runModule("r.mapcalc", expression="a_4 = 400", overwrite=True)
  21. cls.runModule("t.create", type="strds", temporaltype="absolute",
  22. output="A", title="A test", description="A test",
  23. overwrite=True)
  24. cls.runModule("t.register", flags="i", type="raster", input="A",
  25. maps="a_1,a_2,a_3,a_4", start="2001-01-01",
  26. increment="3 months", overwrite=True)
  27. @classmethod
  28. def tearDownClass(cls):
  29. """Remove the temporary region
  30. """
  31. cls.runModule("t.remove", flags="rf", type="strds",
  32. inputs="A")
  33. cls.del_temp_region()
  34. def test_1(self):
  35. t_rast_univar = SimpleModule("t.rast.univar", input="A",
  36. where="start_time >= '2001-01-01'",
  37. overwrite=True, verbose=True)
  38. self.runModule("g.region", res=1)
  39. self.assertModule(t_rast_univar)
  40. univar_text="""id|start|end|mean|min|max|mean_of_abs|stddev|variance|coeff_var|sum|null_cells|cells|non_null_cells
  41. a_1@testing|2001-01-01 00:00:00|2001-04-01 00:00:00|100|100|100|100|0|0|0|960000|0|9600|9600
  42. a_2@testing|2001-04-01 00:00:00|2001-07-01 00:00:00|200|200|200|200|0|0|0|1920000|0|9600|9600
  43. a_3@testing|2001-07-01 00:00:00|2001-10-01 00:00:00|300|300|300|300|0|0|0|2880000|0|9600|9600
  44. a_4@testing|2001-10-01 00:00:00|2002-01-01 00:00:00|400|400|400|400|0|0|0|3840000|0|9600|9600
  45. """
  46. for ref, res in zip(univar_text.split("\n"), t_rast_univar.outputs.stdout.split("\n")):
  47. if ref and res:
  48. ref_line = ref.split("|", 1)[1]
  49. res_line = res.split("|", 1)[1]
  50. self.assertLooksLike(ref_line, res_line)
  51. def test_2(self):
  52. t_rast_univar = SimpleModule("t.rast.univar", input="A",
  53. where="start_time >= '2001-03-01'",
  54. overwrite=True, verbose=True)
  55. self.runModule("g.region", res=1)
  56. self.assertModule(t_rast_univar)
  57. univar_text="""id|start|end|mean|min|max|mean_of_abs|stddev|variance|coeff_var|sum|null_cells|cells|non_null_cells
  58. a_2@testing|2001-04-01 00:00:00|2001-07-01 00:00:00|200|200|200|200|0|0|0|1920000|0|9600|9600
  59. a_3@testing|2001-07-01 00:00:00|2001-10-01 00:00:00|300|300|300|300|0|0|0|2880000|0|9600|9600
  60. a_4@testing|2001-10-01 00:00:00|2002-01-01 00:00:00|400|400|400|400|0|0|0|3840000|0|9600|9600
  61. """
  62. for ref, res in zip(univar_text.split("\n"), t_rast_univar.outputs.stdout.split("\n")):
  63. if ref and res:
  64. ref_line = ref.split("|", 1)[1]
  65. res_line = res.split("|", 1)[1]
  66. self.assertLooksLike(ref_line, res_line)
  67. def test_3(self):
  68. t_rast_univar = SimpleModule("t.rast.univar", input="A",
  69. where="start_time >= '2001-03-01'",
  70. overwrite=True, verbose=True)
  71. self.runModule("g.region", res=10)
  72. self.assertModule(t_rast_univar)
  73. univar_text="""id|start|end|mean|min|max|mean_of_abs|stddev|variance|coeff_var|sum|null_cells|cells|non_null_cells
  74. a_2@testing|2001-04-01 00:00:00|2001-07-01 00:00:00|200|200|200|200|0|0|0|19200|0|96|96
  75. a_3@testing|2001-07-01 00:00:00|2001-10-01 00:00:00|300|300|300|300|0|0|0|28800|0|96|96
  76. a_4@testing|2001-10-01 00:00:00|2002-01-01 00:00:00|400|400|400|400|0|0|0|38400|0|96|96
  77. """
  78. for ref, res in zip(univar_text.split("\n"), t_rast_univar.outputs.stdout.split("\n")):
  79. if ref and res:
  80. ref_line = ref.split("|", 1)[1]
  81. res_line = res.split("|", 1)[1]
  82. self.assertLooksLike(ref_line, res_line)
  83. def test_4(self):
  84. self.runModule("g.region", res=10)
  85. self.assertModule("t.rast.univar", input="A", flags="r",
  86. output="univar_output.txt",
  87. where="start_time >= '2001-03-01'",
  88. overwrite=True, verbose=True)
  89. univar_text="""id|start|end|mean|min|max|mean_of_abs|stddev|variance|coeff_var|sum|null_cells|cells|non_null_cells
  90. a_2@testing|2001-04-01 00:00:00|2001-07-01 00:00:00|200|200|200|200|0|0|0|1920000|0|9600|9600
  91. a_3@testing|2001-07-01 00:00:00|2001-10-01 00:00:00|300|300|300|300|0|0|0|2880000|0|9600|9600
  92. a_4@testing|2001-10-01 00:00:00|2002-01-01 00:00:00|400|400|400|400|0|0|0|3840000|0|9600|9600
  93. """
  94. univar_output = open("univar_output.txt", "r").read()
  95. for ref, res in zip(univar_text.split("\n"), univar_output.split("\n")):
  96. if ref and res:
  97. ref_line = ref.split("|", 1)[1]
  98. res_line = res.split("|", 1)[1]
  99. self.assertLooksLike(ref_line, res_line)
  100. def test_5(self):
  101. self.runModule("g.region", res=10)
  102. self.assertModule("t.rast.univar", input="A", flags="ru",
  103. output="univar_output.txt",
  104. where="start_time >= '2001-03-01'",
  105. overwrite=True, verbose=True)
  106. univar_text="""a_2@testing|2001-04-01 00:00:00|2001-07-01 00:00:00|200|200|200|200|0|0|0|1920000|0|9600|9600
  107. a_3@testing|2001-07-01 00:00:00|2001-10-01 00:00:00|300|300|300|300|0|0|0|2880000|0|9600|9600
  108. a_4@testing|2001-10-01 00:00:00|2002-01-01 00:00:00|400|400|400|400|0|0|0|3840000|0|9600|9600
  109. """
  110. univar_output = open("univar_output.txt", "r").read()
  111. for ref, res in zip(univar_text.split("\n"), univar_output.split("\n")):
  112. if ref and res:
  113. ref_line = ref.split("|", 1)[1]
  114. res_line = res.split("|", 1)[1]
  115. self.assertLooksLike(ref_line, res_line)
  116. def test_6_error_handling_empty_strds(self):
  117. # Empty strds
  118. self.assertModuleFail("t.rast.univar", input="A",
  119. output="univar_output.txt",
  120. where="start_time >= '2015-03-01'",
  121. overwrite=True, verbose=True)
  122. def test_7_error_handling_no_input(self):
  123. # No input
  124. self.assertModuleFail("t.rast.univar", output="out.txt")
  125. if __name__ == '__main__':
  126. from grass.gunittest.main import test
  127. test()