SUBROUTINE SIPC_INIT_MODEL(KMODEL,KINFO)

!**** *SIPC_Init_Model* 

!     Purpose:
!     -------
!     - Attaches model to shared memory segment pool used to exchange 
!       initial infos.

!**   Interface:
!     ---------
!       *CALL*  *SIPC_Init_Model(1, kinfo)*

!     Input:
!     -----
!                1        : kmodel=1 remote model (integer)

!     Output:
!     ------
!                kinfo    : error status

!     Workspace:
!     ---------
!     None

!     Externals:
!     ---------    
!     sleep, svipc_open,  
!     Reference:
!     ---------

!     History:
!     -------
!       Programmer     Date      Description
!       ----------     ----      -----------  
!       S. Valcke      97/08/12  created
!       S. Valcke      98/06/02  adapted to F90 for arpege

! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE PAR_COU     ! Module with number of field exchanged
USE PAR_SIPC    ! Module with number of byte per real, integer, ...
USE YOM_INPC    ! Module with handles of shared memory pools
USE YOM_OAS     ! Module with communication technique, 
                ! experiment and model names
USE YOMLUN   , ONLY : NULOUT

!* ---------------------------- Argument declarations -------------------

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KMODEL
INTEGER(KIND=JPIM),INTENT(OUT)   :: KINFO 
!* ---------------------------- Local declarations ----------------------

INTEGER(KIND=JPIM) :: ic, icumul, imrc
CHARACTER(len=7) :: clmodnamr, clmodnamw
LOGICAL :: lldummy
REAL(KIND=JPRB) :: ZHOOK_HANDLE


!* ---------------------------- Poema verses ----------------------------

! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

!*    1. Initializations
!        ---------------

IF (LHOOK) CALL DR_HOOK('SIPC_INIT_MODEL',0,ZHOOK_HANDLE)
WRITE (NULOUT,*) ' '
WRITE (NULOUT,*) ' Entering  ROUTINE SIPC_Init_Model'
WRITE (NULOUT,*) ' from modele ', cpmodnam
WRITE (NULOUT,*) ' '

!* Zeroes error codes

icumul = 0

!*   2. IF dummy FILE 'DUMMY_file' exists, attach to the shared-memory pool 
!       used to communicate initial infos between coupler and remote model
!       ------------------------------------------------------------------

kinfo = 0
lldummy = .FALSE.
ic = 0
DO WHILE ( .not. lldummy )
  INQUIRE(FILE='DUMMY_SIPC', EXIST=lldummy)
  CALL sleep(2)
  ic=ic+1
  IF(ic == jptest) THEN
    lldummy = .TRUE.
    kinfo = -1
  ENDIF
ENDDO

IF (kinfo == 0) THEN

! Attach to pool for infos written by the model, READ by oasis. 

  imrc = 0
  clmodnamr = cpmodnam//'R'
  CALL svipc_open(mpoolinitr, clmodnamr, 0, imrc)

!* Find error if any

  IF (imrc < 0) THEN
    WRITE (NULOUT,*) 'Problem in attaching to the pool',&
     & clmodnamr   
    icumul = icumul + 1
  ELSE
    WRITE (NULOUT,*) 'Pool for initial infos read by oasis'
    WRITE (NULOUT,*) 'is attached to ', cpmodnam
  ENDIF

! Attach to pool of one segment for infos READ by model, written by oasis

  imrc = 0
  clmodnamw = cpmodnam//'W'
  CALL svipc_open(mpoolinitw, clmodnamw, 0, imrc)

!* Find error if any

  IF (imrc < 0) THEN
    WRITE (NULOUT,*) 'Problem in attaching to the pool ',&
     & clmodnamw   
    icumul = icumul + 1
  ELSE
    WRITE (NULOUT,*) 'Pool for initial infos written by oasis'
    WRITE (NULOUT,*) 'is attached to ',  cpmodnam
  ENDIF

ENDIF

!* Change error status if any error occurs

IF (icumul /= 0) kinfo = icumul
IF (kinfo < 0) kinfo = 2

!*    4. End of routine
!        --------------

WRITE (NULOUT,*) ' '
WRITE (NULOUT,*) &
 & '          --------- End of routine SIPC_Init_Model ---------'  
CALL FLUSH(NULOUT)
IF (LHOOK) CALL DR_HOOK('SIPC_INIT_MODEL',1,ZHOOK_HANDLE)
END SUBROUTINE SIPC_INIT_MODEL
