Skip to content

Commit

Permalink
add new module
Browse files Browse the repository at this point in the history
  • Loading branch information
ninsbl committed Sep 30, 2024
1 parent fe15c65 commit 370afc7
Showing 1 changed file with 137 additions and 0 deletions.
137 changes: 137 additions & 0 deletions src/temporal/t.rast.copytree/testsuite/test_t_rast_copytree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
"""Test t.rast.copytree
(C) 2024 by NVE, Stefan Blumentrath and the GRASS GIS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
:authors: Stefan Blumentrath
"""

# ruff: noqa: RUF012

from pathlib import Path

import grass.script as gs
from grass.gunittest.case import TestCase


class TestTRastCopytree(TestCase):
"""Test case for moving files from STRDS to directory trees"""

default_region = {"s": 0, "n": 80, "w": 0, "e": 120, "b": 0, "t": 50}

@classmethod
def setUpClass(cls):
"""Initiate the temporal GIS and set the region"""
cls.use_temp_region()
cls.tempdir = Path(gs.tempdir())
cls.tempdir_target = Path(gs.tempdir())
cls.runModule("g.region", **cls.default_region, res=1, res3=1)

cls.runModule(
"r.external.out",
format="GTiff",
directory=str(cls.tempdir),
extension="tif",
)
for rmap_idx in range(1, 4):
for prefix in ("a", "b"):
cls.runModule(
"r.mapcalc",
expression=f"{prefix}_{rmap_idx} = {rmap_idx}00",
overwrite=True,
)
cls.runModule(
"r.support", map=f"{prefix}_{rmap_idx}", semantic_label=prefix
)

cls.runModule(
"t.create",
type="strds",
temporaltype="absolute",
output="A",
title="A test",
description="A test",
overwrite=True,
)
cls.runModule(
"t.register",
flags="i",
type="raster",
input="A",
maps="a_1,a_2,a_3",
start="2001-01-01",
increment="3 months",
overwrite=True,
)

cls.runModule(
"t.create",
type="strds",
temporaltype="absolute",
output="B",
title="B test",
description="B test",
overwrite=True,
)
cls.runModule(
"t.register",
flags="i",
type="raster",
input="B",
maps="b_1,b_2,b_3",
start="2001-01-01",
increment="1 day",
overwrite=True,
)

@classmethod
def tearDownClass(cls):
"""Remove the temporary region"""
cls.del_temp_region()
cls.runModule("t.remove", flags="df", type="strds", inputs="A")
cls.runModule("r.external.out", flags="r")
gs.utils.try_rmdir(str(cls.tempdir_target))
gs.utils.try_rmdir(str(cls.tempdir))

def test_t_rast_copytree_move(self):
"""Test moving files into directory tree"""
# Check that t.rast.copytree runs successfully
self.assertModule(
"t.rast.copytree",
flags="m",
input="A",
temporal_tree="%Y/%m",
output_directory=str(self.tempdir_target),
nprocs="1",
)

self.assertFileExists(str(self.tempdir_target / "2001/01/a_1.tif"))
self.assertFileExists(str(self.tempdir_target / "2001/04/a_2.tif"))
self.assertFileExists(str(self.tempdir_target / "2001/07/a_3.tif"))

def test_t_rast_copytree_copy_semantic_label(self):
"""Test moving files into directory tree"""
# Check that t.rast.copytree runs successfully
self.assertModule(
"t.rast.copytree",
flags="s",
input="B",
temporal_tree="%Y/%m/%d",
output_directory=str(self.tempdir_target),
nprocs="1",
)

self.assertFileExists(str(self.tempdir / "b_1.tif"))
self.assertFileExists(str(self.tempdir / "b_2.tif"))
self.assertFileExists(str(self.tempdir / "b_3.tif"))
self.assertFileExists(str(self.tempdir_target / "b/2001/01/01/b_1.tif"))
self.assertFileExists(str(self.tempdir_target / "b/2001/01/02/b_2.tif"))
self.assertFileExists(str(self.tempdir_target / "b/2001/01/03/b_3.tif"))


if __name__ == "__main__":
from grass.gunittest.main import test

test()

0 comments on commit 370afc7

Please sign in to comment.