Skip to content

Commit

Permalink
t.rast.univar: allow r-flag combined with zones option (#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 Nov 8, 2024
1 parent 53c28f3 commit b929809
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
15 changes: 6 additions & 9 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 @@ -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()

Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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
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 @@ -91,7 +91,6 @@

# %rules
# % requires: percentile,-e
# % exclusive: zones,-r
# %end

import grass.script as gs
Expand Down Expand Up @@ -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
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 @@ -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(
Expand Down

0 comments on commit b929809

Please sign in to comment.