\[ \def\d{d} \def\D{D} \def\p{p} \def\t{t} \]
How to convert flow direction between different encodings in GRASS GIS
1 degree to 45degree
\begin{equation} \D=\d/45 \end{equation}
r.mapcalc ex="45degree=int(degree/45)"
2 45degree to degree
\begin{equation} \d=45|\D| \end{equation}
r.mapcalc ex="degree=abs(45degree)*45"
3 degree to power2
\begin{equation} \p=2^{8-\D/45} \end{equation}
r.mapcalc ex="power2=2^(8-degree/45)"
4 power2 to degree
\begin{equation} \d=45(8-\log_2{\p}) \end{equation}
r.mapcalc ex="degree=45*int(8-log(power2,2))"
5 degree to taudem
\begin{equation} \t=1+\left(\d/45\bmod 8\right) \end{equation}
r.mapcalc ex="taudem=1+(degree/45)%8"
6 taudem to degree
\begin{equation} \d= 45\times\begin{cases} \t-1&\text{if }\t-1>0\\ 8&\text{otherwise} \end{cases} \end{equation}
r.mapcalc ex="degree=45*if(taudem-1,taudem-1,8)"
7 45degree to power2
\begin{equation} \p=2^{8-|\D|} \end{equation}
r.mapcalc ex="power2=2^(8-abs(45degree))"
8 power2 to 45degree
\begin{equation} \D=8-\log_2{\p} \end{equation}
r.mapcalc ex="45degree=int(8-log(power2,2))"
9 45degree to taudem
\begin{equation} \t=1+\left(|\D|\bmod 8\right) \end{equation}
r.mapcalc ex="taudem=1+abs(45degree)%8"
10 taudem to 45degree
\begin{equation} \D= \begin{cases} \t-1&\text{if }\t-1>0\\ 8&\text{otherwise} \end{cases} \end{equation}
r.mapcalc ex="45degree=if(taudem-1,taudem-1,8)"
11 power2 to taudem
\begin{equation} \t=1+\left[\left(8-\log_2{\p}\right)\bmod 8\right] \end{equation}
r.mapcalc ex="taudem=1+(8-log(power2,2))%8"
12 taudem to power2
\begin{equation} \p=2^{(9-\t)\bmod 8} \end{equation}
r.mapcalc ex="power2=2^((9-taudem)%8)"