Skip to content

Commit

Permalink
t.rast.univar: allow r-flag combined with zones option (OSGeo#4577)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ninsbl authored and a0x8o committed Nov 11, 2024
1 parent f9041dd commit aec9c42
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
15 changes: 9 additions & 6 deletions python/grass/temporal/univar_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -435,7 +435,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()

Expand Down Expand Up @@ -2529,7 +2529,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")
Expand Down Expand Up @@ -2639,6 +2638,7 @@ def print_gridded_dataset_univar_statistics(

sp = open_old_stds(input, type, dbif)

<<<<<<< HEAD
if output is not None:
out_file = open(output, "w")

Expand Down Expand Up @@ -2713,6 +2713,8 @@ def print_gridded_dataset_univar_statistics(
>>>>>>> osgeo-main
=======
>>>>>>> osgeo-main
=======
>>>>>>> b9298093ee (t.rast.univar: allow r-flag combined with zones option (#4577))
spatial_extent = None
if region_relation:
spatial_extent = gs.parse_command("g.region", flags="3gu")
Expand Down Expand Up @@ -2878,10 +2880,11 @@ def print_gridded_dataset_univar_statistics(
>>>>>>> 6cf60c76a4 (wxpyimgview: explicit conversion to int (#2704))
)

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"]
Expand Down Expand Up @@ -3103,7 +3106,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
Expand Down
5 changes: 1 addition & 4 deletions temporal/t.rast.univar/t.rast.univar.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@
>>>>>>> osgeo-main
# %rules
# % requires: percentile,-e
# % exclusive: zones,-r
# %end

=======
Expand Down Expand Up @@ -449,9 +448,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
Expand Down
39 changes: 39 additions & 0 deletions temporal/t.rast.univar/testsuite/test_t_rast_univar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1937,6 +1937,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(
Expand Down

0 comments on commit aec9c42

Please sign in to comment.