SUBROUTINE JBCHVAR (PSPT,PGP3)

!      JBCHVAR - Variable transform of model variables in gridpoint
!                space needed for the control variables.

!     Arguments :
!     -----------
!     Input:
!       PSPT   - spectral total temperature increment
!     Input and output:
!       PGP3   - 3d gridpoint increments

!     Author:  ELIAS HOLM    *ECMWF*
!     -------

!     Modifications:
!     --------------
!     ORIGINAL 08-10-20       Modularized by separating out from JGNR.
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK    ,DR_HOOK
USE YOMDIM   , ONLY : NFLEVL, NSPEC2, NFLEVG, NPROMA, NGPBLKS
USE YOMCT0   , ONLY : LELAM
USE YOMCST   , ONLY : RV       ,RCPV     ,RETV     ,RCW      ,&
 & RCS      ,RLVTT   ,RLSTT    ,RTT      ,RALPW    ,RBETW    ,&
 & RGAMW    ,RALPS   ,RBETS    ,RGAMS    ,RALPD    ,RBETD    ,&
 & RGAMD
USE YOMJG    , ONLY : LJB_NONLINEAR_CVHUM,  LJB_NONLINEAR_CVO3,&
 & N_SPJB_VARS, SPJB_VARS_INFO
USE YOETHF   , ONLY : R2ES     ,R3LES    ,R3IES    ,R4LES    ,R4IES   ,&
 & R5LES    ,R5IES   ,R5ALVCP  ,R5ALSCP  ,RALVDCP  ,RALSDCP ,&
 & RTWAT    ,RTICE   ,RTICECU  ,RTWAT_RTICE_R, RTWAT_RTICECU_R  
USE YOM_YGFL , ONLY : YGFL     ,YQ       ,YO3
USE YOMJBCHVAR , ONLY : RQS, RQT, RQTBAL, RQLMIN, RQLMAX,&
 & RQLSTD, RQHMIN, RQHMAX, RQHSTD, RO3MIN, RO3STD
USE YOMGRB, ONLY : NGRBQ, NGRBO3, NGRBT
USE YOMMP,     ONLY : NBSETLEV
USE YOMGEM   , ONLY : NGPTOT
USE YOMGMV   , ONLY : YT5
USE TRAJECTORY_MOD, ONLY: GET_TRAJ_GRID

IMPLICIT NONE

REAL(KIND=JPRB)   ,INTENT(IN) :: PSPT(NFLEVL,NSPEC2)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGP3(:,:,:)
REAL(KIND=JPRB) :: ZSPT(NFLEVL,NSPEC2)
REAL(KIND=JPRB), ALLOCATABLE :: ZGPT(:,:,:)
REAL(KIND=JPRB) :: ZPRE5H(NPROMA,0:NFLEVG), ZPRE5F(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZQ5(NPROMA,NFLEVG), ZES5(NPROMA,NFLEVG), ZRH5(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZTM(NPROMA,NFLEVG), ZQM(NPROMA,NFLEVG), ZO3M(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSPM(NPROMA), ZRHM(NPROMA,NFLEVG)
REAL(KIND=JPRB), ALLOCATABLE :: ZGMV7(:,:,:,:), ZGMV7S(:,:,:), ZGFL7(:,:,:,:)
REAL(KIND=JPRB), ALLOCATABLE :: ZGMV5(:,:,:,:), ZGMV5S(:,:,:), ZGFL5(:,:,:,:)

INTEGER(KIND=JPIM) :: ILTROP5(NPROMA),ILTROPM(NPROMA)
INTEGER(KIND=JPIM) :: JROF, JLEV, JFIELD, IT, IQ, IO3
INTEGER(KIND=JPIM) :: JKGLO, ICEND, IBL

REAL(KIND=JPRB) :: ZHOOK_HANDLE

INTERFACE
#include "inv_trans.h"
END INTERFACE
#include "fcttre.h"
#include "fcttrm.h"
#include "fjbchvar.h"
#include "abor1.intfb.h"
#include "pregprh.intfb.h"
!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('JBCHVAR',0,ZHOOK_HANDLE)

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

IT  = HUGE(IT)
IQ  = HUGE(IQ)
IO3 = HUGE(IO3)
DO JFIELD=1,N_SPJB_VARS
  IF (SPJB_VARS_INFO(JFIELD)%IGRIBCODE==NGRBT) THEN
    IT  = 1+NFLEVG*(SPJB_VARS_INFO(JFIELD)%IPTJB-1)
  ELSEIF (SPJB_VARS_INFO(JFIELD)%IGRIBCODE==NGRBQ) THEN
    IQ  = 1+NFLEVG*(SPJB_VARS_INFO(JFIELD)%IPTJB-1)
  ELSEIF (SPJB_VARS_INFO(JFIELD)%IGRIBCODE==NGRBO3) THEN
    IO3 = 1+NFLEVG*(SPJB_VARS_INFO(JFIELD)%IPTJB-1)
  ENDIF
ENDDO

!     *    1.   Trajectory and T increments


!*         1.1 Retrieve gridpoint trajectory/background

ALLOCATE (ZGMV5(NPROMA,NFLEVG,YT5%NDIM,NGPBLKS))
ALLOCATE (ZGMV5S(NPROMA,YT5%NDIMS,NGPBLKS))
ALLOCATE (ZGFL5(NPROMA,NFLEVG,YGFL%NDIM5,NGPBLKS))
CALL GET_TRAJ_GRID(ZGMV5,ZGMV5S,ZGFL5,KSTEP=0)

ALLOCATE (ZGMV7(NPROMA,NFLEVG,YT5%NDIM,NGPBLKS))
ALLOCATE (ZGMV7S(NPROMA,YT5%NDIMS,NGPBLKS))
ALLOCATE (ZGFL7(NPROMA,NFLEVG,YGFL%NDIM5,NGPBLKS))
CALL GET_TRAJ_GRID(ZGMV7,ZGMV7S,ZGFL7,KSTEP=0,LDBACKGR=.TRUE.)

!*         1.2 Transform T increments to gridpoint space

ALLOCATE (ZGPT(NPROMA,NFLEVG,NGPBLKS))
IF (LELAM) THEN
  CALL ABOR1('JBCHCVAR: HUMIDITY ANALYSIS CHANGE - CONTACT E. HOLM')
ELSE
  ZSPT(:,:)=PSPT(:,:)
  CALL INV_TRANS(PSPSCALAR=ZSPT,KVSETSC=NBSETLEV,KPROMA=NPROMA,&
                & PGP=ZGPT)
ENDIF

!     *    2.  Change of variable

DO JKGLO=1,NGPTOT,NPROMA
  ICEND=MIN(NPROMA,NGPTOT-JKGLO+1)
  IBL=(JKGLO-1)/NPROMA+1

!*         2.0 Mean of trajectory and background

  ZTM (1:ICEND,:) = 0.5_JPRB*( ZGMV5 (1:ICEND,:,YT5%MT, IBL)&
                  &           +ZGMV7 (1:ICEND,:,YT5%MT, IBL))
  ZQM (1:ICEND,:) = 0.5_JPRB*( ZGFL5 (1:ICEND,:,YQ%MP5, IBL)&
                  &           +ZGFL7 (1:ICEND,:,YQ%MP5, IBL))
  ZO3M(1:ICEND,:) = 0.5_JPRB*( ZGFL5 (1:ICEND,:,YO3%MP5,IBL)&
                  &           +ZGFL7 (1:ICEND,:,YO3%MP5,IBL))
  ZSPM(1:ICEND)   = 0.5_JPRB*( ZGMV5S(1:ICEND,  YT5%MSP,IBL)&
                  &           +ZGMV7S(1:ICEND,  YT5%MSP,IBL))

!*         2.1  Modified relative humidity, q > 0, full level pressure
!*              and tropopause height

  CALL PREGPRH (ICEND,ZTM,ZQM,ZSPM,ZQ5,ZPRE5H,ZPRE5F,ZES5,ZRHM,ILTROPM)
  CALL PREGPRH (ICEND,ZGMV5(1,1,YT5%MT,IBL),ZGFL5(1,1,YQ%MP5,IBL),&
                &ZGMV5S(1,YT5%MSP,IBL),ZQ5,ZPRE5H,ZPRE5F,ZES5,ZRH5,ILTROP5)

!*         2.2  Humidity transform (q' --> RH'/sigma(RH_b))

  IF (LJB_NONLINEAR_CVHUM) THEN
    DO JLEV=1,NFLEVG
      DO JROF=1,ICEND
        PGP3(JROF,IQ+JLEV-1,IBL) = ZRH5(JROF,JLEV)              &
         &   *(  PGP3(JROF,IQ+JLEV-1,IBL)/ZQ5(JROF,JLEV)        &
         &     - ZGPT(JROF,JLEV,IBL)                            &
         &      *FOETB(ZGMV5(JROF,JLEV,YT5%MT,IBL))             &
         &      *FQTBAL(JLEV,ILTROPM(JROF),ZRHM(JROF,JLEV)) )   &
         &   /FQSIGMA(JLEV,ILTROPM(JROF),ZRHM(JROF,JLEV),       &
         &            ZRHM(JROF,JLEV))
      ENDDO
    ENDDO
  ENDIF

!*         2.2  Ozone transform (o3' --> o3'/sigma(o3_b))

  IF (LJB_NONLINEAR_CVO3) THEN
    IF (.NOT.YO3%LACTIVE) CALL ABOR1 ('JBCHVAR: Jb ozone requires YO3%LACTIVE')
    DO JLEV=1,NFLEVG
      DO JROF=1,ICEND
        PGP3(JROF,IO3+JLEV-1,IBL) =                       &
     &     PGP3(JROF,IO3+JLEV-1,IBL)                      &
     &    /FO3SIGMA(JLEV,ZO3M(JROF,JLEV))
      ENDDO
    ENDDO
  ENDIF

ENDDO


!     *    3.   Deallocate

IF (ALLOCATED(ZGMV5)) DEALLOCATE(ZGMV5)
IF (ALLOCATED(ZGMV5S)) DEALLOCATE(ZGMV5S)
IF (ALLOCATED(ZGFL5)) DEALLOCATE(ZGFL5)
IF (ALLOCATED(ZGMV7))  DEALLOCATE(ZGMV7)
IF (ALLOCATED(ZGMV7S)) DEALLOCATE(ZGMV7S)
IF (ALLOCATED(ZGFL7))  DEALLOCATE(ZGFL7)
IF (ALLOCATED(ZGPT)) DEALLOCATE(ZGPT)

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

IF (LHOOK) CALL DR_HOOK('JBCHVAR',1,ZHOOK_HANDLE)
END SUBROUTINE JBCHVAR
