SUBROUTINE MF_PHYS_PREP( &
 ! --- INPUT -----------------------------------------------------------------
 & KST,KEND,LDALLOC_PRE9FPHY,LDALLOC_PRE9PHY,POROG,PGMV,PR0,PR9, &
 & PGWFT0,PGWFT9,PNHPRE0F,PNHPRE9F,PNHPRE0H,PNHPRE9H, &
 & PRE0F,PRE9F,PRE0,PRE9,PDELP0,PDELP9,PRDELP0,PRDELP9, &
 & PLNPR0,PLNPR9,PALPH0,PALPH9, &
 ! --- OUTPUT ----------------------------------------------------------------
 & PW0,PW9,PRE0F_PHY,PRE9F_PHY,PRE0_PHY,PRE9_PHY, &
 & PREHYD0F_PHY,PREHYD9F_PHY,PREHYD0_PHY,PREHYD9_PHY, &
 & PDELP0_PHY,PDELP9_PHY,PRDELP0_PHY,PRDELP9_PHY, &
 & PLNPR0_PHY,PLNPR9_PHY,PALPH0_PHY,PALPH9_PHY)

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE YOMCT0   , ONLY : LTWOTL   ,LNHDYN  
USE YOMCST   , ONLY : RG
USE YOMDIM   , ONLY : NPROMA   ,NPROMNH  ,NFLEVG
USE YOMARPHY , ONLY : LMPA
USE YOMDYNA  , ONLY : LRWSDLR
USE YOMGMV   , ONLY : YT0      ,YT9      ,NDIMGMV

!**** *MF_PHYS_PREP* .

!     Purpose.
!     --------
!         Does preparations for MF physics (some memory transfers
!          for example)

!**   Interface.
!     ----------
!        *CALL* *MF_PHYS_PREP(...)*

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

!     INPUT:
!     ------
!      KST              : first element of work.
!      KEND             : last element of work.
!      LDALLOC_PRE9FPHY : T: PRE9FPHY is allocated.
!      LDALLOC_PRE9PHY  : T: PRE9PHY is allocated.
!      POROG            : surface orography.
!      PGMV             : upper air GMV variables at time t and t-dt.
!      PR0              : air constant "R" at t.
!      PR9              : air constant "R" at t-dt.
!      PGWFT0           : [gw] at full levels at t.
!      PGWFT9           : [gw] at full levels at t-dt.
!      PNHPRE0F         : "pre" at full levels (time t).
!      PNHPRE9F         : "pre" at full levels (time t-dt).
!      PNHPRE0H         : "pre" at half levels (time t).
!      PNHPRE9H         : "pre" at half levels (time t-dt).
!      PRE0F            : "prehyd" or "presim" at full levels at time t.
!      PRE9F            : "prehyd" or "presim" at full levels at time t-dt.
!      PRE0             : "prehyd" or "presim" at half levels at time t.
!      PRE9             : "prehyd" or "presim" at half levels at time t-dt.
!      PDELP0           : Delta(prehyd) or Delta(presim) at full levels at t.
!      PDELP9           : Delta(prehyd) or Delta(presim) at full levels at t-dt.
!      PRDELP0          : 1/PDELP0 at full levels at t.
!      PRDELP9          : 1/PDELP9 at full levels at t-dt.
!      PLNPR0           : "delta" at full levels at time t.
!      PLNPR9           : "delta" at full levels at time t-dt.
!      PALPH0           : "alpha" at full levels at time t.
!      PALPH9           : "alpha" at full levels at time t-dt.

!     OUTPUT:
!     -------
!      PW0              : "w" at full levels at time t.
!      PW9              : "w" at full levels at time t-dt.
!      PRE0F_PHY        : input "pre" for AROME at full levels at time t.
!      PRE9F_PHY        : input "pre" for AROME at full levels at time t-dt.
!      PRE0_PHY         : input "pre" for AROME at half levels at time t.
!      PRE9_PHY         : input "pre" for AROME at half levels at time t-dt.
!      PREHYD0F_PHY     : input "prehyd" for phys. at full levels at t.
!      PREHYD9F_PHY     : input "prehyd" for phys. at full levels at t-dt.
!      PREHYD0_PHY      : input "prehyd" for phys. at half levels at t.
!      PREHYD9_PHY      : input "prehyd" for phys. at half levels at t-dt.
!      PDELP0_PHY       : input Delta(prehyd) for phys. at full levels at t.
!      PDELP9_PHY       : input Delta(prehyd) for phys. at full levels at t-dt.
!      PRDELP0_PHY      : 1/PDELP0_PHY at full levels at t.
!      PRDELP9_PHY      : 1/PDELP9_PHY at full levels at t-dt.
!      PLNPR0_PHY       : input "delta" for phys. at full levels at time t.
!      PLNPR9_PHY       : input "delta" for phys. at full levels at time t-dt.
!      PALPH0_PHY       : input "alpha" for phys. at full levels at time t.
!      PALPH9_PHY       : input "alpha" for phys. at full levels at time t-dt.

!        Implicit arguments :
!        --------------------

!     Method.
!     -------

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

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

!     Author.
!     -------
!        K. Yessad (March 2008), after some CPG pieces of code.

!     Modifications.
!     --------------
!   K. Yessad (March 2009): correct false comments for LRWSDLG=T
!     -------------------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KEND 
LOGICAL,INTENT(IN)               :: LDALLOC_PRE9FPHY
LOGICAL,INTENT(IN)               :: LDALLOC_PRE9PHY
REAL(KIND=JPRB),INTENT(IN)       :: POROG(NPROMA)
REAL(KIND=JPRB),INTENT(IN)       :: PGMV(NPROMA,NFLEVG,NDIMGMV)
REAL(KIND=JPRB),INTENT(IN)       :: PR0(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)       :: PR9(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)       :: PGWFT0(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)       :: PGWFT9(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)       :: PNHPRE0F(NPROMNH,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)       :: PNHPRE9F(NPROMNH,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)       :: PNHPRE0H(NPROMNH,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)       :: PNHPRE9H(NPROMNH,0:NFLEVG)
REAL(KIND=JPRB),INTENT(INOUT)    :: PRE0F(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(INOUT)    :: PRE9F(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(INOUT)    :: PRE0(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB),INTENT(INOUT)    :: PRE9(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB),INTENT(INOUT)    :: PDELP0(NPROMA,0:NFLEVG+1) 
REAL(KIND=JPRB),INTENT(INOUT)    :: PDELP9(NPROMA,0:NFLEVG+1) 
REAL(KIND=JPRB),INTENT(INOUT)    :: PRDELP0(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(INOUT)    :: PRDELP9(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(INOUT)    :: PLNPR0(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(INOUT)    :: PLNPR9(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(INOUT)    :: PALPH0(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(INOUT)    :: PALPH9(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(OUT)      :: PW0(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(OUT)      :: PW9(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(OUT)      :: PRE0F_PHY(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(OUT)      :: PRE9F_PHY(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(OUT)      :: PRE0_PHY(NPROMA,0:NFLEVG)
REAL(KIND=JPRB),INTENT(OUT)      :: PRE9_PHY(NPROMA,0:NFLEVG)
REAL(KIND=JPRB),INTENT(OUT)      :: PREHYD0F_PHY(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(OUT)      :: PREHYD9F_PHY(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(OUT)      :: PREHYD0_PHY(NPROMA,0:NFLEVG)
REAL(KIND=JPRB),INTENT(OUT)      :: PREHYD9_PHY(NPROMA,0:NFLEVG)
REAL(KIND=JPRB),INTENT(OUT)      :: PDELP0_PHY(NPROMA,0:NFLEVG+1) 
REAL(KIND=JPRB),INTENT(OUT)      :: PDELP9_PHY(NPROMA,0:NFLEVG+1) 
REAL(KIND=JPRB),INTENT(OUT)      :: PRDELP0_PHY(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(OUT)      :: PRDELP9_PHY(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(OUT)      :: PLNPR0_PHY(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(OUT)      :: PLNPR9_PHY(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(OUT)      :: PALPH0_PHY(NPROMA,NFLEVG) 
REAL(KIND=JPRB),INTENT(OUT)      :: PALPH9_PHY(NPROMA,NFLEVG) 

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

REAL(KIND=JPRB) :: ZUSG
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "gnhdlrb.intfb.h"

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

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

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

!        1.    Input for AROME physics: transfer "w" and "pre"
!              -----------------------------------------------

! This piece of code was in CPG before CY34.
! Saves "w" and "pre" (input for AROME physics)

ZUSG=1.0_JPRB/RG

PW9(1:NPROMA,1:NFLEVG)=0.0_JPRB
PW0(1:NPROMA,1:NFLEVG)=0.0_JPRB
PRE0F_PHY(1:NPROMA,1:NFLEVG)=0.0_JPRB
PRE0_PHY(1:NPROMA,0:NFLEVG)=0.0_JPRB
IF (LDALLOC_PRE9FPHY) PRE9F_PHY(1:NPROMA,1:NFLEVG)=0.0_JPRB
IF (LDALLOC_PRE9PHY) PRE9_PHY(1:NPROMA,0:NFLEVG)=0.0_JPRB

IF (LMPA) THEN
  IF (LNHDYN.AND.LTWOTL) THEN
    PRE0F_PHY(KST:KEND,1:NFLEVG)=PNHPRE0F(KST:KEND,1:NFLEVG)
    PRE0_PHY(KST:KEND,0:NFLEVG)=PNHPRE0H(KST:KEND,0:NFLEVG)
    PW0(KST:KEND,1:NFLEVG)=PGWFT0(KST:KEND,1:NFLEVG)*ZUSG
  ELSEIF (LNHDYN.AND.(.NOT.LTWOTL)) THEN
    PRE9F_PHY(KST:KEND,1:NFLEVG)=PNHPRE9F(KST:KEND,1:NFLEVG)
    PRE9_PHY(KST:KEND,0:NFLEVG)=PNHPRE9H(KST:KEND,0:NFLEVG)
    PW9(KST:KEND,1:NFLEVG)=PGWFT9(KST:KEND,1:NFLEVG)*ZUSG
  ELSEIF ((.NOT.LNHDYN).AND.LTWOTL) THEN
    PRE0F_PHY(KST:KEND,1:NFLEVG)=PRE0F(KST:KEND,1:NFLEVG)
    PRE0_PHY(KST:KEND,0:NFLEVG)=PRE0(KST:KEND,0:NFLEVG)
    PW0(KST:KEND,1:NFLEVG)=PGWFT0(KST:KEND,1:NFLEVG)*ZUSG
  ELSEIF ((.NOT.LNHDYN).AND.(.NOT.LTWOTL)) THEN
    PRE9F_PHY(KST:KEND,1:NFLEVG)=PRE9F(KST:KEND,1:NFLEVG)
    PRE9_PHY(KST:KEND,0:NFLEVG)=PRE9(KST:KEND,0:NFLEVG)
    PW9(KST:KEND,1:NFLEVG)=PGWFT9(KST:KEND,1:NFLEVG)*ZUSG
  ENDIF
ENDIF

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

!        2.    LRWSDLR=T: restore variables based on "prehyd" 
!              ----------------------------------------------

! If LRWSDLR=T (NH model only),
!  input quantities (computed in GPPREH+GPXYB+GPPREF) are
!  "presim" quantities. MF physics keeps the thin layer assumption for the
!  time being, so hydrostatic pressures quantities must be transformed
!  to provide "prehyd"-based quantities to the physics input.
!  (k.y.: maybe something must be done also for "etadot d prehyd/d eta" and
!  "omega/prehyd" but I don't know how to proceed). 
!  We recall that d(presim)/d(prehyd)=(r**2/a**2)*(G/g).
!  If LRWSDLR=F we do a simple copy.

! "t" variables.
IF (LNHDYN.AND.LRWSDLR) THEN
  CALL GNHDLRB(NPROMA,KST,KEND,.FALSE., &
   & POROG,PGMV(1,1,YT0%MT),PR0,PGMV(1,1,YT0%MSPD), &
   & PRE0F,PRE0,PDELP0(1,1),PRDELP0,PLNPR0,PALPH0, &
   & PREFO=PREHYD0F_PHY,PREHO=PREHYD0_PHY, &
   & PDELPO=PDELP0_PHY(1,1),PRDELPO=PRDELP0_PHY, &
   & PLNPRO=PLNPR0_PHY,PALPHO=PALPH0_PHY)
  ! Useless content but filled for security.
  PDELP0_PHY(KST:KEND,0)=PDELP0(KST:KEND,0)
  PDELP0_PHY(KST:KEND,NFLEVG+1)=PDELP0(KST:KEND,NFLEVG+1)
ELSE
  PREHYD0F_PHY(KST:KEND,1:NFLEVG)=PRE0F(KST:KEND,1:NFLEVG)
  PREHYD0_PHY(KST:KEND,0:NFLEVG)=PRE0(KST:KEND,0:NFLEVG)
  PDELP0_PHY(KST:KEND,0:NFLEVG+1)=PDELP0(KST:KEND,0:NFLEVG+1)
  PRDELP0_PHY(KST:KEND,1:NFLEVG)=PRDELP0(KST:KEND,1:NFLEVG)
  PLNPR0_PHY(KST:KEND,1:NFLEVG)=PLNPR0(KST:KEND,1:NFLEVG)
  PALPH0_PHY(KST:KEND,1:NFLEVG)=PALPH0(KST:KEND,1:NFLEVG)
ENDIF

! "t-dt" variables.
IF (.NOT.LTWOTL) THEN
  IF (LNHDYN.AND.LRWSDLR) THEN
    CALL GNHDLRB(NPROMA,KST,KEND,.FALSE., &
     & POROG,PGMV(1,1,YT9%MT),PR9,PGMV(1,1,YT9%MSPD), &
     & PRE9F,PRE9,PDELP9(1,1),PRDELP9,PLNPR9,PALPH9, &
     & PREFO=PREHYD9F_PHY,PREHO=PREHYD9_PHY, &
     & PDELPO=PDELP9_PHY(1,1),PRDELPO=PRDELP9_PHY, &
     & PLNPRO=PLNPR9_PHY,PALPHO=PALPH9_PHY)
    ! Useless content but filled for security.
    PDELP9_PHY(KST:KEND,0)=PDELP9(KST:KEND,0)
    PDELP9_PHY(KST:KEND,NFLEVG+1)=PDELP9(KST:KEND,NFLEVG+1)
  ELSE
    PREHYD9F_PHY(KST:KEND,1:NFLEVG)=PRE9F(KST:KEND,1:NFLEVG)
    PREHYD9_PHY(KST:KEND,0:NFLEVG)=PRE9(KST:KEND,0:NFLEVG)
    PDELP9_PHY(KST:KEND,0:NFLEVG+1)=PDELP9(KST:KEND,0:NFLEVG+1)
    PRDELP9_PHY(KST:KEND,1:NFLEVG)=PRDELP9(KST:KEND,1:NFLEVG)
    PLNPR9_PHY(KST:KEND,1:NFLEVG)=PLNPR9(KST:KEND,1:NFLEVG)
    PALPH9_PHY(KST:KEND,1:NFLEVG)=PALPH9(KST:KEND,1:NFLEVG)
  ENDIF
ENDIF

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

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