Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
28 changes: 26 additions & 2 deletions scripts/r.out.xyz/r.out.xyz.py
Comment thread
Khushjeet58 marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
# % key: i
# % description: Include no data values
# %end
# %flag
# % key: b
# % description: Output rows from bottom to top
# %end

import sys
from grass.script import core as grass
Expand All @@ -47,21 +51,41 @@ def main():
# if no output filename, output to stdout
output = options["output"]
donodata = flags["i"]
bottom_to_top = flags["b"]

statsflags = "1g" if donodata else "1gn"
parameters = {
"flags": statsflags,
"input": options["input"],
"separator": options["separator"],
}
if output:
if output and not bottom_to_top:
parameters.update(output=output)

ret = 0

try:
grass.run_command("r.stats", **parameters)
if bottom_to_top:
output_text = grass.read_command("r.stats", **parameters)

lines = output_text.splitlines()
lines.reverse()

if output_text.endswith("\n"):
output_text = "\n".join(lines) + "\n"
else:
output_text = "\n".join(lines)

if output:
pathlib.Path(output).write_text(output_text, encoding="utf-8")
else:
print(output_text, end="")
else:
grass.run_command("r.stats", **parameters)

except CalledModuleError:
ret = 1

sys.exit(ret)


Expand Down
33 changes: 33 additions & 0 deletions scripts/r.out.xyz/testsuite/test_r_out_xyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def tearDownClass(cls):
"""Remove temporary region"""
cls.del_temp_region()
Path(cls.csvFile).unlink(missing_ok=True)
Path("normal.csv").unlink(missing_ok=True)
Path("bottom.csv").unlink(missing_ok=True)

def test_r_out_xyz(self):
"""ASCII text file test"""
Expand All @@ -37,6 +39,37 @@ def test_r_out_xyz(self):

self.assertFileExists(filename=self.csvFile)

def test_r_out_xyz_bottom_to_top(self):
"""Test bottom-to-top output ordering"""

normal_file = "normal.csv"
bottom_file = "bottom.csv"

module = SimpleModule(
"r.out.xyz",
input=self.mapName,
output=normal_file,
separator=",",
)
self.assertModule(module)

module = SimpleModule(
"r.out.xyz",
input=self.mapName,
output=bottom_file,
separator=",",
flags="b",
)
self.assertModule(module)

with open(normal_file, encoding="utf-8") as f:
normal_lines = f.read().splitlines()

with open(bottom_file, encoding="utf-8") as f:
bottom_lines = f.read().splitlines()

self.assertEqual(bottom_lines, list(reversed(normal_lines)))


if __name__ == "__main__":
test()
Loading