SUBROUTINE GNH_CONV_NHVAR( &
 ! ----- INPUT ---------------------------------------------------------------
 & LDMODEL_TO_FILE, &
 ! ----- INPUT-OUTPUT --------------------------------------------------------
 & PU,PV,PT,PTL,PTM,PSPD,PSPDL,PSPDM,PSVD,PSP,PSPL,PSPM, &
 & PQ,PQL,PQM,PL,PLL,PLM,PI,PIL,PIM,PR,PS,PG, &
 ! ----- OUTPUT --------------------------------------------------------------
 & PSNHX &
 &)


!* GNH_CONV_NHVAR - Conversion of NH variables (model to file and vice-versa)
!                   Grid-point part.

! Purpose
! -------
!   Converts NH pressure departure and vertical divergence
!   between model and file.
!   Files contain "pre-prehyd" and "-G.dw". 
!   Modifies spectral buffers:
!     SPSPD
!     SPSVD
!     SPNHX (only for d4 + file ---> model conversion)

!   Remark:
!    If LRWSDLR=T, replace "prehyd" by "presim" in comments, and note that
!     horizontal gradients are semi-reduced ones (i.e. (r/a)*nabla).

! Interface
! ---------
!  * INPUT:
!     LDMODEL_TO_FILE - switch for model to file / file to model conversion

!  * INPUT-OUTPUT:
!     PU     : U-wind
!     PV     : V-wind
!     PT     : temperature
!     PTL    : zonal comp of grad(temperature)
!     PTM    : merid comp of grad(temperature)
!     PSPD   : NH pressure departure variable "spd"
!     PSPDL  : zonal comp of grad(spd)
!     PSPDM  : merid comp of grad(spd)
!     PSVD   : NH vertical divergence variable "svd"
!     PSP    : log(prehyd)
!     PSPL   : zonal comp of grad(log(prehyd))
!     PSPM   : merid comp of grad(log(prehyd))
!     PQ     : moisture
!     PQL    : zonal comp of grad(moisture)
!     PQM    : merid comp of grad(moisture)
!     PL     : liquid water
!     PLL    : zonal comp of grad(liquid water)
!     PLM    : merid comp of grad(liquid water)
!     PI     : ice
!     PIL    : zonal comp of grad(ice)
!     PIM    : merid comp of grad(ice)
!     PR     : rain
!     PS     : snow
!     PG     : graupels

!  * OUTPUT:
!     PSNHX: "X" divergence variable for case nvdvar=4.


! Externals
! ---------

! Method
! ------
!   See documentation

! Reference
! ---------

! Author
! ------
!   07-Feb-2005 K. Yessad (after the old routine GNHPDVDCONV)

! Modifications
! -------------
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!------------------------------------------------------------------------------

USE PARKIND1 , ONLY : JPIM     ,JPRB
USE YOMHOOK  , ONLY : LHOOK    ,DR_HOOK
USE YOMCST   , ONLY : RD
USE YOMCVER  , ONLY : LVFE_GW
USE YOMDIM   , ONLY : NFLEVG   ,NPROMA
USE YOMDYNA  , ONLY : NVDVAR   ,NPDVAR   ,LRWSDLR  ,LRWSDLR2
USE YOMDYN   , ONLY : RC_PD1   ,RCORPDF
USE YOMGC    , ONLY : GM       ,OROG     ,OROGL    ,OROGM
USE YOMGEM   , ONLY : VAH      ,VBH      ,VC       ,VDELB    ,NGPTOT
USE YOM_YGFL , ONLY : YQ       ,YL       ,YI
USE PAR_RDLR , ONLY : JP_DIMR  ,JP_RSAF  ,JP_ASRF  ,JP_RSAH  ,JP_ASRH

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

IMPLICIT NONE

LOGICAL,INTENT(IN)            :: LDMODEL_TO_FILE 
REAL(KIND=JPRB),INTENT(INOUT) :: PU   (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PV   (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PT   (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PTL  (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PTM  (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PSPD (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PSPDL(NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PSPDM(NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PSVD (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PSP  (NGPTOT)
REAL(KIND=JPRB),INTENT(INOUT) :: PSPL (NGPTOT)
REAL(KIND=JPRB),INTENT(INOUT) :: PSPM (NGPTOT)
REAL(KIND=JPRB),INTENT(INOUT) :: PQ   (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PQL  (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PQM  (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PL   (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PLL  (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PLM  (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PI   (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PIL  (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PIM  (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PR   (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PS   (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT) :: PG   (NGPTOT,NFLEVG)
REAL(KIND=JPRB),INTENT(OUT)   :: PSNHX(NGPTOT,NFLEVG)

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

INTEGER(KIND=JPIM) :: JIST,JLEV
INTEGER(KIND=JPIM) :: IST,IEND,IPROMA
LOGICAL            :: LLDER,LLGWF,LLGDWI

REAL(KIND=JPRB) :: ZOROGL(NGPTOT)
REAL(KIND=JPRB) :: ZOROGM(NGPTOT)
REAL(KIND=JPRB) :: ZPDEP (NPROMA,  NFLEVG)
REAL(KIND=JPRB) :: ZDELP (NPROMA,  NFLEVG)
REAL(KIND=JPRB) :: ZRDELP(NPROMA,  NFLEVG)
REAL(KIND=JPRB) :: ZLNPR (NPROMA,  NFLEVG)
REAL(KIND=JPRB) :: ZALPH (NPROMA,  NFLEVG)
REAL(KIND=JPRB) :: ZRTGR (NPROMA,  NFLEVG)
REAL(KIND=JPRB) :: ZRPRE (NPROMA,  NFLEVG)
REAL(KIND=JPRB) :: ZRPP  (NPROMA,  NFLEVG)
REAL(KIND=JPRB) :: ZPREH (NPROMA,0:NFLEVG)
REAL(KIND=JPRB) :: ZPREF (NPROMA,  NFLEVG)
REAL(KIND=JPRB) :: ZX    (NPROMA,  NFLEVG)
REAL(KIND=JPRB) :: ZR    (NPROMA,  NFLEVG)
REAL(KIND=JPRB) :: ZRNHPPI(NPROMA,  NFLEVG)
REAL(KIND=JPRB) :: ZUS   (NPROMA)
REAL(KIND=JPRB) :: ZVS   (NPROMA)
REAL(KIND=JPRB) :: ZGWH  (NPROMA,0:NFLEVG)
REAL(KIND=JPRB) :: ZGWF  (NPROMA,  NFLEVG)
REAL(KIND=JPRB) :: ZRDLR (NPROMA,0:NFLEVG,JP_DIMR)

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"
#include "gnhdlr.intfb.h"
#include "gnhx.intfb.h"
#include "gpgw.intfb.h"
#include "gppref.intfb.h"
#include "gppreh.intfb.h"
#include "gprcp.intfb.h"
#include "gpuvs.intfb.h"
#include "gpxyb.intfb.h"

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

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

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

IF (LRWSDLR2.AND.LVFE_GW) THEN
  ! Conversions between G(Delta w) and "dver" not yet coded in this case.
  CALL ABOR1(' GNH_CONV_NHVAR: LRWSDLR2=T not yet coded for LVFE_GW')
ENDIF

!* 1. GRID POINT COMPUTATIONS
!     -----------------------

IF (NVDVAR == 4) THEN

  !* 1.0 MULTIPLICATION BY MAP FACTOR

  ! orography
  ZOROGL(1:NGPTOT)=OROGL(1:NGPTOT)*GM(1:NGPTOT)
  ZOROGM(1:NGPTOT)=OROGM(1:NGPTOT)*GM(1:NGPTOT)

  ! surface pressure (GMVS)
  PSPL(1:NGPTOT)=PSPL(1:NGPTOT)*GM(1:NGPTOT)
  PSPM(1:NGPTOT)=PSPM(1:NGPTOT)*GM(1:NGPTOT)

  ! GMV:
  DO JLEV = 1,NFLEVG
    PU   (1:NGPTOT,JLEV)=PU   (1:NGPTOT,JLEV)*GM(1:NGPTOT)
    PV   (1:NGPTOT,JLEV)=PV   (1:NGPTOT,JLEV)*GM(1:NGPTOT)
    PTL  (1:NGPTOT,JLEV)=PTL  (1:NGPTOT,JLEV)*GM(1:NGPTOT)
    PTM  (1:NGPTOT,JLEV)=PTM  (1:NGPTOT,JLEV)*GM(1:NGPTOT)
    PSPDL(1:NGPTOT,JLEV)=PSPDL(1:NGPTOT,JLEV)*GM(1:NGPTOT)
    PSPDM(1:NGPTOT,JLEV)=PSPDM(1:NGPTOT,JLEV)*GM(1:NGPTOT)
  ENDDO ! JLEV

  ! GFL:
  DO JLEV = 1,NFLEVG
    IF (YQ%LSP) THEN
      PQM(1:NGPTOT,JLEV)=PQM(1:NGPTOT,JLEV)*GM(1:NGPTOT)
      PQL(1:NGPTOT,JLEV)=PQL(1:NGPTOT,JLEV)*GM(1:NGPTOT)
    ENDIF
    IF (YL%LSP) THEN
      PLM(1:NGPTOT,JLEV)=PLM(1:NGPTOT,JLEV)*GM(1:NGPTOT)
      PLL(1:NGPTOT,JLEV)=PLL(1:NGPTOT,JLEV)*GM(1:NGPTOT)
    ENDIF
    IF (YI%LSP) THEN
      PIM(1:NGPTOT,JLEV)=PIM(1:NGPTOT,JLEV)*GM(1:NGPTOT)
      PIL(1:NGPTOT,JLEV)=PIL(1:NGPTOT,JLEV)*GM(1:NGPTOT)
    ENDIF
  ENDDO ! JLEV

ENDIF

IF ( LDMODEL_TO_FILE ) THEN

  !* 1.1 TRANSFORM FROM MODEL TO FILE

  ! loop through sections of length NPROMA
  DO JIST=1,NGPTOT,NPROMA

    IST    = JIST                      ! first element
    IEND   = MIN(IST+NPROMA-1,NGPTOT)  ! last element
    IPROMA = IEND-IST+1                ! actual number of elements

    !* 1.1.1 COMPUTE PRESSURES

    ZPREH(1:IPROMA,NFLEVG)=EXP(PSP(IST:IEND))

    CALL GPPREH(IPROMA,1,IPROMA,NFLEVG,VAH,VBH,ZPREH(1:IPROMA,:))
    CALL GPXYB(IPROMA,1,IPROMA,NFLEVG,VDELB,VC, &
     & ZPREH(1:IPROMA,:),ZDELP(1:IPROMA,:),ZRDELP(1:IPROMA,:), &
     & ZLNPR(1:IPROMA,:),ZALPH(1:IPROMA,:),ZRTGR (1:IPROMA,:), &
     & ZRPRE(1:IPROMA,:),ZRPP (1:IPROMA,:))  
    CALL GPPREF(IPROMA,1,IPROMA,NFLEVG, &
     & ZALPH(1:IPROMA,:),ZPREH(1:IPROMA,:),ZPREF(1:IPROMA,:))  

    !* 1.1.2 COMPUTE TERM X (IF NVDVAR=4)

    IF (NVDVAR == 4) THEN

      CALL GNHX(IPROMA,1,IPROMA,OROG(IST:IEND),&
       & ZOROGL(IST:IEND),ZOROGM(IST:IEND),&
       & PSP(IST:IEND),PSPL(IST:IEND),PSPM(IST:IEND),&
       & PT(IST:IEND,:),PTL(IST:IEND,:),PTM(IST:IEND,:),&
       & PQ(IST:IEND,:),PQL(IST:IEND,:),PQM(IST:IEND,:),&
       & PL(IST:IEND,:),PLL(IST:IEND,:),PLM(IST:IEND,:),&
       & PI(IST:IEND,:),PIL(IST:IEND,:),PIM(IST:IEND,:),&
       & PR(IST:IEND,:),PS(IST:IEND,:),PG(IST:IEND,:),&
       & PU(IST:IEND,:),PV(IST:IEND,:),&
       & PSPD(IST:IEND,:),PSPDL(IST:IEND,:),PSPDM(IST:IEND,:),&
       & ZX(1:IPROMA,:) &
       & )  

    ENDIF

    !* 1.1.4 Compute "r/a", "a/r" when useful. 

    IF (LRWSDLR) THEN

      ! Compute presim/pre:
      !  log(pre/presim) + rc_pd1*rcorpdf -> presim/pre
      IF ( NPDVAR == 2 ) THEN
        DO JLEV=1,NFLEVG
          ZRNHPPI(1:IPROMA,JLEV)= &
           & 1.0_JPRB/EXP(PSPD(IST:IEND,JLEV)-RC_PD1*RCORPDF(JLEV))
        ENDDO
      ENDIF

      ! Compute R:
      CALL GPRCP(IPROMA,1,IPROMA,NFLEVG, &
       & PQ=PQ(IST:IEND,:),PQI=PI(IST:IEND,:),PQL=PL(IST:IEND,:), &
       & PQR=PR(IST:IEND,:),PQS=PS(IST:IEND,:),PQG=PG(IST:IEND,:), &
       & PR=ZR(1:IPROMA,:))

      ! Compute "r/a", "a/r":
      CALL GNHDLR(IPROMA,NFLEVG,1,IPROMA,PT(IST:IEND,:),ZR(1:IPROMA,:), &
       & ZRNHPPI(1:IPROMA,:),ZLNPR(1:IPROMA,:),ZALPH(1:IPROMA,:), &
       & OROG(IST:IEND),ZRDLR(1:IPROMA,0:NFLEVG,1:JP_DIMR))

    ENDIF

    !* 1.1.5 TRANSFORM PD

    DO JLEV=1,NFLEVG

      IF ( NPDVAR == 2 ) THEN
        IF (LRWSDLR) THEN
          ! LRWSDLR=T: log(pre/presim) + rc_pd1*rcorpdf -> pre-presim
          PSPD(IST:IEND,JLEV) = &
           & ( EXP(PSPD(IST:IEND,JLEV)-RC_PD1*RCORPDF(JLEV))-1.0_JPRB )* &
           & ZPREF(1:IPROMA,JLEV)
        ELSE
          ! LRWSDLR=F: log(pre/prehyd) -> pre-prehyd
          PSPD(IST:IEND,JLEV) = ( EXP(PSPD(IST:IEND,JLEV))-1.0_JPRB )* &
           & ZPREF(1:IPROMA,JLEV)
        ENDIF
      ENDIF

    ENDDO  ! JLEV = 1,NFLEVG

    !* 1.1.6 TRANSFORM VD

    DO JLEV=1,NFLEVG

      IF ( NVDVAR == 3 ) THEN
        ! dver -> -G(r**2/a**2)(dw) or -G*(d(r**2/a**2)w)
        PSVD(IST:IEND,JLEV) = RD*PT(IST:IEND,JLEV)* &
         & ZLNPR(1:IPROMA,JLEV)*PSVD(IST:IEND,JLEV)/ &
         & ( PSPD(IST:IEND,JLEV)/ZPREF(1:IPROMA,JLEV)+1.0_JPRB )  
      ELSEIF ( NVDVAR == 4 ) THEN
        ! d4 -> -G(r**2/a**2)(dw) or -G*(d(r**2/a**2)w)
        PSVD(IST:IEND,JLEV) = (PSVD(IST:IEND,JLEV)-ZX(1:IPROMA,JLEV))* &
         & RD*PT(IST:IEND,JLEV)*ZLNPR(1:IPROMA,JLEV)/ &
         & ( PSPD(IST:IEND,JLEV)/ZPREF(1:IPROMA,JLEV)+1.0_JPRB)  
      ENDIF

    ENDDO  ! JLEV = 1,NFLEVG

    IF (LRWSDLR) THEN
      IF (LRWSDLR2) THEN
        ! -G*(d(r**2/a**2)w) -> -G(dw)
        IF (LVFE_GW) THEN
          CALL ABOR1('GNH_CONV_NHVAR 1.1.6: case not yet coded!') 
          ! integral and derivative must be applied to (r**2/a**2 -1)w
        ELSE
          ! Compute surface wind:
          LLDER=.FALSE.
          CALL GPUVS(NFLEVG,IPROMA,1,IPROMA,LLDER, &
           & PU(IST:IEND,:),PV(IST:IEND,:),ZUS(1:IPROMA),ZVS(1:IPROMA))
          ! Compute G(r**2/a**2)w:
          LLGWF=.FALSE.
          LLGDWI=.TRUE.
          CALL GPGW(NFLEVG,IPROMA,1,IPROMA,LLGWF,LLGDWI, &
           & ZOROGL(IST:IEND),ZOROGM(IST:IEND), &
           & ZLNPR(1:IPROMA,:),ZALPH(1:IPROMA,:),&
           & ZUS(1:IPROMA),ZVS(1:IPROMA), &
           & PT(IST:IEND,:),PSVD(IST:IEND,:),ZRNHPPI(1:IPROMA,:), &
           & ZGWH(1:IPROMA,:),ZGWF(1:IPROMA,:),LDRWSDLR=LRWSDLR, &
           & LDRWSDLR2=LRWSDLR2,PRDLR=ZRDLR(1:IPROMA,:,1:JP_DIMR))
          ! Compute Gw:
          DO JLEV=0,NFLEVG
            ZGWH(1:IPROMA,JLEV)=ZGWH(1:IPROMA,JLEV)* &
             & ZRDLR(1:IPROMA,JLEV,JP_ASRH)*ZRDLR(1:IPROMA,JLEV,JP_ASRH)
          ENDDO
          ! Compute -G(dw):
          DO JLEV=1,NFLEVG
            PSVD(IST:IEND,JLEV)=ZGWH(1:IPROMA,JLEV-1)-ZGWH(1:IPROMA,JLEV)
          ENDDO
        ENDIF
      ELSE
        ! -G(r**2/a**2)(dw) -> -G(dw)
        DO JLEV=1,NFLEVG
          PSVD(IST:IEND,JLEV) = PSVD(IST:IEND,JLEV)* &
           & ZRDLR(1:IPROMA,JLEV,JP_ASRF)*ZRDLR(1:IPROMA,JLEV,JP_ASRF)
        ENDDO
      ENDIF
    ENDIF

  ENDDO  ! JIST = 1,NGPTOT,NPROMA

ELSE

  !* 1.2 TRANSFORM FROM FILE TO MODEL

  ! loop through sections of length NPROMA
  DO JIST=1,NGPTOT,NPROMA

    IST    = JIST                      ! first element
    IEND   = MIN(IST+NPROMA-1,NGPTOT)  ! last element
    IPROMA = IEND-IST+1                ! actual number of elements

    !* 1.2.1 COMPUTE PRESSURES

    ZPREH(1:IPROMA,NFLEVG)=EXP(PSP(IST:IEND))

    CALL GPPREH(IPROMA,1,IPROMA,NFLEVG,VAH,VBH,ZPREH(1:IPROMA,:))
    CALL GPXYB(IPROMA,1,IPROMA,NFLEVG,VDELB,VC, &
     & ZPREH(1:IPROMA,:),ZDELP(1:IPROMA,:),ZRDELP(1:IPROMA,:), &
     & ZLNPR(1:IPROMA,:),ZALPH(1:IPROMA,:),ZRTGR (1:IPROMA,:), &
     & ZRPRE(1:IPROMA,:),ZRPP (1:IPROMA,:))  
    CALL GPPREF(IPROMA,1,IPROMA,NFLEVG, &
     & ZALPH(1:IPROMA,:),ZPREH(1:IPROMA,:),ZPREF(1:IPROMA,:))  

    !* 1.2.2 Transform PD

    DO JLEV=1,NFLEVG

      IF ( NPDVAR == 2 ) THEN
        ! LRWSDLR=F: save "pre-prehyd" in ZPDEP 
        ! LRWSDLR=T: save "pre-presim" in ZPDEP 
        ZPDEP(1:IPROMA,JLEV)=PSPD(IST:IEND,JLEV)
        PSPD(IST:IEND,JLEV) = &
         & LOG( PSPD(IST:IEND,JLEV)/ZPREF(1:IPROMA,JLEV)+1.0_JPRB )  
        IF (LRWSDLR) &
         & PSPD(IST:IEND,JLEV)=PSPD(IST:IEND,JLEV)+RC_PD1*RCORPDF(JLEV)
      ENDIF

      ! For the horizontal derivatives of "spd" the formula used is:
      !  dQcha = [d(pre-prehyd) - (pre-prehyd) (dprehyd/prehyd)]
      !          /[(pre-prehyd)+prehyd]
      !  (if LRWSDLR, read "presim" instead of "prehyd")
      IF ( NVDVAR == 4 ) THEN
        IF ( NPDVAR == 2 ) THEN
          PSPDL(IST:IEND,JLEV)= &
           & ( PSPDL(IST:IEND,JLEV) &
           & - ZPDEP(1:IPROMA,JLEV)*ZRTGR(1:IPROMA,JLEV)*PSPL(IST:IEND) ) &
           & /(ZPDEP(1:IPROMA,JLEV)+ZPREF(1:IPROMA,JLEV))
          PSPDM(IST:IEND,JLEV)= &
           & ( PSPDM(IST:IEND,JLEV) &
           & - ZPDEP(1:IPROMA,JLEV)*ZRTGR(1:IPROMA,JLEV)*PSPM(IST:IEND) ) &
           & /(ZPDEP(1:IPROMA,JLEV)+ZPREF(1:IPROMA,JLEV))
        ENDIF
      ENDIF

    ENDDO ! JLEV

    !* 1.2.3 Compute "r/a", "a/r" when useful. 

    IF (LRWSDLR) THEN

      ! Compute presim/pre:
      DO JLEV=1,NFLEVG
        ZRNHPPI(1:IPROMA,JLEV)=ZPREF(1:IPROMA,JLEV)&
         & /(ZPDEP(1:IPROMA,JLEV)+ZPREF(1:IPROMA,JLEV))
      ENDDO

      ! Compute R:
      CALL GPRCP(IPROMA,1,IPROMA,NFLEVG, &
       & PQ=PQ(IST:IEND,:),PQI=PI(IST:IEND,:),PQL=PL(IST:IEND,:), &
       & PQR=PR(IST:IEND,:),PQS=PS(IST:IEND,:),PQG=PG(IST:IEND,:), &
       & PR=ZR(1:IPROMA,:))

      ! Compute "r/a", "a/r":
      CALL GNHDLR(IPROMA,NFLEVG,1,IPROMA,PT(IST:IEND,:),ZR(1:IPROMA,:), &
       & ZRNHPPI(1:IPROMA,:),ZLNPR(1:IPROMA,:),ZALPH(1:IPROMA,:), &
       & OROG(IST:IEND),ZRDLR(1:IPROMA,0:NFLEVG,1:JP_DIMR))

    ENDIF

    !* 1.2.4 Transform VD (first part without X-term (NVDVAR=4))

    IF (LRWSDLR) THEN
      IF (LRWSDLR2) THEN
        ! -G(dw) -> -G(d(r**2/a**2)w)
        IF (LVFE_GW) THEN
          CALL ABOR1('GNH_CONV_NHVAR 1.2.4: case not yet coded!') 
          ! integral and derivative must be applied to (r**2/a**2 -1)w
        ELSE
          ! Compute surface wind:
          LLDER=.FALSE.
          CALL GPUVS(NFLEVG,IPROMA,1,IPROMA,LLDER, &
           & PU(IST:IEND,:),PV(IST:IEND,:),ZUS(1:IPROMA),ZVS(1:IPROMA))
          ! Compute Gw:
          LLGWF=.FALSE.
          LLGDWI=.TRUE.
          CALL GPGW(NFLEVG,IPROMA,1,IPROMA,LLGWF,LLGDWI, &
           & ZOROGL(IST:IEND),ZOROGM(IST:IEND), &
           & ZLNPR(1:IPROMA,:),ZALPH(1:IPROMA,:),&
           & ZUS(1:IPROMA),ZVS(1:IPROMA), &
           & PT(IST:IEND,:),PSVD(IST:IEND,:),ZRNHPPI(1:IPROMA,:), &
           & ZGWH(1:IPROMA,:),ZGWF(1:IPROMA,:),LDRWSDLR=LRWSDLR, &
           & LDRWSDLR2=.FALSE.,PRDLR=ZRDLR(1:IPROMA,:,1:JP_DIMR))
          ! Compute G(r**2/a**2)w:
          DO JLEV=0,NFLEVG
            ZGWH(1:IPROMA,JLEV)=ZGWH(1:IPROMA,JLEV)* &
             & ZRDLR(1:IPROMA,JLEV,JP_RSAH)*ZRDLR(1:IPROMA,JLEV,JP_RSAH)
          ENDDO
          ! Compute -G(d(r**2/a**2)w):
          DO JLEV=1,NFLEVG
            PSVD(IST:IEND,JLEV)=ZGWH(1:IPROMA,JLEV-1)-ZGWH(1:IPROMA,JLEV)
          ENDDO
        ENDIF
      ELSE
        ! -G(dw) -> -G(r**2/a**2)(dw)
        DO JLEV=1,NFLEVG
          PSVD(IST:IEND,JLEV) = PSVD(IST:IEND,JLEV)* &
           & ZRDLR(1:IPROMA,JLEV,JP_RSAF)*ZRDLR(1:IPROMA,JLEV,JP_RSAF)
        ENDDO
      ENDIF
    ENDIF
    ! Provides "dver".
    DO JLEV=1,NFLEVG
      PSVD(IST:IEND,JLEV) = PSVD(IST:IEND,JLEV)/ &
       & ( RD*PT(IST:IEND,JLEV)*ZLNPR(1:IPROMA,JLEV) )* &
       & ( ZPDEP(1:IPROMA,JLEV)/ZPREF(1:IPROMA,JLEV)+1.0_JPRB )  
    ENDDO

    !* 1.2.5 COMPUTE TERM X (IF NVDVAR=4) THEN ADD IT TO d3

    IF (NVDVAR == 4) THEN

      CALL GNHX(IPROMA,1,IPROMA,OROG(IST:IEND),&
       & ZOROGL(IST:IEND),ZOROGM(IST:IEND),&
       & PSP(IST:IEND),PSPL(IST:IEND),PSPM(IST:IEND),&
       & PT(IST:IEND,:),PTL(IST:IEND,:),PTM(IST:IEND,:),&
       & PQ(IST:IEND,:),PQL(IST:IEND,:),PQM(IST:IEND,:),&
       & PL(IST:IEND,:),PLL(IST:IEND,:),PLM(IST:IEND,:),&
       & PI(IST:IEND,:),PIL(IST:IEND,:),PIM(IST:IEND,:),&
       & PR(IST:IEND,:),PS(IST:IEND,:),PG(IST:IEND,:),&
       & PU(IST:IEND,:),PV(IST:IEND,:),&
       & PSPD(IST:IEND,:),PSPDL(IST:IEND,:),PSPDM(IST:IEND,:),&
       & ZX(1:IPROMA,:) &
       & )  

      DO JLEV=1,NFLEVG
        PSVD(IST:IEND,JLEV)=PSVD(IST:IEND,JLEV)+ZX(1:IPROMA,JLEV)
        PSNHX(IST:IEND,JLEV) = ZX(1:IPROMA,JLEV)
      ENDDO ! JLEV

    ENDIF

  ENDDO ! JIST

ENDIF  ! LDMODEL_TO_FILE

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

IF (LHOOK) CALL DR_HOOK('GNH_CONV_NHVAR',1,ZHOOK_HANDLE)

END SUBROUTINE GNH_CONV_NHVAR

