subroutine qneglimtl (pq,pq5,k1dim1,k1dim2,k2dim1,k2dim2,&
 & k1ist,k1end,k2ist,k2end)  

! *QNEGLIM* - limits specific humidity - tangent linear and adjoint

!**   Interface.
!     ----------

!        call qneglimtl (pq,pq5,k1dim,k2dim,k1ist,k1end,k2ist,k2end)

!        Explicit arguments :
!        --------------------

!           pq     = humidity field departures or gradient
!           pq5    = humidity field reference
!           k1dim1 = lower bound of first dimension of pq
!           k1dim2 = upper bound of first dimension of pq
!           k2dim1 = lower bound of second dimension of pq
!           k2dim2 = upper bound of second dimension of pq
!           k1ist  = first column to be limited
!           k1end  = last column to be limited
!           k2ist  = first row to be limited
!           k2end  = last row to be limited

!     Method.
!     -------

!        pq is replaced by:
!               0.5*(tanh(a*(pq5-b))) + 1.)*pq
!        where:
!               a = QNLCURV
!               b = QNLMINQ

!     Externals.
!     ----------
!        none

!     Reference.
!     --------------
!        none

!     Modifications.
!     --------------
!        96-03-13 Mike Fisher - Original.
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      01-Dec-2003 CY28R1 Cleaning
!     ------------------------------------------------------------------

!--- globals

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMRINC  , ONLY : QNLMINQ  ,QNLCURV

!--- arguments

IMPLICIT NONE
INTEGER(KIND=JPIM),INTENT(IN)    :: K1DIM1 
INTEGER(KIND=JPIM),INTENT(IN)    :: K1DIM2 
INTEGER(KIND=JPIM),INTENT(IN)    :: K2DIM1 
INTEGER(KIND=JPIM),INTENT(IN)    :: K2DIM2 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQ(K1DIM1:K1DIM2,K2DIM1:K2DIM2) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQ5(K1DIM1:K1DIM2,K2DIM1:K2DIM2) 
INTEGER(KIND=JPIM),INTENT(IN)    :: K1IST 
INTEGER(KIND=JPIM),INTENT(IN)    :: K1END 
INTEGER(KIND=JPIM),INTENT(IN)    :: K2IST 
INTEGER(KIND=JPIM),INTENT(IN)    :: K2END 
!--- locals

INTEGER(KIND=JPIM) :: j1,j2
REAL(KIND=JPRB) ::    zq5
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('QNEGLIMTL',0,ZHOOK_HANDLE)
do j2=k2ist,k2end
  do j1=k1ist,k1end
    zq5 = QNLCURV*(pq5(j1,j2)-QNLMINQ)

    if      (zq5 >  17._JPRB) then
      pq(j1,j2) = pq(j1,j2)
    ELSEIF (zq5 < -17._JPRB) then
      pq(j1,j2) = 0.0_JPRB
    else
      pq(j1,j2) = 0.5_JPRB*( tanh(zq5) + 1.0_JPRB )*pq(j1,j2)
    endif
  enddo
enddo

IF (LHOOK) CALL DR_HOOK('QNEGLIMTL',1,ZHOOK_HANDLE)
end subroutine qneglimtl
