[GRASS-SVN] r64349 - in grass/trunk/temporal/t.rast.what: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jan 28 14:39:56 PST 2015


Author: huhabla
Date: 2015-01-28 14:39:56 -0800 (Wed, 28 Jan 2015)
New Revision: 64349

Added:
   grass/trunk/temporal/t.rast.what/testsuite/
   grass/trunk/temporal/t.rast.what/testsuite/test_what.py
Modified:
   grass/trunk/temporal/t.rast.what/t.rast.what.py
Log:
temporal modules: Added test, disabled some (for now) unimportant options


Modified: grass/trunk/temporal/t.rast.what/t.rast.what.py
===================================================================
--- grass/trunk/temporal/t.rast.what/t.rast.what.py	2015-01-28 20:21:26 UTC (rev 64348)
+++ grass/trunk/temporal/t.rast.what/t.rast.what.py	2015-01-28 22:39:56 UTC (rev 64349)
@@ -37,9 +37,6 @@
 #%option G_OPT_T_WHERE
 #%end
 
-#%option G_OPT_M_COORDS
-#%end
-
 #%option G_OPT_M_NULL_VALUE
 #%end
 
@@ -80,23 +77,23 @@
 #% description: Output header row
 #%end
 
-#%flag
-#% key: f
-#% description: Show the category labels of the grid cell(s)
-#%end
+##%flag
+##% key: f
+##% description: Show the category labels of the grid cell(s)
+##%end
 
-#%flag
-#% key: r
-#% description: Output color values as RRR:GGG:BBB
-#%end
+##%flag
+##% key: r
+##% description: Output color values as RRR:GGG:BBB
+##%end
 
-#%flag
-#% key: i
-#% description: Output integer category values, not cell values
-#%end
+##%flag
+##% key: i
+##% description: Output integer category values, not cell values
+##%end
 
 import copy
-import grass.script as grass
+import grass.script as gscript
 import grass.temporal as tgis
 import grass.pygrass.modules as pymod
 
@@ -112,20 +109,16 @@
     where = options["where"]
     order = options["order"]
     layout = options["layout"]
-    coordinates = options["coordinates"]
     null_value = options["null_value"]
     separator = options["separator"]
     
     nprocs = int(options["nprocs"])
     write_header = flags["n"]
-    output_cat_label = flags["f"]
-    output_color = flags["r"]
-    output_cat = flags["i"]
+    #output_cat_label = flags["f"]
+    #output_color = flags["r"]
+    #output_cat = flags["i"]
     
-    overwrite = grass.overwrite()
-    
-    if coordinates and points:
-        grass.error(_("Options coordinates and points are mutually exclusive"))
+    overwrite = gscript.overwrite()
 
     # Make sure the temporal database exists
     tgis.init()
@@ -139,8 +132,7 @@
     dbif.close()
 
     if not maps:
-        grass.warning(_("Space time raster dataset <%s> is empty") % sp.get_id())
-        return
+        gscript.fatal(_("Space time raster dataset <%s> is empty") % sp.get_id())
 
     # Setup separator
     if separator == "pipe":
@@ -156,30 +148,23 @@
 
     # Setup flags
     flags = ""
-    if output_cat_label is True:
-        flags += "f"
-    if output_color is True:
-        flags += "r"
-    if output_cat is True:
-        flags += "f"
+    #if output_cat_label is True:
+    #    flags += "f"
+    #if output_color is True:
+    #    flags += "r"
+    #if output_cat is True:
+    #    flags += "i"
 
     # Configure the r.what module
-    if points:
-        r_what = pymod.Module("r.what", map="dummy",
-                              output="dummy", run_=False,
-                              separator=separator, points=points,
-                              overwrite=overwrite, flags=flags,
-                              quiet=True)
-    elif coordinates:
-        r_what = pymod.Module("r.what", map="dummy",
-                              output="dummy", run_=False,
-                              separator=separator, 
-                              coordinates=coordinates,
-                              overwrite=overwrite, flags=flags,
-                              quiet=True)
-    else:
-        grass.error(_("Please specify points or coordinates"))
+    r_what = pymod.Module("r.what", map="dummy",
+                          output="dummy", run_=False,
+                          separator=separator, points=points,
+                          overwrite=overwrite, flags=flags,
+                          quiet=True)
 
+    if len(maps) < nprocs:
+        nprocs = len(maps)
+
     # The module queue for parallel execution
     process_queue = pymod.ParallelModuleQueue(int(nprocs))
     num_maps = len(maps)
@@ -209,18 +194,17 @@
 
     count = 0
     for loop in range(num_loops):
-        file_name = "out_%i"%(loop)
+        file_name = gscript.tempfile() + "_%i"%(loop)
         count = process_loop(nprocs, maps, file_name, count, maps_per_process, 
                              remaining_maps_per_loop, output_files, 
                              output_time_list, r_what, process_queue)
     
     process_queue.wait()
     
-    print "Remaining maps", remaining_maps, count, len(maps)
+    gscript.message("Number of raster map layers remaining for sampling %i"%(remaining_maps))
     if remaining_maps > 0:
         # Use a single process if less then 400 maps
         if remaining_maps <= 100:
-            print "Single remain process"
             mod = copy.deepcopy(r_what)
             mod(map=map_names, output=file_name)
             process_queue.put(mod)
@@ -262,8 +246,8 @@
     
     for count in range(len(output_files)):
         file_name = output_files[count]
+        gscript.message(_("Transforming r.what output file %s"%(file_name)))
         map_list = output_time_list[count]
-        print "Transform", file_name
         in_file = open(file_name, "r")
         for line in in_file:
             line = line.split(separator)
@@ -273,7 +257,7 @@
             values = line[3:]
             for i in range(len(values)):
                 start, end = map_list[i].get_temporal_extent_as_tuple()
-                coor_string = "%(x)10.7f%(sep)s%(y)10.7f%(sep)s"\
+                coor_string = "%(x)10.10f%(sep)s%(y)10.10f%(sep)s"\
                                %({"x":float(x),"y":float(y),"sep":separator})
                 time_string = "%(start)s%(sep)s%(end)s%(sep)s%(val)s\n"\
                                %({"start":str(start), "end":str(end),
@@ -300,8 +284,8 @@
     first = True
     for count in range(len(output_files)):
         file_name = output_files[count]
+        gscript.message(_("Transforming r.what output file %s"%(file_name)))
         map_list = output_time_list[count]
-        print "Transform", file_name
         in_file = open(file_name, "r")
         lines = in_file.readlines()
         
@@ -317,7 +301,7 @@
                 for line in lines:
                     x = matrix[0][0]
                     y = matrix[0][1]
-                    out_file.write("%(sep)s%(x)10.7f;%(y)10.7f"\
+                    out_file.write("%(sep)s%(x)10.10f;%(y)10.10f"\
                                    %({"sep":separator,
                                       "x":float(x), 
                                       "y":float(y)}))
@@ -361,8 +345,8 @@
     first = True
     for count in range(len(output_files)):
         file_name = output_files[count]
+        gscript.message("Transforming r.what output file %s"%(file_name))
         map_list = output_time_list[count]
-        print "Transform orig", file_name
         in_file = open(file_name, "r")
 
         if write_header:
@@ -391,6 +375,7 @@
 
     out_file.write(header + "\n")
 
+    gscript.message(_("Writing the output file %s"%(output)))
     for row in matrix:
         first = True
         for col in row:
@@ -434,7 +419,7 @@
 
         output_time_list.append(map_list)
 
-        print "Process", process, "Maps", len(map_names)
+        gscript.message(_("Process number %(proc)i samples %(num)i raster maps"%({"proc":process, "num":len(map_names)})))
         mod = copy.deepcopy(r_what)
         mod(map=map_names, output=final_file_name)
         #print(mod.get_bash())
@@ -445,5 +430,5 @@
 ############################################################################
 
 if __name__ == "__main__":
-    options, flags = grass.parser()
+    options, flags = gscript.parser()
     main(options, flags)

Added: grass/trunk/temporal/t.rast.what/testsuite/test_what.py
===================================================================
--- grass/trunk/temporal/t.rast.what/testsuite/test_what.py	                        (rev 0)
+++ grass/trunk/temporal/t.rast.what/testsuite/test_what.py	2015-01-28 22:39:56 UTC (rev 64349)
@@ -0,0 +1,94 @@
+"""Test t.rast.what
+
+(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.
+
+ at author Soeren Gebbert
+"""
+
+import grass.pygrass.modules as pymod
+import subprocess
+from grass.gunittest.case import TestCase
+from grass.gunittest.gmodules import SimpleModule
+
+class TestRasterWhat(TestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        """Initiate the temporal GIS and set the region
+        """
+        cls.use_temp_region()
+        cls.runModule("g.region",  s=0,  n=80,  w=0,  e=120,  b=0,  t=50,  res=10,  res3=10)
+
+        cls.runModule("r.mapcalc", expression="a_1 = 100",  overwrite=True)
+        cls.runModule("r.mapcalc", expression="a_2 = 200",  overwrite=True)
+        cls.runModule("r.mapcalc", expression="a_3 = 300",  overwrite=True)
+        cls.runModule("r.mapcalc", expression="a_4 = 400",  overwrite=True)
+        
+        cls.runModule("v.random", output="points", npoints=3, seed=1, overwrite=True)
+
+        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",  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",  
+                                   inputs="A")
+        cls.del_temp_region()
+
+    def test_row_output(self):
+        self.assertModule("t.rast.what",  strds="A",  output="out_row.txt", 
+                          points="points", flags="n",
+                          layout="row",
+                          nprocs=1,  overwrite=True)
+
+        self.assertFileMd5("out_row.txt", "55209718566d70e1427bd1fecf844d53")
+
+    def test_col_output(self):
+        self.assertModule("t.rast.what",  strds="A",  output="out_col.txt", 
+                          points="points", flags="n",
+                          layout="col",
+                          nprocs=1,  overwrite=True)
+
+        self.assertFileMd5("out_col.txt", "825f73e284c0f6e09cf873bd3a1bf15f")
+
+    def test_timerow_output(self):
+        self.assertModule("t.rast.what",  strds="A",  output="out_timerow.txt", 
+                          points="points", flags="n",
+                          layout="timerow",
+                          nprocs=1,  overwrite=True)
+
+        self.assertFileMd5("out_timerow.txt", "129fe0b63019e505232efa20ad42c03a")
+
+    def test_row_output_where_parallel(self):
+        self.assertModule("t.rast.what",  strds="A",  output="out_where.txt", 
+                          points="points", flags="n",
+                          where="start_time > '2001-03-01'",  
+                          nprocs=4,  overwrite=True)
+
+        self.assertFileMd5("out_where.txt", "af731bec01fedc262f4ac162fe420707")
+
+    def test_empty_strds(self):
+        self.assertModuleFail("t.rast.what",  strds="A",  output="out_error.txt", 
+                          points="points", flags="n",
+                          where="start_time > '2002-03-01'",  
+                          nprocs=4,  overwrite=True)
+
+
+class TestRasterWhatFails(TestCase):
+
+    def test_error_handling(self):
+        # No vector map, no strds
+        self.assertModuleFail("t.rast.what",  output="out.txt")
+
+if __name__ == '__main__':
+    from grass.gunittest.main import test
+    test()



More information about the grass-commit mailing list