function TWB_FROM_TPQ_1D(PT, PP, PQ) RESULT(PTWB)
!! MODIFICATIONS
!! -------------
!! D.Meyer and R. Schoetter 2018: see a9c4c46889dca998ea9ca40e5e0edc62ecc75715
!arguments and results
REAL, DIMENSION(:), INTENT(IN) :: PT !air temperature (K)
REAL, DIMENSION(:), INTENT(IN) :: PQ !humidity content (kg/kg)
REAL, DIMENSION(:), INTENT(IN) :: PP !atmos. pressure (Pa)
REAL, DIMENSION(SIZE(PT)) :: PTWB !Wet Bulb Temp. (K)
!local variable
REAL, DIMENSION(SIZE(PT)) :: ZTD !Dew Point Temp. (K)
REAL, DIMENSION(SIZE(PT)) :: ZTWBINF, ZTWBSUP, ZRV
INTEGER :: JITER, JI
REAL(KIND=JPRB) :: ZHOOK_HANDLE
IF (LHOOK) CALL DR_HOOK('MODE_PSYCHRO:TWB_FROM_TPQ_1D',0,ZHOOK_HANDLE)
ZTD = TD_FROM_TQ(PT, PQ, PP)
!initial guess
ZTWBSUP = PT
ZTWBINF = ZTD
PTWB = 0.5 * (ZTWBSUP + ZTWBINF)
DO JI=1,SIZE(PT)
JITER = 1
DO WHILE ( (ABS(ZTWBSUP(JI) - ZTWBINF(JI)) .GT. 0.01) .AND. (JITER .LE. 50))
ZRV(JI) = RV_FROM_TPTWB(PT(JI), PP(JI), PTWB(JI))
IF (ZRV(JI) .GT. PQ(JI)/(1 - PQ(JI))) THEN
ZTWBSUP(JI) = PTWB(JI)
ELSE
ZTWBINF(JI) = PTWB(JI)
ENDIF
PTWB(JI) = 0.5 * (ZTWBINF(JI) + ZTWBSUP(JI))
END DO
IF (JITER.GE.49) STOP ("Maximum number of iterations exceeded in twb_from_tpq_1d")
END DO
IF (LHOOK) CALL DR_HOOK('MODE_PSYCHRO:TWB_FROM_TPQ_1D',1,ZHOOK_HANDLE)
end function TWB_FROM_TPQ_1D