  MODULE obccombine

!!======================================================================
!!                   ***  MODULE  obccombine  ***
!! Open boundary management : Combine different sources of open boundary fields.
!!
!!   !  2007-03  (F. Vandermeirsch, V. Garnier) Review of initialisation of obc_uw,e,n,s
!!                                              for the use of Dirichlet and characteristics operators
!!
!!======================================================================





























































!! * Modules used
   USE parameters

   IMPLICIT NONE
   PRIVATE

!! * Accessibility
   PUBLIC obc_combine                  ! routine called by step3dxy.F90 step3dyx.F90
! step2dxy.F90 step2dyx.F90
   PUBLIC alloc_obc,dealloc_obc        ! routine called by alloc.F90

!! * Shared or public module variables
   LOGICAL,PUBLIC               ::    &   ! .true. if existence of open boundary
             l_obc_south, l_obc_north,    &
             l_obc_east,l_obc_west

   LOGICAL,PUBLIC               ::    &   ! to choose the method to be applied
             l_obc_mars,l_obc_diri,l_obc_char,l_obc_cycl

   LOGICAL,PUBLIC               ::    &   ! if .true. use of tidal or/and ogcm boundary conditions
             l_obc_tide,l_obc_ogcm

   LOGICAL,PUBLIC               ::    &   ! .true. if reading of boundary conditions
             l_obc_ogcm_rssh,l_obc_ogcm_rt,l_obc_ogcm_rs,  &   ! for ssh, temperature or salinity fields
             l_obc_ogcm_rc,l_obc_ogcm_ruv,l_obc_ogcm_ruvz      ! for tracers, barotropic and total velocity fields

   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:) ::                        &
             obc_sshn,obc_sshs
   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:) ::                        &
             obc_sshe,obc_sshw

   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:,:) :: &
             obc_uzs,obc_vzs              ! southern open boundary fields (3D)
   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:) :: &
             obc_us,obc_vs                ! southern open boundary fields (2D)

   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:,:) :: &
             obc_uzw,obc_vzw              ! western open boundary fields (3D)
   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:) :: &
             obc_uw,obc_vw                ! western open boundary fields (2D)


   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:,:) :: &
             obc_uzn,obc_vzn              ! northern open boundary fields (3D)
   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:) :: &
             obc_un,obc_vn                ! northern open boundary fields (2D)

   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:,:) :: &
             obc_uze,obc_vze              ! eastern open boundary fields (3D)
   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:) :: &
             obc_ue,obc_ve                ! eastern open boundary fields (2D)

! ogcm data

   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:) ::                        &
             obc_ogcm_sshn,obc_ogcm_sshs
   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:) ::                        &
             obc_ogcm_sshe,obc_ogcm_sshw

   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:,:) :: &
             obc_ogcm_uzs,obc_ogcm_vzs              ! southern open boundary fields (3D)
   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:) :: &
             obc_ogcm_us,obc_ogcm_vs                ! southern open boundary fields (2D)

   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:,:) :: &
             obc_ogcm_uzw,obc_ogcm_vzw              ! western open boundary fields (3D)
   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:) :: &
             obc_ogcm_uw,obc_ogcm_vw                ! western open boundary fields (2D)


   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:,:) :: &
             obc_ogcm_uzn,obc_ogcm_vzn              ! northern open boundary fields (3D)
   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:) :: &
             obc_ogcm_un,obc_ogcm_vn                ! northern open boundary fields (2D)

   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:,:) :: &
             obc_ogcm_uze,obc_ogcm_vze              ! eastern open boundary fields (3D)
   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:) :: &
             obc_ogcm_ue,obc_ogcm_ve                ! eastern open boundary fields (2D)


! tide data

   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:) ::                        &
             obc_tide_sshn,obc_tide_sshs
   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:) ::                        &
             obc_tide_sshe,obc_tide_sshw

   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:) :: &
             obc_tide_us,obc_tide_vs                ! southern open boundary tidal current

   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:) :: &
             obc_tide_uw,obc_tide_vw                ! western open boundary tidal current

   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:) :: &
             obc_tide_un,obc_tide_vn                ! northern open boundary tidal current

   REAL(kind=rsh), PUBLIC, ALLOCATABLE, DIMENSION(:,:) :: &
             obc_tide_ue,obc_tide_ve                ! eastern open boundary tidal current

  CONTAINS

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

  SUBROUTINE obc_combine(xe)

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE obc_combine  ***
!&E
!&E ** Purpose : Combine ogcm and tide open boundaries
!&E
!&E ** Description : Add obc_ogcm_* and obc_tide_* if required
!&E
!&E ** Called by : initxe.F90,step2dxy.F90,step2dyx.F90,step3dxy.F90,step3dyx.F90
!&E
!&E ** External calls :
!&E
!&E ** Used ij-arrays :
!&E
!&E ** Modified variables : obc_sshw,obc_uw,obc_uzw,obc_vw,obc_vzw
!&E                         obc_sshe,obc_ue,obc_uze,obc_ve,obc_vze
!&E                         obc_sshs,obc_us,obc_uzs,obc_vs,obc_vzs
!&E                         obc_sshn,obc_un,obc_uzn,obc_vn,obc_vzn
!&E
!&E ** Reference :
!&E
!&E ** History :
!&E       !  2009-03  (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---------------------------------------------------------------------
!! * Modules used
   USE comvars2d, ONLY : l_modele2d

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

!! * Local declarations
   REAL(kind=rsh),DIMENSION(liminm2:limaxp2) :: coef_south,coef_north
   REAL(kind=rsh),DIMENSION(ljminm2:ljmaxp2) :: coef_west,coef_east
   INTEGER                                   :: k

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

! western boundary
   IF ( l_obc_west ) THEN

    IF (limin==imin) THEN

! tide
     IF ( l_obc_tide .AND. .NOT.l_obc_ogcm ) THEN
       obc_sshw(ljminm2:ljmaxp2) = obc_tide_sshw(ljminm2:ljmaxp2)
       obc_uw(imin:imin+1,ljmin:ljmax) = obc_tide_uw(imin:imin+1,ljmin:ljmax)
       obc_vw(imin+1,ljmin:ljmax) = obc_tide_vw(imin+1,ljmin:ljmax)
       IF ( .NOT. l_modele2d) THEN
         DO k=1,kmax
           obc_uzw(k,imin,ljmin:ljmax) = obc_tide_uw(imin,ljmin:ljmax)
           obc_vzw(k,imin+1,ljmin:ljmax) = obc_tide_vw(imin+1,ljmin:ljmax)
         END DO
       END IF
     END IF

! OGCM
     IF ( l_obc_ogcm .AND. l_obc_ogcm_rssh .AND. .NOT.l_obc_tide ) THEN
       coef_west(ljminm2:ljmaxp2)=1.0_rsh
       obc_sshw(ljminm2:ljmaxp2) = coef_west(ljminm2:ljmaxp2)*obc_ogcm_sshw(ljminm2:ljmaxp2)+ &
                                 (1.0_rsh-coef_west(ljminm2:ljmaxp2))*xe(imin+2,ljminm2:ljmaxp2)
     END IF
     IF ( l_obc_ogcm .AND. (l_obc_ogcm_ruvz .OR. l_obc_ogcm_ruv) .AND. .NOT.l_obc_tide ) THEN
       obc_uw(imin:imin+obc_width,ljmin:ljmax) = obc_ogcm_uw(imin:imin+obc_width,ljmin:ljmax)
       obc_vw(imin:imin+obc_width,ljmin:ljmax) = obc_ogcm_vw(imin:imin+obc_width,ljmin:ljmax)
       IF ( .NOT. l_modele2d) THEN
         obc_uzw(:,imin:imin+obc_width,ljmin:ljmax) = obc_ogcm_uzw(:,imin:imin+obc_width,ljmin:ljmax)
         obc_vzw(:,imin:imin+obc_width,ljmin:ljmax) = obc_ogcm_vzw(:,imin:imin+obc_width,ljmin:ljmax)
       END IF
     END IF

! OGCM + tide
     IF ( l_obc_ogcm .AND. l_obc_tide ) THEN
       IF (l_obc_ogcm_rssh) THEN
         coef_west(ljminm2:ljmaxp2)=1.0_rsh
         obc_sshw(ljminm2:ljmaxp2) = obc_tide_sshw(ljminm2:ljmaxp2) + &
                                     coef_west(ljminm2:ljmaxp2)*obc_ogcm_sshw(ljminm2:ljmaxp2)+ &
                                     (1.0_rsh-coef_west(ljminm2:ljmaxp2))*xe(imin+2,ljminm2:ljmaxp2)
       ELSE
         obc_sshw(ljminm2:ljmaxp2) = obc_tide_sshw(ljminm2:ljmaxp2)
       END IF
       IF ( l_obc_ogcm_ruvz .OR. l_obc_ogcm_ruv ) THEN
         obc_uw(imin:imin+1,ljmin:ljmax) = obc_tide_uw(imin:imin+1,ljmin:ljmax) + obc_ogcm_uw(imin:imin+1,ljmin:ljmax)
         obc_vw(imin+1,ljmin:ljmax) = obc_tide_vw(imin+1,ljmin:ljmax) + obc_ogcm_vw(imin+1,ljmin:ljmax)
         IF ( .NOT. l_modele2d) THEN
           DO k=1,kmax
             obc_uzw(k,imin  ,ljmin:ljmax) = obc_tide_uw(imin  ,ljmin:ljmax) + obc_ogcm_uzw(k,imin  ,ljmin:ljmax)
             obc_vzw(k,imin+1,ljmin:ljmax) = obc_tide_vw(imin+1,ljmin:ljmax) + obc_ogcm_vzw(k,imin+1,ljmin:ljmax)
           END DO
         END IF
       ELSE
         obc_uw(imin:imin+1,ljmin:ljmax) = obc_tide_uw(imin:imin+1,ljmin:ljmax)
         obc_vw(imin+1,ljmin:ljmax) = obc_tide_vw(imin+1,ljmin:ljmax)
         IF ( .NOT. l_modele2d) THEN
           DO k=1,kmax
             obc_uzw(k,imin  ,ljmin:ljmax) = obc_tide_uw(imin  ,ljmin:ljmax)
             obc_vzw(k,imin+1,ljmin:ljmax) = obc_tide_vw(imin+1,ljmin:ljmax)
           END DO
         END IF
       END IF
     END IF
    ENDIF
   END IF

! eastern boundary
   IF ( l_obc_east ) THEN

    IF (limax==imax) THEN

! tide
     IF ( l_obc_tide .AND. .NOT.l_obc_ogcm ) THEN
       obc_sshe(ljminm2:ljmaxp2) = obc_tide_sshe(ljminm2:ljmaxp2)
       obc_ue(imax-1:imax,ljmin:ljmax) = obc_tide_ue(imax-1:imax,ljmin:ljmax)
       obc_ve(imax,ljmin:ljmax) = obc_tide_ve(imax,ljmin:ljmax)
       IF ( .NOT. l_modele2d) THEN
         DO k=1,kmax
           obc_uze(k,imax,ljmin:ljmax) = obc_tide_ue(imax,ljmin:ljmax)
           obc_vze(k,imax,ljmin:ljmax) = obc_tide_ve(imax,ljmin:ljmax)
         END DO
       END IF
     END IF

! OGCM
     IF (  l_obc_ogcm .AND. l_obc_ogcm_rssh .AND. .NOT.l_obc_tide ) THEN
       coef_east(ljminm2:ljmaxp2)=1.0_rsh
       obc_sshe(ljminm2:ljmaxp2) = coef_east(ljminm2:ljmaxp2)*obc_ogcm_sshe(ljminm2:ljmaxp2)+ &
                                   (1.0_rsh-coef_east(ljminm2:ljmaxp2))*xe(imax-1,ljminm2:ljmaxp2)
     END IF
     IF ( l_obc_ogcm .AND. (l_obc_ogcm_ruvz .OR. l_obc_ogcm_ruv) .AND. .NOT.l_obc_tide ) THEN
       obc_ue(imax-obc_width+1:imax,ljmin:ljmax) = obc_ogcm_ue(imax-obc_width+1:imax,ljmin:ljmax)
       obc_ve(imax-obc_width+1:imax,ljmin:ljmax) = obc_ogcm_ve(imax-obc_width+1:imax,ljmin:ljmax)
       IF ( .NOT. l_modele2d) THEN
         obc_uze(:,imax-obc_width+1:imax,ljmin:ljmax) = obc_ogcm_uze(:,imax-obc_width+1:imax,ljmin:ljmax)
         obc_vze(:,imax-obc_width+1:imax,ljmin:ljmax) = obc_ogcm_vze(:,imax-obc_width+1:imax,ljmin:ljmax)
       END IF
     END IF

! OGCM + tide
     IF ( l_obc_ogcm .AND. l_obc_tide ) THEN
       IF ( l_obc_ogcm_rssh ) THEN
         coef_east(ljminm2:ljmaxp2)=1.0_rsh
         obc_sshe(ljminm2:ljmaxp2) = obc_tide_sshe(ljminm2:ljmaxp2) + &
                                   coef_east(ljminm2:ljmaxp2)*obc_ogcm_sshe(ljminm2:ljmaxp2)+ &
                                   (1.0_rsh-coef_east(ljminm2:ljmaxp2))*xe(imax-1,ljminm2:ljmaxp2)
       ELSE
         obc_sshe(ljminm2:ljmaxp2) = obc_tide_sshe(ljminm2:ljmaxp2)
       END IF
       IF ( l_obc_ogcm_ruvz .OR. l_obc_ogcm_ruv ) THEN
         obc_ue(imax-1:imax,ljmin:ljmax) = obc_tide_ue(imax-1:imax,ljmin:ljmax) + obc_ogcm_ue(imax-1:imax,ljmin:ljmax)
         obc_ve(imax,ljmin:ljmax) = obc_tide_ve(imax,ljmin:ljmax) + obc_ogcm_ve(imax,ljmin:ljmax)
         IF ( .NOT. l_modele2d) THEN
           DO k=1,kmax
             obc_uze(k,imax,ljmin:ljmax) = obc_tide_ue(imax,ljmin:ljmax) + obc_ogcm_uze(k,imax,ljmin:ljmax)
             obc_vze(k,imax,ljmin:ljmax) = obc_tide_ve(imax,ljmin:ljmax) + obc_ogcm_vze(k,imax,ljmin:ljmax)
           END DO
         END IF
       ELSE
         obc_ue(imax-1:imax,ljmin:ljmax) = obc_tide_ue(imax-1:imax,ljmin:ljmax)
         obc_ve(imax,ljmin:ljmax) = obc_tide_ve(imax,ljmin:ljmax)
         IF ( .NOT. l_modele2d) THEN
           DO k=1,kmax
             obc_uze(k,imax,ljmin:ljmax) = obc_tide_ue(imax,ljmin:ljmax)
             obc_vze(k,imax,ljmin:ljmax) = obc_tide_ve(imax,ljmin:ljmax)
           END DO
         END IF
       END IF
     END IF

    ENDIF
   END IF

! southern boundary
   IF ( l_obc_south ) THEN

    IF (ljmin==jmin) THEN
! tide
     IF ( l_obc_tide .AND. .NOT.l_obc_ogcm ) THEN
       obc_sshs(liminm2:limaxp2) = obc_tide_sshs(liminm2:limaxp2)
       obc_us(limin:limax,jmin+1) = obc_tide_us(limin:limax,jmin+1)
       obc_vs(limin:limax,jmin:jmin+1) = obc_tide_vs(limin:limax,jmin:jmin+1)
       IF ( .NOT. l_modele2d) THEN
         DO k=1,kmax
           obc_uzs(k,limin:limax,jmin+1) = obc_tide_us(limin:limax,jmin+1)
           obc_vzs(k,limin:limax,jmin  ) = obc_tide_vs(limin:limax,jmin  )
         END DO
       END IF
     END IF

! OGCM
     IF ( l_obc_ogcm .AND. l_obc_ogcm_rssh .AND. .NOT.l_obc_tide ) THEN
       coef_south(liminm2:limaxp2)=1.0_rsh
       obc_sshs(liminm2:limaxp2) = coef_south(liminm2:limaxp2)*obc_ogcm_sshs(liminm2:limaxp2)+ &
                                 (1.0_rsh-coef_south(liminm2:limaxp2))*xe(liminm2:limaxp2,jmin+2)
     END IF
     IF ( l_obc_ogcm .AND. (l_obc_ogcm_ruvz .OR. l_obc_ogcm_ruv) .AND. .NOT.l_obc_tide ) THEN
       obc_us(limin:limax,jmin:jmin+obc_width) = obc_ogcm_us(limin:limax,jmin:jmin+obc_width)
       obc_vs(limin:limax,jmin:jmin+obc_width) = obc_ogcm_vs(limin:limax,jmin:jmin+obc_width)
       IF ( .NOT. l_modele2d) THEN
         obc_uzs(:,limin:limax,jmin:jmin+obc_width) = obc_ogcm_uzs(:,limin:limax,jmin:jmin+obc_width)
         obc_vzs(:,limin:limax,jmin:jmin+obc_width) = obc_ogcm_vzs(:,limin:limax,jmin:jmin+obc_width)
       END IF
     END IF

! OGCM + tide
     IF ( l_obc_ogcm .AND. l_obc_tide ) THEN
       IF ( l_obc_ogcm_rssh ) THEN
         coef_south(liminm2:limaxp2)=1.0_rsh
         obc_sshs(liminm2:limaxp2) = obc_tide_sshs(liminm2:limaxp2) + &
                                   coef_south(liminm2:limaxp2)*obc_ogcm_sshs(liminm2:limaxp2)+ &
                                   (1.0_rsh-coef_south(liminm2:limaxp2))*xe(liminm2:limaxp2,jmin+2)
       ELSE
         obc_sshs(liminm2:limaxp2) = obc_tide_sshs(liminm2:limaxp2)
       END IF
       IF ( l_obc_ogcm_ruvz .OR. l_obc_ogcm_ruv ) THEN
         obc_us(limin:limax,jmin+1) = obc_tide_us(limin:limax,jmin+1) + obc_ogcm_us(limin:limax,jmin+1)
         obc_vs(limin:limax,jmin:jmin+1) = obc_tide_vs(limin:limax,jmin:jmin+1) + obc_ogcm_vs(limin:limax,jmin:jmin+1)
         IF ( .NOT. l_modele2d) THEN
           DO k=1,kmax
             obc_uzs(k,limin:limax,jmin+1) = obc_tide_us(limin:limax,jmin+1) + obc_ogcm_uzs(k,limin:limax,jmin+1)
             obc_vzs(k,limin:limax,jmin  ) = obc_tide_vs(limin:limax,jmin  ) + obc_ogcm_vzs(k,limin:limax,jmin  )
           END DO
         END IF
       ELSE
         obc_us(limin:limax,jmin+1) = obc_tide_us(limin:limax,jmin+1)
         obc_vs(limin:limax,jmin:jmin+1) = obc_tide_vs(limin:limax,jmin:jmin+1)
         IF ( .NOT. l_modele2d) THEN
           DO k=1,kmax
             obc_uzs(k,limin:limax,jmin+1) = obc_tide_us(limin:limax,jmin+1)
             obc_vzs(k,limin:limax,jmin  ) = obc_tide_vs(limin:limax,jmin  )
           END DO
         END IF
       END IF
     END IF

    ENDIF
   END IF


! northern boundary
   IF ( l_obc_north ) THEN

    IF (ljmax==jmax) THEN

! tide
     IF ( l_obc_tide .AND. .NOT.l_obc_ogcm ) THEN
       obc_sshn(liminm2:limaxp2) = obc_tide_sshn(liminm2:limaxp2)
       obc_un(limin:limax,jmax) = obc_tide_un(limin:limax,jmax)
       obc_vn(limin:limax,jmax-1:jmax) = obc_tide_vn(limin:limax,jmax-1:jmax)
       IF ( .NOT. l_modele2d) THEN
         DO k=1,kmax
           obc_uzn(k,limin:limax,jmax) = obc_tide_un(limin:limax,jmax)
           obc_vzn(k,limin:limax,jmax) = obc_tide_vn(limin:limax,jmax)
         END DO
       END IF
     END IF

! OGCM
     IF ( l_obc_ogcm .AND. l_obc_ogcm_rssh .AND. .NOT.l_obc_tide ) THEN
       coef_north(liminm2:limaxp2)=1.0_rsh
       obc_sshn(liminm2:limaxp2) = coef_north(liminm2:limaxp2)*obc_ogcm_sshn(liminm2:limaxp2)+ &
                                 (1.0_rsh-coef_north(liminm2:limaxp2))*xe(liminm2:limaxp2,jmax-1)
     END IF
     IF ( l_obc_ogcm .AND. (l_obc_ogcm_ruvz .OR. l_obc_ogcm_ruv) .AND. .NOT.l_obc_tide ) THEN
       obc_un(limin:limax,jmax-obc_width+1:jmax) = obc_ogcm_un(limin:limax,jmax-obc_width+1:jmax)
       obc_vn(limin:limax,jmax-obc_width+1:jmax) = obc_ogcm_vn(limin:limax,jmax-obc_width+1:jmax)
       IF ( .NOT. l_modele2d) THEN
         obc_uzn(:,limin:limax,jmax-obc_width+1:jmax) = obc_ogcm_uzn(:,limin:limax,jmax-obc_width+1:jmax)
         obc_vzn(:,limin:limax,jmax-obc_width+1:jmax) = obc_ogcm_vzn(:,limin:limax,jmax-obc_width+1:jmax)
       END IF
     END IF

! OGCM + tide
     IF ( l_obc_ogcm .AND. l_obc_tide ) THEN
       IF ( l_obc_ogcm_rssh ) THEN
         coef_north(liminm2:limaxp2)=1.0_rsh
         obc_sshn(liminm2:limaxp2) = obc_tide_sshn(liminm2:limaxp2) + &
                                   coef_north(liminm2:limaxp2)*obc_ogcm_sshn(liminm2:limaxp2)+ &
                                   (1.0_rsh-coef_north(liminm2:limaxp2))*xe(liminm2:limaxp2,jmax-1)
       ELSE
         obc_sshn(liminm2:limaxp2) = obc_tide_sshn(liminm2:limaxp2)
       END IF
       IF ( l_obc_ogcm_ruvz .OR. l_obc_ogcm_ruv ) THEN
         obc_un(limin:limax,jmax) = obc_tide_un(limin:limax,jmax) + obc_ogcm_un(limin:limax,jmax)
         obc_vn(limin:limax,jmax-1:jmax) = obc_tide_vn(limin:limax,jmax-1:jmax) + obc_ogcm_vn(limin:limax,jmax-1:jmax)
         IF ( .NOT. l_modele2d) THEN
           DO k=1,kmax
             obc_uzn(k,limin:limax,jmax) = obc_tide_un(limin:limax,jmax) + obc_ogcm_uzn(k,limin:limax,jmax)
             obc_vzn(k,limin:limax,jmax) = obc_tide_vn(limin:limax,jmax) + obc_ogcm_vzn(k,limin:limax,jmax)
           END DO
         END IF
       ELSE
         obc_un(limin:limax,jmax) = obc_tide_un(limin:limax,jmax)
         obc_vn(limin:limax,jmax-1:jmax) = obc_tide_vn(limin:limax,jmax-1:jmax)
         IF ( .NOT. l_modele2d) THEN
           DO k=1,kmax
             obc_uzn(k,limin:limax,jmax) = obc_tide_un(limin:limax,jmax)
             obc_vzn(k,limin:limax,jmax) = obc_tide_vn(limin:limax,jmax)
           END DO
         END IF
       END IF
     END IF
    ENDIF
   END IF

  END SUBROUTINE obc_combine

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

  SUBROUTINE alloc_obc

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE alloc_obc  ***
!&E
!&E ** Purpose : Allocate obc arrays
!&E
!&E ** Description :
!&E
!&E ** Called by : alloc.F90
!&E
!&E ** External calls :
!&E
!&E ** Used ij-arrays :
!&E
!&E ** Modified variables : obc_sshw,obc_uw,obc_uzw,obc_vw,obc_vzw
!&E                         obc_sshe,obc_ue,obc_uze,obc_ve,obc_vze
!&E                         obc_sshs,obc_us,obc_uzs,obc_vs,obc_vzs
!&E                         obc_sshn,obc_un,obc_uzn,obc_vn,obc_vzn
!&E
!&E ** Reference :
!&E
!&E ** History :
!&E       !  2010-05  (V. Garnier)  Original code
!&E
!&E---------------------------------------------------------------------
!! * Modules used
   USE comvars2d, ONLY : l_modele2d

   ALLOCATE( obc_ogcm_sshn(liminm2:limaxp2),obc_ogcm_sshs(liminm2:limaxp2) )
   ALLOCATE( obc_ogcm_sshe(ljminm2:ljmaxp2),obc_ogcm_sshw(ljminm2:ljmaxp2) )
   ALLOCATE( obc_ogcm_us(limin:limax,jmin:jmin+obc_width+jshift_obc_south), &
             obc_ogcm_vs(limin:limax,jmin:jmin+obc_width+jshift_obc_south))
   ALLOCATE( obc_ogcm_un(limin:limax,jmax-obc_width-jshift_obc_north+1:jmax), &
             obc_ogcm_vn(limin:limax,jmax-obc_width-jshift_obc_north+1:jmax))
   ALLOCATE( obc_ogcm_uw(imin:imin+obc_width+ishift_obc_west,ljmin:ljmax), &
             obc_ogcm_vw(imin:imin+obc_width+ishift_obc_west,ljmin:ljmax))
   ALLOCATE( obc_ogcm_ue(imax-obc_width-ishift_obc_east+1:imax,ljmin:ljmax), &
             obc_ogcm_ve(imax-obc_width-ishift_obc_east+1:imax,ljmin:ljmax))
   ALLOCATE( obc_sshn(liminm2:limaxp2),obc_sshs(liminm2:limaxp2) )
   ALLOCATE( obc_sshe(ljminm2:ljmaxp2),obc_sshw(ljminm2:ljmaxp2) )
   ALLOCATE( obc_us(limin:limax,jmin:jmin+obc_width+jshift_obc_south), &
             obc_vs(limin:limax,jmin:jmin+obc_width+jshift_obc_south))
   ALLOCATE( obc_un(limin:limax,jmax-obc_width-jshift_obc_north+1:jmax), &
             obc_vn(limin:limax,jmax-obc_width-jshift_obc_north+1:jmax))
   ALLOCATE( obc_uw(imin:imin+obc_width+ishift_obc_west,ljmin:ljmax), &
             obc_vw(imin:imin+obc_width+ishift_obc_west,ljmin:ljmax))
   ALLOCATE( obc_ue(imax-obc_width-ishift_obc_east+1:imax,ljmin:ljmax), &
             obc_ve(imax-obc_width-ishift_obc_east+1:imax,ljmin:ljmax))
   ALLOCATE( obc_tide_sshn(liminm2:limaxp2),obc_tide_sshs(liminm2:limaxp2) )
   ALLOCATE( obc_tide_sshe(ljminm2:ljmaxp2),obc_tide_sshw(ljminm2:ljmaxp2) )
   ALLOCATE( obc_tide_us(limin:limax,jmin  :jmin+obc_width+jshift_obc_south), &
             obc_tide_vs(limin:limax,jmin  :jmin+obc_width+jshift_obc_south))
   ALLOCATE( obc_tide_un(limin:limax,jmax-obc_width-jshift_obc_north+1:jmax), &
             obc_tide_vn(limin:limax,jmax-obc_width-jshift_obc_north+1:jmax))
   ALLOCATE( obc_tide_uw(imin  :imin+obc_width+ishift_obc_west,ljmin:ljmax), &
             obc_tide_vw(imin  :imin+obc_width+ishift_obc_west,ljmin:ljmax))
   ALLOCATE( obc_tide_ue(imax-obc_width-ishift_obc_east+1:imax,ljmin:ljmax), &
             obc_tide_ve(imax-obc_width-ishift_obc_east+1:imax,ljmin:ljmax))

   IF (.NOT. l_modele2d) THEN
     ALLOCATE( obc_ogcm_uzs(kmax,limin:limax,jmin:jmin+obc_width+jshift_obc_south), &
               obc_ogcm_vzs(kmax,limin:limax,jmin:jmin+obc_width+jshift_obc_south))
     ALLOCATE( obc_ogcm_uzw(kmax,imin:imin+obc_width+ishift_obc_west,ljmin:ljmax), &
               obc_ogcm_vzw(kmax,imin:imin+obc_width+ishift_obc_west,ljmin:ljmax))
     ALLOCATE( obc_ogcm_uzn(kmax,limin:limax,jmax-obc_width-jshift_obc_north+1:jmax), &
               obc_ogcm_vzn(kmax,limin:limax,jmax-obc_width-jshift_obc_north+1:jmax))
     ALLOCATE( obc_ogcm_uze(kmax,imax-obc_width-ishift_obc_east+1:imax,ljmin:ljmax), &
               obc_ogcm_vze(kmax,imax-obc_width-ishift_obc_east+1:imax,ljmin:ljmax))
     ALLOCATE( obc_uzs(kmax,limin:limax,jmin:jmin+obc_width+jshift_obc_south), &
               obc_vzs(kmax,limin:limax,jmin:jmin+obc_width+jshift_obc_south))
     ALLOCATE( obc_uzw(kmax,imin:imin+obc_width+ishift_obc_west,ljmin:ljmax), &
               obc_vzw(kmax,imin:imin+obc_width+ishift_obc_west,ljmin:ljmax))
     ALLOCATE( obc_uzn(kmax,limin:limax,jmax-obc_width-jshift_obc_north+1:jmax), &
               obc_vzn(kmax,limin:limax,jmax-obc_width-jshift_obc_north+1:jmax))
     ALLOCATE( obc_uze(kmax,imax-obc_width-ishift_obc_east+1:imax,ljmin:ljmax), &
               obc_vze(kmax,imax-obc_width-ishift_obc_east+1:imax,ljmin:ljmax))
   END IF

    obc_tide_sshn(:)=0.0_rsh ; obc_ogcm_sshn(:)=0.0_rsh ; obc_sshn(:)=0.0_rsh
    obc_tide_sshs(:)=0.0_rsh ; obc_ogcm_sshs(:)=0.0_rsh ; obc_sshs(:)=0.0_rsh
    obc_tide_sshe(:)=0.0_rsh ; obc_ogcm_sshe(:)=0.0_rsh ; obc_sshe(:)=0.0_rsh
    obc_tide_sshw(:)=0.0_rsh ; obc_ogcm_sshw(:)=0.0_rsh ; obc_sshw(:)=0.0_rsh
    obc_tide_un(:,:)=0.0_rsh ; obc_ogcm_un(:,:)=0.0_rsh ; obc_un(:,:)=0.0_rsh
    obc_tide_us(:,:)=0.0_rsh ; obc_ogcm_us(:,:)=0.0_rsh ; obc_us(:,:)=0.0_rsh
    obc_tide_ue(:,:)=0.0_rsh ; obc_ogcm_ue(:,:)=0.0_rsh ; obc_ue(:,:)=0.0_rsh
    obc_tide_uw(:,:)=0.0_rsh ; obc_ogcm_uw(:,:)=0.0_rsh ; obc_uw(:,:)=0.0_rsh
    obc_tide_vn(:,:)=0.0_rsh ; obc_ogcm_vn(:,:)=0.0_rsh ; obc_vn(:,:)=0.0_rsh
    obc_tide_vs(:,:)=0.0_rsh ; obc_ogcm_vs(:,:)=0.0_rsh ; obc_vs(:,:)=0.0_rsh
    obc_tide_ve(:,:)=0.0_rsh ; obc_ogcm_ve(:,:)=0.0_rsh ; obc_ve(:,:)=0.0_rsh
    obc_tide_vw(:,:)=0.0_rsh ; obc_ogcm_vw(:,:)=0.0_rsh ; obc_vw(:,:)=0.0_rsh

  END SUBROUTINE alloc_obc

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

  SUBROUTINE dealloc_obc

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE dealloc_obc  ***
!&E
!&E ** Purpose : Deallocate obc arrays
!&E
!&E ** Description :
!&E
!&E ** Called by : alloc.F90
!&E
!&E ** External calls :
!&E
!&E ** Used ij-arrays :
!&E
!&E ** Modified variables : obc_sshw,obc_uw,obc_uzw,obc_vw,obc_vzw
!&E                         obc_sshe,obc_ue,obc_uze,obc_ve,obc_vze
!&E                         obc_sshs,obc_us,obc_uzs,obc_vs,obc_vzs
!&E                         obc_sshn,obc_un,obc_uzn,obc_vn,obc_vzn
!&E
!&E ** Reference :
!&E
!&E ** History :
!&E       !  2010-05  (V. Garnier)  Original code
!&E
!&E---------------------------------------------------------------------
!! * Modules used
   USE comvars2d, ONLY : l_modele2d

   DEALLOCATE( obc_ogcm_sshn,obc_ogcm_sshs,obc_ogcm_sshe,obc_ogcm_sshw )
   DEALLOCATE( obc_ogcm_us,obc_ogcm_vs,obc_ogcm_un,obc_ogcm_vn)
   DEALLOCATE( obc_ogcm_uw,obc_ogcm_vw,obc_ogcm_ue,obc_ogcm_ve)
   DEALLOCATE( obc_sshn,obc_sshs,obc_sshe,obc_sshw )
   DEALLOCATE( obc_us,obc_vs,obc_un,obc_vn,obc_uw,obc_vw,obc_ue,obc_ve)
   DEALLOCATE( obc_tide_sshn,obc_tide_sshs,obc_tide_sshe,obc_tide_sshw )
   DEALLOCATE( obc_tide_us,obc_tide_vs,obc_tide_un,obc_tide_vn)
   DEALLOCATE( obc_tide_uw,obc_tide_vw,obc_tide_ue,obc_tide_ve)

   IF (.NOT. l_modele2d) THEN
     DEALLOCATE( obc_ogcm_uzs,obc_ogcm_vzs,obc_ogcm_uzw,obc_ogcm_vzw)
     DEALLOCATE( obc_ogcm_uzn,obc_ogcm_vzn,obc_ogcm_uze,obc_ogcm_vze)
     DEALLOCATE( obc_uzs,obc_vzs,obc_uzw,obc_vzw,obc_uzn,obc_vzn,obc_uze,obc_vze)
   END IF

  END SUBROUTINE dealloc_obc

END MODULE obccombine
