test_aggregation_absolute.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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)
  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_1month_time(self):
  99. """Aggregation one month time suffix"""
  100. self.assertModule("t.rast.aggregate", input="A", output="B",
  101. basename="b", granularity="1 months",
  102. method="maximum", sampling=["contains"],
  103. file_limit=0, nprocs=3, suffix='time')
  104. self.assertRasterExists('b_2001_01_01T00_00_00')
  105. def test_aggregation_2months(self):
  106. """Aggregation two month"""
  107. self.assertModule("t.rast.aggregate", input="A", output="B",
  108. basename="b", granularity="2 months",
  109. method="minimum", sampling=["contains"],
  110. nprocs=4, offset=10, suffix='num%02')
  111. tinfo_string="""start_time=2001-01-01 00:00:00
  112. end_time=2001-05-01 00:00:00
  113. granularity=2 months
  114. map_time=interval
  115. aggregation_type=minimum
  116. number_of_maps=2
  117. min_min=100.0
  118. min_max=500.0
  119. max_min=100.0
  120. max_max=500.0"""
  121. info = SimpleModule("t.info", flags="g", input="B")
  122. #info.run()
  123. #print info.outputs.stdout
  124. self.assertModuleKeyValue(module=info, reference=tinfo_string,
  125. precision=2, sep="=")
  126. # Check the map names are correct
  127. lister = SimpleModule("t.rast.list", input="B", columns="name",
  128. flags="s")
  129. self.runModule(lister)
  130. #print lister.outputs.stdout
  131. maps="b_11" + os.linesep + "b_12" + os.linesep
  132. self.assertEqual(maps, lister.outputs.stdout)
  133. def test_aggregation_3months(self):
  134. """Aggregation three month"""
  135. self.assertModule("t.rast.aggregate", input="A", output="B",
  136. basename="b", granularity="3 months",
  137. method="sum", sampling=["contains"],
  138. file_limit=0, nprocs=9, offset=100,
  139. suffix='num%03')
  140. tinfo_string="""start_time=2001-01-01 00:00:00
  141. end_time=2001-04-01 00:00:00
  142. granularity=3 months
  143. map_time=interval
  144. aggregation_type=sum
  145. number_of_maps=1
  146. min_min=1500.0
  147. min_max=1500.0
  148. max_min=1500.0
  149. max_max=1500.0"""
  150. info = SimpleModule("t.info", flags="g", input="B")
  151. #info.run()
  152. #print info.outputs.stdout
  153. self.assertModuleKeyValue(module=info, reference=tinfo_string,
  154. precision=2, sep="=")
  155. # Check the map names are correct
  156. lister = SimpleModule("t.rast.list", input="B", columns="name",
  157. flags="s")
  158. self.runModule(lister)
  159. #print lister.outputs.stdout
  160. maps="b_101" + os.linesep
  161. self.assertEqual(maps, lister.outputs.stdout)
  162. if __name__ == '__main__':
  163. from grass.gunittest.main import test
  164. test()