The main term when counting $d(n)$ in arithmetic progressions For $q,a\in \mathbb N$ write $d=(q,a)$.  Why might be
$$\sum _{h|q}\frac {c_h(a)\log (d/h)}{h}=-\frac {q'}{\phi (q')}\sum _{h|q}\frac {c_h(a)}{h}\sum _{h'|q'}\frac {\mu (h')\log h'}{h'}+\sum _{h|d}\frac {\phi (q/h)}{q/h}\log h?$$
I can see that this is true for $d=q$ or $d=1$ but in general, don't know.
My motivation for the question is quite long - if you really want it see below.  But my question as it stands is just that above.
Here $c_h(a)$ is Ramanujan's sum
\[ c_h(a)=\sum _{n=1\atop {(n,h)=1}}^he^{2\pi ina/h}=\frac {\mu (h/(h,a))\phi (h)}{\phi (h/(h,a))}.\]

By https://www.impan.pl/en/publishing-house/journals-and-series/acta-arithmetica/all/168/4/82214/the-divisor-function-on-residue-classes-i (stated in the abstract, no access necessary) we have
$$\sum _{n\leq X\atop {n\equiv a(q)}}d(n)\approx \frac {X}{q}\sum _{h|q}\frac {c_h(a)}{h}\Big (\log (X/h^2)+2\gamma -1\Big ).$$
On the other hand the main term is provided by a residue which I think should be, writing $d=(q,a)$ and $q'=q/d$,
$$\sum _{n\leq X\atop {n\equiv a(q)}}d(n)\approx \frac {1}{\phi (q')}Res\left \{ \frac {(X/d)^{s}}{s}\sum _{n=1\atop {(n,q')=1}}^\infty \frac {d(nd)}{n^s}\right \} =\frac {1}{\phi (q')}Res\left \{ \frac {X^{s}}{s}\sum _{n=1\atop {(n,q)=d}}^\infty \frac {d(n)}{n^s}\right \} =:\frac {\mathcal R}{\phi (q')}$$
and as a plausability check it's possible to compare with Theorem 1 of http://matwbn.icm.edu.pl/ksiazki/aa/aa47/aa4713.pdf (there it's stated for $d_3(n)$ but I'm hoping it should also be the case for $d(n)$).  So these two main terms should be equal, i.e. we should have
$$\frac {\mathcal R}{\phi (q')}=\frac {X}{q}\sum _{h|q}\frac {c_h(a)}{h}\Big (\log (X/h^2)+2\gamma -1\Big )\hspace {10mm}(1)$$
and my issue is I can't see this. Here's my reasoning: As
$$\sum _{n=1\atop {(n,q)=1}}^\infty \frac {1}{n^s}=\prod _{p\not |q}\left (1-p^{-s}\right )^{-1}=\zeta (s)\prod _{p|q}(1-p^{-s})=\zeta (s)\sum _{h|q}\frac {\mu (h)}{h^s}$$
the series in $\mathcal R$ is
$$\sum _{n,m=1\atop {(nm,q)=d}}^\infty \frac {1}{(nm)^s}=\sum _{\delta |d}\sum _{n,m=1\atop {d|nm\atop {(nm,q)=d\atop {(n,d)=\delta }}}}^\infty \frac {1}{(nm)^s}=\sum _{\delta |d}\frac {1}{\delta ^s}\sum _{n,m=1\atop {d/\delta |m\atop {(n\delta m,q)=d\atop {(n,d/\delta )=1}}}}^\infty \frac {1}{(nm)^s}=\frac {1}{d^s}\sum _{\delta |d}\sum _{n,m=1\atop {(ndm,q)=d\atop {(n,d/\delta )=1}}}^\infty \frac {1}{(nm)^s}=\frac {1}{d^s}\left (\sum _{m=1\atop {(m,q')=1}}^\infty \frac {1}{m^s}\right )\left (\sum _{\delta |d}\sum _{n=1\atop {(n,q/\delta )=1}}^\infty \frac {1}{n^s}\right )=\frac {\zeta (s)^2}{d^s}\left (\sum _{h|q'}\frac {\mu (h)}{h^s}\right )\left (\sum _{\delta |d\atop {h|q/\delta }}\frac {\mu (h)}{h^s}\right )=:\frac {\zeta (s)^2}{d^s}\Delta (s)$$
so
\[ \mathcal R=Res_{s=1}\Bigg \{ \frac {(X/d)^{s}}{s}\zeta (s)^2\Delta (s)\Bigg \} =:Res\Bigg \{ \lambda (s)\zeta (s)^2\Delta (s)\Bigg \} .\]
And here I get stuck - I don't see how the $\log $'s in the residue can reduce to something so simple as in (1).  Below is calculating the residue explicitly.

Here's my work in detail which gets it to the point above I'm stuck at.  We have
\begin{align}
\mathcal R&=\lim _{s\rightarrow 1}\frac {d}{ds}\lambda (s)(s-1)^2\zeta (s)^2\Delta (s)
\\ &=\lim _{s\rightarrow 1}\left (\Delta (s)\frac {d}{ds}\lambda (s)(s-1)^2\zeta (s)^2+\lambda (s)(s-1)^2\zeta (s)^2\frac {d}{ds}\Delta (s)\right )
\\ &=\Delta (1)\lim _{s\rightarrow 1}\left (\lambda (s)\frac {d}{ds}(s-1)^2\zeta (s)^2+(s-1)^2\zeta (s)^2\frac {d}{ds}\lambda (s)\right )+\lambda (1)\Delta '(1)
\\ &=\Delta (1)\Big (2\gamma \lambda (1)+\lambda '(1)\Big )+\lambda (1)\Delta '(1)
\\ &=\frac {X}{d}\Bigg (\left (\log (X/d)+2\gamma -1\right )\Delta (1)+\Delta '(1)\Bigg ).
\end{align}
Then
\[ \sum _{\delta h|q\atop {\delta |d}}\frac {\mu (h)}{h}=\sum _{\delta |h|q\atop {\delta |d}}\frac {\mu (h/\delta )}{h/\delta }=\sum _{h|q}\frac {1}{h}\sum _{\delta |h,d}\mu (h/\delta )\delta =\sum _{h|q}\frac {c_h(a)}{h}\]
and similarly
\[ \sum _{\delta h|q\atop {\delta |d}}\frac {\mu (h)\log h}{h}=\sum _{h|q}\frac {1}{h}\sum _{\delta |h,d}\mu (h/\delta )\delta \log (h/\delta )=\sum _{h|q}\frac {c_h(a)\log h}{h}-\sum _{\delta |d\atop {\delta h|q}}\frac {\mu (h)\log \delta }{h}=\sum _{h|q}\frac {c_h(a)\log h}{h}-\sum _{\delta |d}\frac {\phi (q/\delta )}{q/\delta }\log \delta \]
so
\[ \Delta (1)=\frac {\phi (q')}{q'}\sum _{\delta h|q\atop {\delta |d}}\frac {\mu (h)}{h}=\frac {\phi (q')}{q'}\sum _{h|q}\frac {c_h(a)}{h}\]
and
\begin{align}
\Delta '(1)&=\frac {d}{ds}\vline _{s=1}\left (\sum _{\delta |d\atop {h|q/\delta \atop {h'|q'}}}\frac {\mu (h)\mu (h')}{(hh')^s}\right )
\\ &=-\left (\sum _{\delta |d\atop {h|q/\delta }}\frac {\mu (h)\log h}{h}\right )\left (\sum _{h'|q'}\frac {\mu (h')}{h'}\right )-\left (\sum _{\delta |d\atop {h|q/\delta }}\frac {\mu (h)}{h}\right )\left (\sum _{h'|q'}\frac {\mu (h')\log h'}{h'}\right )
\\ &=-\frac {\phi (q')}{q'}\left (\sum _{h|q}\frac {c_h(a)\log h}{h}-\sum _{\delta |d}\frac {\phi (q/\delta )}{q/\delta }\log \delta \right )-\left (\sum _{h|q}\frac {c_h(a)}{h}\right )\left (\sum _{h'|q'}\frac {\mu (h')\log h'}{h'}\right )
\end{align}
so
\begin{align}
\mathcal R=\frac {X}{d}\sum _{h|q}\frac {c_h(a)}{h}\left (\left (\log (X/d)+2\gamma -1\right )\frac {\phi (q')}{q'}-\frac {\phi (q')\log h}{q'}-\sum _{h'|q'}\frac {\mu (h')\log h'}{h'}\right )
\\ +\frac {X\phi (q')}{dq'}\sum _{\delta |d}\frac {\phi (q/\delta )}{q/\delta }\log \delta 
\end{align}
so, comparing with (1) we should have
\begin{align}
\sum _{h|q}\frac {c_h(a)}{h}\Big (\log (X/h^2)+2\gamma -1\Big )&=\sum _{h|q}\frac {c_h(a)}{h}
\\ &\left (\left (\log (X/d)+2\gamma -1\right )-\log h-\frac {q'}{\phi (q')}\left (\sum _{h'|q'}\frac {\mu (h')\log h'}{h'}\right )\right )
\\ &+\sum _{\delta |d}\frac {\phi (q/\delta )}{q/\delta }\log \delta 
\end{align}
and therefore $$\sum _{h|q}\frac {c_h(a)\log (d/h)}{h}=-\frac {q'}{\phi (q')}\sum _{h|q}\frac {c_h(a)}{h}\sum _{h'|q'}\frac {\mu (h')\log h'}{h'}+\sum _{\delta |d}\frac {\phi (q/\delta )}{q/\delta }\log \delta $$
and that's the identity I want to establish at the start of this post.
 A: I feel I'm answering my own questions too much here but I guess I should post the answer if I posted the question and found the answer in the meantime.
Write $\Delta (n)=\phi (n)/n$ and write $\Delta =1-1/p$.  Let $$R(q,d)=\sum _{h|q}\frac {c_h(d)}{h}\hspace {6mm}\text { and }\hspace {6mm}S(q,d)=\sum _{h|d}\Delta (q'h).$$
Then $$\sum _{h|q}\frac {c_h(d)\log h}{h}=R(q,d)\left (-\sum _{p|q'}\frac {\log p}{p\Delta }+\sum _{p^\alpha |d\atop {p|q'}}\frac {\log p^\alpha }{1+D}+\sum _{p^\alpha |d\atop {p\nmid q'}}\frac {\log p^\alpha \Delta }{1+\Delta D}\right )$$
and $$\sum _{h|d}\Delta (q'h)\log h=R(q,d)\left (\sum _{p^\alpha |d,q'}\frac {\log p^\alpha }{1+D}+\sum _{p^\alpha |d\atop {p\nmid q'}}\frac {\log p^\alpha \Delta }{1+\Delta D}\right ).$$
The claim in my question then follows from these claims and this question How to prove $\sum_{d\mid q}\frac{\mu(d)\log d}{d}=-\frac{\phi(q)}{q}\sum_{p\mid q}\frac{\log p}{p-1}$?
Proof
The first sum in question is
\begin{align}
\sum _{p^\alpha |q}\frac {\log p^\alpha c_{p^\alpha }(d)}{p^\alpha }\sum _{h|q\atop {p\nmid h}}\frac {c_h(d)}{h}=:\sum _{p^\alpha |q}\frac {\log p^\alpha c_{p^\alpha }(d)}{p^\alpha }R_p(q,d).
\end{align}
Then
\begin{align}
R_p(q,d)&=&\prod _{P|q\atop {P\nmid d\atop {P\not =p}}}\sum _{h|q}\frac {c_h(d)}{h}\prod _{P|d,q'\atop {P\not =p}}\sum _{h|q}\frac {c_h(d)}{h}\prod _{P|d\atop {P\nmid q'\atop {P\not =p}}}\sum _{h|q}\frac {c_h(d)}{h}
\\ &=&\prod _{P|q\atop {P\nmid d\atop {P\not =p}}}\Delta \prod _{P|d,q'\atop {P\not =p}}\left (\Delta (1+D)\right )\prod _{P|d\atop {P\nmid q'\atop {P\not =p}}}\left (1+\Delta D\right )
\\ &=&R(q,d)\underbrace {\Delta ^{-1}}_{p|q'}\underbrace {(1+D)^{-1}}_{p|d,q'}\underbrace {(1+\Delta D)^{-1}}_{p|d\atop {p\not |q'}}
\end{align}
so our sum has the correct factor and the rest becomes
\begin{align}
&&\sum _{p^\alpha |q}\frac {\log p^\alpha c_{p^\alpha }(d)}{p^\alpha }\underbrace {\Delta ^{-1}}_{p|q'}\underbrace {(1+D)^{-1}}_{p|d,q'}\underbrace {(1+\Delta D)^{-1}}_{p|d\atop {p\not |q'}}
\\ &&\hspace {10mm}=\hspace {4mm}\sum _{p^\alpha |q\atop {p\not |d}}\frac {\log p^\alpha \mu (p^\alpha )}{p^\alpha \Delta }+\sum _{p^\alpha |q\atop {p|d,q'}}\frac {\log p^\alpha c_{p^\alpha }(d)}{p^\alpha \Delta (1+D)}+\sum _{p^\alpha |q\atop {p|d\atop {p\not |q'}}}\frac {\log p^\alpha c_{p^\alpha }(d)}{p^\alpha (1+\Delta D)}
\\ &&\hspace {10mm}=\hspace {4mm}-\sum _{p|q\atop {p\not |d}}\frac {\log p}{p\Delta }+\sum _{p^\alpha |d\atop {p|d,q'}}\frac {\log p^\alpha }{1+D}-\sum _{p|d,q'}\frac {\log p^{D+1}}{p\Delta (1+D)}+\sum _{p^\alpha |d\atop {p\not |q'}}\frac {\log p^\alpha \Delta }{1+\Delta D}
\\ &&\hspace {10mm}=\hspace {4mm}-\sum _{p|q'}\frac {\log p}{p\Delta }+\sum _{p^\alpha |d\atop {p|q'}}\frac {\log p^\alpha }{1+D}+\sum _{p^\alpha |d\atop {p\nmid q'}}\frac {\log p^\alpha \Delta }{1+\Delta D}
\end{align}
The second sum in question is
\begin{align}
\sum _{p^\alpha |d}\log p^\alpha \sum _{h|d\atop {p\nmid h}}\Delta (q'hp^\alpha )=:\sum _{p^\alpha |d}\log p^\alpha S_p(q,d).
\end{align}
Then
\begin{align}
S_p(q,d)&=&\prod _{P|q\atop {P\nmid d\atop {P\not =p}}}\Delta (q')\prod _{P|d,q'\atop {P\not =p}}\sum _{h|d}\Delta (q'h)\prod _{P|d\atop {P\nmid q'\atop {P\not =p}}}\sum _{h|d}\Delta (h)\cdot \Delta (q'P)
\\ &=&R(q,d)\underbrace {\Delta ^{-1}}_{p|q'}\underbrace {(1+D)^{-1}}_{p|d,q'}\underbrace {(1+\Delta D)^{-1}}_{p|d\atop {p\nmid q'}}\cdot \Delta 
\end{align}
so our sum has the right factor and the rest is
\begin{align}
&&\sum _{p^\alpha |d}\log p^\alpha \underbrace {\Delta ^{-1}}_{p|q'}\underbrace {(1+D)^{-1}}_{p|d,q'}\underbrace {(1+\Delta D)^{-1}}_{p|d\atop {p\nmid q'}}\cdot \Delta 
\\ &&\hspace {10mm}=\hspace {4mm}\sum _{p^\alpha |d,q'}\frac {\log p^\alpha }{1+D}+\sum _{p^\alpha |d\atop {p\nmid q'}}\frac {\log p^\alpha \Delta }{1+\Delta D}
\end{align}
