Back to GRASS: simple mapcalc issue

Dear list,

after a long time "off GRASS" (and using other nastier packages) I have
a chance to work again with this GREAT PIECE OF SOFTWARE. My
congratulations to all the people who are maintaining GRASS.

...I have a problem which should be a trivial issue for more recently
trained users:

I would simply like to calculate the sum of two rasters r1 and r2
(suppose these are rasters with two areas -which partially overlay- and
"null" values around). In my grass4.1 memories, The result of "r1 +r2"
should return

1- the sum of r1 and r2 where the rasters overlay,
2- the values of r1 (or r2) where they do not overlay.

r.mapcalc in grass 5 does (1) but returns Null instead of (2)...

I was thinking of overcoming this behaviour with some logical operators
or other things, but there must be a much simpler way...
(I remember some documentation explaining how Null behaves, but I can't
find it now on the web site).

Regards and TIA for any feedback,

--

Andrea Giacomelli
Centre for Advanced Studies, Research and
Development in Sardinia
Environment Group

http://www.crs4.it/~andreag

Dear Andrea,

On Wed, Apr 05, 2000 at 09:46:58AM +0200, A. Giacomelli wrote:

Dear list,

after a long time "off GRASS" (and using other nastier packages) I have
a chance to work again with this GREAT PIECE OF SOFTWARE. My
congratulations to all the people who are maintaining GRASS.

nice to know you back!

I would simply like to calculate the sum of two rasters r1 and r2
(suppose these are rasters with two areas -which partially overlay- and
"null" values around). In my grass4.1 memories, The result of "r1 +r2"
should return

1- the sum of r1 and r2 where the rasters overlay,
2- the values of r1 (or r2) where they do not overlay.

r.mapcalc in grass 5 does (1) but returns Null instead of (2)...

Currently the behaviour of r.mapcalc in conjuction with NULL
values is a bit strange (at least to me!).

Check, if one or both maps contain NULL values. To my experience
r.mapcalc works o.k. if having maps without NULL values.
If NULLs are existing, map additions do not work sometimes.

r.mapcalc *should* calculate to your wishes, otherwise I guess
it is a bug. Is there anyone else with such experience?

Kind regards

Markus Neteler

The documentation for r.mapcalc and NULL values is at:
http://hgeo02.geog.uni-hannover.de/grass/gdp/html_grass5/programming/raster/fpnv.html

In short, you probably need to use the isnull() function. Otherwise,
sometimes it is appropriate to use r.null to set the nulls to zero.
NULL doesn't behave as zeros did because NULL is suppose to represent
"undefined" and operations using undefined will (by definition)
result in undefined.

I copied it here for you:

--------------------

Division by zero should result in NULL.

     This is a non-backward-compatible change and should be
         noted in the manual entry for r.mapcalc
   as well as in any release notes.

     Modulus by zero should result in NULL.

     This is a non-backward-compatible change and should be
   noted in the manual entry for r.mapcalc
   as well as in any release notes.

     NULL-values in any arithmetic operation should result in NULL.
     NULL-values in logical operations should be handled as follows:
              NULL || true = true
              NULL || false = NULL
              NULL || NULL = NULL
              NULL && true = NULL
              NULL && false = false
              NULL && NULL = NULL
     NULL-values in function arguments should result in NULL.

     Even though this is also true for the if() function,
   if() is spelled out here for clarification:

     if(x)
         NULL if x is NULL; 0 if x is zero; 1 otherwise
     if(x,a)
         NULL if x is NULL; a if x is non-zero; 0 otherwise
     if(x,a,b)
         NULL if x is NULL; a if x is non-zero; b otherwise
     if(x,n,z,p)
         NULL if x is NULL; n if x is negative;
   z if x is zero; p if x is positive

     Exceptions: The (new) function isnull(x) returns: 1 if x is NULL;
                 0 otherwise. The (new) function null()
                 (which has no arguments) returns an integer NULL.

     Non-NULL, but invalid, arguments to functions should result in NULL.

     Examples:
     log(-2)
     pow(a,b) where a is negative and b is not an integer

     Exception: sqrt(-n) returns -sqrt(n)

--------------------