   !!======================================================================

  SUBROUTINE obc2d_rank_lagoon
#ifdef key_lagoon

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE obc2d_rank_lagoon ***
   !&E
   !&E ** Purpose : Read a sea surface height from an obc file issued from
   !&E              a previous rank - specific for lagoon
   !&E
   !&E ** Description : At open boundaries, the final signal is
   !&E                  - the sea level issued from a previous rank
   !&E
   !&E                  Straight boundaries case
   !&E
   !&E ** Called by : obc2drank.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays :
   !&E
   !&E ** Modified variables : obc_tide_sshw,obc_tide_sshe,
   !&E                         obc_tide_sshs,obc_tide_sshn
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2008-02  (F. Dumas)  Original code
   !&E       !  2009-03 (V. Garnier)  removing of xeuv argument
   !&E                                initialization of obc_tide_ssh*
   !&E                                old obc2drankstraight.F90
   !&E       !  2009-10 (F. Dumas) Introduction of overtide functionality
   !&E       !  2010-05 (F. Vandermeirsch) Interpolation of velocities
   !&E       !          (dirichlet and characteristics methodology
   !&E       !  2010-11 (F. Dumas, S. Theetten) management of child ranks with coarser resolution (imxx,jmxx)
   !&E       !  2011-10 (V. Garnier) Evolution required by the spatial extension of ssh(liminm1:limaxp1,ljminm1:ljmaxp1)
   !&E       !  2011-10 (V. Garnier) Correction of the inconsistent test on the location on cell (child/mother)
   !&E       !  2011-11  (V. Garnier) Spatial extension of ssh(liminm2:limaxp2,ljminm2:ljmaxp2)
   !&E       !  2016-12  (B.Thouvenin, A.Fiandrino) integration of key_lagoon
   !&E
   !&E---------------------------------------------------------------------

#include "toolcpp.h"

   !! * Modules used
   USE parameters, ONLY : valmanq,liminp1m2,limaxm1,limax,ljminp1m2,ljmaxm1, &
                          ljmax,liminp1,ljminp1
   USE comvars2d
   USE comstatic
   USE obccombine, ONLY : obc_tide_sshs,obc_tide_sshn,obc_tide_sshe,obc_tide_sshw, &
                          obc_tide_us,obc_tide_vs,obc_tide_uw,obc_tide_vw,         &
                          obc_tide_un,obc_tide_vn,obc_tide_ue,obc_tide_ve
   USE_MPI toolmpi,ONLY : MASTER

   IMPLICIT NONE

   !! * Arguments

   !! * Local declarations
   INTEGER              :: i,j,l,IERR_MPI
   REAL(kind=rsh)       :: alf,x,h1,h2,hautd,yy
   REAL(kind=rsh),DIMENSION(imxx+jmxx)           :: an,bn,cn,as,bs,cs
   CHARACTER (len=19) :: tool_sectodat,ccdate
   INTEGER              :: ng,i0,j0,kk,eof
   REAL(kind=rsh)       :: heau_cl1,heau_cl2
   REAL(kind=rlg)       :: tool_datosec,tintl1,tintl2,tl1,dtl1
   CHARACTER(len=lchain) :: rec
   CHARACTER(len=lchain) :: ficlheau

   !!----------------------------------------------------------------------
   !! * Executable part

   IF (.NOT. l_tide_surge) THEN
     obc_tide_sshs(:)=0.0_rsh
     obc_tide_sshn(:)=0.0_rsh
     obc_tide_sshe(:)=0.0_rsh
     obc_tide_sshw(:)=0.0_rsh
   ELSE
     filecl=file_surge_meteo
   END IF

   IF(nbouc == 0) THEN
     obc_tide_sshn(:)=valmanq
     obc_tide_sshe(:)=valmanq
     obc_tide_sshs(:)=valmanq
     obc_tide_sshw(:)=valmanq

     obc_tide_un(:,:)=0.0_rsh
     obc_tide_ue(:,:)=0.0_rsh
     obc_tide_us(:,:)=0.0_rsh
     obc_tide_uw(:,:)=0.0_rsh
     
     obc_tide_vn(:,:)=0.0_rsh
     obc_tide_ve(:,:)=0.0_rsh
     obc_tide_vs(:,:)=0.0_rsh
     obc_tide_vw(:,:)=0.0_rsh
          
     print*,'lecture des cl dans le grau 1 seulement'
     ALLOCATE(cl_heau(nvalmax))
!     IF (type_grau(1)==1) ficlheau=file_obc_ogcm_n
!     IF(type_grau(1)==2) ficlheau=file_obc_ogcm_e
!     IF(type_grau(1)==3) ficlheau=file_obc_ogcm_s
!     IF (type_grau(1)==4) ficlheau=file_obc_ogcm_w 
! lagune de Thau : grau sud
     ficlheau=file_obc_ogcm_s
     print*,'Fichier de Cond. limite en hauteur d eau', ficlheau
     
     OPEN(29,file = ficlheau, form='formatted')
     i=1
!     READ(29,*)
!     READ(29,*)
!     READ(29,*)
!     READ(29,*)
           
     READ(29,'(a)',end=999,err=999) rec
     kk=index(rec,'/')-2 
     READ(rec(kk+19:),*) heau_cl1                   
     tintl1 = tool_datosec(rec(kk:kk+18))
     tl1=tchrono(i)
                      
     DO WHILE(tl1 < tintl1 .AND. i<=nvalmax)
       print*,'le debut de simu n est pas couvert par CL heau'
       cl_heau(i) = heau_cl1
       i=i+1
       IF(i<=nvalmax) tl1=tchrono(i)
     END DO   
     
     IF(i<nvalmax) THEN
       eof=0
       DO WHILE(eof==0) 
         READ(29,'(a)',iostat=eof) rec
         IF(eof==0) THEN
           kk=index(rec,'/')-2
           READ(rec(kk+19:),*) heau_cl2

           IF(heau_cl2.gt.-5) THEN
             tintl2 = tool_datosec(rec(kk:kk+18))
             DO WHILE ((tchrono(i)-tintl1)*(tchrono(i)-tintl2) <= 0.d0)
               dtl1=(tchrono(i)-tintl1)/(tintl2-tintl1)
               cl_heau(i) = heau_cl1*(1.d0-dtl1)+heau_cl2*dtl1        
               i=i+1
               if(i > nvalmax) goto 999
             END DO
             tintl1=tintl2
             heau_cl1 = heau_cl2
           ENDIF
         END IF
       END DO
     END IF   
                 
 999 continue 
 
     IF(i < nvalmax) THEN
       IF_MPI (MASTER) THEN
       write(iscreenlog,*) ' '
       write(iscreenlog,*) 'la totalite de la simulation n est pas couverte'
       write(iscreenlog,*) 'par les donnees de CL en heau dans le grau 1  '
       write(iscreenlog,*) 'contenues dans le fichier : ',trim(ficlheau)
       ENDIF_MPI
     END IF
     close(29) 

   END IF

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

 !     deroulement normal

   dtl1=(t-tchrono(indchrono))/(tchrono(indchrono+1)-tchrono(indchrono))
       
   DO ng=1,nbgrau
     i0=igrau(ng)
     j0=jgrau(ng)
     
     
!     xe(i0,j0) = dtl1*cl_heau(indchrono+1)+(1.d0-dtl1)*cl_heau(indchrono)-znmzh 
!     xe(i0,j0-1) = xe(i0,j0)
     
     IF (type_grau(ng)==1) THEN
     !! grau sur la frontiere Nord
       IF_MPI ( (i0 >= liminm2) .AND. (i0 <= limaxp2) .AND.(j0 >= ljminm2) .AND. (j0 <= ljmaxp2) ) THEN
          obc_tide_sshn(i0)= dtl1*cl_heau(indchrono+1)+(1.d0-dtl1)*cl_heau(indchrono) &
                         - hnivmoy(i0,j0)
       ENDIF_MPI 
     END IF

     IF (type_grau(ng)==2) THEN
     !! grau sur la frontiere Est
       IF_MPI ( (i0 >= liminm2) .AND. (i0 <= limaxp2) .AND.(j0 >= ljminm2) .AND. (j0 <= ljmaxp2) ) THEN
          obc_tide_sshe(j0)= dtl1*cl_heau(indchrono+1)+(1.d0-dtl1)*cl_heau(indchrono) &
                         - hnivmoy(i0,j0)
       ENDIF_MPI 
     END IF
     
     IF (type_grau(ng)==3) THEN
     !! grau sur la frontiere Sud
       IF_MPI ( (i0 >= liminm2) .AND. (i0 <= limaxp2) .AND.(j0 >= ljminm2) .AND. (j0 <= ljmaxp2) ) THEN
          obc_tide_sshs(i0)= dtl1*cl_heau(indchrono+1)+(1.d0-dtl1)*cl_heau(indchrono) &
                         - hnivmoy(i0,j0)
       ENDIF_MPI 
     END IF

     IF (type_grau(ng)==4) THEN
     !! grau sur la frontiere Ouest
       IF_MPI ( (i0 >= liminm2) .AND. (i0 <= limaxp2) .AND.(j0 >= ljminm2) .AND. (j0 <= ljmaxp2) ) THEN
          obc_tide_sshw(j0)= dtl1*cl_heau(indchrono+1)+(1.d0-dtl1)*cl_heau(indchrono) &
                         - hnivmoy(i0,j0)
       ENDIF_MPI 
     END IF
   END DO  

#endif
  END SUBROUTINE obc2d_rank_lagoon
