[GRASS-user] summation

the script for adding 120 rasters worked.
set i=1
set str="r.mapcalc map=ts.1"

while($i <= 120)
set str="$str + map.$i"
@ i++
end

There is a limit to the command line around 383 rasters, so if I want to add more rasters together than this I need to use a loop command. What I have so far is

set sum=0
set i=1
set str="r.mapcalc sum=temp.1"
foreach raster ( `g.mlist rast pattern="temp.*" )

Basically what I need is to have a script that will take create a running sum to add to the next raster file. Like (temp.1 + temp.2)=Sum.temp. This first sum needs to be added to temp.3. Then the loop needs to take action so I can continue to culminate the sums to be added to the next map layer.
Thanks for reaading,
Aaron

On 12.07.2007 22:51, goldneaa@onid.orst.edu wrote:

Basically what I need is to have a script that will take create a
running sum to add to the next raster file. Like (temp.1 +
temp.2)=Sum.temp. This first sum needs to be added to temp.3. Then the
loop needs to take action so I can continue to culminate the sums to be
added to the next map layer.
Thanks for reaading,
Aaron

Here is a (t)csh script which will copy up to 2000 maps in lots of 200

#!/usr/bin/tcsh

set tmp=2
set sstr="res=tmp.1"

while($tmp <= 11)
  @ t = $tmp - 1
  set str="tmp.$t=ts.1"
  set i=2
  while($i <= 200)
    set str="$str+ts.$i"
    @ i++
  end
  echo r.mapcalc $str
  r.mapcalc $str
  if($tmp <= 10) then
    set sstr="$sstr+tmp.$tmp"
  endif
  @ tmp++
end
echo r.mapcalc $sstr
r.mapcalc $sstr

I'll leave it up to the reader to make code to clean up the temp files :wink:

oh BTW if you know bash better you can always write bash scripts and
store them into files and use #!/bin/bash as the first line of the file.

--Wolf

--

<:3 )---- Wolf Bergenheim ----( 8:>

Hi.

On 13.07.2007 05:28, goldneaa@onid.orst.edu wrote:

Thank you very much you have helped me tremendously, I am very new to
scripting so just seeing the syntax helps a lot.

My pleasure, but beware of purists. They say you should not write
scripts in csh :wink: (http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/)

Using that script you sent me is there a way to pull in all 2000 or less
rasters at one time using that script or by using a loop script. Like
if I have (temp.1 through temp.1500) and wanted to sum all of them into
one map.

That is exactly what this script does. Hmm or should do. But there is a
bug. here is the correct version:

---------8<--------------------8<-------------
#!/usr/bin/tcsh

set tmp=2
set sstr="tmp.1"
set erase="tmp.1"
while($tmp <= 11)
  @ t = $tmp - 1
  set str="tmp.$t=ts.1"
  set i=2
  while($i <= 200)
    @ ti = $t * $i
    set str="$str+ts.$ti"
    @ i++
  end
  echo r.mapcalc $str
  r.mapcalc $str
  if($tmp <= 10) then
    set sstr="$sstr+tmp.$tmp"
    set erase="$erase,tmp.$tmp"
  endif
  @ tmp++
end
echo r.mapcalc res=$sstr
r.mapcalc res=$sstr
echo g.remove rast=$erase
g.remove rast=$erase
---------8<--------------------8<-------------

It sums up all the ts.1 through ts.2000 to res. Just change ts. to temp.
and change 200 to 150 and you will get it for temp.1 through temp.1500.
This version also cleans up the temp files it creates.

I don't know what you mean by one go, but you could also write it not to
use temporary files like this, but I suspect it will be much slower.

---------8<--------------------8<-------------
#!/usr/bin/tcsh

set tmp=2

echo r.mapcalc res=ts.1
r.mapcalc res=ts.1
while($tmp <= 2000)
  echo r.mapcalc res=res+ts.$tmp
  r.mapcalc res=res+ts.$tmp
  @ tmp++
end
---------8<--------------------8<-------------

--Wolf

--

<:3 )---- Wolf Bergenheim ----( 8:>

goldneaa wrote:

the script for adding 120 rasters worked.
set i=1
set str="r.mapcalc map=ts.1"

while($i <= 120)
set str="$str + map.$i"
@ i++
end

There is a limit to the command line around 383 rasters, so if I want
to add more rasters together than this I need to use a loop command.

You are probably hitting a maximum command line length of 4096 chars,
not a max open map/file limit. (?)

Basically what I need is to have a script that will take create a
running sum to add to the next raster file. Like (temp.1 +
temp.2)=Sum.temp. This first sum needs to be added to temp.3. Then
the loop needs to take action so I can continue to culminate the sums
to be added to the next map layer.

another idea is to add 1-100 together into sum1, 101-200 into sum2,
201-300 into sum3 etc, then finish with
  r.series method=sum in=sum1,sum2,sum3 out=sum_total

Hamish

Aaron is likely running into both problems. Mac OSX has a limit of 256 open files per process. I don't know how to change it but I'd imagine it involves a kernel compile.

Cheers,

Mike

On 15-Jul-07, at 5:27 PM, Hamish wrote:

goldneaa wrote:

There is a limit to the command line around 383 rasters, so if I want
to add more rasters together than this I need to use a loop command.

You are probably hitting a maximum command line length of 4096 chars,
not a max open map/file limit. (?)

On 13.07.2007 09:22, Wolf Bergenheim wrote:

That is exactly what this script does. Hmm or should do. But there is a
bug. here is the correct version:

Nope, still not. This works:

-----------8<-------------------------8<--------------
#!/usr/bin/tcsh

set tmp=2
set sstr="tmp.1"
set erase="tmp.1"
while($tmp <= 11)
  @ t = $tmp - 1
  set str="tmp.$t=temp.1"
  set i=2
  while($i <= 156)
    @ ti = $t * $i
    set str="$str+temp.$ti"
    @ i++
  end
  r.mapcalc $str
  if($tmp <= 10) then
    set sstr="$sstr+tmp.$tmp"
    set erase="$erase,tmp.$tmp"
  endif
  echo "Total progress: ${t}0%"
  @ tmp++
end
r.mapcalc sum=$sstr
echo "Cleaning temporary files..."
g.remove rast=$erase

echo ""
echo "All done. Result is called sum"
-----------8<-------------------------8<--------------

--Wolf

--

<:3 )---- Wolf Bergenheim ----( 8:>