t.rast.algebra(1grass)

✨ t.rast.algebra ✨ (that's a GRASS User's Manual page, bestie)

okay so this is t.rast.algebra from the GRASS manual and it is iconic πŸ’… it does temporal + spatial map algebra on space time raster datasets. yes it's a lot. no we're not skipping any of it. let's go πŸ¦‹


🌸 NAME 🌸

t.rast.algebra β€” apply temporal and spatial operations on space time raster datasets using temporal raster algebra.


πŸŽ€ KEYWORDS πŸŽ€

temporal, algebra, raster, time


✨ SYNOPSIS ✨

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

🫢 Flags 🫢

  • -s β€” check the spatial topology of temporally related maps, and only process maps that are spatially related πŸ’«
  • -n β€” register Null maps πŸ•³οΈ
  • -g β€” use granularity sampling instead of the temporal topology approach ⏳
  • -d β€” do a dry run: compute all the dependencies and module calls but don't actually run them πŸ™ˆ
  • --help β€” print usage summary
  • --verbose β€” verbose module output (she's chatty, we love her)
  • --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 πŸ”–
  • suffix=name β€” suffix to add at basename: set 'gran' for granularity, 'time' for the full time format, 'num' for numerical suffix with a specific number of digits (default %%05). default: num
  • 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) using the temporal raster algebra. no cap. 🌈

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

"result = expression"

The statement structure is similar to that of r.mapcalc. In this statement, result represents the name of the 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 expressions 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 only with space time raster datasets (STRDS). The algebra provides methods for map selection 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 map lists resulting of operations between STRDS. πŸ’…

By default, only temporal topological relations among space time datasets (STDS) are evaluated. The -s flag can be used to additionally activate the evaluation of the spatial topology based on the spatial extent of maps. πŸ—ΊοΈ

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

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

Where C is the new space time raster dataset that will contain maps with the basename "result" and a numerical suffix separated by an underscore that represent the sum of maps from the STRDS A and temporally equal maps (i.e., maps with equal temporal topology relation) from the STRDS B. πŸ”₯

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

πŸ’€ do not forget the basename okay. it's required. required required.

πŸ”₯ 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 are supported between maps registered in space time datasets:

equals            A ------
                  B ------
during            A  ----
                  B ------
contains          A ------
                  B  ----
starts            A ----
                  B ------
started           A ------
                  B ----
finishes          A   ----
                  B ------
finished          A ------
                  B   ----
precedes          A ----
                  B     ----
follows           A     ----
                  B ----
overlapped        A   ------
                  B ------
overlaps          A ------
                  B   ------
over              both 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 with curly brackets {}. πŸŽ€

✨ 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 temporally related maps and calculate the resulting temporal extent in five possible different ways.

  • 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 any raster or vector data. The algebra provides a selection operator : that by default selects parts of a space time dataset that are temporally equal to parts of a second space time dataset. The following expression

C = A : B

means: select all parts of space time dataset A that are equal to B and store them in space time dataset C. These 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 them in space time dataset C. πŸ’…

To select parts of a STRDS using different topological relations regarding to other STRDS, the temporal topology selection operator can be used. This 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 brackets as follows: {"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 resulting STRDS 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 as showed below. Note that A and B can be either space time datasets or expressions. The temporal relationship between the conditions and the conclusions can be defined at the beginning of the if statement (third and fourth examples below). The relationship between then and else conclusion must be always equal. πŸ‘‘

  • if(if, then, else) β€” the general form, bestie
  • 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 explicitly specified by topologies.
  • if(topologies, conditions, A, B) β€” A if conditions are True, B otherwise; temporal topological relation between if, then and else is explicitly 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 STRDS.

⚠️ Important: The conditions are evaluated from left to right. fr fr.

🌸 Logical operators 🌸

  • == β€” equal
  • != β€” not equal
  • > β€” greater than
  • >= β€” greater than or equal
  • < β€” less than
  • <= β€” less than or equal
  • && β€” and
  • || β€” or

okay and that's literally not even the whole thing β€” there's more, so don't go anywhere bestie πŸ’œ

✨ Temporal functions ✨

These ones only get evaluated for the STDS that you hand them β€” and bestie, you have to put that STDS in parentheses. Non-negotiable, no cap. 🫢

  • td(A) β€” hands you back a whole list of time intervals of STDS A
  • start_time(A) β€” start time, formatted as HH::MM:SS
  • start_date(A) β€” start date, formatted as yyyy-mm-DD
  • start_datetime(A) β€” start datetime, formatted as yyyy-mm-DD HH:MM:SS
  • end_time(A) β€” end time, formatted as HH:MM:SS
  • end_date(A) β€” end date, formatted as yyyy-mm-DD
  • end_datetime(A) β€” end datetime, formatted as yyyy-mm-DD HH:MM
  • start_doy(A) β€” day of year (doy) from the start time, range [1 - 366]
  • start_dow(A) β€” day of week (dow) from the start time, range [1 - 7], and the start of the week is Monday == 1
  • start_year(A) β€” the year of the start time, range [0 - 9999]
  • start_month(A) β€” the month of the start time, range [1 - 12]
  • start_week(A) β€” week of year of the start time, range [1 - 54]
  • start_day(A) β€” day of month from the start time, range [1 - 31]
  • start_hour(A) β€” the hour of the start time, range [0 - 23]
  • start_minute(A) β€” the minute of the start time, range [0 - 59]
  • start_second(A) β€” the second of the start time, range [0 - 59]
  • end_doy(A) β€” day of year (doy) from the end time, range [1 - 366]
  • end_dow(A) β€” day of week (dow) from the end time, range [1 - 7], and again, start of the week is Monday == 1
  • end_year(A) β€” the year of the end time, range [0 - 9999]
  • end_month(A) β€” the month of the end time, range [1 - 12]
  • end_week(A) β€” week of year of the end time, range [1 - 54]
  • end_day(A) β€” day of month from the start time, range [1 - 31]
  • end_hour(A) β€” the hour of the end time, range [0 - 23]
  • end_minute(A) β€” the minute of the end time, range [0 - 59]
  • end_second(A) β€” the second of the end time, range [0 - 59]

Okay so here's the little sneaky bit about that last block πŸ‘€ β€” to actually use those numbers, you've gotta add an offset value. So like, start_doy(A, 0) returns the DOY of the current map in STDS A. And end_hour(A, -1) returns the end hour of the previous map in STDS A. Cute right? πŸ’…


🌈 Comparison operator 🌈

Like I said up top, the conditions are comparison expressions, and they're what we use to evaluate space time datasets. Specific values of temporal variables get compared with logical operators and evaluated for every single map of the STDS and (if you want) the related maps. And for the messy, complicated relations, you can use the comparison operator to combine conditions together. ✨

The structure is basically the select operator but with an aggregation operator bolted on:

{"comparison operator", "topological relations", aggregation operator, "temporal operator"}

This aggregation operator (| or &) decides what happens when a map is related to more than one map β€” think the topological relation 'contains', for example. Do all (&) the conditions for the related maps have to be true, or is it enough to have any (|) condition that's true? Either way works. Then that resulting boolean value gets compared to the first condition by the comparison operator (|| or &&).

And by default, the aggregation operator is matched up to the comparison operator like this:

comparison operator -> aggregation operator:

|| -> | and && -> &

Examples, bestie:

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 πŸŽ€

Not me about to tell you that you can also count the number of maps in intervals and use them in conditional statements with the hash (#) operator. Obsessed. πŸ’–

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. It returns a list of integers (scalars) that correspond to the maps of A that contain maps from B.

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

This one selects every map 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 parts of the if expression. This is very important β€” the way it saves us from needing a global time reference (a space time dataset) for our temporal processing? Iconic behaviour. πŸ‘‘

Plus!! (and this is highkey the fun part) the temporal algebra lets you do temporal buffering, shifting and snapping with the functions buff_t(), tshift() and tsnap():

  • 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 that have time stamps, via the tmap() function.

tmap()

For example:

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

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


πŸ“ Spatial raster operators πŸ“

The module supports these raster operations β€” and yes, the precedence column matters, keep an eye on it:

  • % β€” modulus β€” precedence 1
  • / β€” division β€” precedence 1
  • * β€” multiplication β€” precedence 1
  • + β€” addition β€” precedence 2
  • - β€” subtraction β€” precedence 2

And the 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 also features a function to integrate single raster maps that don't have time stamps into your expressions. Look at her go:

map()

For example:

C = A * map(constant_value)

This statement multiplies every raster map from space time raster data set A with the raster map 'constant_value'. Fr fr, that's it. 🫢


🌸 Combinations of temporal, raster and select operators 🌸

Okay so you can combine the temporal topology relations, the temporal operators, and the spatial/select operators to build spatio-temporal operators, like this:

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

Now, when you've got multiple topological relations or several related maps, these spatio-temporal operators do implicit aggregation. The algebra evaluates the STDS you named by their temporal topologies and applies the given spatio-temporal operators in an aggregated form. So if we have two STDS A and B, and 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 implicitly aggregate all maps of B into one result map for a1 of A:

 C = A {+, contains} B --> c1 = a1 + b1 + b2 + b3
Important: 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 from r.mapcalc has been extended for the temporal raster algebra with 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. A single neighborhood modifier gets read as a temporal offset [t], while two neighborhood modifiers get read as row and column offsets [r,c]. πŸ’…

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 πŸŽ€

okok this is the part everyone actually scrolls to, and bestie, it delivers. real recipes, real expressions, no cap πŸ’–


πŸ’… Computation of NDVI πŸ’…

the classic. sentinel-2 bands live in two separate STDRS and we're about to make them kiss:

# Sentinel-2 bands are stored separately in two STDRS "S2_b4" and "S2_b8"
g.region raster=sentinel2_B04_10m -p
t.rast.list S2_b4
t.rast.list S2_b8
t.rast.algebra basename=ndvi expression="ndvi = float(S2_b8 - S2_b4) / ( S2_b8 + S2_b4 )"
t.rast.colors input=ndvi color=ndvi

set the region, peek at both STRDS, run the algebra, slap the ndvi color table on it. done. iconic 🌈


πŸ¦‹ Sum of space-time raster datasets πŸ¦‹

adding STRDS A to STRDS B where the time stamps line up and they're temporally before Jan. 1. 2005, then dumping the result into STRDS D:

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

and if you just want the straight-up sum of every map in A and B that shares a time stamp, new maps landing in STRDS C:

C = A + B

two lines, whole pipelines of work. slay ✨


πŸ“ Sum of space-time raster datasets with temporal topology relation πŸ“

same expression, but now we're spelling out the temporal topology relation and the temporal operators like the main characters we are:

C = A {+,equal,l} B

🌸 Selection of raster cells 🌸

grabbing every cell from STRDS B that has an equal temporal relation to STRDS A β€” but only if the cells of A sit in the range [100.0, 1600] and we're in time intervals longer than 30 days (that's Jan, Mar, May, Jul, Aug, Oct, Dec, the long months, the ones with range πŸ’€):

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

⭐ Selection of raster cells with temporal topology relation ⭐

same thing, but the temporal topology relation and temporal operators are written out explicitly, no vibes left ambiguous:

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

πŸ’œ Conditional computation πŸ’œ

computing recharge in meters per second for every cell of precipitation STRDS Prec, but only if the mean temperature in STRDS Temp is above 10 degrees. and it only happens when Prec and Temp have equal time stamps. the number of days (or fraction of days) per interval comes from the td() function, which here takes STRDS Prec as its argument:

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

🧚 Conditional computation with temporal topology relation 🧚

you already know what time it is β€” same expression, temporal topology relation and temporal operators spelled out for real:

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

πŸ‘‘ Computation with time intervals πŸ‘‘

compute the mean value of all maps from STRDS A that fall inside time intervals of STRDS B β€” but if more than one map of A is contained in an interval of B, use A instead. the resulting time intervals come from either B or A:

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

πŸ’€ Computation with time intervals with temporal topology relation πŸ’€

yep. same expression. temporal topology relation and temporal operators, explicit, every single one:

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

🫢 Compute DOY for spatio-temporal conditions 🫢

compute the DOY for all maps from STRDS A where the conditions are met at three consecutive time intervals (say, temperature > 0):

B = if(A > 0.0 && A[-1] > 0.0 && A[-2] > 0.0, start_doy(A, -1), 0)"

🌈 SEE ALSO 🌈

go read these next, fr fr:

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

also the Temporal data processing Wiki, which you should absolutely go check πŸ¦‹


πŸ“š REFERENCES πŸ“š

the related publications, because citing your sources is the highest form of main character energy:

  • Gebbert, S., Pebesma, E. 2014. TGRASS: A temporal GIS for field based environmental modeling. Environmental Modelling & Software 53, 1-12 (DOI) - preprint PDF
  • Gebbert, S., Pebesma, E. 2017. The GRASS GIS temporal framework. International Journal of Geographical Information Science 31, 1273-1292 (DOI)
  • Gebbert, S., Leppelt, T., Pebesma, E., 2019. A topology based spatio-temporal map algebra for big data analysis. Data 4, 86. (DOI)

✨ SEE ALSO (the remix) ✨

yes bestie there's a second one, and it's a different list, don't skip it:

  • v.overlay
  • v.buffer
  • v.patch
  • r.mapcalc

πŸ’– AUTHORS πŸ’–

Thomas Leppelt, SΓΆren Gebbert, ThΓΌnen Institute of Climate-Smart Agriculture β€” the literal legends who built this πŸ‘‘


πŸ”₯ SOURCE CODE πŸ”₯

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

Latest change: Tuesday Jan 27 12:40:13 2026 in commit: 5e8b13d47dd4fc81efa5abefe90c34f7a8291fb9

handy index lineup, go wander:

  • Main index
  • Temporal index
  • Topics index
  • Keywords index
  • Graphical index
  • Full index

Β© 2003-2026 GRASS Development Team, GRASS 8.5.0 Reference Manual


and that's the whole page, bestie πŸ’… this was t.rast.algebra(1grass) from the GRASS 8.5.0 Reference Manual, last touched in commit 5e8b13d47dd4fc81efa5abefe90c34f7a8291fb9 on Tuesday Jan 27 12:40:13 2026. go make some beautiful space-time rasters, you've got this πŸŽ€πŸ¦‹βœ¨

πŸ“¦ where this page comes from