## NAME

t.rast.algebra.py - Apply temporal and spatial operations on space time raster datasets using temporal raster algebra.

## KEYWORDS

temporal, algebra, raster, time

## SYNOPSIS

t.rast.algebra.py
t.rast.algebra.py --help
t.rast.algebra.py [-sngd] expression=string basename=string [nprocs=integer] [--help] [--verbose] [--quiet] [--ui]

### Flags:

-s
  Check the spatial topology of temporally related maps and process only spatially related maps
-n
  Register Null maps
-g
  Use granularity sampling instead of the temporal topology approach
-d
  Perform a dry run, compute all dependencies and module calls but don't run them
--help
  Print usage summary
--verbose
  Verbose module output
--quiet
  Quiet module output
--ui
  Force launching GUI dialog

### Parameters:

expression=string [required]
  r.mapcalc expression for temporal and spatial analysis of space time raster datasets
basename=string [required]
  Basename of the new generated output maps
  A numerical suffix separated by an underscore will be attached to create a unique identifier
nprocs=integer
  Number of r.mapcalc processes to run in parallel
  Default: 1

## DESCRIPTION

t.rast.algebra performs temporal and spatial map algebra operations on space time raster datasets (STRDS) by using the temporal raster algebra.

### PROGRAM USE

The module expects an expression as input parameter in the following form:

"result = expression"

The statement structure is similar to r.mapcalc, see r.mapcalc. Where result represents the name of a space time raster dataset (STRDS)that will contain the result of the calculation that is given as expression on the right side of the equality sign. These expression can be any valid or nested combination of temporal operations and spatial overlay or buffer functions that are provided by the temporal algebra.
The temporal raster algebra works with space time raster datasets only (STRDS). The algebra provides methods for map selection from STRDS based on their temporal relations. It is also possible to temporally shift maps, to create temporal buffer and to snap time instances to create a valid temporal topology. Furthermore expressions can be nested and evaluated in conditional statements (if, else statements). Within if-statements the algebra provides temporal variables like start time, end time, day of year, time differences or number of maps per time interval to build up conditions.
In addition the algebra provides a subset of the spatial operations from r.mapcalc. All these operations can be assigned to STRDS or to the resulting map lists of operations between STRDS.

As default, topological relationships between space time datasets will be evaluated only temporal. Use the s flag to activate the additionally evaluate the spatial topology based on the spatial extents of maps.

The expression option must be passed as quoted expression, for example:

    t.rast.mapcalc2 expression="C = A + B" basename=result

Where C is the new space time raster dataset that will contain maps with the basename "result" that represent the sum of maps from STRDS A and equally temporal related maps from STRDS B.

The map basename for the result STRDS must always be specified.

## TEMPORAL RASTER ALGEBRA

The temporal algebra provides a wide range of temporal operators and functions that will be presented in the following section.

### TEMPORAL RELATIONS

Several temporal topology relations between registered maps of space time datasets are supported:

    equals            A ------
                      B ------

    during            A  ----
                      B ------

    contains          A ------
                      B  ----

    starts            A ----
                      B ------

    started           A ------
                      B ----

    finishs           A   ----
                      B ------

    finished          A ------
                      B   ----

    precedes          A ----
                      B     ----

    follows           A     ----
                      B ----

    overlapped        A   ------
                      B ------

    overlaps          A ------
                      B   ------

    over              booth overlaps and overlapped

The relations must be read as: A is related to B, like - A equals B - A is during B - A contains B

Topological relations must be specified in {} parentheses.

### TEMPORAL OPERATORS

The temporal algebra defines temporal operators that can be combined with other operators to perform spatio-temporal operations. The temporal operators process the time instances and intervals of two temporal related maps and calculate the result temporal extent by five different possibilities.

    LEFT REFERENCE     l       Use the time stamp of the left space time dataset
    INTERSECTION       i       Intersection
    DISJOINT UNION     d       Disjoint union
    UNION              u       Union
    RIGHT REFERENCE    r       Use the time stamp of the right space time dataset

### TEMPORAL SELECTION

The temporal selection simply selects parts of a space time dataset without processing raster or vector data. The algebra provides a selection operator : that selects parts of a space time dataset that are temporally equal to parts of a second one by default. The following expression

    C = A : B

means: Select all parts of space time dataset A that are equal to B and store it in space time dataset C. The parts are time stamped maps.

In addition the inverse selection operator !: is defined as the complement of the selection operator, hence the following expression

    C = A !: B

means: select all parts of space time time dataset A that are not equal to B and store it in space time dataset (STDS) C.

To select parts of a STDS by different topological relations to other STDS, the temporal topology selection operator can be used. The operator consists of the temporal selection operator, the topological relations, that must be separated by the logical OR operator | and the temporal extent operator. All three parts are separated by comma and surrounded by curly braces: {"temporal selection operator", "topological relations", "temporal operator"}

Examples:

    C = A {:, equals} B
    C = A {!:, equals} B

We can now define arbitrary topological relations using the OR operator "|" to connect them:

    C = A {:,equals|during|overlaps} B

Select all parts of A that are equal to B, during B or overlaps B.
In addition we can define the temporal extent of the result STDS by adding the temporal operator.

    C = A {:, during,r} B

Select all parts of A that are during B and use the temporal extents from B for C.
The selection operator is implicitly contained in the temporal topology selection operator, so that the following statements are exactly the same:

    C = A : B
    C = A {:} B
    C = A {:,equal} B
    C = A {:,equal,l} B

Same for the complementary selection:

    C = A !: B
    C = A {!:} B
    C = A {!:,equal} B
    C = A {!:,equal,l} B

### CONDITIONAL STATEMENTS

Selection operations can be evaluated within conditional statements.

    Note A and B can either be space time datasets or expressions. The temporal
    relationship between the conditions and the conclusions can be defined at the
    beginning of the if statement. The relationship between then and else conclusion
    must be always equal.

    if statement                         decision option                        temporal relations
      if(if, then, else)
      if(conditions, A)                    A if conditions are True;              temporal topological relation between if and then is equal.
      if(conditions, A, B)                 A if conditions are True, B otherwise; temporal topological relation between if, then and else is equal.
      if(topologies, conditions, A)        A if conditions are True;              temporal topological relation between if and then is explicit specified by topologies.
      if(topologies, conditions, A, B)     A if conditions are True, B otherwise; temporal topological relation between if, then and else is explicit specified by topologies.

The conditions are comparison expressions that are used to evaluate space time datasets. Specific values of temporal variables are compared by logical operators and evaluated for each map of the STDS.
Important: The conditions are evaluated from left to right.

#### Logical operators

    Symbol  description

      ==    equal
      !=    not equal
      >     greater than
      >=    greater than or equal
      <     less than
      <=    less than or equal
      &&    and
      ||    or

#### Temporal functions

The following temporal function are evaluated only for the STDS that must be given in parenthesis.

    td(A)                    Returns a list of time intervals of STDS A

    start_time(A)            Start time as HH::MM:SS
    start_date(A)            Start date as yyyy-mm-DD
    start_datetime(A)        Start datetime as yyyy-mm-DD HH:MM:SS
    end_time(A)              End time as HH:MM:SS
    end_date(A)              End date as yyyy-mm-DD
    end_datetime(A)          End datetime as  yyyy-mm-DD HH:MM

    start_doy(A)             Day of year (doy) from the start time [1 - 366]
    start_dow(A)             Day of week (dow) from the start time [1 - 7], the start of the week is Monday == 1
    start_year(A)            The year of the start time [0 - 9999]
    start_month(A)           The month of the start time [1 - 12]
    start_week(A)            Week of year of the start time [1 - 54]
    start_day(A)             Day of month from the start time [1 - 31]
    start_hour(A)            The hour of the start time [0 - 23]
    start_minute(A)          The minute of the start time [0 - 59]
    start_second(A)          The second of the start time [0 - 59]
    end_doy(A)               Day of year (doy) from the end time [1 - 366]
    end_dow(A)               Day of week (dow) from the end time [1 - 7], the start of the week is Monday == 1
    end_year(A)              The year of the end time [0 - 9999]
    end_month(A)             The month of the end time [1 - 12]
    end_week(A)              Week of year of the end time [1 - 54]
    end_day(A)               Day of month from the start time [1 - 31]
    end_hour(A)              The hour of the end time [0 - 23]
    end_minute(A)            The minute of the end time [0 - 59]
    end_second(A)            The second of the end time [0 - 59]

#### Comparison operator

The conditions are comparison expressions that are used to evaluate space time datasets. Specific values of temporal variables are compared by logical operators and evaluated for each map of the STDS and the related maps. For complex relations the comparison operator can be used to combine conditions:
The structure is similar to the select operator with the extension of an aggregation operator: {"comparison operator", "topological relations", aggregation operator, "temporal operator"}
This aggregation operator (| or &) define the behaviour if a map is related the more than one map, e.g for the topological relations 'contains'. Should all (&) conditions for the related maps be true or is it sufficient to have any (|) condition that is true. The resulting boolean value is then compared to the first condition by the comparison operator (|| or &&). As default the aggregation operator is related to the comparison operator:
Comparison operator -> aggregation operator:

    || -> | and && -> &

Examples:

    Condition 1 {||, equal, r} Condition 2
    Condition 1 {&&, equal|during, l} Condition 2
    Condition 1 {&&, equal|contains, |, l} Condition 2
    Condition 1 {&&, equal|during, l} Condition 2 && Condition 3
    Condition 1 {&&, equal|during, l} Condition 2 {&&,contains, |, r} Condition 3

#### Hash operator

Additionally the number of maps in intervals can be computed and used in conditional statements with the hash (#) operator.

    A{#, contains}B

This expression computes the number of maps from space time dataset B which are during the time intervals of maps from space time dataset A.
A list of integers (scalars) corresponding to the maps of A that contain maps from B will be returned.

    C = if({equal}, A {#, contains} B > 2, A {:, contains} B)

This expression selects all maps from A that temporally contains at least 2 maps from B and stores them in space time dataset C. The leading equal statement in the if condition specifies the temporal relation between the if and then part of the if expression. This is very important, so we do not need to specify a global time reference (a space time dataset) for temporal processing.

Furthermore the temporal algebra allows temporal buffering, shifting and snapping with the functions buff_t(), tshift() and tsnap() respectively.

    buff_t(A, size)         Buffer STDS A with granule ("1 month" or 5)
    tshift(A, size)         Shift STDS A with granule ("1 month" or 5)
    tsnap(A)                Snap time instances and intervals of STDS A

#### Single map with temporal extent

The temporal algebra can also handle single maps with time stamps in the tmap function.

    tmap()

For example:

     C = A {:,during} tmap(event)

This statement select all maps from space time data set A that are during the temporal extent of single map 'event'

### Spatial raster operators

The module supports the following raster operations:

    Symbol  description     precedence

      %     modulus         1
      /     division        1
      *     multiplication  1
      +     addition        2
      -     subtraction     2

And raster functions:

    abs(x)                  return absolute value of x
    float(x)                convert x to foating point
    int(x)                  convert x to integer [ truncates ]
    log(x)                  natural log of x
    sqrt(x)                 square root of x
    tan(x)                  tangent of x (x is in degrees)
    round(x)		round x to nearest integer
    sin(x)			sine of x (x is in degrees)
    isnull(x)               check if x = NULL
    isntnull(x)             check if x is not NULL
    null                    set null value
    exist(x)                Check if x is in the current mapset

#### Single raster map

The temporal raster algebra feature also a function to integrate single raster maps without time stamps into the expressions.

     map()

For example:

     C = A * map(constant_value)

This statement multiply all raster maps from space time raster data set A with the raster map 'constant_value'

### Combinations of temporal, raster and select operators

We combine the temporal topology relations, the temporal operators and the spatial/select operators to create spatio-temporal operators:

    {"spatial or select operator", "list of temporal relations", "temporal operator"}

For multiple topological relations or several related maps the spatio-temporal operators feature implicit aggregation. The algebra evaluates the stated STDS by their temporal topologies and apply the given spatio temporal operators in a aggregated form. If we have two STDS A and B, B has three maps: b1, b2, b3 that are all during the temporal extent of the single map a1 of A, then the following arithmetic calculations would implicitly aggregate all maps of B into one result map for a1 of A:

     C = A {+, contains} B --> c1 = a1 + b1 + b2 + b3

Keep attention that the aggregation behaviour is not symmetric:

     C = B {+, during} A --> c1 = b1 + a1
                             c2 = b2 + a1
                             c3 = b3 + a1

### Temporal neighbourhood modifier

The neighbourhood modifier of r.mapcalc is extended for the temporal raster algebra by the temporal dimension. The format is strds[t,r,c], where t is the temporal offset, r is the row offset and c is the column offset.

    strds[2]

Refers to the second successor of the current map

    strds[1,2]

Refers to the cell one row below and two columns to the right of the current cell in the current map

    strds[1,-2,-1]

Refers to the cell two rows above and one column to the left of the current cell of the first successor map

    strds[-2,0,1]

Refers to the cell one column to the right of the current cell in the second predecessor map.

### Examples:

Sum maps from space time dataset A with maps from space time dataset B which have equal time stamps and are temporary before Jan. 1. 2005 and store them in space time dataset D.

    D = if(start_date(A) < "2005-01-01", A + B)

Create the sum of all maps from STRDS A and B that have equal time stamps and store the new maps in STRDS C:

    C = A + B

Same expression with explicit definition of the temporal topology relation and temporal operators:

    C = A {+,equal,l} B

Select all cells from STRDS B with equal temporal relations to STRDS A, if the cells of A are in the range of [100.0, 1600] of time intervals that have more then 30 days (Jan, Mar, Mai, Jul, Aug, Oct, Dec):

    C = if(A > 100 && A < 1600 && td(A) > 30, B)

Same expression with explicit definition of the temporal topology relation and temporal operators:

    C = if({equal}, A > 100 && A < 1600 {&&,equal} td(A) > 30, B)

Compute the recharge in meter per second for all cells of precipitation STRDS "Prec" if the mean temperature specified in STRDS "Temp" is higher than 10 degrees. Computation is performed if STRDS "Prec" and "Temp" have equal time stamps. The number of days or fraction of days per interval is computed using the td() function that has as argument the STRDS "Prec":

    C = if(Temp > 10.0, Prec / 3600.0 /24.0 / td(Prec))

Same expression with explicit definition of the temporal topology relation and temporal operators:

    C = if({equal}, Temp > 10.0, Prec / 3600.0 / 24.0 {/,equal,l} td(Prec))

Compute the mean value of all maps from STRDS A that are located during time intervals of STRDS B if more than one map of A is contained in an interval of B, use A otherwise, the resulting time intervals are either from B or A:

    C = if(B {#,contain} A > 1, (B {+,contain,l} A - B) / (B {#,contain} A), A)

Same expression with explicit definition of the temporal topology relation and temporal operators:

    C = if({equal}, B {#,contain} A > 1, (B {+,contain,l} A {-,equal,l} B) {equal,=/} (B {#,contain} A), A)

## SEE ALSO

r.mapcalc t.vect.algebra t.rast3d.algebra t.select t.rast3d.mapcalc t.rast.mapcalc

Temporal data processing Wiki

## REFERENCES

PLY(Python-Lex-Yacc)

## AUTHORS

Thomas Leppelt, Sören Gebbert, Thünen Institute of Climate-Smart Agriculture

Last changed: $Date: 2016-11-14 00:09:36 +0100 (Mon, 14 Nov 2016) $

## SOURCE CODE

Available at: t.rast.algebra source code (history)

Main index | Temporal index | Topics index | Keywords index | Graphical index | Full index

© 2003-2016 GRASS Development Team, GRASS GIS 7.2.0 Reference Manual