C program test_bicubic
        Program test_bicubic

          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

          integer dlower(6), dupper(6), lindex, comm_s, dwin(4, 30)
          common /dvars/ dlower, dupper, lindex, comm_s, dwin

          integer dmax_iter(10), dny0(10), i, j, k
          integer djj(imp3,jmp3), jj(imp3, max_col_p3)
          integer njj(imp3, max_col_p3)

          real      win_d(imp3,max_col_p3)
          integer   upper, lower
          integer   max_iter(2)
          real      win_n(imp3,jmp3)
          integer   wr_south, wr_lower, co_lower, width, wrapd
          integer   wr_north, range

          
 100      format( 6I4)
 101      format( 2(5I4))
 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))

          open (unit = 10, file = 'dloup.dat', status = 'old' )
          read (10, 100) (dlower(i), i = 1, 6)
          read (10, 100) (dupper(i), i = 1, 6)
          close(10)

          open (unit = 11, file = 'dtnorth.dat', status = 'old' )
          read (11, 100) (dny0(i), i = 1, 6)
          close(11)

          open (unit = 12, file = 'djj.dat', status = 'old' )
          read (12, 102) ((djj(i,j), i = 1, imp3), j = 1, jmp3)
          close(12)

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

          do i = 1, imp3
            do ny0 = 0, 5
              do j = 1, 5
                 dwin(i, ny0 * 5 + j) = ny0
              end do
            end do
          end do

          do i = 1, 6
             ny0 = dny0(i)
             lindex = ny0 + 1
             upper = dupper(ny0 + 1)
             lower = dlower(ny0 + 1)

             do k = 1, imp3
                do j = 1, (upper - lower + 1)
                   jj(k, j) = djj(k, lower + j -1)
                end do
             end do

c             max_iter(2) = dmax_iter(i + 5)

             call  set_lsd_commons(nx,nx0,ny,ny0, nynth, 
     *                             nysth, nxest, nxwst)

             range = upper - lower + 1
             call get_lsd_comm_rng(4, 1, range, jj, max_iter,
     *                             ny, ny0, nysth)
             print *, "North Iter", max_iter(1), "South Iter", 
     *                max_iter(2)

             do k = 1, imp3
                do j = 1, max_col_p3
                   win_d(k, j) = dwin(k, ny0 * max_col_p3 + j)
                end do
             end do

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

             comm_s = 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)  
        
          write (13, 103) ny0, wr_south, wr_north
          write (13, 104) width, wr_lower, co_lower, wrapd 
          write (13, 105) ((jj(k,j), k = 1, imp3), j = 1, range)
          write (13, 106) ((njj(k,j), k = 1, imp3), j = 1, range)

          end do

          close(13)
        end


        subroutine set_lsd_commons(nx,nx0,ny,ny0, nynth, 
     *                             nysth, nxest, nxwst)
        
          implicit none
          
          include "slgcm.mp"

          integer nx,
     &            nx0,
     &            ny,
     &            ny0,
     &            nynth,
     &            nysth,
     &            nxest,
     &            nxwst

          nx = 1
          nx0 = 0
D          ny  = CMMD_partition_size()
D          ny0 = CMMD_self_address()
          ny  = 6
          nynth  = mod( ny0 + 1, ny )
          if (ny0 .eq. 0) then
            nysth = 5
          else
             nysth  = mod( ny0 - 1, ny )
          endif
          
C1          if (nysth .eq. -1) then
C1            nysth = nysth + ny
C1          endif

          nxest = -1
          nxwst = -1
C          print 201, ny0, nynth, nysth
 201      format("Node : ", I4, " SRC : ", I4, " DEST ", I4 )

        end


      function mp_send_and_rcvint( src_pn, src_tag, in,
     *                             dest_pn, dest_tag, out )

        implicit none

        integer mp_send_and_rcvint

        integer src_pn,
     &           src_tag,
     &           in,
     &           dest_pn,
     &           dest_tag,
     &           out

      include 'slgcm.mp'

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

      integer dlower(6), dupper(6), lindex, comm_s, dwin(4,30)
      common /dvars/ dlower, dupper, lindex, comm_s, dwin

      integer tmp

        tmp = 0

C     For Comm South

        if (comm_s .eq. 1) then
           lindex = lindex - 1
        else
           lindex = lindex + 1
        endif

        if (lindex .eq. 0) then
           lindex = ny
        else if (lindex .eq. 7) then
           lindex = 1
        endif 

        if (comm_s .eq. 1) then 
          in = dlower(lindex)
        else 
          in = dupper(lindex)
        endif

        mp_send_and_rcvint = tmp
        end


       subroutine circ_winbuf(src_pn, src_tag, inbuf, in_i,
     &                        in_j, dest_pn, dest_tag, outbuf, 
     &                        out_i, out_j)

        implicit none

        integer src_pn, src_tag,
     &           in_i, in_j,
     &           dest_pn, dest_tag,
     &           out_i, out_j

        real inbuf(in_i, in_j)
        real outbuf(out_i, out_j)

        integer dlower(6), dupper(6), lindex, comm_s, dwin(4,30)
        common /dvars/ dlower, dupper, lindex, comm_s, dwin


        include 'slgcm.mp'
      
        integer i, j, st

        st = (lindex - 1) * max_col_p3

        do i = 1, in_i
          do j = 1, in_j
           inbuf(i,j) = dwin(i, st + j)
          end do
        end do
      end


      subroutine mp_sync()

        implicit none
        
        include 'slgcm.mp'

        integer i

        i = i + 1
        
      End


