test_aggregation_absolute_parallel.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. """Test t.rast.aggregation
  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. :authors: Soeren Gebbert
  7. """
  8. from __future__ import print_function
  9. import os
  10. import grass.pygrass.modules as pymod
  11. import grass.temporal as tgis
  12. from grass.gunittest.case import TestCase
  13. from grass.gunittest.gmodules import SimpleModule
  14. from datetime import datetime
  15. class TestAggregationAbsoluteParallel(TestCase):
  16. @classmethod
  17. def setUpClass(cls):
  18. """Initiate the temporal GIS and set the region"""
  19. os.putenv("GRASS_OVERWRITE", "1")
  20. tgis.init()
  21. cls.use_temp_region()
  22. cls.runModule("g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10)
  23. name_list = []
  24. for i in range(540):
  25. cls.runModule(
  26. "r.mapcalc", expression="a%i = %i" % (i + 1, i + 1), overwrite=True
  27. )
  28. name_list.append("a%i" % (i + 1))
  29. cls.runModule(
  30. "t.create",
  31. type="strds",
  32. temporaltype="absolute",
  33. output="A",
  34. title="A test",
  35. description="A test",
  36. overwrite=True,
  37. )
  38. cls.runModule(
  39. "t.register",
  40. flags="i",
  41. type="raster",
  42. input="A",
  43. maps=name_list,
  44. start="2001-01-01",
  45. increment="4 hours",
  46. overwrite=True,
  47. )
  48. @classmethod
  49. def tearDownClass(cls):
  50. """Remove the temporary region"""
  51. cls.del_temp_region()
  52. cls.runModule("t.remove", flags="rf", type="strds", inputs="A")
  53. def tearDown(self):
  54. """Remove generated data"""
  55. self.runModule("t.remove", flags="rf", type="strds", inputs="B")
  56. def test_aggregation_12hours(self):
  57. """Aggregation one month"""
  58. self.assertModule(
  59. "t.rast.aggregate",
  60. input="A",
  61. output="B",
  62. basename="b",
  63. granularity="12 hours",
  64. method="sum",
  65. sampling=["contains"],
  66. nprocs=9,
  67. file_limit=2,
  68. )
  69. tinfo_string = """start_time='2001-01-01 00:00:00'
  70. end_time='2001-04-01 00:00:00'
  71. granularity='12 hours'
  72. map_time=interval
  73. aggregation_type=sum
  74. number_of_maps=180
  75. min_min=6.0
  76. min_max=1617.0
  77. max_min=6.0
  78. max_max=1617.0"""
  79. info = SimpleModule("t.info", flags="g", input="B")
  80. # info.run()
  81. # print info.outputs.stdout
  82. self.assertModuleKeyValue(
  83. module=info, reference=tinfo_string, precision=2, sep="="
  84. )
  85. def test_aggregation_1day_4procs(self):
  86. """Aggregation one month"""
  87. start = datetime.now()
  88. self.assertModule(
  89. "t.rast.aggregate",
  90. input="A",
  91. output="B",
  92. basename="b",
  93. granularity="1 day",
  94. method="sum",
  95. sampling=["contains"],
  96. nprocs=4,
  97. )
  98. end = datetime.now()
  99. delta = end - start
  100. print("test_aggregation_1day_4procs:", delta.total_seconds())
  101. tinfo_string = """start_time='2001-01-01 00:00:00'
  102. end_time='2001-04-01 00:00:00'
  103. granularity='1 day'
  104. map_time=interval
  105. aggregation_type=sum
  106. number_of_maps=90"""
  107. info = SimpleModule("t.info", flags="g", input="B")
  108. # info.run()
  109. # print info.outputs.stdout
  110. self.assertModuleKeyValue(
  111. module=info, reference=tinfo_string, precision=2, sep="="
  112. )
  113. def test_aggregation_1day_3procs(self):
  114. """Aggregation one month"""
  115. start = datetime.now()
  116. self.assertModule(
  117. "t.rast.aggregate",
  118. input="A",
  119. output="B",
  120. basename="b",
  121. granularity="1 day",
  122. method="sum",
  123. sampling=["contains"],
  124. nprocs=3,
  125. )
  126. end = datetime.now()
  127. delta = end - start
  128. print("test_aggregation_1day_3procs:", delta.total_seconds())
  129. tinfo_string = """start_time='2001-01-01 00:00:00'
  130. end_time='2001-04-01 00:00:00'
  131. granularity='1 day'
  132. map_time=interval
  133. aggregation_type=sum
  134. number_of_maps=90
  135. min_min=21.0
  136. min_max=3225.0
  137. max_min=21.0
  138. max_max=3225.0"""
  139. info = SimpleModule("t.info", input="B", flags="g")
  140. # info.run()
  141. # print info.outputs.stdout
  142. self.assertModuleKeyValue(
  143. module=info, reference=tinfo_string, precision=2, sep="="
  144. )
  145. def test_aggregation_1day_2procs(self):
  146. """Aggregation one month"""
  147. start = datetime.now()
  148. self.assertModule(
  149. "t.rast.aggregate",
  150. input="A",
  151. output="B",
  152. basename="b",
  153. granularity="1 day",
  154. method="sum",
  155. sampling=["contains"],
  156. nprocs=2,
  157. )
  158. end = datetime.now()
  159. delta = end - start
  160. print("test_aggregation_1day_2procs:", delta.total_seconds())
  161. tinfo_string = """start_time='2001-01-01 00:00:00'
  162. end_time='2001-04-01 00:00:00'
  163. granularity='1 day'
  164. map_time=interval
  165. aggregation_type=sum
  166. number_of_maps=90
  167. min_min=21.0
  168. min_max=3225.0
  169. max_min=21.0
  170. max_max=3225.0"""
  171. info = SimpleModule("t.info", flags="g", input="B")
  172. # info.run()
  173. # print info.outputs.stdout
  174. self.assertModuleKeyValue(
  175. module=info, reference=tinfo_string, precision=2, sep="="
  176. )
  177. if __name__ == "__main__":
  178. from grass.gunittest.main import test
  179. test()