test_accumulation.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. """Test t.rast.accumulate
  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. import grass.temporal as tgis
  9. import datetime
  10. from grass.gunittest.case import TestCase
  11. from grass.gunittest.gmodules import SimpleModule
  12. class TestAccumulate(TestCase):
  13. @classmethod
  14. def setUpClass(cls):
  15. """Initiate the temporal GIS and set the region"""
  16. tgis.init(True) # Raise on error instead of exit(1)
  17. cls.use_temp_region()
  18. cls.runModule("g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10)
  19. cls.runModule("r.mapcalc", expression="a_1 = 5", overwrite=True)
  20. cls.runModule("r.mapcalc", expression="a_2 = 10", overwrite=True)
  21. cls.runModule("r.mapcalc", expression="a_3 = 20", overwrite=True)
  22. cls.runModule("r.mapcalc", expression="a_4 = 35", overwrite=True)
  23. cls.runModule("r.mapcalc", expression="a_5 = 20", overwrite=True)
  24. cls.runModule("r.mapcalc", expression="a_6 = 10", overwrite=True)
  25. cls.runModule("r.mapcalc", expression="a_7 = 5", overwrite=True)
  26. cls.runModule("r.mapcalc", expression="lower = 10", overwrite=True)
  27. cls.runModule("r.mapcalc", expression="upper = 30", overwrite=True)
  28. cls.runModule(
  29. "t.create",
  30. type="strds",
  31. temporaltype="absolute",
  32. output="A",
  33. title="A test",
  34. description="A test",
  35. overwrite=True,
  36. )
  37. cls.runModule(
  38. "t.register",
  39. flags="i",
  40. type="raster",
  41. input="A",
  42. maps="a_1,a_2,a_3,a_4,a_5,a_6,a_7",
  43. start="2001-01-01",
  44. increment="1 day",
  45. overwrite=True,
  46. )
  47. cls.runModule(
  48. "t.create",
  49. type="strds",
  50. temporaltype="absolute",
  51. output="Lower",
  52. title="Lower",
  53. description="Lower",
  54. overwrite=True,
  55. )
  56. cls.runModule(
  57. "t.register",
  58. type="raster",
  59. input="Lower",
  60. maps="lower",
  61. start="2001-01-01",
  62. end="2001-01-10",
  63. overwrite=True,
  64. )
  65. cls.runModule(
  66. "t.create",
  67. type="strds",
  68. temporaltype="absolute",
  69. output="Upper",
  70. title="Upper",
  71. description="Upper",
  72. overwrite=True,
  73. )
  74. cls.runModule(
  75. "t.register",
  76. type="raster",
  77. input="Upper",
  78. maps="upper",
  79. start="2001-01-01",
  80. end="2001-01-10",
  81. overwrite=True,
  82. )
  83. @classmethod
  84. def tearDownClass(cls):
  85. """Remove the temporary region"""
  86. cls.runModule("t.remove", flags="df", type="strds", inputs="A")
  87. cls.runModule("t.remove", flags="df", type="strds", inputs="Lower")
  88. cls.runModule("t.remove", flags="df", type="strds", inputs="Upper")
  89. cls.del_temp_region()
  90. def tearDown(self):
  91. """Remove generated data"""
  92. self.runModule("t.remove", flags="df", type="strds", inputs="B")
  93. def test_1(self):
  94. self.assertModule(
  95. "t.rast.accumulate",
  96. input="A",
  97. output="B",
  98. limits=[0, 40],
  99. method="gdd",
  100. start="2001-01-01",
  101. cycle="7 days",
  102. basename="b",
  103. overwrite=True,
  104. verbose=True,
  105. )
  106. D = tgis.open_old_stds("B", type="strds")
  107. self.assertEqual(D.metadata.get_number_of_maps(), 7)
  108. self.assertEqual(D.metadata.get_min_min(), 5)
  109. self.assertEqual(D.metadata.get_max_max(), 105)
  110. start, end = D.get_absolute_time()
  111. self.assertEqual(start, datetime.datetime(2001, 1, 1))
  112. self.assertEqual(end, datetime.datetime(2001, 1, 8))
  113. self.assertEqual(D.check_temporal_topology(), True)
  114. self.assertEqual(D.get_granularity(), "1 day")
  115. def test_2(self):
  116. self.assertModule(
  117. "t.rast.accumulate",
  118. input="A",
  119. output="B",
  120. limits=[10, 40],
  121. method="gdd",
  122. start="2001-01-01",
  123. cycle="7 days",
  124. basename="b",
  125. overwrite=True,
  126. verbose=True,
  127. )
  128. D = tgis.open_old_stds("B", type="strds")
  129. self.assertEqual(D.metadata.get_number_of_maps(), 7)
  130. self.assertEqual(D.metadata.get_min_min(), 0.0)
  131. self.assertEqual(D.metadata.get_max_max(), 45.0)
  132. start, end = D.get_absolute_time()
  133. self.assertEqual(start, datetime.datetime(2001, 1, 1))
  134. self.assertEqual(end, datetime.datetime(2001, 1, 8))
  135. self.assertEqual(D.check_temporal_topology(), True)
  136. self.assertEqual(D.get_granularity(), "1 day")
  137. def test_3(self):
  138. self.assertModule(
  139. "t.rast.accumulate",
  140. input="A",
  141. output="B",
  142. limits=[10, 30],
  143. method="bedd",
  144. start="2001-01-01",
  145. cycle="7 days",
  146. basename="b",
  147. overwrite=True,
  148. verbose=True,
  149. )
  150. D = tgis.open_old_stds("B", type="strds")
  151. self.assertEqual(D.metadata.get_number_of_maps(), 7)
  152. self.assertEqual(D.metadata.get_min_min(), 0.0)
  153. self.assertEqual(D.metadata.get_max_max(), 40.0)
  154. start, end = D.get_absolute_time()
  155. self.assertEqual(start, datetime.datetime(2001, 1, 1))
  156. self.assertEqual(end, datetime.datetime(2001, 1, 8))
  157. self.assertEqual(D.check_temporal_topology(), True)
  158. self.assertEqual(D.get_granularity(), "1 day")
  159. def test_3_a(self):
  160. self.assertModule(
  161. "t.rast.accumulate",
  162. input="A",
  163. output="B",
  164. lower="Lower",
  165. upper="Upper",
  166. limits=[0, 40],
  167. method="bedd",
  168. start="2001-01-01",
  169. cycle="7 days",
  170. basename="b",
  171. overwrite=True,
  172. verbose=True,
  173. )
  174. D = tgis.open_old_stds("B", type="strds")
  175. self.assertEqual(D.metadata.get_number_of_maps(), 7)
  176. self.assertEqual(D.metadata.get_min_min(), 0.0)
  177. self.assertEqual(D.metadata.get_max_max(), 40.0)
  178. start, end = D.get_absolute_time()
  179. self.assertEqual(start, datetime.datetime(2001, 1, 1))
  180. self.assertEqual(end, datetime.datetime(2001, 1, 8))
  181. self.assertEqual(D.check_temporal_topology(), True)
  182. self.assertEqual(D.get_granularity(), "1 day")
  183. def test_4(self):
  184. self.assertModule(
  185. "t.rast.accumulate",
  186. input="A",
  187. output="B",
  188. limits=[10, 30],
  189. method="bedd",
  190. start="2001-01-01",
  191. cycle="7 days",
  192. basename="b",
  193. granularity="2 days",
  194. overwrite=True,
  195. verbose=True,
  196. )
  197. D = tgis.open_old_stds("B", type="strds")
  198. self.assertEqual(D.metadata.get_number_of_maps(), 4)
  199. self.assertEqual(D.metadata.get_min_min(), 0.0)
  200. self.assertEqual(D.metadata.get_max_max(), 22.5)
  201. start, end = D.get_absolute_time()
  202. self.assertEqual(start, datetime.datetime(2001, 1, 1))
  203. self.assertEqual(end, datetime.datetime(2001, 1, 9))
  204. self.assertEqual(D.check_temporal_topology(), True)
  205. self.assertEqual(D.get_granularity(), "2 days")
  206. def test_4_a(self):
  207. self.assertModule(
  208. "t.rast.accumulate",
  209. input="A",
  210. output="B",
  211. lower="Lower",
  212. upper="Upper",
  213. limits=[0, 40],
  214. method="bedd",
  215. start="2001-01-01",
  216. cycle="7 days",
  217. basename="b",
  218. granularity="2 days",
  219. overwrite=True,
  220. verbose=True,
  221. )
  222. D = tgis.open_old_stds("B", type="strds")
  223. self.assertEqual(D.metadata.get_number_of_maps(), 4)
  224. self.assertEqual(D.metadata.get_min_min(), 0.0)
  225. self.assertEqual(D.metadata.get_max_max(), 22.5)
  226. start, end = D.get_absolute_time()
  227. self.assertEqual(start, datetime.datetime(2001, 1, 1))
  228. self.assertEqual(end, datetime.datetime(2001, 1, 9))
  229. self.assertEqual(D.check_temporal_topology(), True)
  230. self.assertEqual(D.get_granularity(), "2 days")
  231. def test_5(self):
  232. self.assertModule(
  233. "t.rast.accumulate",
  234. input="A",
  235. output="B",
  236. limits=[0, 40],
  237. method="gdd",
  238. start="2001-01-01",
  239. cycle="7 days",
  240. basename="b",
  241. stop="2001-01-05",
  242. overwrite=True,
  243. verbose=True,
  244. )
  245. D = tgis.open_old_stds("B", type="strds")
  246. self.assertEqual(D.metadata.get_number_of_maps(), 4)
  247. self.assertEqual(D.metadata.get_min_min(), 5)
  248. self.assertEqual(D.metadata.get_max_max(), 70)
  249. start, end = D.get_absolute_time()
  250. self.assertEqual(start, datetime.datetime(2001, 1, 1))
  251. self.assertEqual(end, datetime.datetime(2001, 1, 5))
  252. self.assertEqual(D.check_temporal_topology(), True)
  253. self.assertEqual(D.get_granularity(), "1 day")
  254. def test_count_suffix(self):
  255. self.assertModule(
  256. "t.rast.accumulate",
  257. input="A",
  258. output="B",
  259. limits=[0, 40],
  260. method="gdd",
  261. start="2001-01-01",
  262. cycle="7 days",
  263. basename="b",
  264. stop="2001-01-05",
  265. suffix="num",
  266. overwrite=True,
  267. verbose=True,
  268. )
  269. self.assertRasterExists("b_00001")
  270. self.assertRasterDoesNotExist("b_2001_01_07")
  271. def test_count3_suffix(self):
  272. self.assertModule(
  273. "t.rast.accumulate",
  274. input="A",
  275. output="B",
  276. limits=[0, 40],
  277. method="gdd",
  278. start="2001-01-01",
  279. cycle="7 days",
  280. basename="b",
  281. stop="2001-01-05",
  282. suffix="num%03",
  283. overwrite=True,
  284. verbose=True,
  285. )
  286. self.assertRasterExists("b_001")
  287. self.assertRasterDoesNotExist("b_2001_01_07")
  288. def test_time_suffix(self):
  289. self.assertModule(
  290. "t.rast.accumulate",
  291. input="A",
  292. output="B",
  293. limits=[0, 40],
  294. method="gdd",
  295. start="2001-01-01",
  296. cycle="7 days",
  297. basename="b",
  298. stop="2001-01-05",
  299. suffix="time",
  300. overwrite=True,
  301. verbose=True,
  302. )
  303. self.assertRasterExists("b_2001_01_01T00_00_00")
  304. if __name__ == "__main__":
  305. from grass.gunittest.main import test
  306. test()