I may be off base with what you want, but it seems like may you want to use GRASS r.series, namely…<div><br><div>base=$(basename $input .grib2)</div><div>•</div><div>•</div><div>•</div><div>r.series input="`g.mlist rast pattern=${base} .* sep=,`" output=${base".total method=sum --overwrite</div>
<div><br></div><div><br></div><div>So, if you have a bunch of *.grib2 files, for instance, that have been read into GRASS that you want to sum; these GRASS raster maps all have the same base filename root, $base.*</div></div>
<div><br></div><div>From the GRASS manual:</div><div><br></div><div><em>r.series</em> makes each output cell value a function of the values assigned to the corresponding cells in the input raster map layers. Following methods are available:<li>
average: average value</li><li>count: count of non-NULL cells</li><li>median: median value</li><li>mode: most frequently occuring value</li><li>minimum: lowest value</li><li>maximum: highest value</li><li>range: range of values (max - min)</li>
<li>stddev: standard deviation</li><li>sum: sum of values</li><li>variance: statistical variance</li><li>diversity: number of different values</li><li>slope: linear regression slope</li><li>offset: linear regression offset</li>
<li>detcoeff: linear regression coefficient of determination</li><li>min_raster: raster map number with the minimum time-series value</li><li><span style="color:rgb(0,0,0);font-family:arial,sans-serif">max_raster: raster map number with the maximum time-series value</span></li>
</div><div><br></div><div><br></div><div>I hope this helps…</div><div><br></div><div>Tom</div><div><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Dec 28, 2012 at 7:36 AM, S. Koukoulas (lists) <span dir="ltr"><<a href="mailto:sotkouklistes@gmail.com" target="_blank">sotkouklistes@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">To my experience with r.mapcalc you cannot do what yo do with<br>
programming languages... e.g. i=i+1 will not work directly. Instead you<br>
could try a more indirect way, something like (here is an example with<br>
bash shell script):<br>
<br>
#g.mremove rast=out* (to remove previous stuff, if needed)<br>
r.mapcalc sun=global1<br>
for a in ‘seq 2 10‘; do<br>
r.mapcalc out = sun + global$a<br>
g.rename --o rast=out,sun<br>
done<br>
<br>
basically the main idea is to use an intermediate map and then before<br>
the loop ends, pass it (with g.rename) to the map variable you want.<br>
There might be a more elegant way with python, but hopefully this shell<br>
script example might help you.<br>
HTH,<br>
sotiris<br>
<div class="im"><br>
<br>
On 12/28/2012 01:34 PM, Paul Meems wrote:<br>
> Thanks again Daniel for your help.<br>
><br>
> When I do the calculation in the GUI instead of in the Python shell I<br>
> get these results:<br>
> r.mapcalc sun = global1@temp+ global2@temp<br>
> (Fri Dec 28 12:26:10 2012) Command finished (1 sec)<br>
> r.mapcalc sun = sun@temp+ global3@temp<br>
> ERROR: Unable to close raster map<br>
><br>
> When I Google on the error message I find this post from 2010<br>
> <a href="http://lists.osgeo.org/pipermail/grass-user/2010-October/058463.html" target="_blank">http://lists.osgeo.org/pipermail/grass-user/2010-October/058463.html</a><br>
> saying:<br>
</div>> >/ Why am I obtaining this error? and How can I avoid it?<br>
<div class="im">> /<br>
> Because you're using the same map as both input and output, which<br>
> won't work. Either use a temporary name for the intermediate map, or<br>
> replace the second grass.mapcalc call with r.null.<br>
> I'll have a look in using a temp file. This will make my script more<br>
> complicated ;)<br>
><br>
> Thanks,<br>
><br>
> Paul<br>
><br>
</div>> *Paul Meems *<br>
<div class="im">> Release manager, configuration manager<br>
> and forum moderator of MapWindow GIS.<br>
</div>> <a href="http://www.mapwindow.org" target="_blank">www.mapwindow.org</a> <<a href="http://www.mapwindow.org/" target="_blank">http://www.mapwindow.org/</a>><br>
<div class="im">><br>
> Owner of MapWindow.nl - Support for<br>
> Dutch speaking users.<br>
</div>> <a href="http://www.mapwindow.nl" target="_blank">www.mapwindow.nl</a> <<a href="http://www.mapwindow.nl/" target="_blank">http://www.mapwindow.nl/</a>><br>
><br>
> *<br>
> *<br>
><br>
><br>
><br>
> 2012/12/28 Daniel Lee <<a href="mailto:lee@isi-solutions.org">lee@isi-solutions.org</a><br>
> <mailto:<a href="mailto:lee@isi-solutions.org">lee@isi-solutions.org</a>>><br>
<div class="HOEnZb"><div class="h5">><br>
> Hi there,<br>
><br>
> I'm now trying to use r.sun and r.mapcalc with Python to<br>
> create my annual solar radiation map.<br>
> I'm using the Python shell inside GRASS.<br>
><br>
> For testing purposes I start with 9 days.<br>
> The r.sun part seems to be working:<br>
> for x in range(1, 10, 1):<br>
> print "Working on day %d" % (x)<br>
> glob_rad = 'global' + str(x)<br>
> grass.run_command('r.sun', flags = 's', elevin =<br>
> 'w001001@temp', aspin = 'aspect@temp', slopein = 'slope@temp',<br>
> glob_rad = glob_rad, day = x)<br>
> I do have a question about parsing the --overwrite flag. How<br>
> to do that. Adding flags='s, --overwrite' or flags='s,<br>
> -overwrite' gives a compile error.<br>
><br>
><br>
> I believe you overwrite by setting:<br>
> flags="s, o"<br>
><br>
><br>
> Now I try to combine the results of r.sun into 1 raster.<br>
> This is working:<br>
> for x in range(1, 10, 1):<br>
> if x == 2:<br>
> exp = 'sun = global1@temp + global2@temp'<br>
> print exp<br>
> grass.mapcalc(exp, quiet=False, verbose=False, overwrite=True)<br>
><br>
> This is also working:<br>
> for x in range(1, 10, 1):<br>
> if x == 2:<br>
> exp = 'sun = global1@temp + global2@temp'<br>
> print exp<br>
> grass.mapcalc(exp, quiet=False, verbose=False, overwrite=True)<br>
> elif x > 2:<br>
> exp = 'sun = sun@temp + global' + str(x) + '@temp'<br>
> print exp<br>
> and produces this output:<br>
> sun = sun@temp + global3@temp<br>
> sun = sun@temp + global4@temp<br>
> sun = sun@temp + global5@temp<br>
> sun = sun@temp + global6@temp<br>
> sun = sun@temp + global7@temp<br>
> sun = sun@temp + global8@temp<br>
> sun = sun@temp + global9@temp<br>
> But when I execute this using<br>
> grass.mapcalc(exp, quiet=False, verbose=False, overwrite=True)<br>
> Grass crashes completely.<br>
><br>
> Has this something to do with having the output file is the<br>
> input file as well?<br>
> If this is not allowed how can I combine all output files from<br>
> r.sun into 1 raster?<br>
><br>
><br>
> GRASS doesn't care what you're combining, when you use the map<br>
> calculator it just sees numbers so that shouldn't be a problem.<br>
> I don't know why the map calculator to overwrite the input map<br>
> would be a problem either. I'm not familiar with your syntax<br>
> though. What happens when you just enter the expression on the<br>
> "normal" command line?<br>
><br>
> r.mapcalc "sun = sun + global3"<br>
><br>
> That should work.<br>
><br>
> Best,<br>
> Daniel<br>
><br>
><br>
><br>
><br>
</div></div><div class="HOEnZb"><div class="h5">> _______________________________________________<br>
> grass-user mailing list<br>
> <a href="mailto:grass-user@lists.osgeo.org">grass-user@lists.osgeo.org</a><br>
> <a href="http://lists.osgeo.org/mailman/listinfo/grass-user" target="_blank">http://lists.osgeo.org/mailman/listinfo/grass-user</a><br>
<br>
_______________________________________________<br>
grass-user mailing list<br>
<a href="mailto:grass-user@lists.osgeo.org">grass-user@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/grass-user" target="_blank">http://lists.osgeo.org/mailman/listinfo/grass-user</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><pre>Thomas E Adams</pre><pre>Development & Operations Hydrologist
National Weather Service
Ohio River Forecast Center
1901 South State Route 134
Wilmington, OH 45177</pre><pre><a href="http://www.erh.noaa.gov/er/ohrfc/" target="_blank">http://www.erh.noaa.gov/er/ohrfc/</a></pre><pre>EMAIL: <a href="mailto:thomas.adams@noaa.gov" target="_blank">thomas.adams@noaa.gov</a><font size="4">
</font>
VOICE: 937-383-0528
FAX: 937-383-0033
<br></pre>
</div>