def test_3(self):
        """Simple arithmetik test with null map"""
        tra = tgis.TemporalRasterAlgebraParser(run=True, debug=True)
        expr = "R = A + B + C + tmap(nullmap)"
        ret = tra.setup_common_granularity(
            expression=expr, lexer=tgis.TemporalRasterAlgebraLexer())
        self.assertEqual(ret, True)

        tra.parse(expression=expr, basename="r", overwrite=True)

        D = tgis.open_old_stds("R", type="strds")

        self.assertEqual(D.metadata.get_number_of_maps(), 0)
        self.assertEqual(D.metadata.get_min_min(), None)
        self.assertEqual(D.metadata.get_max_max(), None)
        start, end = D.get_absolute_time()
        self.assertEqual(start, None)
        self.assertEqual(end, None)
        self.assertEqual(D.check_temporal_topology(), False)
        self.assertEqual(D.get_granularity(), None)

        tra = tgis.TemporalRasterAlgebraParser(run=True,
                                               debug=True,
                                               dry_run=True)
        tra.setup_common_granularity(expression=expr,
                                     lexer=tgis.TemporalRasterAlgebraLexer())
        pc = tra.parse(expression=expr, basename="r", overwrite=True)

        print(pc)

        self.assertEqual(len(pc["register"]), 0)
        self.assertEqual(len(pc["processes"]), 0)
        self.assertEqual(pc["STDS"]["name"], "R")
        self.assertEqual(pc["STDS"]["stdstype"], "strds")
    def test_2(self):
        """Simple arithmetik test"""
        tra = tgis.TemporalRasterAlgebraParser(run=True, debug=True)
        expr = "R = A + B + C"
        ret = tra.setup_common_granularity(
            expression=expr, lexer=tgis.TemporalRasterAlgebraLexer())
        self.assertEqual(ret, True)

        tra.parse(expression=expr, basename="r", overwrite=True)

        D = tgis.open_old_stds("R", type="strds")

        self.assertEqual(D.metadata.get_number_of_maps(), 6)
        self.assertEqual(D.metadata.get_min_min(), 17)  # 1 + 7 + 9
        self.assertEqual(D.metadata.get_max_max(), 23)  # 6 + 8 + 9
        start, end = D.get_absolute_time()
        self.assertEqual(start, datetime.datetime(2001, 1, 1))
        self.assertEqual(end, datetime.datetime(2001, 7, 1))
        self.assertEqual(D.check_temporal_topology(), True)
        self.assertEqual(D.get_granularity(), u'1 month')

        tra = tgis.TemporalRasterAlgebraParser(run=True,
                                               debug=True,
                                               dry_run=True)
        tra.setup_common_granularity(expression=expr,
                                     lexer=tgis.TemporalRasterAlgebraLexer())
        pc = tra.parse(expression=expr, basename="r", overwrite=True)

        self.assertEqual(len(pc["register"]), 6)
        self.assertEqual(len(pc["processes"]), 6)
        self.assertEqual(pc["processes"][0]["name"], "r.mapcalc")
        self.assertEqual(pc["processes"][5]["name"], "r.mapcalc")
        self.assertEqual(pc["STDS"]["name"], "R")
        self.assertEqual(pc["STDS"]["stdstype"], "strds")
def main():
    expression = options['expression']
    basename = options['basename']
    nprocs = options["nprocs"]
    spatial = flags["s"]
    register_null = flags["n"]
    granularity = flags["g"]

    # Check for PLY istallation
    try:
        import ply.lex as lex
        import ply.yacc as yacc
    except:
        grass.script.fatal(
            _("Please install PLY (Lex and Yacc Python implementation) to use the temporal algebra modules. "
              "You can use t.rast3d.mapcalc that provides a limited but useful alternative to "
              "t.rast3d.mapcalc2 without PLY requirement."))

    tgis.init(True)
    p = tgis.TemporalRaster3DAlgebraParser(run=True,
                                           debug=False,
                                           spatial=spatial,
                                           nprocs=nprocs,
                                           register_null=register_null)

    if granularity:
        if not p.setup_common_granularity(
                expression=expression,
                stdstype='str3ds',
                lexer=tgis.TemporalRasterAlgebraLexer()):
            grass.script.fatal(
                _("Unable to process the expression in granularity algebra mode"
                  ))

    p.parse(expression, basename, grass.script.overwrite())
Exemple #4
0
def main():
    # lazy imports
    import grass.temporal as tgis

    expression = options["expression"]
    basename = options["basename"]
    nprocs = options["nprocs"]
    time_suffix = options["suffix"]
    spatial = flags["s"]
    register_null = flags["n"]
    granularity = flags["g"]
    dry_run = flags["d"]

    # Check for PLY istallation
    try:
        # Intentionally unused imports
        import ply.lex as lex  # noqa: F401
        import ply.yacc as yacc  # noqa: F401
    except ImportError:
        grass.script.fatal(
            _(
                "Please install PLY (Lex and Yacc Python implementation) to use the temporal algebra modules. "
                "You can use t.rast.mapcalc that provides a limited but useful alternative to "
                "t.rast.algebra without PLY requirement."
            )
        )

    tgis.init(True)
    p = tgis.TemporalRasterAlgebraParser(
        run=True,
        debug=False,
        spatial=spatial,
        nprocs=nprocs,
        register_null=register_null,
        dry_run=dry_run,
        time_suffix=time_suffix,
    )

    if granularity:
        if not p.setup_common_granularity(
            expression=expression, lexer=tgis.TemporalRasterAlgebraLexer()
        ):
            grass.script.fatal(
                _("Unable to process the expression in granularity algebra mode")
            )

    pc = p.parse(expression, basename, grass.script.overwrite())

    if dry_run is True:
        import pprint

        pprint.pprint(pc)
Exemple #5
0
    def test_map(self):
        """Test STDS + single map without timestamp"""
        tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
        expr = "R = A + map(singletmap)"
        ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
        self.assertEqual(ret, True)

        tra.parse(expression=expr, basename="r", overwrite=True)

        D = tgis.open_old_stds("R", type="strds")

        self.assertEqual(D.metadata.get_number_of_maps(), 6)
        self.assertEqual(D.metadata.get_min_min(), 100)  # 1 + 99
        self.assertEqual(D.metadata.get_max_max(), 105) # 6 + 99
        start, end = D.get_absolute_time()
        self.assertEqual(start, datetime.datetime(2001, 1, 1))
        self.assertEqual(end, datetime.datetime(2001, 7, 1))
Exemple #6
0
    def test_temporal_neighbors(self):
        """Simple temporal neighborhood computation test"""
        tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
        expr = 'R = (A[0,0,-1] : D) + (A[0,0,1] : D)'
        ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
        self.assertEqual(ret, True)

        tra.parse(expression=expr, basename="r", overwrite=True)

        D = tgis.open_old_stds("R", type="strds")

        self.assertEqual(D.metadata.get_number_of_maps(), 14)
        self.assertEqual(D.metadata.get_min_min(), 2)  # 1 + 1
        self.assertEqual(D.metadata.get_max_max(), 10) # 5 + 5
        start, end = D.get_absolute_time()
        self.assertEqual(start, datetime.datetime(2001, 1, 2))
        self.assertEqual(end, datetime.datetime(2001, 5, 6))
Exemple #7
0
    def test_complex_arith_if_1(self):
        """Complex arithmetic test with if condition"""
        tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
        expr = 'R = if(start_date(A) < "2001-03-01" && A#A == 1, A+C, A-C)'
        ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
        self.assertEqual(ret, True)

        tra.parse(expression=expr, basename="r", overwrite=True)

        D = tgis.open_old_stds("R", type="strds")

        self.assertEqual(D.metadata.get_number_of_maps(), 6)
        self.assertEqual(D.metadata.get_min_min(), -6)  # 3 - 9
        self.assertEqual(D.metadata.get_max_max(), 11) # 2 + 2
        start, end = D.get_absolute_time()
        self.assertEqual(start, datetime.datetime(2001, 1, 1))
        self.assertEqual(end, datetime.datetime(2001, 7, 1))
Exemple #8
0
    def test_simple_arith_if_1(self):
        """Simple arithmetic test with if condition"""
        tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
        expr = 'R = if(start_date(A) >= "2001-02-01", A + A)'
        ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
        self.assertEqual(ret, True)

        tra.parse(expression=expr, basename="r", overwrite=True)

        D = tgis.open_old_stds("R", type="strds")

        self.assertEqual(D.metadata.get_number_of_maps(), 5)
        self.assertEqual(D.metadata.get_min_min(), 4)
        self.assertEqual(D.metadata.get_max_max(), 12)
        start, end = D.get_absolute_time()
        self.assertEqual(start, datetime.datetime(2001, 2, 1))
        self.assertEqual(end, datetime.datetime(2001, 7, 1))
Exemple #9
0
    def test_simple_arith_td_1(self):
        """Simple arithmetic test"""
        tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
        expr = 'R = A + td(A:D)'
        ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
        self.assertEqual(ret, True)

        tra.parse(expression=expr, basename="r", overwrite=True)

        D = tgis.open_old_stds("R", type="strds")

        self.assertEqual(D.metadata.get_number_of_maps(), 15)
        self.assertEqual(D.metadata.get_min_min(), 2) # 1 - 1
        self.assertEqual(D.metadata.get_max_max(), 6) # 5 + 1
        start, end = D.get_absolute_time()
        self.assertEqual(start, datetime.datetime(2001, 1, 1))
        self.assertEqual(end, datetime.datetime(2001, 5, 6))
        self.assertEqual(D.check_temporal_topology(), True)
        self.assertEqual(D.get_granularity(), u'1 day')
Exemple #10
0
    def test_1(self):
        """Simple arithmetik test"""
        tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
        expr = "R = if(C == 9,  A - 1)"
        ret = tra.setup_common_granularity(expression=expr,  lexer = tgis.TemporalRasterAlgebraLexer())
        self.assertEqual(ret, True)
        
        tra.parse(expression=expr, basename="r", overwrite=True)

        D = tgis.open_old_stds("R", type="strds")

        self.assertEqual(D.metadata.get_number_of_maps(), 6)
        self.assertEqual(D.metadata.get_min_min(), 0) # 1 - 1
        self.assertEqual(D.metadata.get_max_max(), 5) # 6 - 1
        start, end = D.get_absolute_time()
        self.assertEqual(start, datetime.datetime(2001, 1, 1))
        self.assertEqual(end, datetime.datetime(2001, 7, 1))
        self.assertEqual( D.check_temporal_topology(),  True)
        self.assertEqual(D.get_granularity(),  u'1 month')