[GRASS-SVN] r67936 - in grass/trunk/temporal: t.rast.accdetect t.rast.accdetect/testsuite t.rast.accumulate t.rast.accumulate/testsuite t.rast.contour t.rast.contour/testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Feb 24 05:04:46 PST 2016


Author: lucadelu
Date: 2016-02-24 05:04:45 -0800 (Wed, 24 Feb 2016)
New Revision: 67936

Added:
   grass/trunk/temporal/t.rast.accdetect/testsuite/test_simple.py
Modified:
   grass/trunk/temporal/t.rast.accdetect/t.rast.accdetect.py
   grass/trunk/temporal/t.rast.accumulate/t.rast.accumulate.py
   grass/trunk/temporal/t.rast.accumulate/testsuite/test_accumulation.py
   grass/trunk/temporal/t.rast.contour/t.rast.contour.py
   grass/trunk/temporal/t.rast.contour/testsuite/test_convert.py
Log:
t.rast.accdetect, t.rast.accumulate, t.rast.contour: added suffix option

Modified: grass/trunk/temporal/t.rast.accdetect/t.rast.accdetect.py
===================================================================
--- grass/trunk/temporal/t.rast.accdetect/t.rast.accdetect.py	2016-02-24 12:59:01 UTC (rev 67935)
+++ grass/trunk/temporal/t.rast.accdetect/t.rast.accdetect.py	2016-02-24 13:04:45 UTC (rev 67936)
@@ -92,6 +92,15 @@
 #%end
 
 #%option
+#% key: suffix
+#% type: string
+#% description: Suffix to add at basename: set 'gran' for granularity, 'time' for the full time format, 'count' for numerical suffix with a specific number of digits (default %05)
+#% answer: gran
+#% required: no
+#% multiple: no
+#%end
+
+#%option
 #% key: range
 #% type: double
 #% key_desc: min,max
@@ -144,6 +153,7 @@
     staend = options["staend"]
     register_null = flags["n"]
     reverse = flags["r"]
+    time_suffix = options["suffix"]
 
     grass.set_raise_on_error(True)
 
@@ -301,8 +311,9 @@
         grass.message(_("Processing cycle %s - %s"%(str(start), str(end))))
 
         count = compute_occurrence(occurrence_maps, input_strds, input_maps,
-                                   start, base, count, mapset, where, reverse,
-                                   range, minimum_strds, maximum_strds, dbif)
+                                   start, base, count, time_suffix, mapset,
+                                   where, reverse, range, minimum_strds,
+                                   maximum_strds, dbif)
 
         # Indicator computation is based on the occurrence so we need to start it after
         # the occurrence cycle
@@ -314,7 +325,15 @@
                 else:
                     map = input_maps[i]
 
-                indicator_map_name = "%s_indicator_%i" % (base, indi_count)
+                if input_strds.get_temporal_type() == 'absolute' and time_suffix == 'gran':
+                    suffix = tgis.create_suffix_from_datetime(map.temporal_extent.get_start_time(),
+                                                              input_strds.get_granularity())
+                    indicator_map_name = "{ba}_{su}".format(ba=base, su=suffix)
+                elif input_strds.get_temporal_type() == 'absolute' and time_suffix == 'time':
+                    suffix = tgis.create_time_suffix(map)
+                    indicator_map_name = "{ba}_{su}".format(ba=base, su=suffix)
+                else:
+                    indicator_map_name = tgis.create_numeric_suffic(base, indi_count, time_suffix)
                 indicator_map_id = dummy.build_id(indicator_map_name, mapset)
                 indicator_map = input_strds.get_new_map_instance(indicator_map_id)
 
@@ -464,8 +483,8 @@
 ############################################################################
 
 def compute_occurrence(occurrence_maps, input_strds, input_maps, start, base,
-               count, mapset, where, reverse, range, minimum_strds,
-               maximum_strds, dbif):
+                       count, tsuffix, mapset, where, reverse, range,
+                       minimum_strds, maximum_strds, dbif):
 
     if minimum_strds:
         input_maps_minimum = input_strds.get_registered_maps_as_objects(where=where,
@@ -498,7 +517,16 @@
         else:
             days = td
 
-        occurrence_map_name = "%s_%i" % (base, count)
+        if input_strds.get_temporal_type() == 'absolute' and tsuffix == 'gran':
+            suffix = tgis.create_suffix_from_datetime(map.temporal_extent.get_start_time(),
+                                                      input_strds.get_granularity())
+            occurrence_map_name = "{ba}_{su}".format(ba=base, su=suffix)
+        elif input_strds.get_temporal_type() == 'absolute' and tsuffix == 'time':
+            suffix = tgis.create_time_suffix(map)
+            occurrence_map_name = "{ba}_{su}".format(ba=base, su=suffix)
+        else:
+            occurrence_map_name = tgis.create_numeric_suffic(base, count, tsuffix)
+
         occurrence_map_id = map.build_id(occurrence_map_name, mapset)
         occurrence_map = input_strds.get_new_map_instance(occurrence_map_id)
 

Added: grass/trunk/temporal/t.rast.accdetect/testsuite/test_simple.py
===================================================================
--- grass/trunk/temporal/t.rast.accdetect/testsuite/test_simple.py	                        (rev 0)
+++ grass/trunk/temporal/t.rast.accdetect/testsuite/test_simple.py	2016-02-24 13:04:45 UTC (rev 67936)
@@ -0,0 +1,96 @@
+"""test distributed temporal databases with stvds
+
+(C) 2014 by the GRASS Development Team
+This program is free software under the GNU General Public
+License (>=v2). Read the file COPYING that comes with GRASS
+for details.
+
+:authors: Luca Delucchi
+"""
+
+from grass.gunittest.case import TestCase
+from grass.gunittest.gmodules import SimpleModule
+import os
+
+
+class TestRasterExtraction(TestCase):
+    
+    @classmethod
+    def setUpClass(cls):
+        """Initiate the temporal GIS and set the region
+        """
+        os.putenv("GRASS_OVERWRITE",  "1")
+        cls.use_temp_region()
+        cls.runModule("g.region",  s=0,  n=80,  w=0,  e=120,  b=0,
+                      t=50,  res=10,  res3=10)
+        for i in range(1,101):
+            cls.runModule("r.mapcalc", flags='s',  overwrite=True,
+                          expression="a_mapcalc{nu} = rand(1,10)".format(nu=i))
+
+        cls.runModule("t.create", type="strds", temporaltype="absolute",
+                      output="A", title="A test", description="A test",
+                      overwrite=True)
+        glist = SimpleModule('g.list', type='raster', pattern="a_mapcalc*",
+                             separator=',')
+        cls.runModule(glist, expecting_stdout=True)
+        maps = glist.outputs.stdout.strip()
+        cls.runModule("t.register", flags="i",  type="raster",  input="A",
+                      maps=maps.strip(), increment="1 month",
+                      start="2001-01-01 00:00:00", overwrite=True)
+    @classmethod
+    def tearDownClass(cls):
+        """Remove the temporary region
+        """
+        cls.del_temp_region()
+        cls.runModule("t.remove", flags="rf", type="strds", inputs="A")
+
+    def tearDown(self):
+        """Remove generated data"""
+        self.runModule("t.remove", flags="rf", type="strds", inputs="B")
+        
+    def test_simple(self):
+        self.assertModule('t.rast.accdetect', input='A', occurrence='B',
+                          start="2001-01-01", cycle="12 months",
+                          basename='b_occu', range=(1,8))
+        tinfo_string="""semantic_type=mean
+        start_time=2001-01-01 00:00:00
+        end_time=2009-05-01 00:00:00
+        granularity=1 month
+        map_time=interval
+        number_of_maps=100"""
+        info = SimpleModule("t.info", flags="g", type="strds", input="B")
+        self.assertModuleKeyValue(module=info, reference=tinfo_string,
+                                  precision=2, sep="=")
+
+    def test_stop(self):
+        self.assertModule('t.rast.accdetect', input='A', occurrence='B',
+                          start="2001-01-01", stop='2008-12-31',
+                          cycle="12 months", basename='b_occu', range=(1,8))
+        tinfo_string="""semantic_type=mean
+        start_time=2001-01-01 00:00:00
+        end_time=2009-01-01 00:00:00
+        granularity=1 month
+        map_time=interval
+        number_of_maps=96"""
+        info = SimpleModule("t.info", flags="g", type="strds", input="B")
+        self.assertModuleKeyValue(module=info, reference=tinfo_string,
+                                  precision=2, sep="=")
+      
+    def test_time_suffix(self):
+        self.assertModule('t.rast.accdetect', input='A', occurrence='B',
+                          start="2001-01-01", cycle="12 months", suffix='time',
+                          basename='b_occu', range=(1,8))
+        self.assertRasterDoesNotExist('b_occu_2001_01')
+        self.assertRasterExists('b_occu_2001_01_01T00.00.00')
+
+    def test_num_suffix(self):
+        self.assertModule('t.rast.accdetect', input='A', occurrence='B',
+                          start="2001-01-01", cycle="12 months",
+                          suffix='count%03', basename='b_occu', range=(1,8))
+        self.assertRasterDoesNotExist('b_occu_2001_01')
+        self.assertRasterExists('b_occu_001')
+        self.assertRasterDoesNotExist('b_occu_00001')
+
+if __name__ == '__main__':
+    from grass.gunittest.main import test
+    test()

Modified: grass/trunk/temporal/t.rast.accumulate/t.rast.accumulate.py
===================================================================
--- grass/trunk/temporal/t.rast.accumulate/t.rast.accumulate.py	2016-02-24 12:59:01 UTC (rev 67935)
+++ grass/trunk/temporal/t.rast.accumulate/t.rast.accumulate.py	2016-02-24 13:04:45 UTC (rev 67936)
@@ -93,6 +93,15 @@
 #%end
 
 #%option
+#% key: suffix
+#% type: string
+#% description: Suffix to add at basename: set 'gran' for granularity, 'time' for the full time format, 'num' for numerical suffix with a specific number of digits (default %05)
+#% answer: gran
+#% required: no
+#% multiple: no
+#%end
+
+#%option
 #% key: limits
 #% type: double
 #% key_desc: lower,upper
@@ -138,6 +147,7 @@
 #% description: Reverse time direction in cyclic accumulation
 #%end
 
+
 import grass.script as grass
 import grass.temporal as tgis
 from grass.pygrass.modules import Module
@@ -163,6 +173,7 @@
     granularity = options["granularity"]
     register_null = flags["n"]
     reverse = flags["r"]
+    time_suffix = options["suffix"]
 
     # Make sure the temporal database exists
     tgis.init()
@@ -369,7 +380,16 @@
                 continue
 
             # New output map
-            output_map_name = "%s_%i" % (base, count)
+            if input_strds.get_temporal_type() == 'absolute' and time_suffix == 'gran':
+                suffix = tgis.create_suffix_from_datetime(map.temporal_extent.get_start_time(),
+                                                          input_strds.get_granularity())
+                output_map_name = "{ba}_{su}".format(ba=base, su=suffix)
+            elif input_strds.get_temporal_type() == 'absolute' and time_suffix == 'time':
+                suffix = tgis.create_time_suffix(map)
+                output_map_name = "{ba}_{su}".format(ba=base, su=suffix)
+            else:
+                output_map_name = tgis.create_numeric_suffic(base, count, time_suffix)
+
             output_map_id = map.build_id(output_map_name, mapset)
             output_map = input_strds.get_new_map_instance(output_map_id)
 

Modified: grass/trunk/temporal/t.rast.accumulate/testsuite/test_accumulation.py
===================================================================
--- grass/trunk/temporal/t.rast.accumulate/testsuite/test_accumulation.py	2016-02-24 12:59:01 UTC (rev 67935)
+++ grass/trunk/temporal/t.rast.accumulate/testsuite/test_accumulation.py	2016-02-24 13:04:45 UTC (rev 67936)
@@ -35,10 +35,10 @@
         cls.runModule("r.mapcalc", expression="lower = 10",  overwrite=True)
         cls.runModule("r.mapcalc", expression="upper = 30",  overwrite=True)
 
-        cls.runModule("t.create",  type="strds",  temporaltype="absolute",  
+        cls.runModule("t.create",  type="strds",  temporaltype="absolute",
                                    output="A",  title="A test",
                                    description="A test", overwrite=True)
-        cls.runModule("t.register",  flags="i",  type="raster",  input="A",  
+        cls.runModule("t.register",  flags="i",  type="raster",  input="A",
                                      maps="a_1,a_2,a_3,a_4,a_5,a_6,a_7",
                                      start="2001-01-01",
                                      increment="1 day",  overwrite=True)
@@ -68,13 +68,15 @@
         cls.runModule("t.remove",  flags="rf",  type="strds",
                                    inputs="A")
         cls.runModule("t.remove",  flags="rf",  type="strds",
-                                   inputs="B")
-        cls.runModule("t.remove",  flags="rf",  type="strds",
                                    inputs="Lower")
         cls.runModule("t.remove",  flags="rf",  type="strds",
                                    inputs="Upper")
         cls.del_temp_region()
 
+    def tearDown(self):
+        """Remove generated data"""
+        self.runModule("t.remove", flags="rf", type="strds", inputs="B")
+
     def test_1(self):
         self.assertModule("t.rast.accumulate",  input="A", output="B",
                           limits=[0,40], method="gdd",
@@ -207,6 +209,24 @@
         self.assertEqual( D.check_temporal_topology(),  True)
         self.assertEqual(D.get_granularity(),  u'1 day')
 
+    def test_count_suffix(self):
+        self.assertModule("t.rast.accumulate",  input="A", output="B",
+                          limits=[0,40], method="gdd",
+                          start="2001-01-01", cycle="7 days",
+                          basename="b", stop="2001-01-05", suffix="num",
+                          overwrite=True,  verbose=True)
+        self.assertRasterExists('b_00001')
+        self.assertRasterDoesNotExist('b_2001_01_07')
+
+    def test_count3_suffix(self):
+        self.assertModule("t.rast.accumulate",  input="A", output="B",
+                          limits=[0,40], method="gdd",
+                          start="2001-01-01", cycle="7 days",
+                          basename="b", stop="2001-01-05", suffix="num%03",
+                          overwrite=True,  verbose=True)
+        self.assertRasterExists('b_001')
+        self.assertRasterDoesNotExist('b_2001_01_07')
+
 if __name__ == '__main__':
     from grass.gunittest.main import test
     test()

Modified: grass/trunk/temporal/t.rast.contour/t.rast.contour.py
===================================================================
--- grass/trunk/temporal/t.rast.contour/t.rast.contour.py	2016-02-24 12:59:01 UTC (rev 67935)
+++ grass/trunk/temporal/t.rast.contour/t.rast.contour.py	2016-02-24 13:04:45 UTC (rev 67936)
@@ -43,6 +43,15 @@
 #%end
 
 #%option
+#% key: suffix
+#% type: string
+#% description: Suffix to add at basename: set 'gran' for granularity, 'time' for the full time format, 'num' for numerical suffix with a specific number of digits (default %05)
+#% answer: gran
+#% required: no
+#% multiple: no
+#%end
+
+#%option
 #% key: step
 #% type: double
 #% description: Increment between contour levels
@@ -102,7 +111,6 @@
 #% description: Do not create attribute tables
 #%end
 
-
 import sys
 import copy
 import grass.script as gscript
@@ -126,11 +134,12 @@
     minlevel = options["minlevel"]
     maxlevel = options["maxlevel"]
     cut = options["cut"]
+    time_suffix = options["suffix"]
 
     register_null = flags["n"]
     t_flag = flags["t"]
-    
 
+
     # Make sure the temporal database exists
     tgis.init()
     # We need a database interface
@@ -150,12 +159,12 @@
     # Check the new stvds
     new_sp = tgis.check_new_stds(output, "stvds", dbif=dbif,
                                  overwrite=overwrite)
-                                               
+
     # Setup the flags
     flags = ""
     if t_flag is True:
         flags += "t"
-    
+
     # Configure the r.to.vect module
     contour_module = pymod.Module("r.contour", input="dummy",
                                    output="dummy", run_=False,
@@ -191,7 +200,16 @@
     # run r.to.vect all selected maps
     for map in maps:
         count += 1
-        map_name = "%s_%i" % (base, count)
+
+        if sp.get_temporal_type() == 'absolute' and time_suffix == 'gran':
+            suffix = tgis.create_suffix_from_datetime(map.temporal_extent.get_start_time(),
+                                                      sp.get_granularity())
+            map_name = "{ba}_{su}".format(ba=base, su=suffix)
+        elif sp.get_temporal_type() == 'absolute' and time_suffix == 'time':
+            suffix = tgis.create_time_suffix(map)
+            map_name = "{ba}_{su}".format(ba=base, su=suffix)
+        else:
+            map_name = tgis.create_numeric_suffic(base, count, time_suffix)
         new_map = tgis.open_new_map_dataset(map_name, None, type="vector",
                                             temporal_extent=map.get_temporal_extent(),
                                             overwrite=overwrite, dbif=dbif)
@@ -254,7 +272,7 @@
             else:
                 names += ",%s" % (map.get_name())
 
-        gscript.run_command("g.remove", flags='f', type='vector', name=names, 
+        gscript.run_command("g.remove", flags='f', type='vector', name=names,
                             quiet=True)
 
     dbif.close()

Modified: grass/trunk/temporal/t.rast.contour/testsuite/test_convert.py
===================================================================
--- grass/trunk/temporal/t.rast.contour/testsuite/test_convert.py	2016-02-24 12:59:01 UTC (rev 67935)
+++ grass/trunk/temporal/t.rast.contour/testsuite/test_convert.py	2016-02-24 13:04:45 UTC (rev 67936)
@@ -8,10 +8,10 @@
 @author Soeren Gebbert
 """
 
-import subprocess
 from grass.gunittest.case import TestCase
 from grass.gunittest.gmodules import SimpleModule
 
+
 class TestRasterContour(TestCase):
 
     @classmethod
@@ -27,28 +27,28 @@
         cls.runModule("r.mapcalc", expression="a_4 = col()", overwrite=True)
         cls.runModule("r.mapcalc", expression="a_5 = null()", overwrite=True)
 
-        cls.runModule("t.create", type="strds", temporaltype="absolute",  
-                                 output="A", title="A test", description="A test",  
+        cls.runModule("t.create", type="strds", temporaltype="absolute",
+                                 output="A", title="A test", description="A test",
                                  overwrite=True)
-        cls.runModule("t.register",  flags="i", type="raster", input="A",  
-                                     maps="a_1,a_2,a_3,a_4,a_5", start="2001-01-01", 
+        cls.runModule("t.register",  flags="i", type="raster", input="A",
+                                     maps="a_1,a_2,a_3,a_4,a_5", start="2001-01-01",
                                      increment="3 months", overwrite=True)
 
     @classmethod
     def tearDownClass(cls):
         """Remove the temporary region
         """
-        cls.runModule("t.remove", flags="rf", type="strds",  
+        cls.runModule("t.remove", flags="rf", type="strds",
                                    inputs="A")
         cls.del_temp_region()
-        
+
     def tearDown(self):
         """Remove generated data"""
-        self.runModule("t.remove", flags="rf", type="stvds",  
+        self.runModule("t.remove", flags="rf", type="stvds",
                                    inputs="result")
 
     def test_register_empty_maps(self):
-        self.assertModule("t.rast.contour", input="A", output="result", 
+        self.assertModule("t.rast.contour", input="A", output="result",
                           levels=(1,2,3,4,5,6,7,8), flags="n",
                           basename="test",
                           nprocs=1, overwrite=True, verbose=True)
@@ -69,7 +69,7 @@
 
     def test_simple(self):
         """Do not register empty maps"""
-        self.assertModule("t.rast.contour", input="A", output="result", 
+        self.assertModule("t.rast.contour", input="A", output="result",
                           levels=(1,2,3,4,5,6,7,8),
                           basename="test",
                           nprocs=1, overwrite=True, verbose=True)
@@ -90,7 +90,7 @@
 
     def test_where(self):
         """Use where statement and do not register empty maps"""
-        self.assertModule("t.rast.contour", input="A", output="result", 
+        self.assertModule("t.rast.contour", input="A", output="result",
                           levels=(1,2,3,4,5,6,7,8),
                           basename="test",
                           where="start_time > '2001-02-01'",
@@ -112,7 +112,7 @@
 
     def test_parallel(self):
         """Run 4 contour processes do not create attribute tables"""
-        self.assertModule("t.rast.contour", input="A", output="result", 
+        self.assertModule("t.rast.contour", input="A", output="result",
                           levels=(1,2,3,4,5,6,7,8),
                           basename="test", flags="t",
                           nprocs=4, overwrite=True, verbose=True)
@@ -133,7 +133,7 @@
 
     def test_parallel_cut(self):
         """Do not register empty maps"""
-        self.assertModule("t.rast.contour", input="A", output="result", 
+        self.assertModule("t.rast.contour", input="A", output="result",
                           levels=(1,2,3,4,5,6,7,8), cut=2,
                           basename="test", flags="t",
                           nprocs=4, overwrite=True, verbose=True)
@@ -154,7 +154,7 @@
 
     def test_where_step(self):
         """Use where statement and do not register empty maps"""
-        self.assertModule("t.rast.contour", input="A", output="result", 
+        self.assertModule("t.rast.contour", input="A", output="result",
                           step=1, minlevel=1, maxlevel=8,
                           basename="test",
                           where="start_time > '2001-02-01'",
@@ -175,6 +175,19 @@
         self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
 
 
+    def test_time_num(self):
+        """Test the -s flag"""
+        self.assertModule("t.rast.contour", input="A", output="result",
+                          step=1, minlevel=1, maxlevel=8,
+                          basename="time", suffix='num%03',
+                          where="start_time > '2001-02-01'",
+                          nprocs=1, overwrite=True, verbose=True)
+
+        self.assertVectorExists('time_004')
+        self.assertVectorDoesNotExist('time_00005')
+        self.assertVectorDoesNotExist('time_2001_07')
+
+
 class TestRasterContourFails(TestCase):
     @classmethod
     def setUpClass(cls):
@@ -185,11 +198,11 @@
 
         cls.runModule("r.mapcalc", expression="a_1 = 100", overwrite=True)
 
-        cls.runModule("t.create",  type="strds", temporaltype="absolute",  
-                                 output="A", title="A test", description="A test",  
+        cls.runModule("t.create",  type="strds", temporaltype="absolute",
+                                 output="A", title="A test", description="A test",
                                  overwrite=True)
-        cls.runModule("t.register",  flags="i", type="raster", input="A",  
-                                     maps="a_1", start="2001-01-01", 
+        cls.runModule("t.register",  flags="i", type="raster", input="A",
+                                     maps="a_1", start="2001-01-01",
                                      increment="3 months", overwrite=True)
 
     @classmethod
@@ -202,7 +215,7 @@
 
     def test_error_handling(self):
         # No basename
-        self.assertModuleFail("t.rast.contour", input="A", output="result", 
+        self.assertModuleFail("t.rast.contour", input="A", output="result",
                               step=1, minlevel=1, maxlevel=8,
                               where="start_time > '2001-02-01'",
                               nprocs=1, overwrite=True, verbose=True)



More information about the grass-commit mailing list