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

  SUBROUTINE obc2d_rank_default

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE obc2d_rank_default ***
!&E
!&E ** Purpose : Read a sea surface height from an obc file issued from
!&E              a previous rank
!&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
!&E---------------------------------------------------------------------





























































!! * 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 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

!!----------------------------------------------------------------------
!! * 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
     mx=ieac-iwac+1
     my=jnac-jsac+1



     IF (mx > imxx+jmxx .OR. my > imxx+jmxx) THEN
       WRITE(ierrorlog,*)' '
       WRITE(ierrorlog,*)'ERROR - obc2d_rank_default'
       WRITE(ierrorlog,*)'Your child rank has a coarser spatial resolution'
       WRITE(ierrorlog,*)'than your mother rank'
       WRITE(ierrorlog,*)'Here, mx = ',mx,' my = ',my
       WRITE(ierrorlog,*)'while imxx and jmxx are set to',imxx,jmxx
       WRITE(ierrorlog,*)'In comstatic.F90, increase imxx and jmxx'
       WRITE(ierrorlog,*)'The simulation is stopped'
       WRITE(ierrorlog,*)' '
       CALL MPI_FINALIZE(IERR_MPI)
       STOP
     END IF

     DO i=1,mx
       xlim(i)=(i-1)*dgp
     END DO
     DO j=1,my
       ylim(j)=(j-1)*dfip
     END DO
     OPEN(13,file = filecl,form='unformatted')
     READ(13) t1,(xe1_n(i),xe1_s(i),u1_n(i),v1_n(i,1),v1_n(i,2),u1_s(i),v1_s(i,1),v1_s(i,2),i=1,mx)
     READ(13)    (xe1_w(j),xe1_e(j),u1_w(1,j),u1_w(2,j),v1_w(j),u1_e(1,j),u1_e(2,j),v1_e(j),j=1,my)

     IF(t < t1) THEN
       WRITE(ierrorlog,*) 'ERROR: In OBC2D_RANK.F90,'
       ccdate=tool_sectodat(t)
       WRITE(ierrorlog,*) 'the simulation starts at ',ccdate
       ccdate=tool_sectodat(t1)
       WRITE(ierrorlog,*) 'before the 1st available SSH condition that is ',ccdate
       WRITE(ierrorlog,*) 'If this is not rank 0, modify the "date_startshift" parameter'
       WRITE(ierrorlog,*) 'or'
       WRITE(ierrorlog,*) 'if l_initfromfile=.true. make sure l_init_rtime is also set to .true.'
       WRITE(ierrorlog,*) 'in the "paraspec.txt" file.'
       WRITE(ierrorlog,*) 'Simulation stopped.'
       STOP
     ENDIF

     ccdate=tool_sectodat(t1)
     IF (MASTER) THEN
     WRITE(iscreenlog,*) ' '
     WRITE(iscreenlog,*) 'OBC2D_RANK.F90:'
     WRITE(iscreenlog,*) 'The 1st open boundary condition is available at: '
     WRITE(iscreenlog,*) 'time = ',t1,' in sec., corresponding to the date: ',ccdate
     ENDIF
     ccdate=tool_sectodat(t)
     IF (MASTER) &
     WRITE(iscreenlog,*) 'The start date of the simulation is: ',ccdate

     READ(13,END=34,err=35)t2,(xe2_n(i),xe2_s(i),u2_n(i),v2_n(i,1),v2_n(i,2),u2_s(i),v2_s(i,1),v2_s(i,2),i=1,mx)
     READ(13)                 (xe2_w(j),xe2_e(j),u2_w(1,j),u2_w(2,j),v2_w(j),u2_e(1,j),u2_e(2,j),v2_e(j),j=1,my)

   END IF

!---------------------------
9 CONTINUE                 !     deroulement normal
   IF(t-t1 <= 0.0_rlg)THEN

     ulim_n(1:mx)=u1_n(1:mx)
     ulim_s(1:mx)=u1_s(1:mx)
     ulim_e(1:2,1:my)=u1_e(1:2,1:my)
     ulim_w(1:2,1:my)=u1_w(1:2,1:my)

     vlim_n(1:mx,1:2)=v1_n(1:mx,1:2)
     vlim_s(1:mx,1:2)=v1_s(1:mx,1:2)
     vlim_e(1:my)=v1_e(1:my)
     vlim_w(1:my)=v1_w(1:my)

     hlim_s(1:mx)=xe1_s(1:mx)
     hlim_n(1:mx)=xe1_n(1:mx)
     hlim_w(1:my)=xe1_w(1:my)
     hlim_e(1:my)=xe1_e(1:my)

   ELSE

     IF(t-t2 <= 0.0_rlg) THEN
       alf=(t2-t)/(t2-t1)

       ulim_s(1:mx)=alf*u1_s(1:mx)+(1.0_rlg-alf)*u2_s(1:mx)
       ulim_n(1:mx)=alf*u1_n(1:mx)+(1.0_rlg-alf)*u2_n(1:mx)
       ulim_w(1:2,1:my)=alf*u1_w(1:2,1:my)+(1.0_rlg-alf)*u2_w(1:2,1:my)
       ulim_e(1:2,1:my)=alf*u1_e(1:2,1:my)+(1.0_rlg-alf)*u2_e(1:2,1:my)

       vlim_s(1:mx,1:2)=alf*v1_s(1:mx,1:2)+(1.0_rlg-alf)*v2_s(1:mx,1:2)
       vlim_n(1:mx,1:2)=alf*v1_n(1:mx,1:2)+(1.0_rlg-alf)*v2_n(1:mx,1:2)
       vlim_w(1:my)=alf*v1_w(1:my)+(1.0_rlg-alf)*v2_w(1:my)
       vlim_e(1:my)=alf*v1_e(1:my)+(1.0_rlg-alf)*v2_e(1:my)

       hlim_s(1:mx)=alf*xe1_s(1:mx)+(1.0_rlg-alf)*xe2_s(1:mx)
       hlim_n(1:mx)=alf*xe1_n(1:mx)+(1.0_rlg-alf)*xe2_n(1:mx)
       hlim_w(1:my)=alf*xe1_w(1:my)+(1.0_rlg-alf)*xe2_w(1:my)
       hlim_e(1:my)=alf*xe1_e(1:my)+(1.0_rlg-alf)*xe2_e(1:my)

     ELSE
       t1=t2

       u1_s(1:mx)=u2_s(1:mx)
       u1_n(1:mx)=u2_n(1:mx)
       u1_w(1:2,1:my)=u2_w(1:2,1:my)
       u1_e(1:2,1:my)=u2_e(1:2,1:my)

       v1_s(1:mx,1:2)=v2_s(1:mx,1:2)
       v1_n(1:mx,1:2)=v2_n(1:mx,1:2)
       v1_w(1:my)=v2_w(1:my)
       v1_e(1:my)=v2_e(1:my)

       xe1_s(1:mx)=xe2_s(1:mx)
       xe1_n(1:mx)=xe2_n(1:mx)
       xe1_w(1:my)=xe2_w(1:my)
       xe1_e(1:my)=xe2_e(1:my)

       READ(13,END=34,err=35)t2,(xe2_n(i),xe2_s(i),u2_n(i),v2_n(i,1),v2_n(i,2),u2_s(i),v2_s(i,1),v2_s(i,2),i=1,mx)
       READ(13)                 (xe2_w(j),xe2_e(j),u2_w(1,j),u2_w(2,j),v2_w(j),u2_e(1,j),u2_e(2,j),v2_e(j),j=1,my)

       GOTO 9
     END IF

   END IF

   DO i=2,mx-1
     as(i)=(hlim_s(i+1)+hlim_s(i-1)-2.0_rsh*hlim_s(i))/(2.0_rlg*dgp*dgp)
     an(i)=(hlim_n(i+1)+hlim_n(i-1)-2.0_rsh*hlim_n(i))/(2.0_rlg*dgp*dgp)
     bs(i)=(hlim_s(i)-hlim_s(i-1))/dgp-as(i)*(xlim(i)+xlim(i-1))
     bn(i)=(hlim_n(i)-hlim_n(i-1))/dgp-an(i)*(xlim(i)+xlim(i-1))
     cs(i)=hlim_s(i)-(as(i)*xlim(i)+bs(i))*xlim(i)
     cn(i)=hlim_n(i)-(an(i)*xlim(i)+bn(i))*xlim(i)
   END DO

   IF (limin==imin .AND. ljmin==jmin) THEN
     obc_tide_sshw(1)=obc_tide_sshw(1)+hlim_s(1)
     obc_tide_sshs(1)=obc_tide_sshs(1)+hlim_s(1)
   ENDIF

   l=2
   DO i=liminp1m2,limaxp2
     x=(i-1)*dgac
     IF(x <= xlim(2))THEN
       obc_tide_sshn(i)=obc_tide_sshn(i)+(an(2)*x+bn(2))*x+cn(2)
       obc_tide_sshs(i)=obc_tide_sshs(i)+(as(2)*x+bs(2))*x+cs(2)
     ELSEIF(x >= xlim(mx-1))THEN
       obc_tide_sshs(i)=obc_tide_sshs(i)+(as(mx-1)*x+bs(mx-1))*x+cs(mx-1)
       obc_tide_sshn(i)=obc_tide_sshn(i)+(an(mx-1)*x+bn(mx-1))*x+cn(mx-1)
     ELSE
       DO WHILE(x > xlim(l+1))
         l=l+1
       END DO
       h1=(an(l  )*x+bn(l  ))*x+cn(l  )
       h2=(an(l+1)*x+bn(l+1))*x+cn(l+1)
       obc_tide_sshn(i)=obc_tide_sshn(i)+(h1*(xlim(l+1)-x)+(x-xlim(l))*h2)/dgp
       h1=(as(l  )*x+bs(l  ))*x+cs(l  )
       h2=(as(l+1)*x+bs(l+1))*x+cs(l+1)
       obc_tide_sshs(i)=obc_tide_sshs(i)+(h1*(xlim(l+1)-x)+(x-xlim(l))*h2)/dgp
     END IF
   END DO

   IF (limax==imax .AND. ljmax==jmax) THEN
     hautd=obc_tide_sshn(imax)
   ENDIF

   DO i=2,my-1
     as(i)=(hlim_w(i+1)+hlim_w(i-1)-2.0_rsh*hlim_w(i))/(2.0_rlg*dfip*dfip)
     an(i)=(hlim_e(i+1)+hlim_e(i-1)-2.0_rsh*hlim_e(i))/(2.0_rlg*dfip*dfip)
     bs(i)=(hlim_w(i)-hlim_w(i-1))/dfip -as(i)*(ylim(i)+ylim(i-1))
     bn(i)=(hlim_e(i)-hlim_e(i-1))/dfip -an(i)*(ylim(i)+ylim(i-1))
     cs(i)=hlim_w(i)-(as(i)*ylim(i)+bs(i))*ylim(i)
     cn(i)=hlim_e(i)-(an(i)*ylim(i)+bn(i))*ylim(i)
   END DO

   l=2
   DO j=ljminp1m2,ljmaxp2
     yy=(j-1)*dfiac
     IF(yy <= ylim(2))THEN
       obc_tide_sshe(j)=obc_tide_sshe(j)+(an(2)*yy+bn(2))*yy+cn(2)
       obc_tide_sshw(j)=obc_tide_sshw(j)+(as(2)*yy+bs(2))*yy+cs(2)
     ELSEIF(yy >= ylim(my-1))THEN
       obc_tide_sshw(j)=obc_tide_sshw(j)+(as(my-1)*yy+bs(my-1))*yy+cs(my-1)
       obc_tide_sshe(j)=obc_tide_sshe(j)+(an(my-1)*yy+bn(my-1))*yy+cn(my-1)
     ELSE
       DO WHILE(yy > ylim(l+1))
       l=l+1
       END DO
       h1=(an(l  )*yy+bn(l  ))*yy+cn(l  )
       h2=(an(l+1)*yy+bn(l+1))*yy+cn(l+1)
       obc_tide_sshe(j)=obc_tide_sshe(j)+(h1*(ylim(l+1)-yy)+(yy-ylim(l))*h2)/dfip

       h1=(as(l  )*yy+bs(l  ))*yy+cs(l  )
       h2=(as(l+1)*yy+bs(l+1))*yy+cs(l+1)
       obc_tide_sshw(j)=obc_tide_sshw(j)+(h1*(ylim(l+1)-yy)+(yy-ylim(l))*h2)/dfip
     END IF
   END DO

   IF (limax==imax .AND. ljmax==jmax) THEN
     obc_tide_sshe(jmax)=0.5*(obc_tide_sshe(jmax)+hautd)
     obc_tide_sshn(imax)=obc_tide_sshe(jmax)
   ENDIF

   IF (ljmax==jmax) THEN
     WHERE(h0(liminp1m2:limaxp2,jmax)==-valmanq) obc_tide_sshn(liminp1m2:limaxp2)=valmanq
   ENDIF
   IF (ljmin==jmin) THEN
     WHERE(h0(liminp1m2:limaxp2,jmin+1)==-valmanq) obc_tide_sshs(liminp1m2:limaxp2)=valmanq
   ENDIF
   IF (limax==imax) THEN
     WHERE(h0(imax,ljminp1m2:ljmaxp2)==-valmanq) obc_tide_sshe(ljminp1m2:ljmaxp2)=valmanq
   ENDIF
   IF (limin==imin) THEN
     WHERE(h0(imin+1,ljminp1m2:ljmaxp2)==-valmanq) obc_tide_sshw(ljminp1m2:ljmaxp2)=valmanq
   ENDIF

! linear interpolation of velocities

   idh=2
 
   DO i=liminp1,limaxm1
     x=(i-0.5)*dgac
     IF(x  > xlim(idh)+.5_rsh*dgp)THEN
!DO WHILE (x >= xlim(idh)+.5_rsh*dgp.AND. idh.LE.mx) ! to make sure seq and mpi choose the same jhh when x=ylim
       DO WHILE (x > xlim(idh)+.5_rsh*dgp.AND. idh.LE.mx)
        idh=idh+1
       END DO
     END IF
     obc_tide_un(i,jmax)=  ((x-xlim(idh-1)-.5_rsh*dgp)*ulim_n(idh)+(xlim(idh)+.5_rsh*dgp-x)*ulim_n(idh-1))/dgp
     obc_tide_us(i,jmin+1)=((x-xlim(idh-1)-.5_rsh*dgp)*ulim_s(idh)+(xlim(idh)+.5_rsh*dgp-x)*ulim_s(idh-1))/dgp
   END DO

   idh=2
   DO i=liminp1,limaxm1
     x=(i-1)*dgac
     IF(x  > xlim(idh))THEN
!DO WHILE (x >= xlim(idh).AND. idh.LE.mx)
       DO WHILE (x > xlim(idh).AND. idh.LE.mx) ! to make sure seq and mpi choose the same jhh when x=ylim
        idh=idh+1
       END DO
     END IF
     obc_tide_vn(i,jmax-1:jmax)=((x-xlim(idh-1))*vlim_n(idh,1:2)+(xlim(idh)-x)*vlim_n(idh-1,1:2))/dgp
     obc_tide_vs(i,jmin:jmin+1)=((x-xlim(idh-1))*vlim_s(idh,1:2)+(xlim(idh)-x)*vlim_s(idh-1,1:2))/dgp
   END DO

   jhh=2
   DO j=ljminp1,ljmaxm1
     x=(j-0.5)*dfiac
     IF(x  > ylim(jhh)+.5_rsh*dfip)THEN
!DO WHILE (x >= ylim(jhh)+.5_rsh*dfip.AND. jhh.LE.my)
       DO WHILE (x > ylim(jhh)+.5_rsh*dfip.AND. jhh.LE.my) ! to make sure seq and mpi choose the same jhh when x=ylim
        jhh=jhh+1
       END DO
     END IF
     obc_tide_ve(imax,j)  =((x-ylim(jhh-1)-.5_rsh*dfip)*vlim_e(jhh)+(ylim(jhh)+.5_rsh*dfip-x)*vlim_e(jhh-1))/dfip
     obc_tide_vw(imin+1,j)=((x-ylim(jhh-1)-.5_rsh*dfip)*vlim_w(jhh)+(ylim(jhh)+.5_rsh*dfip-x)*vlim_w(jhh-1))/dfip
   END DO

   jhh=2
   DO j=ljminp1,ljmaxm1
     x=(j-1)*dfiac
     IF(x  > ylim(jhh))THEN
!DO WHILE (x >= ylim(jhh).AND. jhh.LE.my ) ! to make sure seq and mpi choose the same jhh when x=ylim
       DO WHILE (x > ylim(jhh).AND. jhh.LE.my )
        jhh=jhh+1
       END DO
     END IF
     obc_tide_ue(imax-1:imax,j)=((x-ylim(jhh-1))*ulim_e(1:2,jhh)+(ylim(jhh)-x)*ulim_e(1:2,jhh-1))/dfip
     obc_tide_uw(imin:imin+1,j)=((x-ylim(jhh-1))*ulim_w(1:2,jhh)+(ylim(jhh)-x)*ulim_w(1:2,jhh-1))/dfip
   END DO


   RETURN
34 WRITE(ierrorlog,*) 'ERROR: In OBC2D_RANK.F90,'
   WRITE(ierrorlog,*) 'The open boundary file (from previous rank) is not long enough.'
   WRITE(ierrorlog,*) 'Simulation stopped.'
   STOP
35 WRITE(ierrorlog,*) 'ERROR: In OBC2D_RANK.F90,'
   WRITE(ierrorlog,*) 'Problem while reading the 2nd open boundary file (from previous rank).'
   WRITE(ierrorlog,*) 'Simulation stopped.'
   STOP

  END SUBROUTINE obc2d_rank_default
