[GRASS-SVN] r66572 - in grass/trunk: lib/python/temporal temporal/t.rast.univar temporal/t.rast.univar/testsuite
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Oct 22 02:48:51 PDT 2015
Author: huhabla
Date: 2015-10-22 02:48:51 -0700 (Thu, 22 Oct 2015)
New Revision: 66572
Added:
grass/trunk/temporal/t.rast.univar/testsuite/test_univar.py
Removed:
grass/trunk/temporal/t.rast.univar/testsuite/test.t.rast.univar.sh
Modified:
grass/trunk/lib/python/temporal/univar_statistics.py
grass/trunk/temporal/t.rast.univar/t.rast.univar.py
Log:
temporal framework: Added output and region flag to t.rast.univar
Modified: grass/trunk/lib/python/temporal/univar_statistics.py
===================================================================
--- grass/trunk/lib/python/temporal/univar_statistics.py 2015-10-22 08:58:18 UTC (rev 66571)
+++ grass/trunk/lib/python/temporal/univar_statistics.py 2015-10-22 09:48:51 UTC (rev 66572)
@@ -7,8 +7,9 @@
import grass.temporal as tgis
- tgis.print_gridded_dataset_univar_statistics(type, input, where, extended, no_header, fs)
+ tgis.print_gridded_dataset_univar_statistics(type, input, output, where, extended, no_header, fs, rast_region)
+..
(C) 2012-2013 by the GRASS Development Team
This program is free software under the GNU General Public
@@ -24,16 +25,21 @@
###############################################################################
-def print_gridded_dataset_univar_statistics(type, input, where, extended,
- no_header=False, fs="|"):
+def print_gridded_dataset_univar_statistics(type, input, output, where, extended,
+ no_header=False, fs="|",
+ rast_region=False):
"""Print univariate statistics for a space time raster or raster3d dataset
:param type: Must be "strds" or "str3ds"
:param input: The name of the space time dataset
+ :param output: Name of the optional output file, if None stdout is used
:param where: A temporal database where statement
:param extended: If True compute extended statistics
:param no_header: Supress the printing of column names
:param fs: Field separator
+ :param rast_region: If set True ignore the current region settings
+ and use the raster map regions for univar statistical calculation.
+ Only available for strds.
"""
# We need a database interface
@@ -42,6 +48,9 @@
sp = open_old_stds(input, type, dbif)
+ if output is not None:
+ out_file = open(output, "w")
+
rows = sp.get_registered_maps(
"id,start_time,end_time", where, "start_time", dbif)
@@ -61,7 +70,10 @@
string += fs + "first_quartile" + fs + "median" + fs
string += "third_quartile" + fs + "percentile_90"
- print string
+ if output is None:
+ print string
+ else:
+ out_file.write(string + "\n")
for row in rows:
string = ""
@@ -73,6 +85,8 @@
if extended is True:
flag += "e"
+ if type == "strds" and rast_region is True:
+ flag += "r"
if type == "strds":
stats = gscript.parse_command("r.univar", map=id, flags=flag)
@@ -97,10 +111,18 @@
if extended is True:
string += fs + str(stats["first_quartile"]) + fs + str(stats["median"])
string += fs + str(stats["third_quartile"]) + fs + str(stats["percentile_90"])
- print string
+ if output is None:
+ print string
+ else:
+ out_file.write(string + "\n")
+
dbif.close()
+ if output is not None:
+ out_file.close()
+
+
###############################################################################
@@ -110,6 +132,7 @@
"""Print univariate statistics for a space time vector dataset
:param input: The name of the space time dataset
+ :param output: Name of the optional output file, if None stdout is used
:param twhere: A temporal database where statement
:param layer: The layer number used in case no layer is present
in the temporal dataset
Modified: grass/trunk/temporal/t.rast.univar/t.rast.univar.py
===================================================================
--- grass/trunk/temporal/t.rast.univar/t.rast.univar.py 2015-10-22 08:58:18 UTC (rev 66571)
+++ grass/trunk/temporal/t.rast.univar/t.rast.univar.py 2015-10-22 09:48:51 UTC (rev 66572)
@@ -25,6 +25,12 @@
#%option G_OPT_STRDS_INPUT
#%end
+
+#%option G_OPT_F_OUTPUT
+#% required: no
+#%end
+
+
#%option G_OPT_T_WHERE
#% guisection: Selection
#%end
@@ -40,6 +46,11 @@
#%end
#%flag
+#% key: r
+#% description: Ignore the current region settings and use the raster map regions for univar statistical calculation
+#%end
+
+#%flag
#% key: s
#% description: Suppress printing of column names
#% guisection: Formatting
@@ -55,16 +66,23 @@
# Get the options
input = options["input"]
+ output = options["output"]
where = options["where"]
extended = flags["e"]
no_header = flags["s"]
+ rast_region = bool(flags["r"])
separator = grass.separator(options["separator"])
# Make sure the temporal database exists
tgis.init()
+ if not output:
+ output = None
+ if output == "-":
+ output = None
+
tgis.print_gridded_dataset_univar_statistics(
- "strds", input, where, extended, no_header, separator)
+ "strds", input, output, where, extended, no_header, separator, rast_region)
if __name__ == "__main__":
options, flags = grass.parser()
Deleted: grass/trunk/temporal/t.rast.univar/testsuite/test.t.rast.univar.sh
===================================================================
--- grass/trunk/temporal/t.rast.univar/testsuite/test.t.rast.univar.sh 2015-10-22 08:58:18 UTC (rev 66571)
+++ grass/trunk/temporal/t.rast.univar/testsuite/test.t.rast.univar.sh 2015-10-22 09:48:51 UTC (rev 66572)
@@ -1,27 +0,0 @@
-#!/bin/sh
-# Test univariate statistics for space time raster datasets
-
-# We need to set a specific region in the
-# @preprocess step of this test.
-# The region setting should work for UTM and LL test locations
-g.region s=0 n=80 w=0 e=120 b=0 t=50 res=10 res3=10 -p3
-
-export GRASS_OVERWRITE=1
-
-# Data generation
-r.mapcalc expr="prec_1 = rand(0, 550)" -s
-r.mapcalc expr="prec_2 = rand(0, 450)" -s
-r.mapcalc expr="prec_3 = rand(0, 320)" -s
-r.mapcalc expr="prec_4 = rand(0, 510)" -s
-r.mapcalc expr="prec_5 = rand(0, 300)" -s
-r.mapcalc expr="prec_6 = rand(0, 650)" -s
-
-t.create type=strds temporaltype=absolute output=precip_abs1 title="A test" descr="A test"
-t.register type=raster --v -i input=precip_abs1 maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-01-15 12:05:45" increment="14 days"
-
-# The first @test
-t.rast.univar -e input=precip_abs1
-
-t.rast.univar -s input=precip_abs1
-
-t.remove -rf type=strds input=precip_abs1
Added: grass/trunk/temporal/t.rast.univar/testsuite/test_univar.py
===================================================================
--- grass/trunk/temporal/t.rast.univar/testsuite/test_univar.py (rev 0)
+++ grass/trunk/temporal/t.rast.univar/testsuite/test_univar.py 2015-10-22 09:48:51 UTC (rev 66572)
@@ -0,0 +1,135 @@
+"""Test t.rast.univar
+
+(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
+"""
+
+from grass.gunittest.case import TestCase
+from grass.gunittest.gmodules import SimpleModule
+
+class TestRasterUnivar(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=1, res3=1)
+
+ 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("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_1(self):
+
+ t_rast_univar = SimpleModule("t.rast.univar", input="A",
+ where="start_time >= '2001-01-01'",
+ overwrite=True, verbose=True)
+ self.runModule("g.region", res=1)
+ self.assertModule(t_rast_univar)
+
+ univar_text="""id|start|end|mean|min|max|mean_of_abs|stddev|variance|coeff_var|sum|null_cells|cells
+a_1 at testing|2001-01-01 00:00:00|2001-04-01 00:00:00|100|100|100|100|0|0|0|960000|0|9600
+a_2 at testing|2001-04-01 00:00:00|2001-07-01 00:00:00|200|200|200|200|0|0|0|1920000|0|9600
+a_3 at testing|2001-07-01 00:00:00|2001-10-01 00:00:00|300|300|300|300|0|0|0|2880000|0|9600
+a_4 at testing|2001-10-01 00:00:00|2002-01-01 00:00:00|400|400|400|400|0|0|0|3840000|0|9600
+"""
+ self.assertLooksLike(univar_text, t_rast_univar.outputs.stdout)
+
+ def test_2(self):
+
+ t_rast_univar = SimpleModule("t.rast.univar", input="A",
+ where="start_time >= '2001-03-01'",
+ overwrite=True, verbose=True)
+ self.runModule("g.region", res=1)
+ self.assertModule(t_rast_univar)
+
+ univar_text="""id|start|end|mean|min|max|mean_of_abs|stddev|variance|coeff_var|sum|null_cells|cells
+a_2 at testing|2001-04-01 00:00:00|2001-07-01 00:00:00|200|200|200|200|0|0|0|1920000|0|9600
+a_3 at testing|2001-07-01 00:00:00|2001-10-01 00:00:00|300|300|300|300|0|0|0|2880000|0|9600
+a_4 at testing|2001-10-01 00:00:00|2002-01-01 00:00:00|400|400|400|400|0|0|0|3840000|0|9600
+"""
+ self.assertLooksLike(univar_text, t_rast_univar.outputs.stdout)
+
+ def test_3(self):
+
+ t_rast_univar = SimpleModule("t.rast.univar", input="A",
+ where="start_time >= '2001-03-01'",
+ overwrite=True, verbose=True)
+ self.runModule("g.region", res=10)
+ self.assertModule(t_rast_univar)
+
+ univar_text="""id|start|end|mean|min|max|mean_of_abs|stddev|variance|coeff_var|sum|null_cells|cells
+a_2 at testing|2001-04-01 00:00:00|2001-07-01 00:00:00|200|200|200|200|0|0|0|19200|0|96
+a_3 at testing|2001-07-01 00:00:00|2001-10-01 00:00:00|300|300|300|300|0|0|0|28800|0|96
+a_4 at testing|2001-10-01 00:00:00|2002-01-01 00:00:00|400|400|400|400|0|0|0|38400|0|96
+"""
+ self.assertLooksLike(univar_text, t_rast_univar.outputs.stdout)
+
+ def test_4(self):
+
+ self.runModule("g.region", res=10)
+ self.assertModule("t.rast.univar", input="A", flags="r",
+ output="univar_output.txt",
+ where="start_time >= '2001-03-01'",
+ overwrite=True, verbose=True)
+
+ univar_text="""id|start|end|mean|min|max|mean_of_abs|stddev|variance|coeff_var|sum|null_cells|cells
+a_2 at testing|2001-04-01 00:00:00|2001-07-01 00:00:00|200|200|200|200|0|0|0|1920000|0|9600
+a_3 at testing|2001-07-01 00:00:00|2001-10-01 00:00:00|300|300|300|300|0|0|0|2880000|0|9600
+a_4 at testing|2001-10-01 00:00:00|2002-01-01 00:00:00|400|400|400|400|0|0|0|3840000|0|9600
+"""
+ univar_output = open("univar_output.txt", "r").read()
+
+ self.assertLooksLike(univar_text, univar_output)
+
+ def test_5(self):
+
+ self.runModule("g.region", res=10)
+ self.assertModule("t.rast.univar", input="A", flags="rs",
+ output="univar_output.txt",
+ where="start_time >= '2001-03-01'",
+ overwrite=True, verbose=True)
+
+ univar_text="""a_2 at testing|2001-04-01 00:00:00|2001-07-01 00:00:00|200|200|200|200|0|0|0|1920000|0|9600
+a_3 at testing|2001-07-01 00:00:00|2001-10-01 00:00:00|300|300|300|300|0|0|0|2880000|0|9600
+a_4 at testing|2001-10-01 00:00:00|2002-01-01 00:00:00|400|400|400|400|0|0|0|3840000|0|9600
+"""
+ univar_output = open("univar_output.txt", "r").read()
+
+ self.assertLooksLike(univar_text, univar_output)
+
+ def test_6_error_handling_empty_strds(self):
+ # Empty strds
+ self.assertModuleFail("t.rast.univar", input="A",
+ output="univar_output.txt",
+ where="start_time >= '2015-03-01'",
+ overwrite=True, verbose=True)
+
+ def test_7_error_handling_no_input(self):
+ # No input
+ self.assertModuleFail("t.rast.univar", output="out.txt")
+
+if __name__ == '__main__':
+ from grass.gunittest.main import test
+ test()
More information about the grass-commit
mailing list