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

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jan 30 04:53:17 PST 2015


Author: huhabla
Date: 2015-01-30 04:53:17 -0800 (Fri, 30 Jan 2015)
New Revision: 64365

Modified:
   grass/trunk/temporal/t.rast.what/t.rast.what.py
   grass/trunk/temporal/t.rast.what/testsuite/test_what.py
Log:
temporal modules: Fixed col layout header, messages are now verbose, integrated stdout output and text coordinates support, integrated suggestions from Stefan Blumentrath. All new features are covered by tests.



Modified: grass/trunk/temporal/t.rast.what/t.rast.what.py
===================================================================
--- grass/trunk/temporal/t.rast.what/t.rast.what.py	2015-01-30 12:49:34 UTC (rev 64364)
+++ grass/trunk/temporal/t.rast.what/t.rast.what.py	2015-01-30 12:53:17 UTC (rev 64365)
@@ -26,13 +26,21 @@
 
 #%option G_OPT_V_INPUT
 #% key: points
+#% required: no
 #%end
 
+#%option G_OPT_M_COORDS
+#% required: no
+#%end
+
 #%option G_OPT_STRDS_INPUT
 #% key: strds
 #%end
 
 #%option G_OPT_F_OUTPUT
+#% required: no
+#% description: Name for the output file or "-" in case stdout should be used
+#% answer: -
 #%end
 
 #%option G_OPT_T_WHERE
@@ -67,7 +75,7 @@
 #%option
 #% key: nprocs
 #% type: integer
-#% description: Number of r.neighbor processes to run in parallel
+#% description: Number of r.what processes to run in parallel
 #% required: no
 #% multiple: no
 #% answer: 1
@@ -94,6 +102,7 @@
 ##% description: Output integer category values, not cell values
 ##%end
 
+import sys
 import copy
 import grass.script as gscript
 import grass.temporal as tgis
@@ -106,6 +115,7 @@
 
     # Get the options
     points = options["points"]
+    coordinates = options["coordinates"] 
     strds = options["strds"]
     output = options["output"]
     where = options["where"]
@@ -121,7 +131,13 @@
     #output_cat = flags["i"]
     
     overwrite = gscript.overwrite()
+    
+    if coordinates and points: 
+        gscript.fatal(_("Options coordinates and points are mutually exclusive"))
 
+    if not coordinates and not points: 
+        gscript.fatal(_("Please specify the coordinates or the points option, to provide the sampling coordinates"))
+
     # Make sure the temporal database exists
     tgis.init()
     # We need a database interface
@@ -158,11 +174,23 @@
     #    flags += "i"
 
     # Configure the r.what module
-    r_what = pymod.Module("r.what", map="dummy",
-                          output="dummy", run_=False,
-                          separator=separator, points=points,
-                          overwrite=overwrite, flags=flags,
-                          quiet=True)
+    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: 
+        # Create a list of values
+        coord_list = coordinates.split(",")
+        r_what = pymod.Module("r.what", map="dummy", 
+                                        output="dummy", run_=False, 
+                                        separator=separator,  
+                                        coordinates=coord_list, 
+                                        overwrite=overwrite, flags=flags, 
+                                        quiet=True) 
+    else: 
+        grass.error(_("Please specify points or coordinates"))
 
     if len(maps) < nprocs:
         nprocs = len(maps)
@@ -203,9 +231,9 @@
     
     process_queue.wait()
     
-    gscript.message("Number of raster map layers remaining for sampling %i"%(remaining_maps))
+    gscript.verbose("Number of raster map layers remaining for sampling %i"%(remaining_maps))
     if remaining_maps > 0:
-        # Use a single process if less then 400 maps
+        # Use a single process if less then 100 maps
         if remaining_maps <= 100:
             mod = copy.deepcopy(r_what)
             mod(map=map_names, output=file_name)
@@ -241,14 +269,15 @@
        output is of type: x,y,start,end,value
     """
     # open the output file for writing
-    out_file = open(output, "w")
+    out_file = open(output, 'w') if output != "-" else sys.stdout
+    
     if write_header is True:
         out_file.write("x%(sep)sy%(sep)sstart%(sep)send%(sep)svalue\n"\
                        %({"sep":separator}))
     
     for count in range(len(output_files)):
         file_name = output_files[count]
-        gscript.message(_("Transforming r.what output file %s"%(file_name)))
+        gscript.verbose(_("Transforming r.what output file %s"%(file_name)))
         map_list = output_time_list[count]
         in_file = open(file_name, "r")
         for line in in_file:
@@ -268,7 +297,9 @@
                 out_file.write(coor_string + time_string)
         
         in_file.close()
-    out_file.close()
+    
+    if out_file is not sys.stdout:
+        out_file.close()
         
 ############################################################################
 
@@ -281,12 +312,12 @@
        Each row represents a single raster map, hence a single time stamp
     """
     # open the output file for writing
-    out_file = open(output, "w")
+    out_file = open(output, 'w') if output != "-" else sys.stdout
         
     first = True
     for count in range(len(output_files)):
         file_name = output_files[count]
-        gscript.message(_("Transforming r.what output file %s"%(file_name)))
+        gscript.verbose(_("Transforming r.what output file %s"%(file_name)))
         map_list = output_time_list[count]
         in_file = open(file_name, "r")
         lines = in_file.readlines()
@@ -299,10 +330,10 @@
         
         if first is True:
             if write_header is True:
-                out_file.write("star%(sep)send"%({"sep":separator}))
-                for line in lines:
-                    x = matrix[0][0]
-                    y = matrix[0][1]
+                out_file.write("start%(sep)send"%({"sep":separator}))
+                for row in matrix:
+                    x = row[0]
+                    y = row[1]
                     out_file.write("%(sep)s%(x)10.10f;%(y)10.10f"\
                                    %({"sep":separator,
                                       "x":float(x), 
@@ -325,7 +356,8 @@
             out_file.write("\n")
 
         in_file.close()
-    out_file.close()
+    if out_file is not sys.stdout:
+        out_file.close()
 
 ############################################################################
 
@@ -339,7 +371,7 @@
         3730731.49590371|5642483.51236521|6|8|7|7
         3581249.04638104|5634411.97526282|5|8|7|7
     """
-    out_file = open(output, "w")
+    out_file = open(output, 'w') if output != "-" else sys.stdout
 
     matrix = []
     header = ""
@@ -347,7 +379,7 @@
     first = True
     for count in range(len(output_files)):
         file_name = output_files[count]
-        gscript.message("Transforming r.what output file %s"%(file_name))
+        gscript.verbose("Transforming r.what output file %s"%(file_name))
         map_list = output_time_list[count]
         in_file = open(file_name, "r")
 
@@ -377,7 +409,7 @@
 
     out_file.write(header + "\n")
 
-    gscript.message(_("Writing the output file <%s>"%(output)))
+    gscript.verbose(_("Writing the output file <%s>"%(output)))
     for row in matrix:
         first = True
         for col in row:
@@ -390,8 +422,8 @@
             first = False
 
         out_file.write("\n")
-
-    out_file.close()
+    if out_file is not sys.stdout:
+        out_file.close()
  
 ############################################################################
 
@@ -421,7 +453,9 @@
 
         output_time_list.append(map_list)
 
-        gscript.message(_("Process number %(proc)i samples %(num)i raster maps"%({"proc":process, "num":len(map_names)})))
+        gscript.verbose(_("Process maps %(samp_start)i to %(samp_end)i (of %(total)i)"\
+                                  %({"samp_start":count-len(map_names)+1, 
+                                  "samp_end":count, "total":len(maps)})))
         mod = copy.deepcopy(r_what)
         mod(map=map_names, output=final_file_name)
         #print(mod.get_bash())

Modified: grass/trunk/temporal/t.rast.what/testsuite/test_what.py
===================================================================
--- grass/trunk/temporal/t.rast.what/testsuite/test_what.py	2015-01-30 12:49:34 UTC (rev 64364)
+++ grass/trunk/temporal/t.rast.what/testsuite/test_what.py	2015-01-30 12:53:17 UTC (rev 64365)
@@ -48,31 +48,99 @@
         self.assertModule("t.rast.what",  strds="A",  output="out_row.txt", 
                           points="points", flags="n",
                           layout="row",
-                          nprocs=1,  overwrite=True)
+                          nprocs=1,  overwrite=True,  verbose=True)
 
         self.assertFileMd5("out_row.txt", "55209718566d70e1427bd1fecf844d53")
 
+
+    def test_row_output_coords(self):
+        self.assertModule("t.rast.what",  strds="A",  output="out_row_coords.txt", 
+                          coordinates=(30,30, 45, 45), flags="n",
+                          layout="row",
+                          nprocs=1,  overwrite=True,  verbose=True)
+
+        self.assertFileMd5("out_row_coords.txt", "cd917ac4848786f1b944512eed1da5bc")
+
     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)
+                          nprocs=1,  overwrite=True,  verbose=True)
 
-        self.assertFileMd5("out_col.txt", "825f73e284c0f6e09cf873bd3a1bf15f")
+        self.assertFileMd5("out_col.txt", "8720cc237b46ddb18f11440469d0e13f")
 
+    def test_col_output_coords(self):
+        self.assertModule("t.rast.what",  strds="A",  output="out_col_coords.txt", 
+                          coordinates=(30,30, 45, 45), flags="n",
+                          layout="col",
+                          nprocs=1,  overwrite=True,  verbose=True)
+
+        self.assertFileMd5("out_col_coords.txt", "ac44ebc5aa040d4ce2a5787e95f208ec")
+
     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)
+                          nprocs=1,  overwrite=True,  verbose=True)
 
         self.assertFileMd5("out_timerow.txt", "129fe0b63019e505232efa20ad42c03a")
 
+    def test_timerow_output_coords(self):
+        self.assertModule("t.rast.what",  strds="A",  output="out_timerow_coords.txt", 
+                          coordinates=(30,30, 45, 45), flags="n",
+                          layout="timerow",
+                          nprocs=1,  overwrite=True,  verbose=True)
+
+        self.assertFileMd5("out_timerow_coords.txt", "ca4ee0e7e4aaca170d6034e0d57d292d")
+
+    def test_row_stdout_where_parallel(self):
+        
+        t_rast_what = SimpleModule("t.rast.what",  strds="A",  output="-", 
+                                                      points="points", flags="n",
+                                                      where="start_time > '2001-03-01'",  
+                                                      nprocs=4,  overwrite=True,  verbose=True)
+        self.assertModule(t_rast_what)
+
+        text="""x|y|start|end|value
+115.0043586274|36.3593955783|2001-04-01 00:00:00|2001-07-01 00:00:00|200
+79.6816763826|45.2391522853|2001-04-01 00:00:00|2001-07-01 00:00:00|200
+97.4892579600|79.2347263950|2001-04-01 00:00:00|2001-07-01 00:00:00|200
+115.0043586274|36.3593955783|2001-07-01 00:00:00|2001-10-01 00:00:00|300
+79.6816763826|45.2391522853|2001-07-01 00:00:00|2001-10-01 00:00:00|300
+97.4892579600|79.2347263950|2001-07-01 00:00:00|2001-10-01 00:00:00|300
+115.0043586274|36.3593955783|2001-10-01 00:00:00|2002-01-01 00:00:00|400
+79.6816763826|45.2391522853|2001-10-01 00:00:00|2002-01-01 00:00:00|400
+97.4892579600|79.2347263950|2001-10-01 00:00:00|2002-01-01 00:00:00|400
+"""
+        self.assertLooksLike(text,  t_rast_what.outputs.stdout)
+
+    def test_row_stdout_where_parallel2(self):
+        """Here without output definition, the default is used then"""
+        
+        t_rast_what = SimpleModule("t.rast.what",  strds="A",  
+                                                      points="points", flags="n",
+                                                      where="start_time > '2001-03-01'",  
+                                                      nprocs=4,  overwrite=True,  verbose=True)
+        self.assertModule(t_rast_what)
+
+        text="""x|y|start|end|value
+115.0043586274|36.3593955783|2001-04-01 00:00:00|2001-07-01 00:00:00|200
+79.6816763826|45.2391522853|2001-04-01 00:00:00|2001-07-01 00:00:00|200
+97.4892579600|79.2347263950|2001-04-01 00:00:00|2001-07-01 00:00:00|200
+115.0043586274|36.3593955783|2001-07-01 00:00:00|2001-10-01 00:00:00|300
+79.6816763826|45.2391522853|2001-07-01 00:00:00|2001-10-01 00:00:00|300
+97.4892579600|79.2347263950|2001-07-01 00:00:00|2001-10-01 00:00:00|300
+115.0043586274|36.3593955783|2001-10-01 00:00:00|2002-01-01 00:00:00|400
+79.6816763826|45.2391522853|2001-10-01 00:00:00|2002-01-01 00:00:00|400
+97.4892579600|79.2347263950|2001-10-01 00:00:00|2002-01-01 00:00:00|400
+"""
+        self.assertLooksLike(text,  t_rast_what.outputs.stdout)
+
     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)
+                          nprocs=4,  overwrite=True,  verbose=True)
 
         self.assertFileMd5("out_where.txt", "af731bec01fedc262f4ac162fe420707")
 
@@ -80,14 +148,18 @@
         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)
+                          nprocs=4,  overwrite=True,  verbose=True)
 
 
 class TestRasterWhatFails(TestCase):
 
     def test_error_handling(self):
-        # No vector map, no strds
+        # No vector map, no strds, no coordinates
         self.assertModuleFail("t.rast.what",  output="out.txt")
+        # No vector map, no coordinates
+        self.assertModuleFail("t.rast.what",  strds="A",  output="out.txt")
+        # Points and coordinates are mutually exclusive
+        self.assertModuleFail("t.rast.what",  points="points", coordinates=(30, 30, 45, 45),  output="out.txt",  strds="A")
 
 if __name__ == '__main__':
     from grass.gunittest.main import test



More information about the grass-commit mailing list