From b9298093ee685c98555c0e275c0961afb6fb5932 Mon Sep 17 00:00:00 2001 From: Stefan Blumentrath Date: Fri, 8 Nov 2024 02:05:26 +0100 Subject: [PATCH] t.rast.univar: allow r-flag combined with zones option (#4577) * allow r-flag and zones * add test for r-flag and zones * use region env only with zones * use region env for r3.univar * Update docstring --- python/grass/temporal/univar_statistics.py | 15 +++---- temporal/t.rast.univar/t.rast.univar.py | 5 +-- .../testsuite/test_t_rast_univar.py | 39 +++++++++++++++++++ 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/python/grass/temporal/univar_statistics.py b/python/grass/temporal/univar_statistics.py index b6a9c1ac508..77ebaca30b1 100755 --- a/python/grass/temporal/univar_statistics.py +++ b/python/grass/temporal/univar_statistics.py @@ -13,7 +13,7 @@ .. -(C) 2012-2013 by the GRASS Development Team +(C) 2012-2024 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. @@ -57,7 +57,7 @@ def compute_univar_stats(registered_map_info, stats_module, fs, rast_region=Fals ) stats_module.inputs.map = id - if rast_region: + if rast_region and (stats_module.inputs.zones or stats_module.name == "r3.univar"): stats_module.env = gs.region_env(raster=id) stats_module.run() @@ -139,7 +139,6 @@ def print_gridded_dataset_univar_statistics( :param nprocs: Number of cores to use for processing :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. :param region_relation: Process only maps with the given spatial relation to the computational region. A string with one of the following values: "overlaps": maps that spatially overlap ("intersect") @@ -154,9 +153,6 @@ def print_gridded_dataset_univar_statistics( sp = open_old_stds(input, type, dbif) - if output is not None: - out_file = open(output, "w") - spatial_extent = None if region_relation: spatial_extent = gs.parse_command("g.region", flags="3gu") @@ -187,10 +183,11 @@ def print_gridded_dataset_univar_statistics( ).format(type=sp.get_new_map_instance(None).get_type(), id=sp.get_id()) ) - if output is not None: - out_file.close() return + if output is not None: + out_file = open(output, "w") + if no_header is False: cols = ( ["id", "semantic_label", "start", "end"] @@ -235,7 +232,7 @@ def print_gridded_dataset_univar_statistics( flag = "g" if extended is True: flag += "e" - if type == "strds" and rast_region is True: + if type == "strds" and rast_region is True and not zones: flag += "r" # Setup pygrass module to use for computation diff --git a/temporal/t.rast.univar/t.rast.univar.py b/temporal/t.rast.univar/t.rast.univar.py index bd199f9e801..dbc2a31ab4d 100755 --- a/temporal/t.rast.univar/t.rast.univar.py +++ b/temporal/t.rast.univar/t.rast.univar.py @@ -91,7 +91,6 @@ # %rules # % requires: percentile,-e -# % exclusive: zones,-r # %end import grass.script as gs @@ -130,9 +129,7 @@ def main(): # Make sure the temporal database exists tgis.init() - if not output: - output = None - if output == "-": + if not output or output == "-": output = None # Check if zones map exists and is of type CELL diff --git a/temporal/t.rast.univar/testsuite/test_t_rast_univar.py b/temporal/t.rast.univar/testsuite/test_t_rast_univar.py index 83e5c2d6229..7bdfe9f7f6a 100644 --- a/temporal/t.rast.univar/testsuite/test_t_rast_univar.py +++ b/temporal/t.rast.univar/testsuite/test_t_rast_univar.py @@ -348,6 +348,45 @@ def test_with_zones(self): a_4@PERMANENT||2001-10-01 00:00:00|2002-01-01 00:00:00|1|400|400|400|400|0|0|0|240000|0|600|600 a_4@PERMANENT||2001-10-01 00:00:00|2002-01-01 00:00:00|2|400|400|400|400|0|0|0|672000|0|1680|1680 a_4@PERMANENT||2001-10-01 00:00:00|2002-01-01 00:00:00|3|400|400|400|400|0|0|0|2928000|0|7320|7320 +""" + + for ref, res in zip( + univar_text.split("\n"), t_rast_univar.outputs.stdout.split("\n") + ): + if ref and res: + ref_line = ref.split("|", 1)[1] + res_line = res.split("|", 1)[1] + self.assertLooksLike(ref_line, res_line) + + @xfail_windows + def test_with_zones_and_r(self): + """Test use of zones and r-flag""" + + t_rast_univar = SimpleModule( + "t.rast.univar", + flags="r", + input="A", + where="start_time >= '2001-01-01'", + zones="zones", + overwrite=True, + verbose=True, + ) + self.runModule("g.region", **self.default_region, res=1) + self.assertModule(t_rast_univar) + + univar_text = """id|semantic_label|start|end|zone|mean|min|max|mean_of_abs|stddev|variance|coeff_var|sum|null_cells|cells|non_null_cells +a_1@PERMANENT||2001-01-01 00:00:00|2001-04-01 00:00:00|1|100|100|100|100|0|0|0|60000|0|600|600 +a_1@PERMANENT||2001-01-01 00:00:00|2001-04-01 00:00:00|2|100|100|100|100|0|0|0|168000|0|1680|1680 +a_1@PERMANENT||2001-01-01 00:00:00|2001-04-01 00:00:00|3|100|100|100|100|0|0|0|732000|0|7320|7320 +a_2@PERMANENT||2001-04-01 00:00:00|2001-07-01 00:00:00|1|200|200|200|200|0|0|0|120000|0|600|600 +a_2@PERMANENT||2001-04-01 00:00:00|2001-07-01 00:00:00|2|200|200|200|200|0|0|0|336000|0|1680|1680 +a_2@PERMANENT||2001-04-01 00:00:00|2001-07-01 00:00:00|3|200|200|200|200|0|0|0|1464000|0|7320|7320 +a_3@PERMANENT||2001-07-01 00:00:00|2001-10-01 00:00:00|1|300|300|300|300|0|0|0|180000|0|600|600 +a_3@PERMANENT||2001-07-01 00:00:00|2001-10-01 00:00:00|2|300|300|300|300|0|0|0|504000|0|1680|1680 +a_3@PERMANENT||2001-07-01 00:00:00|2001-10-01 00:00:00|3|300|300|300|300|0|0|0|2196000|0|7320|7320 +a_4@PERMANENT||2001-10-01 00:00:00|2002-01-01 00:00:00|1|400|400|400|400|0|0|0|240000|0|600|600 +a_4@PERMANENT||2001-10-01 00:00:00|2002-01-01 00:00:00|2|400|400|400|400|0|0|0|672000|0|1680|1680 +a_4@PERMANENT||2001-10-01 00:00:00|2002-01-01 00:00:00|3|400|400|400|400|0|0|0|2928000|0|7320|7320 """ for ref, res in zip(