[GRASS-SVN] r71175 - in grass/trunk/temporal/t.rast.what: . testsuite
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Jun 10 14:53:51 PDT 2017
Author: huhabla
Date: 2017-06-10 14:53:51 -0700 (Sat, 10 Jun 2017)
New Revision: 71175
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 null value bug, null value definitions are now passed to r.what correctly
Modified: grass/trunk/temporal/t.rast.what/t.rast.what.py
===================================================================
--- grass/trunk/temporal/t.rast.what/t.rast.what.py 2017-06-08 20:52:35 UTC (rev 71174)
+++ grass/trunk/temporal/t.rast.what/t.rast.what.py 2017-06-10 21:53:51 UTC (rev 71175)
@@ -5,10 +5,10 @@
# MODULE: t.rast.what
# AUTHOR(S): Soeren Gebbert
#
-# PURPOSE: Sample a space time raster dataset at specific vector point
-# coordinates and write the output to stdout using different
+# PURPOSE: Sample a space time raster dataset at specific vector point
+# coordinates and write the output to stdout using different
# layouts
-#
+#
# COPYRIGHT: (C) 2015 by the GRASS Development Team
#
# This program is free software under the GNU General Public
@@ -129,7 +129,7 @@
# Get the options
points = options["points"]
- coordinates = options["coordinates"]
+ coordinates = options["coordinates"]
strds = options["strds"]
output = options["output"]
where = options["where"]
@@ -137,7 +137,7 @@
layout = options["layout"]
null_value = options["null_value"]
separator = gscript.separator(options["separator"])
-
+
nprocs = int(options["nprocs"])
write_header = flags["n"]
use_stdin = flags["i"]
@@ -146,13 +146,13 @@
#output_cat_label = flags["f"]
#output_color = flags["r"]
#output_cat = flags["i"]
-
+
overwrite = gscript.overwrite()
-
- if coordinates and points:
+
+ if coordinates and points:
gscript.fatal(_("Options coordinates and points are mutually exclusive"))
- if not coordinates and not points and not use_stdin:
+ if not coordinates and not points and not use_stdin:
gscript.fatal(_("Please specify the coordinates, the points option or use the 'i' flag to pipe coordinate positions to t.rast.what from stdin, to provide the sampling coordinates"))
if vcat and not points:
@@ -176,7 +176,7 @@
dbif.connect()
sp = tgis.open_old_stds(strds, "strds", dbif)
- maps = sp.get_registered_maps_as_objects(where=where, order=order,
+ maps = sp.get_registered_maps_as_objects(where=where, order=order,
dbif=dbif)
dbif.close()
if not maps:
@@ -194,29 +194,32 @@
flags += "v"
# 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:
+ if points:
+ r_what = pymod.Module("r.what", map="dummy",
+ output="dummy", run_=False,
+ separator=separator, points=points,
+ overwrite=overwrite, flags=flags,
+ null_value=null_value,
+ 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)
+ r_what = pymod.Module("r.what", map="dummy",
+ output="dummy", run_=False,
+ separator=separator,
+ coordinates=coord_list,
+ overwrite=overwrite, flags=flags,
+ null_value=null_value,
+ 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:
+ r_what = pymod.Module("r.what", map="dummy",
+ output="dummy", run_=False,
+ separator=separator,
+ stdin_=coordinates_stdin,
+ overwrite=overwrite, flags=flags,
+ null_value=null_value,
+ quiet=True)
+ else:
gscript.error(_("Please specify points or coordinates"))
if len(maps) < nprocs:
@@ -225,7 +228,7 @@
# The module queue for parallel execution
process_queue = pymod.ParallelModuleQueue(int(nprocs))
num_maps = len(maps)
-
+
# 400 Maps is the absolute maximum in r.what
# We need to determie the number of maps that can be processed
# in parallel
@@ -252,12 +255,12 @@
count = 0
for loop in range(num_loops):
file_name = gscript.tempfile() + "_%i"%(loop)
- count = process_loop(nprocs, maps, file_name, count, maps_per_process,
- remaining_maps_per_loop, output_files,
+ 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()
-
+
gscript.verbose("Number of raster map layers remaining for sampling %i"%(remaining_maps))
if remaining_maps > 0:
# Use a single process if less then 100 maps
@@ -273,15 +276,15 @@
else:
maps_per_process = int(remaining_maps / nprocs)
remaining_maps_per_loop = remaining_maps % nprocs
-
+
file_name = "out_remain"
- process_loop(nprocs, maps, file_name, count, maps_per_process,
- remaining_maps_per_loop, output_files,
+ process_loop(nprocs, maps, file_name, count, maps_per_process,
+ remaining_maps_per_loop, output_files,
output_time_list, r_what, process_queue)
# Wait for unfinished processes
process_queue.wait()
-
+
# Out the output files in the correct order together
if layout == "row":
one_point_per_row_output(separator, output_files, output_time_list,
@@ -302,7 +305,7 @@
"""
# open the output file for writing
out_file = open(output, 'w') if output != "-" else sys.stdout
-
+
if write_header is True:
out_str = ""
if vcat:
@@ -312,7 +315,7 @@
else:
out_str += "x{sep}y{sep}start{sep}end{sep}value\n"
out_file.write(out_str.format(sep=separator))
-
+
for count in range(len(output_files)):
file_name = output_files[count]
gscript.verbose(_("Transforming r.what output file %s"%(file_name)))
@@ -328,7 +331,7 @@
if site_input:
site = line[3]
values = line[5:]
-
+
else:
x = line[0]
y = line[1]
@@ -353,25 +356,25 @@
"val":(values[i].strip()),"sep":separator})
out_file.write(cat_str + coor_string + time_string)
-
+
in_file.close()
-
+
if out_file is not sys.stdout:
out_file.close()
-
+
############################################################################
def one_point_per_col_output(separator, output_files, output_time_list,
output, write_header, site_input, vcat):
"""Write one point per col
- output is of type:
+ output is of type:
start,end,point_1 value,point_2 value,...,point_n value
-
+
Each row represents a single raster map, hence a single time stamp
"""
# open the output file for writing
out_file = open(output, 'w') if output != "-" else sys.stdout
-
+
first = True
for count in range(len(output_files)):
file_name = output_files[count]
@@ -379,13 +382,13 @@
map_list = output_time_list[count]
in_file = open(file_name, "r")
lines = in_file.readlines()
-
+
matrix = []
for line in lines:
matrix.append(line.split(separator))
-
+
num_cols = len(matrix[0])
-
+
if first is True:
if write_header is True:
out_str = "start%(sep)send"%({"sep":separator})
@@ -442,9 +445,9 @@
def one_point_per_timerow_output(separator, output_files, output_time_list,
output, write_header, site_input, vcat):
- """Use the original layout of the r.what output and print instead of
+ """Use the original layout of the r.what output and print instead of
the raster names, the time stamps as header
-
+
One point per line for all time stamps:
x|y|1991-01-01 00:00:00;1991-01-02 00:00:00|1991-01-02 00:00:00;1991-01-03 00:00:00|1991-01-03 00:00:00;1991-01-04 00:00:00|1991-01-04 00:00:00;1991-01-05 00:00:00
3730731.49590371|5642483.51236521|6|8|7|7
@@ -505,21 +508,21 @@
first = True
for col in row:
value = col.strip()
-
+
if first is False:
out_file.write("%s"%(separator))
out_file.write(value)
-
+
first = False
out_file.write("\n")
if out_file is not sys.stdout:
out_file.close()
-
+
############################################################################
-def process_loop(nprocs, maps, file_name, count, maps_per_process,
- remaining_maps_per_loop, output_files,
+def process_loop(nprocs, maps, file_name, count, maps_per_process,
+ remaining_maps_per_loop, output_files,
output_time_list, r_what, process_queue):
"""Call r.what in parallel subprocesses"""
first = True
@@ -533,7 +536,7 @@
# Temporary output file
final_file_name = file_name + "_%i"%(process)
output_files.append(final_file_name)
-
+
map_names = []
map_list = []
for i in range(num):
@@ -545,7 +548,7 @@
output_time_list.append(map_list)
gscript.verbose(_("Process maps %(samp_start)i to %(samp_end)i (of %(total)i)"\
- %({"samp_start":count-len(map_names)+1,
+ %({"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)
@@ -553,7 +556,7 @@
process_queue.put(mod)
return count
-
+
############################################################################
if __name__ == "__main__":
Modified: grass/trunk/temporal/t.rast.what/testsuite/test_what.py
===================================================================
--- grass/trunk/temporal/t.rast.what/testsuite/test_what.py 2017-06-08 20:52:35 UTC (rev 71174)
+++ grass/trunk/temporal/t.rast.what/testsuite/test_what.py 2017-06-10 21:53:51 UTC (rev 71175)
@@ -215,6 +215,54 @@
self.assertModuleFail("t.rast.what", points="points", strds="A",
coordinates=(30, 30, 45, 45), output="out.txt")
+
+class TestRasterWhatNull(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_null = null()", overwrite=True)
+
+ cls.runModule("v.random", output="points", npoints=1, 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_null", 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_null_value(self):
+ """Test setting the null value
+ """
+
+ t_rast_what = SimpleModule("t.rast.what", strds="A", output="-",
+ points="points", flags="n",
+ where="start_time >= '2001-01-01'",
+ null_value="NULL",
+ nprocs=4, overwrite=True, verbose=True)
+ self.assertModule(t_rast_what)
+
+ text="""x|y|start|end|value
+115.0043586274|36.3593955783|2001-01-01 00:00:00|2001-04-01 00:00:00|100
+115.0043586274|36.3593955783|2001-04-01 00:00:00|2001-07-01 00:00:00|NULL
+"""
+ self.assertLooksLike(text, t_rast_what.outputs.stdout)
+
+
if __name__ == '__main__':
from grass.gunittest.main import test
test()
More information about the grass-commit
mailing list