subroutine sump0(kulout,kulnam)

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMCT0   , ONLY : NCONF    ,NPROC    ,&
 & NPRINTLEV,LFDBOP   ,LALLOPR  ,LWRTCFOU 
USE YOMCT0B  , ONLY : LECMWF
USE YOMMP    , ONLY : NPRCIDS  ,LSPLIT    ,&
 & LSPLITOUT,NGATHOUT,NWRTOUT,NBLKOUT,&
 & NSTRIN   ,NSTROUT  ,NFLDIN   ,&
 & NFLDOUT  ,NSLPAD   ,NINTYPE  ,NOUTTYPE ,&
 & NUMLL    ,NPSURF   ,LEQ_REGIONS ,&
 & NCOMBFLEN,LOCKIO   ,LSYNC_SLCOM ,LSYNC_TRANS  
USE YOMOMPDIST , ONLY : LOMPDIST
USE YOMIO    , ONLY : NPPBUFLEN,NPPFILES ,LPPTSF
USE YOMMASK  , ONLY : LSLONDEM, LSLONDEM_ACTIVE
USE YOMCPG   , ONLY : LCPG_SPLIT

!**** *sump0*   - Sets up distributed memory variables - part 0

!     Purpose.
!     --------
!           Define basic distributed memory variables

!**   Interface.
!     ----------
!        *call* *sump0a

!        explicit arguments :
!        --------------------

!        implicit arguments :
!        --------------------

!     method.
!     -------
!        see documentation

!     externals.
!     ----------

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

!     author.
!     -------
!        MPP Group *ECMWF*

!     modifications.
!     --------------
!        original : 95-10-01
!        V. Saravanne : 98-01-14, default value for LSPLITIN and LSPLITOUT
!                                                   NSTRIN and NPROC
!        Modified 99-02-17 by K. YESSAD: options LRETCFOU, LWRTCFOU.
!        A. Bogatchev 03-04-13 : Removing protection of aladin LSPLIT 
!                                after G.R. b-level work
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      01-Dec-2003 CY28R1 Cleaning
!        R. El Khatib  11-May-2007 LSLONDEM=.T. as default
!        Y. Seity      31-Aug-2007 LSYNC_SLCOM LSYNC_TRANS
!     ------------------------------------------------------------------

IMPLICIT NONE

! * DUMMY:
INTEGER(KIND=JPIM),INTENT(IN)    :: KULOUT 
INTEGER(KIND=JPIM),INTENT(IN)    :: KULNAM 
!     ------------------------------------------------------------------

INTEGER(KIND=JPIM), EXTERNAL :: iinitfdb_vpp

INTEGER(KIND=JPIM) :: istat, iu

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

#include "abor1.intfb.h"
#include "posnam.intfb.h"

!     ------------------------------------------------------------------
#include "nampar1.h"
!     ------------------------------------------------------------------

!     read NAMPAR1 namelist 
!     --------------------

IF (LHOOK) CALL DR_HOOK('SUMP0',0,ZHOOK_HANDLE)
if(nconf/100 == 1) then
  lsplit=.false.
else
  IF (LWRTCFOU) THEN
    LSPLIT=.FALSE.
  ELSE
    LSPLIT=.TRUE.
  ENDIF
endif
LSLONDEM=.TRUE.
lslondem_active=.false.
lsplitout=.false.
IF ( lecmwf ) THEN
  leq_regions=.true.
ELSE
  leq_regions=.false.
ENDIF
ngathout=1
if(lfdbop) then
  nwrtout=1
else
  nwrtout=nproc
endif
nblkout=1
lockio=.true.
lpptsf=.true.
nslpad=0
nintype=1
nouttype=-1
nfldin=0
nfldout=0
if ( lecmwf ) then
  nstrin=1
  nstrout=0
else
  nstrin=nproc
  nstrout=1
endif

! set ncombflen (in words) to a value less than the mailbox size (in bytes).
! recommend ncombflen=VPP_MBX_SIZE*0.9/NREALEN
! e.g. VPP_MBX_SIZE  NCOMBFLEN
!      16,000,000    1,800,000
!      64,000,000    7,200,000

ncombflen=1800000
nppbuflen=100000
nppfiles=0

lompdist=.false.
lsync_slcom=.false.
lsync_trans=.false.

call posnam(kulnam,'NAMPAR1')
read(kulnam,nampar1)

! * Test that LSPLIT=.F. when using option LWRTCFOU with DM.
IF (LWRTCFOU.AND.LSPLIT) THEN
  WRITE(KULOUT,'(1X,A,A)') ' SUMP0: when LWRTCFOU=T ,'&
   & ,' LSPLIT must be equal to .FALSE. .'  
  WRITE(KULOUT,'(1X,A)') '        Relaunch job with LSPLIT = .FALSE.'
  CALL FLUSH(KULOUT)
  CALL ABOR1(' SUMP0: ABOR1 CALLED')
ENDIF

if( nouttype > 0) then
  if(nouttype == 2) then
    lfdbop=.true.
  else
    lfdbop=.false.
  endif
else
  if(lfdbop) then
    nouttype=2
    lsplitout=.true.
  else
    nouttype=1
  endif
endif
if(.not.lsplitout) nwrtout=nproc
if( nslpad < 0 ) call abor1(' nslpad must be >=0 ')

!     Initialise FDB if required

if(nproc > 1.and.lfdbop) then
  istat=iinitfdb_vpp('fdb')
  if(istat /= 0) then
    write(0,'(a,i4)') 'error from iinitfdb_vpp ',istat
    call abor1('error from iinitfdb_vpp ')
  endif
endif

!     Print shape and size of arrays allocated in sualmp0
!     this is first possible now when NULOUT (kulout) is defined.

llp = nprintlev >= 1.or. lallopr
iu = kulout
write(iu,*) ' OUTPUT FROM SUMP0'
if(llp)write(iu,9) 'NUMLL    ',size(numll    ),shape(numll    )
if(llp)write(iu,9) 'NPSURF   ',size(npsurf   ),shape(npsurf   )
if(llp)write(iu,9) 'NPRCIDS  ',size(nprcids  ),shape(nprcids  )
9 format(1X,'ARRAY ',A10,' ALLOCATED ',8I8)

write(iu,'('' NCOMBFLEN  '',I12)') ncombflen
write(iu,'('' LSYNC_SLCOM  '',L2)') lsync_slcom
write(iu,'('' LSYNC_TRANS  '',L2)') lsync_trans
write(iu,'('' LOMPDIST = '',L2,'' LSLONDEM = '',L2)')LOMPDIST,LSLONDEM
write(iu,'('' NOUTTYPE = '',I2,'' NSTRIN = '',I2,'' NSTROUT = '',I2)') &
 & NOUTTYPE,NSTRIN,NSTROUT  
write(iu,'('' NFLDIN = '',I2,'' NFLDOUT = '',I2)') NFLDIN,NFLDOUT
write(iu,*)' LSPLITOUT = ',LSPLITOUT,' NGATHOUT = ',NGATHOUT,&
 & ' NWRTOUT = ',NWRTOUT,' NBLKOUT = ',NBLKOUT  

IF (LHOOK) CALL DR_HOOK('SUMP0',1,ZHOOK_HANDLE)
end subroutine sump0
