SUBROUTINE SPNH_CONV_PRHS(LDMODEL_TO_FILE)

!* SPNH_CONV_PRHS
!  - Conversion of NH variables (model to file and vice-versa).
!    Part of the conversion relevant if LRWSDLR=T, to convert quantities
!    based on "prehyd" towards quantities based on "presim" (or the reverse
!    one).

! Purpose
! -------
!   Converts:
!    log(prehyds) <-> log(presims)
!    pre-prehyd <-> pre-presim  
!   Conversion also acts on derivatives, but it does not appear in this routine.

! Interface
! ---------
!   LDMODEL_TO_FILE - switch for model to file / file to model conversion

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

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

! Reference
! ---------
!        ECMWF Research Department documentation of the IFS
!        ARPEGE documentation (IDEUL)
!        Wood and Staniforth, 2003 (in QJRMS)

! Author
! ------
!   March-2008 K. Yessad (METEO-FRANCE/CNRM/GMAP)

! Modifications
! -------------
! End Modifications
!------------------------------------------------------------------------------

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

USE YOMCT0   , ONLY : LELAM    ,NPRINTLEV
USE YOMDIM   , ONLY : NFLEVG   ,LSPT 
USE YOMDYNA  , ONLY : LRWSDLR 
USE YOMGEM   , ONLY : NGPTOT
USE YOMLUN   , ONLY : NULOUT

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

IMPLICIT NONE

LOGICAL,INTENT(IN) :: LDMODEL_TO_FILE 

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

REAL(KIND=JPRB) :: ZT     (NGPTOT,NFLEVG)
REAL(KIND=JPRB) :: ZPD    (NGPTOT,NFLEVG)
REAL(KIND=JPRB) :: ZSP    (NGPTOT)
REAL(KIND=JPRB) :: ZQ     (NGPTOT,NFLEVG)
REAL(KIND=JPRB) :: ZL     (NGPTOT,NFLEVG)
REAL(KIND=JPRB) :: ZI     (NGPTOT,NFLEVG)
REAL(KIND=JPRB) :: ZR     (NGPTOT,NFLEVG)
REAL(KIND=JPRB) :: ZS     (NGPTOT,NFLEVG)
REAL(KIND=JPRB) :: ZG     (NGPTOT,NFLEVG)

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"
#include "etransdir_nhconvprhs.intfb.h"
#include "etransinv_nhconvprhs.intfb.h"
#include "gnh_conv_prhs.intfb.h"
#include "transdir_nhconvprhs.intfb.h"
#include "transinv_nhconvprhs.intfb.h"

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

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

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

!* 1. CHECK CONSISTENCY
!     -----------------

IF (NPRINTLEV>=1) THEN
  IF (LDMODEL_TO_FILE) THEN
    WRITE(NULOUT,*)
    WRITE(NULOUT,*) ' ENTER SPNH_CONV_PRHS MODEL TO FILE'
  ELSE
    WRITE(NULOUT,*)
    WRITE(NULOUT,*) ' ENTER SPNH_CONV_PRHS FILE TO MODEL'
  ENDIF
ENDIF

IF ( .NOT.LRWSDLR ) THEN
  WRITE(NULOUT,*) 'LRWSDLR=.F. => SPNH_CONV_PRHS SHOULD NOT BE CALLED!'
  CALL ABOR1('SPNH_CONV_PRHS: ABOR1 CALLED!')
ELSEIF ( .NOT.LSPT ) THEN
  WRITE(NULOUT,*) 'LSPT=.F. => CANNOT TRANSFORM NH VARIABLES!'
  CALL ABOR1('SPNH_CONV_PRHS: ABOR1 CALLED!')
ENDIF

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

!* 2. TRANSFORM SPECTRAL FIELDS TO GRID POINT SPACE
!     ---------------------------------------------

IF (LELAM) THEN
  CALL ETRANSINV_NHCONVPRHS(ZT,ZPD,ZSP,ZQ,ZL,ZI,ZR,ZS,ZG)
ELSE
  CALL TRANSINV_NHCONVPRHS(ZT,ZPD,ZSP,ZQ,ZL,ZI,ZR,ZS,ZG)
ENDIF

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

!* 3. GRID POINT COMPUTATIONS
!     -----------------------

CALL GNH_CONV_PRHS(LDMODEL_TO_FILE,ZT,ZQ,ZL,ZI,ZR,ZS,ZG,ZPD,ZSP)

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

!* 4. TRANSFORM NH FIELDS BACK TO SPECTRAL SPACE
!     ------------------------------------------

IF (LELAM) THEN
  CALL ETRANSDIR_NHCONVPRHS(ZPD,ZSP)
ELSE
  CALL TRANSDIR_NHCONVPRHS(ZPD,ZSP)
ENDIF

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

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

END SUBROUTINE SPNH_CONV_PRHS

