[GRASSLIST:2915] Re: Puzzling mapcalc command

Glynn Clements glynn.clements at virgin.net
Thu Mar 11 21:14:29 EST 2004


SWlab wrote:

> I want to compute some maps with mapcalc (GRASS53 v2004/03.. on a SuSE box). 
> Depending on the way I parse the info to the command I have results or none
> 
> If I have a file "tmp" with
> Vold=V
> V=V+Down.0
> Down=1
> V=V-Down
> Out=2
> In=100000.*On.NN*Out[-1,0]
> 
> the command "cat tmp | r.mapcalc" gives me:
> parse error, unexpected '[', expecting $end
> Parse error
> 
> The commands entered one by one gives satisfactory results (in particular,
> r.mapcalc "In=100000.*On.NN*Out[-1,0]"
> or
> echo "In=100000.*On.NN*Out[-1,0]" | r.mapcalc
> both works.
> 
> I'd be very happy to get any insight :)

Actually, I'm slightly surprised that r.mapcalc actually made it to
the last line without reporting an error. In retrospect, I can see
why; however, r.mapcalc should probably be changed to prohibit certain
aspects of the above. In particular, it should probably prohibit using
an identifier on the LHS of an assignment after it has occurred
elsewhere.

Identifiers can be interpreted in two different ways: as either a map
name or as a variable. If an identifier has previously occurred on the
LHS of an assignment, it is interpreted as a variable, otherwise it is
interpreted as a map name. [Names enclosed within quotes are always
interpreted as map names.]

In your first example, the line "Out=2" defines "Out" as a variable. 
Consequently, the expression "Out[-1,0]" is invalid, as neighbourhood
modifiers can only be applied to maps, not variables. The same also
applies to colour and category lookups (i.e. @map, #map, etc).

OTOH, if you evaluate the statments individually, the identifier "Out"
in the expression "Out[-1,0]" will be interpreted as a map name, so
the neighbourhood modifier is valid.

An r.mapcalc "program" is essentially a list of assignments, each of
the form "<name> = <expression>". Assignment statements can occur
elsewhere, just like in C, and always define a variable that can be
accessed later in the program. However, those which occur at the top
level also create new maps.

Furthermore, any maps which are created by r.mapcalc don't actually
exist as maps until the program completes, so they cannot be accessed
as maps within the program.

-- 
Glynn Clements <glynn.clements at virgin.net>




More information about the grass-user mailing list