subroutine mwave_diags ( &
 & klev,            &  ! in: dimensions   
 & ksat,            &  ! in: satellite ID (for mwave) 
 & yd_mwave_phys,   &  ! in: 2d/3d fields, FW
 & pdiag            )  ! inout: diagnostics array

!**** 4D-Var in clouds/precipitation for microwave radiances 

!     Diagnostics and observation error calculations used by all 
!     mwave_obsop_* routines

!     Also provides TL and adjoint calculations via optional variables

!     Modifications:
!     23-Sep-2008    Alan Geer  Initial version
!     04-Aug-2009    Alan Geer  New error formulation (eventually may mean 
!                               errors part of this function will be pointless)
!     07-Apr-2010    Alan Geer  Changed routine name and removed old error stuff

!**** MODULES      

!* KIND     
use parkind1, only: jpim, jprb
use yomhook , only: lhook, dr_hook

!* IFS
use yommwave, only: mwave_phys_type
use parmwave, only: imwave_rain_sfc, imwave_snow_sfc, &
  & imwave_tcwv, imwave_tclw, imwave_tciw, imwave_rwp, imwave_swp, & 
  & nmwave_diag 
use yomcst  , only: rg, rd

implicit none
          
!* Common variables
integer (kind=jpim), intent    (in) :: klev, ksat
type (mwave_phys_type), intent (in) :: yd_mwave_phys 
real (kind=jprb), intent    (inout) :: pdiag(nmwave_diag)

!* OTHER
integer (kind=jpim) :: ilev

real    (kind=jprb), dimension (klev) :: zdp, zdlnp, zdz, zrhoair_rain, zrhoair_snow
real    (kind=jprb) :: zconv_rain (2), zconv_snow (2)
real    (kind=jprb), parameter :: zem3 = 0.001_jprb

! adjustment variables
real (kind=jprb) :: zhook_handle

if (lhook) call dr_hook ('MWAVE_DIAGS',0,zhook_handle)

call mwave_share_rtcoeff (zconv_rain, zconv_snow, ksat) 

!* Calculate condensate paths 
zdlnp (1) = 0.0_JPRB
do ilev = 1, klev
  zdp (ilev) = yd_mwave_phys % ph (ilev+1) - yd_mwave_phys % ph (ilev)
  if (ilev > 1) zdlnp (ilev) = log (yd_mwave_phys % ph (ilev+1) / yd_mwave_phys % ph (ilev))  
  zdz (ilev) = yd_mwave_phys % t (ilev) * zdlnp (ilev) * rd / rg 
enddo 

pdiag (imwave_rain_sfc) =      yd_mwave_phys % rfl (klev)
pdiag (imwave_snow_sfc) =      yd_mwave_phys % sfl (klev)
pdiag (imwave_tcwv)     = sum (yd_mwave_phys % q   (1:klev) * zdp / rg)
pdiag (imwave_tclw)     = sum (yd_mwave_phys % cwc (1:klev) * zdp / rg)
pdiag (imwave_tciw)     = sum (yd_mwave_phys % cic (1:klev) * zdp / rg)
   
zrhoair_rain (:) = 0.0_JPRB
zrhoair_snow (:) = 0.0_JPRB
     
where (yd_mwave_phys % rfl + yd_mwave_phys % crfl > 0.0_JPRB) &
  & zrhoair_rain = zem3 * (((yd_mwave_phys % rfl + yd_mwave_phys % crfl) &
  &                            * zconv_rain (1)) ** zconv_rain (2))
where (yd_mwave_phys % sfl + yd_mwave_phys % csfl > 0.0_JPRB) &
  & zrhoair_snow = zem3 * (((yd_mwave_phys % sfl + yd_mwave_phys % csfl) &
  &                            * zconv_snow (1)) ** zconv_snow (2))

pdiag (imwave_rwp) = sum (zdz (1:klev) * zrhoair_rain (1:klev))
pdiag (imwave_swp) = sum (zdz (1:klev) * zrhoair_snow (1:klev))

if (lhook) call dr_hook ('MWAVE_DIAGS',1,zhook_handle) 

end subroutine mwave_diags
