SUBROUTINE PPOPEN(CDLFN)

!**** *PPOPEN*  - Open file for post processing

!     Purpose.
!     --------
!     Open file for post processing

!        Explicit arguments :     CDLFN - file name to open
!        --------------------

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

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

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

!     Author.
!     -------
!        George Mozdzynski *ECMWF*

!     Modifications.
!     --------------
!       Original : 97-10-14 
!       Modified : 00-12-08 G.Mozdzynski: MPE to MPL conversion
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      01-Dec-2003 CY28R1 Cleaning

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

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

USE YOMMP    , ONLY : MYPROC   ,NSTROUT  ,NOUTTYPE
USE YOMIO    , ONLY : NPPFILES ,LPPTSF   ,NPPBUFL  ,NPPBUFH  ,&
 & NPPBUFS  ,NPPBUF2  ,NPPBUFA  ,CPPLFN   ,NPPOUTTYPE  
USE YOMCT0   , ONLY : CNMEXP
USE YOMLUN   , ONLY : NULOUT
USE MPL_MODULE

IMPLICIT NONE

CHARACTER(LEN=*)  ,INTENT(IN)    :: CDLFN 
CHARACTER (LEN = 24) ::  CLFN

INTEGER(KIND=JPIM) :: IBUF, IERR, IND, IOP, IU, J

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

#include "abor1.intfb.h"

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

!     RETURN IF NON OUTPUT PROCESSOR

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

IF( MYPROC <= NSTROUT ) THEN

!     TRUNCATE FILENAME IF REQUIRED

  IF( LPPTSF )THEN
    CLFN=CDLFN
  ELSE
    IND=INDEX(CDLFN,CNMEXP(1:4))
    IF( IND == 0 )THEN
      WRITE(NULOUT,'("PPOPEN: INVALID FILENAME - ",A)') CDLFN
      CALL ABOR1('PPOPEN: INVALID FILENAME')
    ENDIF
    CLFN=CDLFN(1:IND+3)
  ENDIF

!     CHECK IF FILE ALREADY OPEN

  LLOPEN=.FALSE.
  DO J=1,NPPFILES
    IF( NPPBUFS(J) == 1.AND. CLFN == CPPLFN(J) )THEN
      LLOPEN=.TRUE.
      EXIT
    ENDIF
  ENDDO

!     IF FILE NOT OPEN, OPEN IT, OTHERWISE RETURN

  IF( .NOT.LLOPEN )THEN

!       FIRST FIND AN UNUSED BUFFER

    IBUF=0
    DO J=1,NPPFILES
      IF( NPPBUFS(J) == 0 )THEN
        IBUF=J
        EXIT
      ENDIF
    ENDDO
    IF( IBUF == 0 )THEN
      WRITE(NULOUT,'("PPOPEN: OUT OF PP FILE BUFFERS, NPPFILES",&
       & " TOO SMALL - ",I2)')NPPFILES  
      CALL ABOR1('PPOPEN: OUT OF PP FILE BUFFERS')
    ENDIF
    IF( NOUTTYPE == 1 )THEN
      IF( MYPROC == 1 )THEN
        CALL PBOPEN(IU,CLFN,'w',IERR)
        IF( IERR /= 0 )THEN
          WRITE(NULOUT,'("PPOPEN: PROBLEM WITH PBOPEN OF FILE - ",&
           & A," IERR=",I6)')CLFN,IERR  
          CALL ABOR1('PPOPEN: PROBLEM WITH PBOPEN')
        ENDIF
      ENDIF
    ELSEIF( NOUTTYPE >= 3.AND. NOUTTYPE <= 6 )THEN
      IOP=2
      CALL MPL_OPEN(IU,IOP,CLFN(1:LEN_TRIM(CLFN)),IERR)
      IF( IERR /= 0 )THEN
        WRITE(NULOUT,'("PPOPEN: PROBLEM WITH MPL_OPEN OF FILE - ",&
         & A," IERR=",I6)')CLFN,IERR  
        CALL ABOR1('PPOPEN: PROBLEM WITH MPL_OPEN')
      ENDIF
    ELSE
      CALL ABOR1('PPOPEN: NOUTTYPE INVALID')
    ENDIF

!       INITIALISE BUFFER ITEMS

    CPPLFN(IBUF)=CLFN
    NPPOUTTYPE(IBUF)=NOUTTYPE
    NPPBUFL(IBUF)=0
    NPPBUFH(IBUF)=IU
    NPPBUFS(IBUF)=1
    NPPBUF2(IBUF)=1
    NPPBUFA(:,:,IBUF)=0
  ENDIF
ENDIF

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

END SUBROUTINE PPOPEN
