test_aggregation_absolute.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. import os
  9. import grass.pygrass.modules as pymod
  10. import grass.temporal as tgis
  11. from grass.gunittest.case import TestCase
  12. from grass.gunittest.gmodules import SimpleModule
  13. class TestAggregationAbsolute(TestCase):
  14. @classmethod
  15. def setUpClass(cls):
  16. """Initiate the temporal GIS and set the region
  17. """
  18. os.putenv("GRASS_OVERWRITE", "1")
  19. tgis.init()
  20. cls.use_temp_region()
  21. cls.runModule("g.region", s=0, n=80, w=0, e=120, b=0,
  22. t=50, res=10, res3=10)
  23. cls.runModule("r.mapcalc", expression="a1 = 100.0", overwrite=True)
  24. cls.runModule("r.mapcalc", expression="a2 = 200.0", overwrite=True)
  25. cls.runModule("r.mapcalc", expression="a3 = 300.0", overwrite=True)
  26. cls.runModule("r.mapcalc", expression="a4 = 400.0", overwrite=True)
  27. cls.runModule("r.mapcalc", expression="a5 = 500.0", overwrite=True)
  28. cls.runModule("r.mapcalc", expression="a6 = 600.0", overwrite=True)
  29. cls.runModule("r.mapcalc", expression="a7 = null()", overwrite=True)
  30. cls.runModule("t.create", type="strds", temporaltype="absolute",
  31. output="A", title="A test",
  32. description="A test", overwrite=True)
  33. cls.runModule("t.register", flags="i", type="raster", input="A",
  34. maps="a1,a2,a3,a4,a5,a6,a7",
  35. start="2001-01-15 12:05:45",
  36. increment="14 days",
  37. overwrite=True)
  38. @classmethod
  39. def tearDownClass(cls):
  40. """Remove the temporary region
  41. """
  42. cls.del_temp_region()
  43. cls.runModule("t.remove", flags="rf", type="strds", inputs="A")
  44. def tearDown(self):
  45. """Remove generated data"""
  46. self.runModule("t.remove", flags="rf", type="strds", inputs="B")
  47. def test_disaggregation(self):
  48. """Disaggregation with empty maps"""
  49. self.assertModule("t.rast.aggregate", input="A", output="B",
  50. basename="b", granularity="2 days",
  51. method="average",
  52. sampling=["overlaps","overlapped","during"],
  53. nprocs=2, flags="n")
  54. tinfo_string="""start_time=2001-01-15 00:00:00
  55. end_time=2001-04-25 00:00:00
  56. granularity=2 days
  57. aggregation_type=average
  58. number_of_maps=50
  59. map_time=interval
  60. min_min=100.0
  61. min_max=600.0
  62. max_min=100.0
  63. max_max=600.0"""
  64. info = SimpleModule("t.info", flags="g", input="B")
  65. #info.run()
  66. #print info.outputs.stdout
  67. self.assertModuleKeyValue(module=info, reference=tinfo_string,
  68. precision=2, sep="=")
  69. def test_aggregation_1month(self):
  70. """Aggregation one month"""
  71. self.assertModule("t.rast.aggregate", input="A", output="B",
  72. basename="b", granularity="1 months",
  73. method="maximum", sampling=["contains"],
  74. file_limit=0, nprocs=3, flags="s")
  75. tinfo_string="""start_time=2001-01-01 00:00:00
  76. end_time=2001-04-01 00:00:00
  77. granularity=1 month
  78. map_time=interval
  79. aggregation_type=maximum
  80. number_of_maps=3
  81. min_min=100.0
  82. min_max=500.0
  83. max_min=100.0
  84. max_max=500.0"""
  85. info = SimpleModule("t.info", flags="g", input="B")
  86. #info.run()
  87. #print info.outputs.stdout
  88. self.assertModuleKeyValue(module=info, reference=tinfo_string,
  89. precision=2, sep="=")
  90. # Check the map names are correct
  91. lister = SimpleModule("t.rast.list", input="B", columns="name",
  92. flags="s")
  93. self.runModule(lister)
  94. #print lister.outputs.stdout
  95. maps="b_2001_01" + os.linesep + "b_2001_02" + os.linesep + \
  96. "b_2001_03" + os.linesep
  97. self.assertEqual(maps, lister.outputs.stdout)
  98. def test_aggregation_2months(self):
  99. """Aggregation two month"""
  100. self.assertModule("t.rast.aggregate", input="A", output="B",
  101. basename="b", granularity="2 months",
  102. method="minimum", sampling=["contains"],
  103. nprocs=4, offset=10)
  104. tinfo_string="""start_time=2001-01-01 00:00:00
  105. end_time=2001-05-01 00:00:00
  106. granularity=2 months
  107. map_time=interval
  108. aggregation_type=minimum
  109. number_of_maps=2
  110. min_min=100.0
  111. min_max=500.0
  112. max_min=100.0
  113. max_max=500.0"""
  114. info = SimpleModule("t.info", flags="g", input="B")
  115. #info.run()
  116. #print info.outputs.stdout
  117. self.assertModuleKeyValue(module=info, reference=tinfo_string,
  118. precision=2, sep="=")
  119. # Check the map names are correct
  120. lister = SimpleModule("t.rast.list", input="B", columns="name",
  121. flags="s")
  122. self.runModule(lister)
  123. #print lister.outputs.stdout
  124. maps="b_11" + os.linesep + "b_12" + os.linesep
  125. self.assertEqual(maps, lister.outputs.stdout)
  126. def test_aggregation_3months(self):
  127. """Aggregation three month"""
  128. self.assertModule("t.rast.aggregate", input="A", output="B",
  129. basename="b", granularity="3 months",
  130. method="sum", sampling=["contains"],
  131. file_limit=0, nprocs=9, offset=100)
  132. tinfo_string="""start_time=2001-01-01 00:00:00
  133. end_time=2001-04-01 00:00:00
  134. granularity=3 months
  135. map_time=interval
  136. aggregation_type=sum
  137. number_of_maps=1
  138. min_min=1500.0
  139. min_max=1500.0
  140. max_min=1500.0
  141. max_max=1500.0"""
  142. info = SimpleModule("t.info", flags="g", input="B")
  143. #info.run()
  144. #print info.outputs.stdout
  145. self.assertModuleKeyValue(module=info, reference=tinfo_string,
  146. precision=2, sep="=")
  147. # Check the map names are correct
  148. lister = SimpleModule("t.rast.list", input="B", columns="name",
  149. flags="s")
  150. self.runModule(lister)
  151. #print lister.outputs.stdout
  152. maps="b_101" + os.linesep
  153. self.assertEqual(maps, lister.outputs.stdout)
  154. if __name__ == '__main__':
  155. from grass.gunittest.main import test
  156. test()