[GRASS-dev] Parsing output of r.category which includes labels

Moritz Lennert mlennert at club.worldonline.be
Thu Aug 23 04:02:03 PDT 2018

On 23/08/18 11:52, Nikos Alexandris wrote:
> * Stefan Blumentrath <Stefan.Blumentrath at nina.no> [2018-08-23 07:23:12 +0000]:
>> Dear Nikos,
>> Can you give us a bit more context?  What is it you want to achieve?
>> How are you using r.stats and what is it you want to do with the
>> output?
>> Personally, I am not too familiar with performance implications of
>> NumPy vs. plain Python, but rather use NumPy for convenience in
>> matrix/table operations (avoiding pandas)...
> ```
> for category in categories:
>      statistics_filename = prefix + '_' + category
>      r.stats(input=(base,reclassified),
>              output=statistics_filename,
>              flags='ncapl',
>              separator=',',
>              quiet=True)
> ```
> Instead, I want to (modify the above so as to) collect/direct all
> iterations in one output file.

You can check the r.neighborhoodmatrix addon for one solution which I 
shamelessly took from a SE answer:


The code takes a list of filenames and the merges these files.

I've been confronted with a similar problem using v.db.select these days 
and I've been thinking about adding a flag / parameter to relevant 
modules allowing to append an existing file, instead of overwriting it. 
Should just be a case of using mode "a" instead of "w", so shouldn't be 
too complicated.

If you want to, try it with r.stats, by applying this change:

Index: raster/r.stats/main.c
--- raster/r.stats/main.c	(révision 72717)
+++ raster/r.stats/main.c	(copie de travail)
@@ -223,7 +223,7 @@

      name = option.output->answer;
      if (name != NULL && strcmp(name, "-") != 0) {
-	if (NULL == freopen(name, "w", stdout)) {
+	if (NULL == freopen(name, "a", stdout)) {
  	    G_fatal_error(_("Unable to open file <%s> for writing"), name);

and report back if it works as expected...


More information about the grass-dev mailing list