Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

t.rast.univar: allow r-flag combined with zones option #4577

Merged
merged 7 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions python/grass/temporal/univar_statistics.py
Original file line number Diff line number Diff line change
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"):
echoix marked this conversation as resolved.
Show resolved Hide resolved
stats_module.env = gs.region_env(raster=id)
stats_module.run()

Expand Down Expand Up @@ -154,9 +154,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 +184,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 +233,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 @@ -347,6 +347,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
Loading