[GRASSLIST:2762] Re: Reclassify with r.stats and awk - solution

To get a list such as below, you can use:
r.stats -l in=rt.rmris.ct.r | awk '{printf "%d=%s\n", $1, $2}'
r.stats: 100%
19564=TSF
19578=TSF
19642=GRA
19657=TSF
19680=GRA
19719=TSF
19736=TSF
19738=TAA
...

But if you want to reclassify your raster map, you need something like:
r.stats -l in=rt.rmris.ct.r | cut -d" " -f2 | sort | uniq | awk '{print $1,NR}' > tmp

cat tmp
GRA 1
NRK 2
SHR 3
SWI 4
TAA 5
TLP 6
TSF 7
WLK 8
WRE 9
no 10

r.stats -l in=rt.rmris.ct.r | awk 'BEGIN{while((getline line < "tmp") > 0){split(line,A," ");R[A[1]]=A[2] }}{print $1,"=",R[$2],$2}'
r.stats: 100%
19564 = 7 TSF
19578 = 7 TSF
19642 = 1 GRA
19657 = 7 TSF
19680 = 1 GRA
19719 = 7 TSF
19736 = 7 TSF
19738 = 5 TAA
...

The entire command for the reclassification would then be:
r.stats -l in=rt.rmris.ct.r | awk 'BEGIN{while((getline line < "tmp") > 0){split(line,A," ");R[A[1]]=A[2] }}{print $1,"=",R[$2],$2}' | r.reclass in=rt.rmris.ct.r out=rt.rmris.ct.tmp.r

Thanks to P. from Soil & Water Lab.!

Christof

From: Christof Bigler <christof.bigler@colorado.edu>
Date: 25. Februar 2004 19:19:13 GMT-07:00
To: Grasslist <GRASSLIST@baylor.edu>
Subject: [GRASSLIST:2754] Reclassify with r.stats and awk

I tried to reclassify a grid map. However, instead of getting the category labels, I get zeros. You see the problem here:

GRASS 5.0.2 > r.stats -l in=rt.rmris.ct.r
r.stats: 100%
19564 TSF
19578 TSF
19642 GRA
19657 TSF
19680 GRA
19719 TSF
19736 TSF
19738 TAA
...

That's what I expect to get.

GRASS 5.0.2 > r.stats -l in=rt.rmris.ct.r | awk '{printf "%d=%d\n", $1, $2}'
r.stats: 100%
19564=0
19578=0
19642=0
19657=0
19680=0
19719=0
19736=0
19738=0
...

Why is the second column replaced by zeros? Do I need first to replace the category labels with e.g. integer values?