SUBROUTINE CPREP4(CDCONF,LDOVER)

!**** *CPREP4*  - TRANSFORMATION FILE ARPEGE TO FILE ARPEGE

!     PURPOSE.
!     --------
!        To transform a file ARPEGE into an other file ARPEGE, using
!        FULL-POS package

!**   INTERFACE.
!     ----------
!       *CALL* *CPREP4*

!        EXPLICIT ARGUMENTS
!        --------------------
!           CDCONF : configuration of STEPO
!           LDOVER : .TRUE. if post-processing is finished for the current step 

!        IMPLICIT ARGUMENTS
!        --------------------

!     METHOD.
!     -------
!        SEE DOCUMENTATION

!     EXTERNALS.
!     ----------

!     REFERENCE.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     AUTHOR.
!     -------
!        RYAD EL KHATIB *METEO-FRANCE*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 94-04-08
!        99-01-26 R. El Khatib LAM domain to global model ("bogussing") + NFPCT0
!        R. El Khatib : 01-08-07 Pruning options
!    R. El Khatib : 02-21-20 Fullpos B-level distribution + remove IO scheme
!    R. El Khatib : 03-04-17 Fullpos improvemnts
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMLUN   , ONLY : NULOUT   ,NULFPOS  ,NPPPSH
USE YOMCT0   , ONLY : CFPNCF   ,LFPART2  ,LFDBOP   ,LARPEGEF ,NPROC
USE YOMFPC   , ONLY :    NFPINCR
USE YOMCT3   , ONLY : NSTEP
USE YOMOP    , ONLY : LINC     ,NFDBREF  ,CFNPL
USE YOMDYN   , ONLY : TSTEP
USE YOMFPCT0 , ONLY : NFPCT0   ,FPINCR
USE YOMSP    , ONLY : SPA3
USE YOMMP    , ONLY : MYPROC
USE MPL_MODULE
USE FDBSUBS_MOD

IMPLICIT NONE

! * DUMMY:
CHARACTER(LEN=9)  ,INTENT(INOUT) :: CDCONF 
LOGICAL           ,INTENT(OUT)   :: LDOVER 
!     ------------------------------------------------------------------

! * EXTERNAL INTEGER FUNCTIONS:

CHARACTER (LEN = 16) ::  CLLEC

INTEGER(KIND=JPIM) :: IFILE, IINC, INFILE, IOS, ISTAT, ISTEP, IREP, INBARI

LOGICAL :: LLEXIST
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "cnt0.intfb.h"
#include "dynfpos.intfb.h"
#include "freemem.intfb.h"
#include "gridfpos.intfb.h"
#include "stepo.intfb.h"
#include "sualspa.intfb.h"
#include "suinif.intfb.h"

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

!*       1.    HORIZONTAL INTERPOLATIONS
!              -------------------------

!       1.0  CONTROL IF POST-PROCESSING IS OVER

IF (LHOOK) CALL DR_HOOK('CPREP4',0,ZHOOK_HANDLE)
ISTEP=NSTEP
IF (LINC) THEN
  IINC=NINT(REAL(ISTEP,JPRB)*TSTEP/3600._JPRB)
ELSE
  IINC=ISTEP
ENDIF
IF(MYPROC == 1) THEN
  OPEN (UNIT=NULFPOS,FILE=CFPNCF,FORM='FORMATTED')
  REWIND(NULFPOS)
  INFILE=-9999999
  READ(NULFPOS,'(I4.4)',ERR=101,END=101,IOSTAT=IOS) INFILE
  101 CONTINUE
  IF (IOS > 0) THEN
    CALL ABOR1 ('INTERNAL ERROR ON FILE')
  ELSEIF(IOS < 0) THEN
    INFILE=-9999999
  ENDIF
ENDIF
CALL GSTATS(662,0)
IF (NPROC > 1) THEN
  CALL MPL_BROADCAST(INFILE,KROOT=1,KTAG=1000,CDSTRING='CPREP4:')
ENDIF
CALL GSTATS(662,1)
LDOVER=INFILE == IINC
IF(MYPROC == 1) THEN
  CLOSE(UNIT=NULFPOS,STATUS='KEEP')
ENDIF

IF (NFPCT0 /= 0) THEN
  IF (.NOT.LDOVER) THEN

!           1.1  FINISH PREVIOUS STEP AND MAKE RESTART FILE IF NECESSARY

    WRITE(NULOUT,*) ' FIRST PART OF CPREP4 '
    IF (CDCONF(1:1) /= '0'.AND.CDCONF(1:1) /= 'A') THEN
      CDCONF(2:9)='00000000'
      CALL STEPO(CDCONF)
    ENDIF
    IF (LARPEGEF) THEN
      INQUIRE(FILE=CFNPL,EXIST=LLEXIST)
      IF (LLEXIST) THEN
!       Remove file properly
        CLLEC='CADRE.INTERIM'
        CALL FAITOU(IREP,NPPPSH,.TRUE.,CFNPL,'OLD',.TRUE.,.TRUE.,1,1,INBARI,&
         & CLLEC)  
        CALL LFIMST(IREP,NPPPSH,.FALSE.)
        CALL FAIRME(IREP,NPPPSH,'DELETE')
      ENDIF
    ENDIF

!              1.2  HORIZONTAL POST-PROCESSING

    IF (NFPINCR == 1) THEN
!           1st horizontal interpolation with bogussed state (increment=+1.)
      FPINCR=+1.0_JPRB
      WRITE(NULOUT,FMT='('' NFPCT0 = '',I2,'' FPINCR = '',F4.1)')&
       & NFPCT0, FPINCR  
    ENDIF
    CALL GRIDFPOS(.TRUE.,.TRUE.,.FALSE.)
    CALL DYNFPOS(CDCONF,.FALSE.)
    CDCONF(2:9)='00000000'
    CALL STEPO(CDCONF)
    IF (NFPINCR == 1) THEN
!           2nd horizontal interpolation with boundary conditions (increment=-1.)
      NFPCT0=NFPCT0+1
      IF (NFPCT0 == -1) THEN
        FPINCR=-1.0_JPRB
        WRITE(NULOUT,FMT='('' NFPCT0 = '',I2,'' FPINCR = '',&
         & F4.1)') NFPCT0, FPINCR  
        IF (.NOT.ALLOCATED(SPA3)) THEN
          CALL SUALSPA
        ENDIF
        IFILE=9
        CALL SUINIF(IFILE)
        CALL GRIDFPOS(.TRUE.,.TRUE.,.FALSE.)
        CDCONF(1:1)='0'
        CALL DYNFPOS(CDCONF,.FALSE.)
        CDCONF(2:9)='00000000'
        CALL STEPO(CDCONF)
      ELSE
        CALL ABOR1('CPREP4 : INTERNAL ERROR NFPCT0')
      ENDIF
    ENDIF

!              1.3  GO TO OUTPUT GEOMETRY

    IF(LFDBOP) THEN
      CALL GSTATS(1719,0)
      ISTAT=ICLOSEFDBSUBS(NFDBREF)
      CALL GSTATS(1719,1)
    ENDIF
    NFPCT0=0
    LFPART2=NFPCT0 == 0
    CALL FREEMEM
    CALL CNT0
    NFPCT0=1
    LFPART2=NFPCT0 == 0
  ELSE
    CDCONF(1:1)='0'
    IF (LHOOK) CALL DR_HOOK('CPREP4',1,ZHOOK_HANDLE)
    RETURN
  ENDIF
ELSE

!*       2.    VERTICAL INTERPOLATIONS
!              -----------------------

  WRITE(NULOUT,*) ' SECOND PART OF CPREP4 '

!           2.2  VERTICAL POST-PROCESSING

  CALL GRIDFPOS(.TRUE.,.TRUE.,.FALSE.)
  CDCONF(1:1)='0'
  CALL DYNFPOS(CDCONF,.TRUE.)
  CDCONF(2:9)='00000000'
  CALL STEPO(CDCONF)
  CDCONF(1:1)='0'

!           2.3  SIGNAL POST-PROCESSING IS OVER

  IF(MYPROC == 1) THEN
    OPEN (UNIT=NULFPOS,FILE=CFPNCF,FORM='FORMATTED')
    REWIND(NULFPOS)
    ISTEP=NSTEP
    IF (LINC) THEN
      IINC=NINT(REAL(ISTEP,JPRB)*TSTEP/3600._JPRB)
    ELSE
      IINC=ISTEP
    ENDIF
    WRITE(NULFPOS,'(I4.4)') IINC
    CLOSE(UNIT=NULFPOS,STATUS='KEEP')
  ENDIF

ENDIF

!     ------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('CPREP4',1,ZHOOK_HANDLE)
END SUBROUTINE CPREP4
