!****************************************************************
!
      subroutine tide_saveobcns(xe2,u,v,obc_numr)
!
!****************************************************************
!
!     objet    : ecriture d un fichier de conditions aux limites
!                en niveau, vitesse pour le rang suivant
!
!     appelant         : main
!                      :
!     appeles          :
!     canal d ecriture : 23
!     canal de lecture :
!     cle cpp          : key_tide_saverotatedobc
!                      :
!     variables in     : x, u, v, obc_numr
!                      :
!     variables out    :
!                      :
!     variables inout  :
!
! Commentaire V. Garnier
! Attention, les inde doivent etre definis a chaque pas de temps
!                                          et a partir de u et v
! Ne jamais tester a partir de hx et hy : imaginons une digue decouvrante,
! en ce point xe=-h0 et sera tres different de xe=(xe(j+1)+xe(j-1))/2

!&E       !  2008-08 (P. Karleskind) simultaneous saving of obc for different child grids
!&E       !  2010-05 (F. Vandermeirch) Saving of two rows or columns for velocity variables
!***********************************************************
!
USE parameters
use comvars2d
use comsubstance
use comvars3d
implicit none
!
integer                                      :: i,ii,i1,i2
integer                                      :: obc_numr,ie,iw,js,jn,obc_ind23
integer,dimension(imin:imax)                 :: indn
real(kind=riosh),dimension(imin:imax)        :: xe_n,xe_s,u_n,u_s
real(kind=riosh),dimension(imin:imax,1:2)    :: v_n,v_s
real(kind=rsh),dimension(imin:imax,jmin:jmax):: xe2,x,u,v
character(len=lchain) :: fileclsvr
!
!************************************************************
!     Executable statements
!************************************************************

      where(h0_g(:,:)/=-valmanq) x(:,:)=xe2(:,:)
      where(h0_g(:,:)==-valmanq) x(:,:)=mslshift

! create and open fileclsv_nameconf
      obc_ind23=100*obc_numr+23        ! index for writing in fileclsv_nameconf
! add nameconf and suffix to fileclsv
      fileclsvr=trim(fileclsv)//trim(obcrk_nameconf(obc_numr))//'.'//trim(suffix)
      if(nbouc == 1) open(obc_ind23,file = fileclsvr,form='unformatted')

      xe_n(:) = rg_valmanq_io
      xe_s(:) = rg_valmanq_io
      u_n(:) = 0.0_riosh
      u_s(:) = 0.0_riosh
      v_n(:,:) = 0.0_riosh
      v_s(:,:) = 0.0_riosh
      indn(:) = 0

! save indexes of obc for each child grid
      ie=obcrk_ie(obc_numr)
      iw=obcrk_iw(obc_numr)
      js=obcrk_js(obc_numr)
      jn=obcrk_jn(obc_numr)


      do i=iw,ie
       if( u(i-1,jn)==0.0_rsh .and. u(i,jn)==0.0_rsh .and. v(i,jn-1)==0.0_rsh ) then
         indn(i)=0
       else
         indn(i)=1
       endif
      enddo

      do i=iw,ie
       if(indn(i) == 0)then
        if(indn(i-1) == 0)then
         if(indn(i+1) == 0)then
          xe_n(i)=xe_n(i-1)
          u_n(i)=0.0_riosh
          v_n(i,2)=0.0_riosh
         else
          if(indn(i+2) == 0)then
           xe_n(i)=x(i+1,jn)
           u_n(i)=0.0_riosh
           v_n(i,2)=(v(i+1,jn)+v(i+1,jn-1))*0.5_riosh
          else
           xe_n(i)=2.0_riosh*x(i+1,jn)-x(i+2,jn)
           u_n(i)= -0.5_riosh*u(i+1,jn)
           v_n(i,2)=v(i+1,jn)+v(i+1,jn-1)-0.5_riosh*(v(i+2,jn)+v(i+2,jn-1))
          endif
         endif
        else
         if(indn(i+1) == 0)then
          if(indn(i-2) == 0)then
           xe_n(i)=x(i-1,jn)
           u_n(i)=0.0_riosh
           v_n(i,2)=(v(i-1,jn)+v(i-1,jn-1))*0.5_riosh
          else
           xe_n(i)=2.0_riosh*x(i-1,jn)-x(i-2,jn)
           u_n(i)=-0.5_riosh*u(i-2,jn)
           v_n(i,2)=v(i-1,jn)+v(i-1,jn-1)-0.5_riosh*(v(i-2,jn)+v(i-2,jn-1))
          endif
         else
          xe_n(i)=0.5_riosh*(x(i-1,jn)+x(i+1,jn))
          u_n(i)=-0.25_riosh*(u(i-1,jn)+u(i+1,jn))
          v_n(i,2)=0.25_riosh*(v(i-1,jn)+v(i-1,jn-1)+v(i+1,jn)+v(i+1,jn-1))
         endif
        endif
       else
        xe_n(i)=x(i,jn)
        u_n(i)=u(i,jn)
        v_n(i,2)=v(i,jn)
        v_n(i,1)=v(i,jn-1)
       endif
      enddo

      indn(:) = 0

      do i=iw,ie
       if( u(i-1,js)==0.0_rsh .and. u(i,js)==0.0_rsh .and. v(i,js)==0.0_rsh ) then
        indn(i)=0
       else
        indn(i)=1
       endif
      enddo

      do i=iw,ie
       if(indn(i) == 0)then
        if(indn(i-1) == 0)then
         if(indn(i+1) == 0)then
          xe_s(i)=xe_s(i-1)
          u_s(i)=0.0_riosh
          v_s(i,1)=0.0_riosh
         else
          if(indn(i+2) == 0)then
           xe_s(i)=x(i+1,js)
           u_s(i)=0.0_riosh
           v_s(i,1)=(v(i+1,js)+v(i+1,js-1))*0.5_riosh
          else
           xe_s(i)=2.0_riosh*x(i+1,js)-x(i+2,js)
           u_s(i)=-0.5_riosh*u(i+1,js)
           v_s(i,1)=v(i+1,js)+v(i+1,js-1)-0.5_riosh*(v(i+2,js)+v(i+2,js-1))
          endif
         endif
        else
         if(indn(i+1) == 0)then
          if(indn(i-2) == 0)then
           xe_s(i)=x(i-1,js)
           u_s(i)=0.0_riosh
           v_s(i,1)=(v(i-1,js)+v(i-1,js-1))*0.5_riosh
          else
           xe_s(i)=2.0_riosh*x(i-1,js)-x(i-2,js)
           u_s(i)=-0.5_riosh*u(i-2,js)
           v_s(i,1)=v(i-1,js)+v(i-1,js-1)-0.5_riosh*(v(i-2,js)+v(i-2,js-1)) 
          endif
         else
          xe_s(i)=0.5_riosh*(x(i-1,js)+x(i+1,js))
          u_s(i)=-0.25_riosh*(u(i-1,js)+u(i+1,js))
          v_s(i,1)=0.25_riosh*(v(i-1,js)+v(i-1,js-1)+v(i+1,js)+v(i+1,js-1))
         endif
        endif
       else
        xe_s(i)=x(i,js)
        u_s(i)=u(i,js)
        v_s(i,1)=v(i,js-1)
        v_s(i,2)=v(i,js-1)
       endif
      enddo

! open boundary value set to rg_valmanq_io if larger than 25 meter height
!
      do i=iw,ie
        xe_n(i)=min(xe_n(i),rg_valmanq_io)
        if(xe_n(i) > 25.0_riosh) xe_n(i)=rg_valmanq_io
        xe_s(i)=min(xe_s(i),rg_valmanq_io)
        if(xe_s(i) > 25.0_riosh) xe_s(i)=rg_valmanq_io
      end do

! management of corners
!
      i=iw
      do while(xe_n(i)==rg_valmanq_io .and. i<=ie)
       i=i+1
      end do
      if(i/=iw.and.i/=ie) then
       do ii=iw,i-1
        xe_n(ii)=xe_n(i)          ! most western values set to the first defined western value
       end do
      end if

      if(i/=ie) then
      i=ie
      do while(xe_n(i)==rg_valmanq_io .and. i>=iw)
       i=i-1
      end do
      if(i/=iw.and.i/=ie) then
       do ii=ie,i+1,-1
        xe_n(ii)=xe_n(i)          ! most eastern values set to the first defined eastern value
       end do
      else
       goto 95
      end if

      do i=iw+1,ie-1
      if(xe_n(i)==rg_valmanq_io) then
        i1=i+1
        do while(xe_n(i1)/=rg_valmanq_io)
         i1=i1+1
        end do
        i2=i-1
        do while(xe_n(i2)/=rg_valmanq_io)
         i2=i2-1
        end do
        xe_n(i)=(real(i1-i,riosh)*xe_n(i2)+real(i-i2,riosh)*xe_n(i1))/real(i1-i2,riosh)  ! linear interpolation from two not consecutive defined values
       end if
      end do
      end if
95    continue

      i=iw
      do while(xe_s(i)==rg_valmanq_io .and. i<=ie)
       i=i+1
      end do
      if(i/=iw.and.i/=ie) then
       do ii=iw,i-1
        xe_s(ii)=xe_s(i)          ! most western values set to the first defined western value
       end do
      end if

      if(i/=ie) then
      i=ie
      do while(xe_s(i)==rg_valmanq_io .and. i>=iw)
       i=i-1
      end do
      if(i/=iw.and.i/=ie) then
       do ii=ie,i+1,-1
        xe_s(ii)=xe_s(i)          ! most eastern values set to the first defined eastern value
       end do
      else
        goto 97 
      end if

      do i=iw+1,ie-1
      if(xe_s(i)==rg_valmanq_io) then
        i1=i+1
        do while(xe_s(i1)/=rg_valmanq_io)
         i1=i1+1
        end do
        i2=i-1
        do while(xe_s(i2)/=rg_valmanq_io)
         i2=i2-1
        end do
        xe_s(i)=(real(i1-i,riosh)*xe_s(i2)+real(i-i2,riosh)*xe_s(i1))/real(i1-i2,riosh)  ! linear interpolation from two not consecutive defined values
       end if
      end do
      end if
!
   97 continue
      write(obc_ind23)t,(xe_n(i),xe_s(i),u_n(i),v_n(i,1),v_n(i,2),u_s(i),v_s(i,1),v_s(i,2),i=iw,ie)
!
end subroutine tide_saveobcns

