      subroutine set_pbc (nxp, nyp, npbc, lpbcw, lpbce, mask)
c----------------------------------------------------------------
      implicit real(a-h,o-z),integer(i-n)
      include 'comm_para.h'
      dimension lpbcw(1), lpbce(1), mask(nxp*nyp)

      npbc = 0
      do j = 1, nyp
         j1  = 1 + (j-1)*nxp
         jnx = j*nxp
         ierr = 0
         if ( mask(j1) .ne. 0 ) then
            if ( mask(jnx) .eq. 0 ) then
               ierr = nxp+1
            else   
               do i = 1, MINSEG - 1
                  if     (ierr.eq.0 .and. mask(j1+i).eq.0) then
                     ierr = i
                  elseif (ierr.eq.0 .and. mask(jnx-i).eq.0) then
                     ierr = nxp+1-i
                  endif
               enddo
            endif
            
            if (ierr .ne. 0) then
               write (6, *) 'A land is too close for PBC [i,j]=',ierr,j
               
            else
               npbc = npbc + 1
               lpbcw(npbc) = mask(j1)
               lpbce(npbc) = mask(jnx)
            endif
         endif
      enddo

      return
      end

c     ------------------------------------------------------------------
      subroutine set_bpx (nxp, nyp, mask, maxnb,minseg,nbx,lxx,snx)
c     ------------------------------------------------------------------
c     find the boundary indices for a x-sort in presence of periodic B.C..
c     a replacement for bound() in bndxy()
c     maxnb  = (input) max. storage space for lxx and snx.
c     minseg = (input) required minimum # of consecutive ocean points
c     nbx    = (output) # of x/y boundary indices of the x or y sort.
c     lxx    = (output) x or y bndry indices of a compressed x or y sort.
c     snx    = (output) nbx signs for the boundaries:=1 if the land -> ocean 
c
      implicit real(a-h,o-z),integer(i-n)
      character*72 msg
      dimension mask(nxp,1), lxx(1),snx(1)
      logical prev, curr
c
      nbx = 0
      do irow = 1, nyp
         prev = (mask(1, irow) .eq. 0)

         ista = 1
         do icol = 2, nxp
c            ista = 1
            ixy = mask(icol, irow)
            curr = (ixy .eq. 0)

            if ( curr .ne. prev ) then
               nbx = nbx + 1
               if (nbx .gt. MAXNB) then
                  write (msg, 101) MAXNB
 101              format ('set_pbx: insufficient space'
     *                    ' for lbx, inrease MAXNB=', i10, '$')
                  call perror1 (msg, 1)
               endif

               if ( prev ) then
                  lxx(nbx) = ixy
                  snx(nbx) = 1.
                  ista = icol
               else
                  if (icol-ista .lt. minseg) then
                     write (msg, 102) icol-ista, ista, irow
 102                 format ('set_bpx: only', i3, 
     *                    ' consecutive ocean grid pts',
     *                    ' next to (i,j)= ', 2i5, '$')
                     call perror1(msg, 1)
                  endif
                  lxx(nbx) = mask(icol-1, irow)
                  snx(nbx) = -1.
               endif

               prev = curr
            endif
         enddo
      enddo

c     end of set_bpx
      end
