[GRASS-user] Re: GRASS script to export raster

Glynn Clements glynn at gclements.plus.com
Wed Mar 9 10:17:19 EST 2011

Martin Landa wrote:

> > so just flipping all the rows in the file you can get now?
> >
> > don't know how to do that in python, but if unix shell powertools are
> > available:
> >   r.out.ascii | tac
> print os.linesep.join(reversed(grass.read_command('r.out.ascii',

FWIW, both of these are sub-optimal.

The Python approach reads everything into memory then generates a
reversed copy, so you need enough memory to hold two copies of the
output from r.out.ascii.

When given a file, tac reads the input file backwards a block at a
time, so it only needs enough memory to hold a complete line, rounded
up to a whole number of blocks.

However, it can't do this when reading from a pipe; it has to store
everything in memory.

If you want to reverse a large file without storing everything in
memory, the simplest solution is e.g.:

	r.out.ascii ... > tmp.txt
	tac tmp.txt > reversed.txt
	rm tmp.txt

If this was likely to be a common operation, r.out.ascii could easily
be modified to flip the row number passed to Rast_get_row(). However,
while that will be optimal for native GRASS rasters, it may be
sub-optimal (or even fail) for "linked" (r.external) rasters; many
raster formats have to be read top-to-bottom (I don't know how GDAL
handles out-of-order reads for such formats).

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-user mailing list