[GRASS-SVN] r65964 - in grass/trunk/temporal/t.rast.what: . testsuite
svn_grass at osgeo.org
svn_grass at osgeo.org
Tue Aug 18 05:50:01 PDT 2015
Author: huhabla
Date: 2015-08-18 05:50:01 -0700 (Tue, 18 Aug 2015)
New Revision: 65964
Modified:
grass/trunk/temporal/t.rast.what/t.rast.what.py
grass/trunk/temporal/t.rast.what/testsuite/test_what.py
Log:
Implemented the use of stdin provided as patch in ticket #2721.
Implemented unittest to test the enhancement.
Modified: grass/trunk/temporal/t.rast.what/t.rast.what.py
===================================================================
--- grass/trunk/temporal/t.rast.what/t.rast.what.py 2015-08-18 11:27:23 UTC (rev 65963)
+++ grass/trunk/temporal/t.rast.what/t.rast.what.py 2015-08-18 12:50:01 UTC (rev 65964)
@@ -31,6 +31,7 @@
#%option G_OPT_M_COORDS
#% required: no
+#% description: Comma separated list of coordinates or "-" in case stdin should be used
#%end
#%option G_OPT_STRDS_INPUT
@@ -86,6 +87,11 @@
#% description: Output header row
#%end
+#%flag
+#% key: i
+#% description: Use stdin as input and ignore coordinates and point option
+#%end
+
## Temporary disabled the r.what flags due to test issues
##%flag
##% key: f
@@ -126,6 +132,8 @@
nprocs = int(options["nprocs"])
write_header = flags["n"]
+ use_stdin = flags["i"]
+
#output_cat_label = flags["f"]
#output_color = flags["r"]
#output_cat = flags["i"]
@@ -135,9 +143,20 @@
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"))
+ if not coordinates and not points and not use_stdin:
+ gscript.fatal(_("Please specify the coordinates, the points option or use the 's' option to pipe coordinate positions to t.rast.what from stdin, to provide the sampling coordinates"))
+ if use_stdin:
+ coordinates_stdin = str(sys.__stdin__.read())
+ # Check if coordinates are given with site names or IDs
+ stdin_length = len(coordinates_stdin.split('\n')[0].split())
+ if stdin_length <= 2:
+ site_input = False
+ elif stdin_length >= 3:
+ site_input = True
+ else:
+ site_input = False
+
# Make sure the temporal database exists
tgis.init()
# We need a database interface
@@ -173,6 +192,8 @@
#if output_cat is True:
# flags += "i"
+
+
# Configure the r.what module
if points:
r_what = pymod.Module("r.what", map="dummy",
@@ -188,7 +209,14 @@
separator=separator,
coordinates=coord_list,
overwrite=overwrite, flags=flags,
- quiet=True)
+ quiet=True)
+ elif use_stdin:
+ r_what = pymod.Module("r.what", map="dummy",
+ output="dummy", run_=False,
+ separator=separator,
+ stdin_=coordinates_stdin,
+ overwrite=overwrite, flags=flags,
+ quiet=True)
else:
grass.error(_("Please specify points or coordinates"))
@@ -253,18 +281,18 @@
# Out the output files in the correct order together
if layout == "row":
one_point_per_row_output(separator, output_files, output_time_list,
- output, write_header)
+ output, write_header, site_input)
elif layout == "col":
one_point_per_col_output(separator, output_files, output_time_list,
- output, write_header)
+ output, write_header, site_input)
else:
one_point_per_timerow_output(separator, output_files, output_time_list,
- output, write_header)
+ output, write_header, site_input)
############################################################################
def one_point_per_row_output(separator, output_files, output_time_list,
- output, write_header):
+ output, write_header, site_input):
"""Write one point per row
output is of type: x,y,start,end,value
"""
@@ -272,8 +300,13 @@
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"\
+ if site_input:
+ out_file.write("x%(sep)sy%(sep)ssite%(sep)sstart%(sep)send%(sep)svalue\n"\
%({"sep":separator}))
+ else:
+ 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]
@@ -284,11 +317,18 @@
line = line.split(separator)
x = line[0]
y = line[1]
+ if site_input:
+ site = line[2]
+
# We ignore the site name
values = line[3:]
for i in range(len(values)):
start, end = map_list[i].get_temporal_extent_as_tuple()
- coor_string = "%(x)10.10f%(sep)s%(y)10.10f%(sep)s"\
+ if site_input:
+ coor_string = "%(x)10.10f%(sep)s%(y)10.10f%(sep)s%(site_name)s%(sep)s"\
+ %({"x":float(x),"y":float(y),"site_name":str(site),"sep":separator})
+ else:
+ 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),
@@ -304,7 +344,7 @@
############################################################################
def one_point_per_col_output(separator, output_files, output_time_list,
- output, write_header):
+ output, write_header, site_input):
"""Write one point per col
output is of type:
start,end,point_1 value,point_2 value,...,point_n value
@@ -331,13 +371,25 @@
if first is True:
if write_header is True:
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"\
+ if site_input:
+ for row in matrix:
+ x = row[0]
+ y = row[1]
+ site = row[2]
+ out_file.write("%(sep)s%(x)10.10f;%(y)10.10f;%(site_name)s"\
%({"sep":separator,
"x":float(x),
+ "y":float(y),
+ "site_name":str(site)}))
+ else:
+ 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),
"y":float(y)}))
+
out_file.write("\n")
first = False
@@ -362,7 +414,7 @@
############################################################################
def one_point_per_timerow_output(separator, output_files, output_time_list,
- output, write_header):
+ output, write_header, site_input):
"""Use the original layout of the r.waht output and print instead of
the raster names, the time stamps as header
@@ -385,7 +437,10 @@
if write_header:
if first is True:
- header = "x%(sep)sy"%({"sep":separator})
+ if site_input:
+ header = "x%(sep)sy%(sep)ssite"%({"sep":separator})
+ else:
+ header = "x%(sep)sy"%({"sep":separator})
for map in map_list:
start, end = map.get_temporal_extent_as_tuple()
time_string = "%(sep)s%(start)s;%(end)s"\
@@ -399,7 +454,10 @@
cols = lines[i].split(separator)
if first is True:
- matrix.append(cols[:2])
+ if site_input:
+ matrix.append(cols[:3])
+ else:
+ matrix.append(cols[:2])
matrix[i] = matrix[i] + cols[3:]
Modified: grass/trunk/temporal/t.rast.what/testsuite/test_what.py
===================================================================
--- grass/trunk/temporal/t.rast.what/testsuite/test_what.py 2015-08-18 11:27:23 UTC (rev 65963)
+++ grass/trunk/temporal/t.rast.what/testsuite/test_what.py 2015-08-18 12:50:01 UTC (rev 65964)
@@ -59,6 +59,15 @@
self.assertFileMd5("out_row_coords.txt", "cd917ac4848786f1b944512eed1da5bc", text=True)
+
+ def test_row_output_coords_stdin(self):
+ self.assertModule("t.rast.what", strds="A", output="out_row_coords.txt",
+ flags="ni",
+ layout="row", stdin_="30 30\n45 45",
+ nprocs=1, overwrite=True, verbose=True)
+
+ self.assertFileMd5("out_row_coords.txt", "cd917ac4848786f1b944512eed1da5bc", text=True)
+
def test_col_output(self):
self.assertModule("t.rast.what", strds="A", output="out_col.txt",
points="points", flags="n",
More information about the grass-commit
mailing list