 MODULE obcapplyssh

!!======================================================================
!!                   ***  MODULE  obcapplyssh  ***
!! Ocean dynamics:  apply sea surface height open boundary conditions
!!======================================================================





























































!! * Modules used
   USE obccombine, ONLY : obc_sshs,obc_sshn,obc_sshe,obc_sshw,              &
                          l_obc_west,l_obc_east,l_obc_north,l_obc_south

   IMPLICIT NONE
   PRIVATE

!! * Accessibility
   PUBLIC obc_apply_ssh                ! routine called by stepu*.F90 stepv*.F90

!! * Interface
   INTERFACE obc_apply_ssh

     MODULE PROCEDURE obc_apply_ssh_ref 

   END INTERFACE

!! * Shared module variables

!! * Private variables

 CONTAINS
!!======================================================================


   SUBROUTINE obc_apply_ssh_ref(xe)

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE obc_apply_ssh  ***
!&E
!&E ** Purpose : save sea surface height open boundary into xe
!&E
!&E ** Description : open boundary sea surface heights (combination of ogcm !and tide)
!&E                  are saved in variables obc_sshs
!&E
!&E ** Called by :
!&E
!&E ** External calls :
!&E
!&E ** Reference :
!&E
!&E ** History :
!&E       !  2008-04 (V. Garnier) Original code
!&E       !  2009-05  (F. Dumas)  AGRIF
!&E       !  2011-11  (V. Garnier) Spatial extension of ssh(liminm2:limaxp2,ljminm2:ljmaxp2)
!&E
!&E ** RQ : this routine can not be parallelized at all until xe is not in dyn3dy.F90
!&E
!&E---------------------------------------------------------------------

!! * Modules used
   USE parameters, ONLY : grav,imin,imax,jmin,jmax,liminp1,limax,ljminp1,ljmax, &
                          limin,liminm2,limaxp2,ljmin,ljminm2,ljmaxp2,rsh,valmanq, &
                          ishift_obc_west,ishift_obc_east,jshift_obc_south,jshift_obc_north
   USE comvars2d,  ONLY : h0,ig,id,jb,jh


!! * Declaration Subroutine

!! * Arguments
   REAL(KIND=rsh),DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2),INTENT( out )   :: xe

!! * Local declarations
   INTEGER :: i,j

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

! western boundary

   IF ( l_obc_west ) THEN
     DO j=ljminm2,ljmaxp2
       IF (j >= 7 .and. j<= 161) then
       i=ig(j)
!print*,"j,ig=",j,i
!IF (i==imin+ishift_obc_west .AND. limin<=i .AND. i<=limax) THEN
       IF (i==imin+31 .AND. limin<=i .AND. i<=limax) THEN
       IF (h0(i,j)/=-valmanq) THEN
         xe(i,j) = obc_sshw(j)
!if (i==31 .and. j==100) print*,"xe west",j,i,h0(i,j),xe(i,j)

       END IF
       ENDIF
      ENDIF
!IF (j>160) THEN
!i=ig(j)
!IF (h0(i,j)/=-valmanq) print*,"xe>160",j,i,h0(i,j),xe(i,j)
!ENDIF
     END DO
   ENDIF

! eastern boundary
   IF ( l_obc_east ) THEN
     DO j=ljminm2,ljmaxp2
       i=id(j)
       IF (i==imax-ishift_obc_east .AND. limin<=i .AND. i<=limax) THEN
       IF  (h0(i,j)/=-valmanq) THEN
         xe(i,j) = obc_sshe(j)

       END IF
       ENDIF
     END DO
   ENDIF

! southern boundary
   IF ( l_obc_south ) THEN
     DO i=liminm2,limaxp2
       j=jb(i)
       IF (j==jmin+1+jshift_obc_south .AND. ljmin<=j .AND. j<=ljmax) THEN
       IF (h0(i,j)/=-valmanq) THEN
         xe(i,j) = obc_sshs(i)

       END IF
       ENDIF
     END DO
   ENDIF

! northern boundary
   IF ( l_obc_north ) THEN
     DO i=liminm2,limaxp2
       j=jh(i)
       IF (j==jmax-jshift_obc_north .AND. ljmin<=j .AND. j<=ljmax) THEN
       IF (h0(i,j)/=-valmanq) THEN
         xe(i,j) = obc_sshn(i)

       END IF
       ENDIF
     END DO
   ENDIF

   END SUBROUTINE obc_apply_ssh_ref


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


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

 END MODULE obcapplyssh
