c %Z% %M% %I% (IDS) %G%

       subroutine locubit(win_d, delx, dely,  wou_d, ii,
     &                    jj, lower, upper, ja, jb)


      include "slgcm.slgprm"
      include "slgcm.mp"

      implicit none

      integer nx, nx0, ny, ny0, nynth, nysth, nxest, nxwst
      common /commvars/ nx,nx0,ny,ny0, nynth, nysth, nxest, nxwst

      real      win_d(imp3,max_col_p3),
     &          delx(imp3,max_col_p3),
     &          dely(imp3,max_col_p3),
     &          wou_d(imp3,max_col_p3)

      integer  ii(imp3,max_col_p3), 
     &          jj(imp3,max_col_p3)
      integer  lower, 
     &          upper,
     &         ja, jb
      integer  jjp1, jjp2, jjp3, jjp4


C****   Function prototypes
C      integer mp_send_and_rcvint

C***    Timing Code 
C      real*8   etime,
C     &         btime,
C     &         itime 
C***    Timing Code 

      real win_n( imp3, jmp3)
C1      real landing( imp3, max_col_p3)

      integer i, j, k 
C1    ,l, m
      integer max_iter(2), range
C1      integer tmp, range, tupper, tlower
      integer lo, up
C1      integer st, fi, rem, lim
      integer dk, dj

      integer wr_south, wr_north, width
      integer wr_lower, co_lower, wrapd
      integer njj(imp3, max_col_p3)

C****
C***  Debug Variables
      integer dlower(6), dupper(6), lindex, comm_s, dwin(4,30)
      common /dvars/ dlower, dupper, lindex, comm_s, dwin


C***    Timing Code 
C      integer mp_timer_clear
C      integer mp_timer_start
C      integer mp_timer_stop
C      real*8 mp_elapsed_time
C      real*8 mp_busy_time
C      real*8 mp_idle_time
C***    Timing Code 

D      print *, "Starting"

C      call mp_init()

      call mp_sync()

      range = upper - lower + 1

      lo = 1
      up = range
      if (ny0 .eq. 0) then
         lo = ja
      else if (ny0 .eq. ny - 1) then
         up = jb
      endif

      do k = 1, imp3
         do j = 1, jmp3
            win_n(k, j) = -1
         end do
      end do

C1      open (unit = 13, file = 'debug.out', status = 'scratch' )
C1      close(13)
C1      open (unit = 13, file = 'debug.out', status = 'new' )

      call get_lsd_comm_rng(max_col, lo, up, jj, max_iter,
     *                        ny, ny0, nysth)


C***    Timing Code 
C	tmp = mp_timer_clear(1)
C        if (tmp .eq. -1) then
C           print *, 'Failure in clearing timer'
C           stop
C        end if
C***    Timing Code 


             comm_s = 1 
             lindex = ny0 + 1
             call comm_south(win_d, upper, lower, max_iter, 
     &                       win_n, wr_south, wr_lower, co_lower, 
     &                       wrapd, width)  

             comm_s = 0
             lindex = ny0 + 1
             call comm_north(win_d, upper, lower, max_iter, 
     &                       win_n, wr_north, width)  

             call readjust_jj(jj, wr_south, wr_north, width,
     &            wr_lower, co_lower, wrapd, njj)  

109      format ( 4F8.4 )
             print *, "Node", ny0
             print 109, ((win_n(k,j), k = 1, imp3), j = 1, jmp3)

C1          write (13, 103) ny0, wr_south, wr_north
C1          write (13, 104) width, wr_lower, co_lower, wrapd 
C1          write (13, 105) ((jj(k,j), k = 1, imp3), j = 1, range)
C1          write (13, 106) ((njj(k,j), k = 1, imp3), j = 1, range)
C1          close(13)

      lo = 1

      if (ny0 .eq. 0) then
         range = upper - lower + 1
         lo = ja
      else if (ny0 .eq. ny - 1) then
         range = jb - lower + 1
      endif
         
      do j = lo, range
         do i = 1, imp3

C1            call findnew_jj(jj(i,j), jjp1, jjp2, jjp3, 
C1     *                            jjp4, width)
            call findnew_jj(njj(i,j), jjp1, jjp2, jjp3, 
     *                            jjp4, width)
            call  cubit( delx(i,j),dely(i,j), 
     &           win_n(ii(i,j)  ,jjp2),win_n(ii(i,j),jjp3  ),
     &           win_n(ii(i,j)  ,jjp1),win_n(ii(i,j),jjp4  ),
     &           win_n(ii(i,j)+1,jjp2),win_n(ii(i,j)+1,jjp3),
     &           win_n(ii(i,j)+1,jjp1),win_n(ii(i,j)+1,jjp4),
     &           win_n(ii(i,j)-1,jjp2),win_n(ii(i,j)-1,jjp3),
     &           win_n(ii(i,j)-1,jjp1),win_n(ii(i,j)-1,jjp4),
     &           win_n(ii(i,j)+2,jjp2),win_n(ii(i,j)+2,jjp3),
     &           win_n(ii(i,j)+2,jjp1),win_n(ii(i,j)+2,jjp4), 
     &           wou_d(i,j))
C1            print 108, i, j, ii(i,j), njj(i,j), 
C1     &                  delx(i,j), dely(i,j),
C1     &           win_n(ii(i,j)  ,jjp2),win_n(ii(i,j),jjp3  ),
C1     &           win_n(ii(i,j)  ,jjp1),win_n(ii(i,j),jjp4  ),
C1     &           win_n(ii(i,j)+1,jjp2),win_n(ii(i,j)+1,jjp3),
C1     &           win_n(ii(i,j)+1,jjp1),win_n(ii(i,j)+1,jjp4),
C1     &           win_n(ii(i,j)-1,jjp2),win_n(ii(i,j)-1,jjp3),
C1     &           win_n(ii(i,j)-1,jjp1),win_n(ii(i,j)-1,jjp4),
C1     &           win_n(ii(i,j)+2,jjp2),win_n(ii(i,j)+2,jjp3),
C1     &           win_n(ii(i,j)+2,jjp1),win_n(ii(i,j)+2,jjp4) 
C1                       
         end do
      end do

 101  format( 1X, '+', 4F16.8 //, '+' 3F16.8)
C1
C1       print *, '+WOU'
C1       print 101, ((wou_d(dk, dj), dj = 1, max_col_p3), dk = 1, imp3)
C1       print *, '+WOU OVER'

C***    Timing Code 
C        btime = mp_busy_time(1)
C        etime = mp_elapsed_time(1)

C       print 200, ">Node : ", ny0, " , Comm. Time:",
C     &             etime
C       print 200, ">Node : ", ny0, " , Comm. Time:",
C     &             btime
C 200   format (A, I4, A, E22.15.3)
C***    Timing Code 


 102      format( 4I4)
 103      format ( "Ny0", I4, "South Wrap", I4, "North Wrap", I4)
 104      format ( "Wdth", I4, "Wr Lr", I4, "Co Lr", I4,  "Wrd", I4 )     
 105      format ( "JJ" //, 4(I4))
 106      format ( "NJJ" //, 4(I4))
 108      format ( "I", I4, " J", I4, " II", I4, " JJ", I4 //, 
     &             "DELX", F16.8, " DELY", F16.8 //,
     &             "W1", F16.8, "W2", F16.8, "W3", F16.8, 
     &             "W4", F16.8 //,
     &             "W5", F16.8, "W6", F16.8, "W7", F16.8, 
     &             "W8", F16.8 //,
     &             "W9", F16.8, "W10", F16.8, "W11", F16.8, 
     &             "W12", F16.8 //,
     &             "W13", F16.8, "W14", F16.8, "W15", F16.8, 
     &             "W16", F16.8)

      end

