[GRASS-user] GRASS Scripting questions

Greetings all

I'm doing a few Script files for GRASS but I'm having a few difficulties in a few issues. My first question is about how to peform raster algebra using the ouput as an input. Example:
I'm trying to create a file which is the sum of a list of input files
Input is defined as:
#% key: input
#% type: string
#% required : yes
#% multiple : yes

And output is defined as
#% key: counter1
#% type: string
#% required : yes

itinialization of counter1
r.mapcalc $GIS_OPT_COUNTER1= "float (0)"

The cycle:
IFS=,
for opt in $GIS_OPT_INPUT; do
    r.mapcalc $GIS_OPT_SOMA= "$opt+$GIS_OPT_SOMA)"
done

And the command output is:
ERROR: Unable to close raster map
(for each $opt file, I get this error)
This means that I'm unable to use the output file as an input. Do anyone has a suggestion of how to do this operation without getting this error?

Thanks for your help

Best regards,
Antonio Rocha

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4695 (20091217) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Hi Antonio,

itinialization of counter1
r.mapcalc $GIS_OPT_COUNTER1= "float (0)"

shouldn't be counter1=
GIS_OPT_SOMA

The cycle:
IFS=,
for opt in $GIS_OPT_INPUT; do
   r.mapcalc $GIS_OPT_SOMA= "$opt+$GIS_OPT_SOMA)"
done |

                                                  |
is there a ')' too much? ------------------------|

?

Achim

Hi Antonio,

This means that I’m unable to use the output file as an input. Do
anyone has a suggestion of how to do this operation without getting this
error?

One solution that would work is to create intermediate map files. Use a common prefix for their creation, so later in the script you can g.mremove them.

Pablo.


Windows 7: agora com recursos que economizam bateria. Clique para conhecer.

Greetings Achim
You are absolutely right. I made a mistake copying the expression.

Here it goes:
#itinialization of counter1
r.mapcalc $GIS_OPT_COUNTER1= "float (0)"

The cycle:
IFS=,
for opt in $GIS_OPT_INPUT; do
   r.mapcalc $GIS_OPT_COUNTER= "$opt+$GIS_OPT_COUNTER"
done

so the question still remains :slight_smile:

Sorry and thanks for your reply.

Antonio

Achim Kisseler wrote:

Hi Antonio,

itinialization of counter1
r.mapcalc $GIS_OPT_COUNTER1= "float (0)"

shouldn't be counter1=
GIS_OPT_SOMA

The cycle:
IFS=,
for opt in $GIS_OPT_INPUT; do
   r.mapcalc $GIS_OPT_SOMA= "$opt+$GIS_OPT_SOMA)"
done |

                                                 |
is there a ')' too much? ------------------------|

?

Achim

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4695 (20091217) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4696 (20091217) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

I Think I have managed to use TMP files but I’m getting a few errors that I can’t understand. I’m using multiple GIS_OPT_INPUT and I want to add them to GIS_OPT_COUNTER

r.mapcalc $GIS_OPT_COUNTER= “float (0)”

IFS=,
for opt in $GIS_OPT_INPUT; do
TMP1=g.tempfile pid=$$
echo $TMP1
r.mapcalc $TMP1= “$GIS_OPT_COUNTER+$opt”
r.mapcalc $GIS_OPT_COUNTER= “$TMP1”
rm -f $TMP1
done

And I’m getting the following errors:
syntax error, unexpected ‘:’, expecting ‘=’
Parse error
Invalid map
Parse error
C:/Data/GISDataBase/North-Carolina/landsat/.tmp/3024.0
C:/Data/GISDataBase/North-Carolina/landsat/.tmp/3024.0
syntax error, unexpected ‘:’, expecting ‘=’
Parse error
Invalid map
Parse error

As anyone has an idea of what am i doing wrong?

Thanks
Best regards,
Antonio

António Rocha wrote:

Greetings Achim
You are absolutely right. I made a mistake copying the expression.

Here it goes:
#itinialization of counter1
r.mapcalc $GIS_OPT_COUNTER1= “float (0)”

The cycle:
IFS=,
for opt in $GIS_OPT_INPUT; do
r.mapcalc $GIS_OPT_COUNTER= “$opt+$GIS_OPT_COUNTER”
done
so the question still remains :slight_smile:

Sorry and thanks for your reply.

Antonio

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4696 (20091217) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

António Rocha wrote:

Greetings Achim
You are absolutely right. I made a mistake copying the expression.

Here it goes:
#itinialization of counter1
r.mapcalc $GIS_OPT_COUNTER1= "float (0)"

The cycle:
IFS=,
for opt in $GIS_OPT_INPUT; do
   r.mapcalc $GIS_OPT_COUNTER= "$opt+$GIS_OPT_COUNTER"
done

Don't use the same map as both input and output. Instead:

  tmp=tmp.myscript.$$
  for opt in $GIS_OPT_INPUT; do
    r.mapcalc "$tmp = $opt + $GIS_OPT_COUNTER"
    g.rename --o rast=$tmp,$GIS_OPT_COUNTER
  done

Although for this specific task (adding a list of maps), use
"r.series ... method=sum".

--
Glynn Clements <glynn@gclements.plus.com>

Hey

I have tried Glynn’s solution like this:

r.mapcalc “$GIS_OPT_COUNTER= float(0)”

tmp=tmp.myscript.$$

for opt in $GIS_OPT_INPUT; do
r.mapcalc “$tmp = $opt + $GIS_OPT_COUNTER”
g.rename rast=$tmp,$GIS_OPT_COUNTER
done

And I got
syntax error, unexpected ‘,’, expecting $end
Parse error
raster <tmp.myscript.724> not found

I changed myscript to r.ndviserie (myscript filename) and i got this:
syntax error, unexpected ‘,’, expecting $end
Parse error
raster <tmp.dwe.ndviserie.3692> not found

It seems that he is not recognizing tmp as a file where to store an image.
yesterday I found a reference of initializing TMP files with a “r_fillnulls_”. I try to find this function, or any reference to this, but nothing was found. What is this?

Thanks Glynn and everyone else

Antonio

Glynn Clements wrote:

António Rocha wrote:

  
Greetings Achim
You are absolutely right. I made a mistake copying the expression.

Here it goes:
#itinialization of counter1
r.mapcalc $GIS_OPT_COUNTER1= "float (0)"

The cycle:
IFS=,
for opt in $GIS_OPT_INPUT; do
   r.mapcalc $GIS_OPT_COUNTER= "$opt+$GIS_OPT_COUNTER"
done                      
    

Don't use the same map as both input and output. Instead:

	tmp=tmp.myscript.$$
	for opt in $GIS_OPT_INPUT; do
		r.mapcalc "$tmp = $opt + $GIS_OPT_COUNTER"
		g.rename --o rast=$tmp,$GIS_OPT_COUNTER
	done

Although for this specific task (adding a list of maps), use
"r.series ... method=sum".

  

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4698 (20091218) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

António Rocha wrote:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

Please don't post HTML-only mail to the list.

   Hey
   I have tried Glynn's solution like this:
   r.mapcalc "$GIS_OPT_COUNTER= float(0)"
tmp=tmp.myscript.$$

   for opt in $GIS_OPT_INPUT; do
       r.mapcalc "$tmp = $opt + $GIS_OPT_COUNTER"
       g.rename rast=$tmp,$GIS_OPT_COUNTER
   done
   And I got
   syntax error, unexpected ',', expecting $end

Right; I forgot that you need:

  IFS=,

as the map names in $GIS_OPT_INPUT will be separated by commas, not
spaces.

Without setting IFS, "$opt" will contain the list of input maps,
separated by commas, rather than individual maps.

--
Glynn Clements <glynn@gclements.plus.com>

After a few emails exchanged with Glynn I finally solved this:
*export GRASS_OVERWRITE=1

r.mapcalc "$GIS_OPT_CONTADOR= float(0)"

tmp=tmp.dwe.ndviserie.$$

IFS=,
for opt in $GIS_OPT_INPUT; do
    r.mapcalc "$tmp = $opt + $GIS_OPT_CONTADOR"
    g.rename rast=$tmp,$GIS_OPT_CONTADOR
done

r.info -r $GIS_OPT_CONTADOR*

To be noted:
- export is used in order to overwrite files
- I use a TMP that is defined by *tmp=tmp.dwe.ndviserie.$$
*- IFS is essential in order to define , as a separator symbol.

One question, Glynn: Where can I get more information about Bash script expression such as "export"?
Thanks

Antonio

António Rocha wrote:

Hey

I have tried Glynn's solution like this:

r.mapcalc "$GIS_OPT_COUNTER= float(0)"
tmp=tmp.myscript.$$
for opt in $GIS_OPT_INPUT; do
    r.mapcalc "$tmp = $opt + $GIS_OPT_COUNTER"
    g.rename rast=$tmp,$GIS_OPT_COUNTER
done

And I got
*/syntax error, unexpected ',', expecting $end
Parse error
raster <tmp.myscript.724> not found/*

I changed myscript to r.ndviserie/* */(myscript filename) and i got this:
/*syntax error, unexpected ',', expecting $end
Parse error
raster <tmp.dwe.ndviserie.3692> not found*/

It seems that he is not recognizing tmp as a file where to store an image.
yesterday I found a reference of initializing TMP files with a "r_fillnulls_". I try to find this function, or any reference to this, but nothing was found. What is this?

Thanks Glynn and everyone else

Antonio

Glynn Clements wrote:

António Rocha wrote:

Greetings Achim
You are absolutely right. I made a mistake copying the expression.

Here it goes:
#itinialization of counter1
r.mapcalc $GIS_OPT_COUNTER1= "float (0)"

The cycle:
IFS=,
for opt in $GIS_OPT_INPUT; do
   r.mapcalc $GIS_OPT_COUNTER= "$opt+$GIS_OPT_COUNTER"
done
    
Don't use the same map as both input and output. Instead:

  tmp=tmp.myscript.$$
  for opt in $GIS_OPT_INPUT; do
    r.mapcalc "$tmp = $opt + $GIS_OPT_COUNTER"
    g.rename --o rast=$tmp,$GIS_OPT_COUNTER
  done

Although for this specific task (adding a list of maps), use
"r.series ... method=sum".

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4698 (20091218) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4743 (20100104) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4743 (20100104) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

António Rocha wrote:

One question, Glynn: Where can I get more information about Bash script
expression such as "export"?

The bash manual page. On Unix: "man bash". Windows probably won't have
"man" installed, and probably not the manual pages either.

There's an online version at:

http://www.gnu.org/software/bash/manual/html_node/index.html

But this is for the latest version (4.0), while MSys uses an older
version (2.04).

Also, you can find the POSIX specification for the shell at:

http://www.opengroup.org/onlinepubs/9699919799/idx/shell.html

Most of what's described there should work with any Bourne shell, not
just bash.

--
Glynn Clements <glynn@gclements.plus.com>