Debian Codegraph @

raster/r.mapcalc/r.mapcalc.html

raw
1 <h2>DESCRIPTION</h2>
2
3 <em>r.mapcalc</em> performs arithmetic on raster map layers.
4 New raster map layers can be created which are arithmetic expressions
5 involving existing raster map layers, integer or floating point constants,
6 and functions.
7
8 <h3>Program use</h3>
9
10 <em>r.mapcalc</em> expression have the form:
11 <p><b>result =</b><em> expression</em>
12 <p>where <em>result</em> is the name of a raster map layer
13 to contain the result of the calculation and
14 <b>expression</b> is any legal arithmetic expression involving existing
15 raster map layers (except <em>result</em> itself), integer or floating point constants,
16 and functions known to the calculator.
17 Parentheses are allowed in the expression and may be nested to any depth.
18 <em>result</em> will be created in the user's current mapset.
19 <p>
20 As <b>expression=</b> is the first option, it is the default. This
21 means that passing an expression on the command line is possible
22 as long as the expression is quoted and a space is included before the
23 first <em>=</em> sign.
24
25 Example ('foo' is the resulting map):
26 <div class="code"><pre>
27 r.mapcalc "foo = 1"
28 </pre></div>
29 or:
30 <div class="code"><pre>
31 r.mapcalc 'foo = 1'
32 </pre></div>
33
34 An unquoted expression (i.e. split over multiple arguments) won't
35 work, nor will omitting the space before the = sign:
36
37 <div class="code"><pre>
38 r.mapcalc 'foo=1'
39 Sorry, &lt;foo&gt; is not a valid parameter
40 </pre></div>
41
42 To read command from the file, use file=
43 explicitly, e.g.:
44
45 <div class="code"><pre>
46 r.mapcalc file=file
47 </pre></div>
48 or:
49 <div class="code"><pre>
50 r.mapcalc file=- &lt; file
51 </pre></div>
52 or:
53 <div class="code"><pre>
54 r.mapcalc file=- &lt;&lt;EOF
55 foo = 1
56 EOF
57 </pre></div>
58
59 <p>The formula entered to <em>r.mapcalc</em> by the user is recorded both in the
60 <em>result</em> map title (which appears in the category file for <em>result</em>)
61 and in the history file for <em>result</em>.
62 <p>Some characters have special meaning to the command shell. If the user
63 is entering input to <em>r.mapcalc</em> on the command line, expressions
64 should be enclosed within single quotes. See NOTES, below.
65 </p>
66 <h3>Computational regions in r.mapcalc</h3>
67 <p>
68 By default <em>r.mapcalc</em> uses the current region
69 as computational region that
70 was set with <a href="g.region.html">g.region</a> for processing.
71 Sometimes it is necessary to use a region that is derived from the
72 raster maps in the expression to set the computational region.
73 This is of high importance for modules that use r.mapcalc internally
74 to process time series of satellite images that all have different
75 spatial extents. A module that requires this feature
76 is <a href="t.rast.algebra.html">t.rast.algebra</a>.
77 The <em>region</em> option of <em>r.mapcalc</em>
78 was implemented to address this requirement.
79 It allows computing and using a region based on
80 all raster maps in an expression. Three modes are supported:
81 </p>
82 <ul>
83 <li>
84 Setting the <em>region</em> parameter to <em>current</em>
85 will result in the use of the current region
86 as computational region. This is the default.
87 The current region can be set with <a href="g.region.html">g.region</a>.
88 </li>
89 <li>
90 The parameter <em>union</em> will force r.mapcalc
91 to compute the disjoint union of all regions from raster
92 maps specified in the expression. This
93 computed region will then be used as computational region at runtime.
94 The region of the mapset will not be modified.
95 The smallest spatial resolution
96 of all raster maps will be used for processing.
97 </li>
98 <li>
99 The parameter <em>intersect</em> will force r.mapcalc
100 to compute the intersection of all regions from raster
101 maps specified in the expression. This
102 computed region will then be used as computational region at runtime.
103 The region of the mapset will not be modified.
104 The smallest spatial resolution
105 of all raster maps will be used for processing.
106 </li>
107 </ul>
108
109 <h3>Operators and order of precedence</h3>
110
111 The following operators are supported:
112 <div class="code"><pre>
113 Operator Meaning Type Precedence
114 --------------------------------------------------------------
115 - negation Arithmetic 12
116 ~ one's complement Bitwise 12
117 ! not Logical 12
118 ^ exponentiation Arithmetic 11
119 % modulus Arithmetic 10
120 / division Arithmetic 10
121 * multiplication Arithmetic 10
122 + addition Arithmetic 9
123 - subtraction Arithmetic 9
124 &lt;&lt; left shift Bitwise 8
125 &gt;&gt; right shift Bitwise 8
126 &gt;&gt;&gt; right shift (unsigned) Bitwise 8
127 &gt; greater than Logical 7
128 &gt;= greater than or equal Logical 7
129 &lt; less than Logical 7
130 &lt;= less than or equal Logical 7
131 == equal Logical 6
132 != not equal Logical 6
133 &amp; bitwise and Bitwise 5
134 | bitwise or Bitwise 4
135 &amp;&amp; logical and Logical 3
136 &amp;&amp;&amp; logical and[1] Logical 3
137 || logical or Logical 2
138 ||| logical or[1] Logical 2
139 ?: conditional Logical 1
140 </pre></div>
141 (modulus is the remainder upon division)
142 <p>[1] The &amp;&amp;&amp; and ||| operators handle null values differently to other
143 operators. See the section entitled <b>NULL support</b> below for more
144 details.
145 <p>The operators are applied from left to right, with those of higher precedence
146 applied before those with lower precedence.
147 Division by 0 and modulus by 0 are acceptable and give a NULL result.
148 The logical operators give a 1 result if the comparison is true, 0 otherwise.
149 <p><p>
150
151 <h3>Raster map layer names</h3>
152
153 Anything in the expression which is not a number, operator, or function name
154 is taken to be a raster map layer name.
155 Examples:
156 <p><div class="code"><pre>
157 elevation
158 x3
159 3d.his
160 </pre></div>
161 <p>Most GRASS raster map layers meet this naming convention.
162 However, if a raster map layer has a name which conflicts with the
163 above rule, it should be quoted. For example, the expression
164 <p><div class="code"><pre>
165 x = a-b
166 </pre></div>
167 <p>would be interpreted as: x equals a minus b, whereas
168 <p><div class="code"><pre>
169 x = "a-b"
170 </pre></div>
171 <p>would be interpreted as: x equals the raster map layer named <em>a-b</em>
172 <p>Also
173 <p><div class="code"><pre>
174 x = 3107
175 </pre></div>
176 <p>would create <em>x</em> filled with the number 3107, while
177 <p><div class="code"><pre>
178 x = "3107"
179 </pre></div>
180 <p>would copy the raster map layer <em>3107</em> to the raster map layer <em>x</em>.
181
182 <p>Quotes are not required unless the raster map layer names
183 look like numbers or contain operators, OR unless the program
184 is run non-interactively. Examples given here assume the
185 program is run interactively. See NOTES, below.
186 <p><em>r.mapcalc</em> will look for the raster map layers according to the
187 user's current mapset search path.
188 It is possible to override the search path and specify the mapset
189 from which to select the raster map layer.
190 This is done by specifying the raster map layer name in the form:
191 <p><div class="code"><pre>
192 name@mapset
193 </pre></div>
194 <p>For example, the following is a legal expression:
195 <p><div class="code"><pre>
196 result = x@PERMANENT / y@SOILS
197 </pre></div>
198 <p>The mapset specified does not have to be in the mapset search path.
199 (This method of overriding the mapset search path is common to all
200 GRASS commands, not just <em>r.mapcalc</em>.)
201 <p>
202
203 <h3>The neighborhood modifier</h3>
204
205 Maps and images are data base files stored in raster format, i.e.,
206 two-dimensional matrices of integer values.
207 In <em>r.mapcalc</em>, maps may be followed by a <em>neighborhood</em> modifier that
208 specifies a relative offset from the current cell being evaluated. The format is
209 <em>map[r,c]</em>, where <em>r</em> is the row offset and <em>c</em> is the column offset.
210 For example, <em>map[1,2]</em> refers to the cell one row below and two columns
211 to the right of the current cell, <em>map[-2,-1]</em> refers to the cell
212 two rows above and one column to the left of the current cell,
213 and <em>map[0,1]</em> refers to the cell one column to the right of the current cell.
214 This syntax permits the development of neighborhood-type filters within a single
215 map or across multiple maps.
216 <p>
217
218 The neighborhood modifier cannot be used on maps generated within same <em>r.mapcalc</em>
219 command run (see "KNOWN ISSUES" section).
220 <p>
221
222 <h3>Raster map layer values from the category file</h3>
223
224 Sometimes it is desirable to use a value associated with a category's
225 <em>label</em> instead of the category value itself. If a raster
226 map layer name is preceded by the <b>@</b>
227 operator, then the labels in the category file for the raster map layer
228 are used in the expression instead of the category value.
229 <p>For example, suppose that the raster map layer <em>soil.ph</em>
230 (representing soil pH values) has a category file with labels as follows:
231 <p><div class="code"><pre>
232 cat label
233 ------------------
234 0 no data
235 1 1.4
236 2 2.4
237 3 3.5
238 4 5.8
239 5 7.2
240 6 8.8
241 7 9.4
242 </pre></div>
243 <p>Then the expression:
244 <p><div class="code"><pre>
245 result = @soils.ph
246 </pre></div>
247 <p>would produce a result with category values
248 0, 1.4, 2.4, 3.5, 5.8, 7.2, 8.8 and 9.4.
249 <p>Note that this operator may only be applied to raster map layers
250 and produces a floating point value in the expression.
251 Therefore, the category label must start with a valid number.
252 If the category label is integer, it will be represented by
253 a floating point number. I the category label does not start
254 with a number or is missing, it will be represented by NULL
255 (no data) in the resulting raster map.
256
257 <h3>Grey scale equivalents and color separates</h3>
258
259 It is often helpful to manipulate the colors assigned to map categories.
260 This is particularly useful when the spectral properties of cells have meaning
261 (as with imagery data), or when the map category values represent real
262 quantities (as when category values reflect true elevation values).
263 Map color manipulation can also aid visual recognition, and map printing.
264 <p>The # operator can be used to either convert map category values to their
265 grey scale equivalents or to extract the red, green, or blue components
266 of a raster map layer into separate raster map layers.
267 <p><div class="code"><pre>
268 result = #map
269 </pre></div>
270 <p>converts each category value in <em>map</em> to a value in the range 0-255 which
271 represents the grey scale level implied by the color for the category.
272 If the map has a grey scale color table, then the grey level is what
273 #map evaluates to. Otherwise, it is computed as:
274 <p><div class="code"><pre>
275 0.10 * red + 0.81 * green + 0.01 * blue
276 </pre></div>
277 <p>Alternatively, you can use:
278 <p><div class="code"><pre>
279 result = y#map
280 </pre></div>
281 <p>to use the NTSC weightings:
282 <p><div class="code"><pre>
283 0.30 * red + 0.59 * green + 0.11 * blue
284 </pre></div>
285 <p>Or, you can use:
286 <p><div class="code"><pre>
287 result = i#map
288 </pre></div>
289 <p>to use equal weightings:
290 <p><div class="code"><pre>
291 0.33 * red + 0.33 * green + 0.33 * blue
292 </pre></div>
293 <p>The # operator has three other forms: r#map, g#map, b#map.
294 These extract the red, green, or blue components in the named raster map,
295 respectively. The GRASS shell script <em><a href="r.blend.html">r.blend</a></em> extracts each of these
296 components from two raster map layers, and combines them by a user-specified
297 percentage.
298 These forms allow color separates to be made. For example, to
299 extract the red component from <em>map</em>
300 and store it in the new 0-255 map layer <em>red</em>,
301 the user could type:
302 <p><div class="code"><pre>
303 red = r#map
304 </pre></div>
305 <p>To assign this map grey colors type:
306 <p><div class="code"><pre>
307 <a href="r.colors.html">r.colors</a> map=red color=rules
308 black
309 white
310 </pre></div>
311 <p>To assign this map red colors type:
312 <p><div class="code"><pre>
313 <a href="r.colors.html">r.colors</a> map=red color=rules
314 black
315 red
316 </pre></div>
317 <p>
318
319 <h3>Functions</h3>
320
321 The functions currently supported are listed in the table below.
322 The type of the result is indicated in the last column.
323 <em>F</em> means that the functions always results in a floating point value,
324 <em>I</em> means that the function gives an integer result, and
325 <em>*</em> indicates that the result is float if any of the arguments to the
326 function are floating point values and integer if all arguments are integer.
327
328 <p><div class="code"><pre>
329 function description type
330 ---------------------------------------------------------------------------
331 abs(x) return absolute value of x *
332 acos(x) inverse cosine of x (result is in degrees) F
333 asin(x) inverse sine of x (result is in degrees) F
334 atan(x) inverse tangent of x (result is in degrees) F
335 atan(x,y) inverse tangent of y/x (result is in degrees) F
336 ceil(x) the smallest integral value not less than x *
337 cos(x) cosine of x (x is in degrees) F
338 double(x) convert x to double-precision floating point F
339 eval([x,y,...,]z) evaluate values of listed expr, pass results to z
340 exp(x) exponential function of x F
341 exp(x,y) x to the power y F
342 float(x) convert x to single-precision floating point F
343 floor(x) the largest integral value not greater than x *
344 graph(x,x1,y1[x2,y2..]) convert the x to a y based on points in a graph F
345 graph2(x,x1[,x2,..],y1[,y2..])
346 alternative form of graph() F
347 if decision options: *
348 if(x) 1 if x not zero, 0 otherwise
349 if(x,a) a if x not zero, 0 otherwise
350 if(x,a,b) a if x not zero, b otherwise
351 if(x,a,b,c) a if x &gt; 0, b if x is zero, c if x &lt; 0
352 int(x) convert x to integer [ truncates ] I
353 isnull(x) check if x = NULL
354 log(x) natural log of x F
355 log(x,b) log of x base b F
356 max(x,y[,z...]) largest value of those listed *
357 median(x,y[,z...]) median value of those listed *
358 min(x,y[,z...]) smallest value of those listed *
359 mod(x,y) return the modulus (the remainder) of x/y *
360 mode(x,y[,z...]) mode value of those listed *
361 nmax(x,y[,z...]) largest value of those listed, excluding NULLs *
362 nmedian(x,y[,z...]) median value of those listed, excluding NULLs *
363 nmin(x,y[,z...]) smallest value of those listed, excluding NULLs *
364 nmode(x,y[,z...]) mode value of those listed, excluding NULLs *
365 not(x) 1 if x is zero, 0 otherwise
366 pow(x,y) x to the power y *
367 rand(a,b) random value x : a &lt;= x &lt; b *
368 round(x) round x to nearest integer I
369 round(x,y) round x to nearest multiple of y
370 round(x,y,z) round x to nearest y*i+z for some integer i
371 sin(x) sine of x (x is in degrees) F
372 sqrt(x) square root of x F
373 tan(x) tangent of x (x is in degrees) F
374 xor(x,y) exclusive-or (XOR) of x and y I
375 </pre></div>
376
377 <div class="code"><pre>
378 Internal variables:
379 row() current row of moving window I
380 col() current col of moving window I
381 nrows() number of rows in computation region I
382 ncols() number of columns in computation region I
383 x() current x-coordinate of moving window F
384 y() current y-coordinate of moving window F
385 ewres() current east-west resolution F
386 nsres() current north-south resolution F
387 area() area of current cell in square meters F
388 null() NULL value
389 </pre></div>
390 Note, that the row() and col() indexing starts with 1.
391
392 <h3>Data types and their precision</h3>
393
394 There are three data types that can be used within the mapcalc expression. They
395 are defined with certain precision and within certain value range. The
396 following table reports precision and value range info on AMD64 (x86-64)
397 architectures compiled with GCC/CLANG. Note that although this setting is the
398 most frequent one, the ranges could differ for different architectures.
399
400 <p><div class="code"><pre>
401 data type precision and value range info
402 -------------------------------------------------------------------------------
403 int a 32-bit integer with a range from -2,147,483,647 to
404 +2,147,483,647. The value -2,147,483,648 is reserved
405 for NODATA.
406 float a 32-bit float (Float32) with a range from -3.4E38 to
407 3.4E38. However, the integer precision can be only
408 ensured between -16,777,216 and 16,777,216. If your
409 raster overpasses this range, it is strongly suggested
410 to use the type double instead.
411 double a 64-bit float (Float64) with a range from -1.79E308 to
412 1.79E308. It is 8 bytes, 15-17 digits precision.
413 null NULL value. Refer to section "NULL support" below.
414 </pre></div>
415
416 Note that the value counter wraps around when the value overflows its range.
417 E.g., if your expression is <code>a = int(2147483648)</code>, you will get NULL
418 value. For expression <code>a = int(2147483649)</code>, you will reach the lowest
419 value possible instead, i.e. -2147483647.
420
421 <h3>Floating point values in the expression</h3>
422
423 Floating point numbers are allowed in the expression. A floating point
424 number is a number which contains a decimal point:
425 <div class="code"><pre>
426 2.3 12.0 12. .81
427 </pre></div>
428 Floating point values in the expression are handled in a special way.
429 With arithmetic and logical operators, if either operand is float,
430 the other is converted to float and the result of the operation is float.
431 This means, in particular that division of integers results in a
432 (truncated) integer, while division of floats results in an accurate
433 floating point value. With functions of type * (see table above),
434 the result is float if any argument is float, integer otherwise.
435 <p>Note: If you calculate with integer numbers, the resulting map will
436 be integer. If you want to get a float result, add the decimal point
437 to integer number(s).
438 <p>If you want floating point division, at least one of the arguments has
439 to be a floating point value. Multiplying one of them by 1.0 will
440 produce a floating-point result, as will using float():
441 <div class="code"><pre>
442 r.mapcalc "ndvi = float(lsat.4 - lsat.3) / (lsat.4 + lsat.3)"
443 </pre></div>
444
445 <h3>NULL support</h3>
446
447 <ul>
448 <li>Division by zero should result in NULL.</li>
449 <li>Modulus by zero should result in NULL.</li>
450 <li>NULL-values in any arithmetic or logical operation should result
451 in NULL. (however, &amp;&amp;&amp; and ||| are treated specially, as described below).</li>
452 <li>The &amp;&amp;&amp; and ||| operators observe the following axioms even when x is NULL:
453 <div class="code"><pre>
454 x &amp;&amp;&amp; false == false
455 false &amp;&amp;&amp; x == false
456 x ||| true == true
457 true ||| x == true
458 </pre></div></li>
459 <li>NULL-values in function arguments should result in NULL (however,
460 if(), eval() and isnull() are treated specially, as described below).</li>
461 <li>The eval() function always returns its last argument</li>
462 <li>The situation for if() is:
463 <div class="code"><pre>
464 if(x)
465 NULL if x is NULL; 0 if x is zero; 1 otherwise
466 if(x,a)
467 NULL if x is NULL; a if x is non-zero; 0 otherwise
468 if(x,a,b)
469 NULL if x is NULL; a if x is non-zero; b otherwise
470 if(x,n,z,p)
471 NULL if x is NULL; n if x is negative;
472 z if x is zero; p if x is positive
473 </pre></div></li>
474 <li>The (new) function isnull(x) returns: 1 if x is NULL;
475 0 otherwise. The (new) function null()
476 (which has no arguments) returns an integer NULL.</li>
477 <li>Non-NULL, but invalid, arguments to functions should result in NULL.
478 <div class="code"><pre>
479 Examples:
480 log(-2)
481 sqrt(-2)
482 pow(a,b) where a is negative and b is not an integer
483 </pre></div></li>
484 </ul>
485 <p>NULL support: Please note that any math performed with NULL cells always
486 results in a NULL value for these cells. If you want to replace a NULL cell
487 on-the-fly, use the isnull() test function in a if-statement.
488 <p>Example: The users wants the NULL-valued cells to be treated like zeros. To
489 add maps A and B (where B contains NULLs) to get a map C the user can use a
490 construction like:<p><div class="code"><pre>
491 C = A + if(isnull(B),0,B)
492 </pre></div>
493 <p><b>NULL and conditions:</b>
494 <p>For the one argument form:
495 <div class="code"><pre>
496 if(x) = NULL if x is NULL
497 if(x) = 0 if x = 0
498 if(x) = 1 otherwise (i.e. x is neither NULL nor 0).
499 </pre></div>
500 <p>For the two argument form:
501 <div class="code"><pre>
502 if(x,a) = NULL if x is NULL
503 if(x,a) = 0 if x = 0
504 if(x,a) = a otherwise (i.e. x is neither NULL nor 0).
505 </pre></div>
506 <p>For the three argument form:
507 <div class="code"><pre>
508 if(x,a,b) = NULL if x is NULL
509 if(x,a,b) = b if x = 0
510 if(x,a,b) = a otherwise (i.e. x is neither NULL nor 0).
511 </pre></div>
512 <p>For the four argument form:
513 <div class="code"><pre>
514 if(x,a,b,c) = NULL if x is NULL
515 if(x,a,b,c) = a if x &gt; 0
516 if(x,a,b,c) = b if x = 0
517 if(x,a,b,c) = c if x &lt; 0
518 </pre></div>
519 More generally, all operators and most functions return NULL if *any*
520 of their arguments are NULL.
521 <br>
522 The functions if(), isnull() and eval() are exceptions.
523 <br>
524 The function isnull() returns 1 if its argument is NULL and 0 otherwise.
525 If the user wants the opposite, the ! operator, e.g. "!isnull(x)" must be
526 used.
527 <p>All forms of if() return NULL if the first argument is NULL. The 2, 3
528 and 4 argument forms of if() return NULL if the "selected" argument is
529 NULL, e.g.:
530 <div class="code"><pre>
531 if(0,a,b) = b regardless of whether a is NULL
532 if(1,a,b) = a regardless of whether b is NULL
533 </pre></div>
534 eval() always returns its last argument, so it only returns NULL if
535 the last argument is NULL.
536 <p><b>Note</b>: The user cannot test for NULL using the == operator, as that
537 returns NULL if either or both arguments are NULL, i.e. if x and y are
538 both NULL, then "x == y" and "x != y" are both NULL rather than 1 and
539 0 respectively.
540 <br>
541 The behaviour makes sense if the user considers NULL as representing an
542 unknown quantity. E.g. if x and y are both unknown, then the values of
543 "x == y" and "x != y" are also unknown; if they both have unknown
544 values, the user doesn't know whether or not they both have the same value.
545
546 <h2>NOTES</h2>
547
548 <h3>Usage from command line</h3>
549
550 Extra care must be taken if the expression is given on the command line.
551 Some characters have special meaning to the UNIX shell.
552 These include, among others:
553 <div class="code"><pre>
554 * ( ) &gt; &amp; |
555 </pre></div>
556 <p>It is advisable to put single quotes around the expression; e.g.:
557 <div class="code"><pre>
558 'result = elevation * 2'
559 </pre></div>
560 Without the quotes, the <code>*</code>, which has special meaning to the UNIX shell,
561 would be altered and <em>r.mapcalc</em> would see something other than the <code>*</code>.
562
563 <h3>Multiple computations</h3>
564 <p>
565 In general, it's preferable to do as much as possible in each
566 r.mapcalc command. E.g. rather than:
567 <div class="code"><pre>
568 r.mapcalc "$GIS_OPT_OUTPUT.r = r#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * r#$GIS_OPT_SECOND"
569 r.mapcalc "$GIS_OPT_OUTPUT.g = g#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * g#$GIS_OPT_SECOND"
570 r.mapcalc "$GIS_OPT_OUTPUT.b = b#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * b#$GIS_OPT_SECOND"
571 </pre></div>
572 <p>use:
573 <div class="code"><pre>
574 r.mapcalc &lt;&lt;EOF
575 $GIS_OPT_OUTPUT.r = r#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * r#$GIS_OPT_SECOND
576 $GIS_OPT_OUTPUT.g = g#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * g#$GIS_OPT_SECOND
577 $GIS_OPT_OUTPUT.b = b#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * b#$GIS_OPT_SECOND
578 EOF
579 </pre></div>
580 <p>as the latter will read each input map only once.
581
582 <h3>Backwards compatibility</h3>
583
584 For the backwards compatibility with GRASS 6,
585 <!-- check wording: -->
586 if no options are given, it manufactures <code>file=-</code> (which reads from
587 stdin), so you can continue to use e.g.:
588 <div class="code"><pre>
589 r.mapcalc &lt; file
590 </pre></div>
591 or:
592 <div class="code"><pre>
593 r.mapcalc &lt;&lt;EOF
594 foo = 1
595 EOF
596 </pre></div>
597 But unless you need compatibility with previous GRASS versions, use <code>file=</code>
598 explicitly, as stated above.
599 <p>
600 When the map name contains uppercase letter(s) or a dot which are not
601 allowed to be in module option names, the <em>r.mapcalc</em> command will
602 be valid also without quotes:
603 <div class="code"><pre>
604 r.mapcalc elevation_A=1
605 r.mapcalc elevation.1=1
606 </pre></div>
607 However, this syntax is not recommended as quotes as stated above more safe.
608 Using quotes is both backwards compatible and valid in future.
609
610 <h3>Interactive input in command line</h3>
611
612 For formulas that the user enters from standard input
613 (rather than from the command line), a line continuation feature now exists.
614 If the user adds a backslash to the end of an input line, <em>r.mapcalc</em> assumes that
615 the formula being entered by the user continues on to the next input line.
616 There is no limit to the possible number of input lines
617 or to the length of a formula.
618 <p>If the <em>r.mapcalc</em> formula entered by the user is very long,
619 the map title will contain only some of it, but most (if not all) of
620 the formula will be placed into the history file for the <em>result</em> map.
621
622 <h3>Raster mask handling</h3>
623 <p>
624 <em>r.mapcalc</em> follows the common GRASS behavior of raster mask handling,
625 so the mask is only applied when reading an existing GRASS raster map.
626 This implies that, for example, the command:
627 <div class="code"><pre>
628 r.mapcalc "elevation_exaggerated = elevation * 3"
629 </pre></div>
630 create a map with NULL cells for the masked-out cells if raster mask is active.
631
632 <p>
633 However, when creating a map which is not based on any map,
634 e.g. a map from a constant:
635 <div class="code"><pre>
636 r.mapcalc "base_height = 200.0"
637 </pre></div>
638 the created raster map is limited only by a computation region
639 but it is not affected by an active raster mask.
640 This is expected because, as mentioned above, the mask is only applied
641 when reading, not when writing a raster map.
642 If the raster mask should be applied in this case, an <code>if()</code>
643 function including the mask raster should be used, e.g.
644 (assuming the mask is called <code>MASK</code>):
645 <div class="code"><pre>
646 r.mapcalc "base_height = if(MASK, 200.0, null())"
647 </pre></div>
648
649 <p>
650 When testing expressions related to mask handling keep in mind that
651 when the raster mask is active,
652 you don't see data in masked areas even if they are not NULL.
653 See <em><a href="r.mask.html">r.mask</a></em> for details.
654
655 <h3>eval function</h3>
656 If the output of the computation should be only one map
657 but the expression is so complex that it is better to split it
658 to several expressions, the <code>eval</code> function can be used:
659 <div class="code"><pre>
660 r.mapcalc &lt;&lt; EOF
661 eval(elev_200 = elevation - 200, \
662 elev_5 = 5 * elevation, \
663 elev_p = pow(elev_5, 2))
664 elevation_result = (0.5 * elev_200) + 0.8 * elev_p
665 EOF
666 </pre></div>
667 This example uses unix-like <code>&lt;&lt; EOF</code> syntax to provide
668 input to <em>r.mapcalc</em>.
669 <p>
670 Note that the temporary variables (maps) are not created
671 and thus it does not matter whether they exists or not.
672 In the example above, if map <code>elev_200</code> exists it will not be
673 overwritten and no error will be generated.
674 The reason is that the name <code>elev_200</code> now denotes the temporary
675 variable (map) and not the existing map.
676 The following parts of the expression will use the temporary <code>elev_200</code>
677 and the existing <code>elev_200</code> will be left intact and will not be used.
678 If a user want to use the existing map, the name of the temporary variable
679 (map) must be changed.
680
681 <h3>Using the same map for input and output results</h3>
682 A map cannot be used both as an input and as an output as in
683 this invalid expression <code>oldmap = oldmap + 1</code>, instead
684 a subsequent rename using <em><a href="g.rename.html">g.rename</a></em> is
685 needed when the same name is desired:
686
687 <div class="code"><pre>
688 r.mapcalc "newmap = oldmap + 1"
689 g.rename raster=newmap,oldmap
690 </pre></div>
691
692 <h3>Random number generator initialization</h3>
693 <p>The pseudo-random number generator used by the rand() function can
694 be initialised to a specific value using the <b>seed</b> option.
695 This can be used to replicate a previous calculation.
696 <p>Alternatively, it can be initialised from the system time and the
697 PID using the <b>-s</b> flag. This should result in a different seed
698 being used each time.
699 <p>In either case, the seed will be written to the map's history, and
700 can be seen using <em>r.info</em>.
701 <p>If you want other people to be able to verify your results, it's
702 preferable to use the <b>seed</b> option to supply a seed which is
703 either specified in the script or generated from a deterministic process
704 such as a pseudo-random number generator given an explicit seed.
705 <p>Note that the rand() function will generate a fatal error if neither
706 the <b>seed</b> option nor the <b>-s</b> flag are given.
707
708 <h2>EXAMPLES</h2>
709
710 To compute the average of two raster map layers
711 <em>a</em> and <em>b</em>:
712 <div class="code"><pre>
713 ave = (a + b)/2
714 </pre></div>
715
716 <p>
717 To form a weighted average:
718 <div class="code"><pre>
719 ave = (5*a + 3*b)/8.0
720 </pre></div>
721
722 <p>
723 To produce a binary representation of the raster map layer
724 <em>a</em> so that category 0 remains 0 and all other categories become 1:
725 <div class="code"><pre>
726 mapmask = a != 0
727 </pre></div>
728 This could also be accomplished by:
729 <div class="code"><pre>
730 mapmask = if(a)
731 </pre></div>
732
733 <p>
734 To mask raster map layer <em>b</em> by raster map layer <em>a</em>:
735 <div class="code"><pre>
736 result = if(a,b)
737 </pre></div>
738
739 <p>
740 To change all values below 5 to NULL, keep 5 otherwise:
741 <div class="code"><pre>
742 newmap = if(map&lt;5, null(), 5)
743 </pre></div>
744
745 <p>
746 To create a map with random values in a defined range (needs either the
747 usage of <b>-s</b> flag or the <em>seed</em> parameter). The precision of
748 the input values determines the output precision (the resulting
749 <a href="rasterintro.html#raster-format">raster map type</a>):
750
751 <div class="code"><pre>
752 # write result as integer map (CELL)
753 random_int = rand(-100,100)
754
755 # write result as double precision floating point map (DCELL)
756 random_dcell = rand(-100.0,100.0)
757
758 # write result as single precision floating point map (FCELL)
759 random_fcell = float(rand(-100.0,100.0))
760 </pre></div>
761
762 <p>
763 The graph() function allows users to specify a x-y conversion using
764 pairs of x,y coordinates.
765 In some situations a transformation from one value to another is not
766 easily established mathematically, but can be represented by a 2-D
767 graph and then linearly interpolated. The graph() function provides
768 the opportunity to accomplish this.
769 An x-axis value is provided to the graph function along with
770 the associated graph represented by a series of x,y pairs. The x
771 values must be monotonically increasing (each larger than or equal to
772 the previous). The graph function linearly interpolates between
773 pairs. Any x value lower the lowest x value (i.e. first) will have
774 the associated y value returned. Any x value higher than the last
775 will similarly have the associated y value returned. Consider the
776 request:
777 <div class="code"><pre>
778 newmap = graph(map, 1,10, 2,25, 3,50)
779 </pre></div>
780 X (map) values supplied and y (newmap) values returned:
781 <div class="code"><pre>
782 0, 10
783 1, 10
784 1.5, 17.5
785 2.9, 47.5
786 4, 50
787 100, 50
788 </pre></div>
789
790 <h2>KNOWN ISSUES</h2>
791
792 The <em>result</em> variable on the left hand side of the equation should not
793 appear in the <em>expression</em> on the right hand side.
794
795 <div class="code"><pre>
796 <s>mymap = if( mymap &gt; 0, mymap, 0)</s>
797 </pre></div>
798
799 <p>Any maps generated by a <em>r.mapcalc</em> command only exist after the entire
800 command has completed. All maps are generated concurrently, row-by-row
801 (i.e. there is an implicit "for row in rows {...}" around the entire expression).
802 Thus the <code>#</code>, <code>@</code>, and <code>[ ]</code> operators cannot be used on a map
803 generated within same <em>r.mapcalc</em> command run.
804 Consequently, the following (strikethrough code) does not work:
805
806 <div class="code"><pre>
807 newmap = oldmap * 3.14
808 <s>othermap = newmap[-1, 0] / newmap[1, 0]</s>
809 </pre></div>
810
811 <p>Continuation lines must end with a <code>\</code> and have <em>no</em> trailing
812 white space (blanks or tabs). If the user does leave white space at the end of
813 continuation lines, the error messages produced by <em>r.mapcalc</em> will
814 be meaningless and the equation will not work as the user intended.
815 This is particularly important for the <code>eval()</code> function.
816 <p><!-- STILL TRUE ??-->
817 Currently, there is no comment mechanism in <em>r.mapcalc</em>.
818 Perhaps adding a capability that would cause the entire line to be
819 ignored when the user inserted a # at the start of a line
820 as if it were not present, would do the trick.
821 <p>The function should require the user to type "end" or "exit" instead
822 of simply a blank line. This would make separation of multiple scripts
823 separable by white space.
824 <p><em>r.mapcalc</em> does not print a warning in case of operations on
825 NULL cells. It is left to the user to utilize the <code>isnull()</code> function.
826
827 <h2>REFERENCES</h2>
828
829 <b><a href="https://grass.osgeo.org/gdp/raster/mapcalc-algebra.pdf">r.mapcalc: An Algebra for GIS and Image
830 Processing</a></b>, by Michael Shapiro and Jim Westervelt, U.S. Army
831 Construction Engineering Research Laboratory (March/1991).
832 <p>
833 <b><a href="https://grass.osgeo.org/history_docs/mapcalc.pdf">Performing Map Calculations on GRASS Data:
834 r.mapcalc Program Tutorial</a></b>, by Marji Larson, Michael Shapiro and Scott
835 Tweddale, U.S. Army Construction Engineering Research Laboratory (December
836 1991)
837 <p>Grey scale conversion is based on the C.I.E. x,y,z system where y represents
838 luminance. See "Fundamentals of Digital Image Processing,"
839 by Anil K. Jain (Prentice Hall, NJ, 1989; p 67).
840
841 <h2>SEE ALSO</h2>
842
843 <em>
844 <a href="g.region.html">g.region</a>,
845 <a href="https://grass.osgeo.org/grass-stable/manuals/addons/r.bitpattern.html">r.bitpattern</a>,
846 <a href="r.blend.html">r.blend</a>,
847 <a href="r.colors.html">r.colors</a>,
848 <a href="r.fillnulls.html">r.fillnulls</a>,
849 <a href="r.mapcalc.simple.html">r.mapcalc.simple</a>
850 </em>
851
852 <h2>AUTHORS</h2>
853
854 Michael Shapiro, U.S.Army Construction Engineering
855 Research Laboratory
856 <p>Glynn Clements