
      subroutine cmax_cshift_1(dest,source,axis,distance,type,
     &                           s1)
      real dest(*), source(*)
      integer axis, distance, type, s1
      if (axis .gt. 1) then
         pause "axis must be <= 1 for a 1d cshift"
      endif
      call cmax_cshift_n(dest,source,axis,distance,type,
     &                     s1, 1,1,1,1,1,1)
      end

      subroutine cmax_cshift_2(dest,source,axis,distance,type, 
     &                           s1, s2)
      real dest(*), source(*)
      integer axis, distance, type, s1, s2
      if (axis .gt. 2) then
         pause "axis must be <= 2 for a 2d cshift"
      endif
      call cmax_cshift_n(dest,source,axis,distance,type,
     &                     s1, s2, 1,1,1,1,1)
      end

      subroutine cmax_cshift_3(dest,source,axis,distance,type,
     &                           s1, s2, s3)
      real dest(*), source(*)
      integer axis, distance, type, s1, s2, s3
      if (axis .gt. 3) then
         pause "axis must be <= 3 for a 3d cshift"
      endif
      call cmax_cshift_n(dest,source,axis,distance,type,
     &                     s1, s2, s3, 1,1,1,1)
      end

      subroutine cmax_cshift_4(dest,source,axis,distance,type,
     &                           s1, s2, s3, s4)
      real dest(*), source(*)
      integer axis, distance, type, s1, s2, s3, s4
      if (axis .gt. 4) then
         pause "axis must be <= 4 for a 4d cshift"
      endif
      call cmax_cshift_n(dest,source,axis,distance,type,
     &                     s1, s2, s3, s4, 1,1,1)
      end

      subroutine cmax_cshift_5(dest,source,axis,distance,type,
     &                           s1, s2, s3, s4, s5)
      real dest(*), source(*)
      integer axis, distance, type, s1, s2, s3, s4, s5
      if (axis .gt. 5) then
         pause "axis must be <= 5 for a 5d cshift"
      endif
      call cmax_cshift_n(dest,source,axis,distance,type,
     &                     s1, s2, s3, s4, s5, 1,1)
      end

      subroutine cmax_cshift_6(dest,source,axis,distance,type,
     &                           s1, s2, s3, s4, s5, s6)
      real dest(*), source(*)
      integer axis, distance, type, s1, s2, s3, s4, s5, s6
      if (axis .gt. 6) then
         pause "axis must be <= 6 for a 6d cshift"
      endif
      call cmax_cshift_n(dest,source,axis,distance,type,
     &                     s1, s2, s3, s4, s5, s6, 1)
      end

      subroutine cmax_cshift_7(dest,source,axis,distance,type,
     &                           s1, s2, s3, s4, s5, s6, s7)
      real dest(*), source(*)
      integer axis, distance, type, s1, s2, s3, s4, s5, s6, s7
      if (axis .gt. 7) then
         pause "axis must be <= 7 for a 7d cshift"
      endif
      call cmax_cshift_n(dest,source,axis,distance,type,
     &                     s1, s2, s3, s4, s5, s6, s7)
      end


      subroutine cmax_cshift_n(dest,source,axis,distance,type, 
     &                           s1, s2, s3, s4, s5, s6, s7)
      real dest(*), source(*)
      integer axis, s1, s2, s3, s4, s5, s6, s7
c dest and source can be integers cause we are just passing them through
      integer axis, distance, type

      integer sizes(7), lower, middle, upper, i, elt_size

      sizes(1) = s1
      sizes(2) = s2
      sizes(3) = s3
      sizes(4) = s4
      sizes(5) = s5
      sizes(6) = s6
      sizes(7) = s7
      lower = 1
      upper = 1

      do i = 1, axis-1
         lower = lower * sizes(i)
      end do
      middle = sizes(axis)
      do i = axis+1, 7
         upper = upper * sizes(i)
      end do
         
      elt_size = -1
c   cmax_logical
      if (type .eq. 1) then
         elt_size = 1
      end if
c   cmax_integer
      if (type .eq. 2) then
         elt_size = 1
      end if
c   cmax_real
      if (type .eq. 3) then
         elt_size = 1
      end if
c   cmax_complex
      if (type .eq. 4) then
         elt_size = 2
      end if
c   cmax_double
      if (type .eq. 5) then
         elt_size = 2
      end if
c   cmax_double_complex
      if (type .eq. 6) then
         elt_size = 4
      end if

      if (elt_size .eq. -1) then
         pause "The type passed to CMAX_CSHIFT was not valid"
      end if

      if (lower .eq. 1) then
         call cmax_cshift_2d_axis1(dest,source,distance,elt_size,
     &                             middle, upper)
      else
         call cmax_cshift_3d_axis2(dest,source,distance,elt_size,
     &                              lower, middle, upper)
      end if
      
      end


      subroutine cmax_cshift_2d_axis1(dest,source,distance,
     &                                   elt_size, s1, s2)
      integer distance, elt_size, s1,s2
      integer dest(s1,s2), source(s1,s2)
      if (elt_size .eq. 1) then
         call cmax_cshift_1w_2d_axis1(dest,source,distance,
     &                                s1, s2)
      end if
      if (elt_size .eq. 2) then
         call cmax_cshift_2w_2d_axis1(dest,source,distance,
     &                                s1, s2)
      end if
      if (elt_size .eq. 4) then
         call cmax_cshift_4w_2d_axis1(dest,source,distance,
     &                                s1, s2)
      end if

      end



      subroutine cmax_cshift_3d_axis2(dest,source,distance, 
     &                                   elt_size, s1, s2, s3)
      integer distance, elt_size, s1,s2,s3
      integer dest(s1,s2,s3), source(s1,s2,s3)
      if (elt_size .eq. 1) then
         call cmax_cshift_1w_3d_axis2(dest,source,distance, 
     &                                s1, s2, s3)
      end if
      if (elt_size .eq. 2) then
         call cmax_cshift_2w_3d_axis2(dest,source,distance, 
     &                                s1, s2, s3)
      end if
      if (elt_size .eq. 4) then
         call cmax_cshift_4w_3d_axis2(dest,source,distance, 
     &                                s1, s2, s3)
      end if

      end


cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      subroutine cmax_cshift_1w_2d_axis1(dest,source,distance, s1, s2)
      integer distance, s1,s2
      integer dest(s1,s2), source(s1,s2)

      integer i,j, real_distance

      real_distance = mod(s1 + mod(distance, s1), s1)

      if (real_distance .ge. 0) then

         do j = 1,s2
            do i = 1, s1 - real_distance
               dest(i,j) = source(i + real_distance, j)
            end do
            do i = s1 - real_distance + 1, s1
               dest(i,j) = source(i - (s1 - real_distance), j)
            end do
         end do
         
      else

         do j = 1,s2
            do i = 1 + real_distance, s1
               dest(i,j) = source(i - real_distance, j)
            end do
            do i = 1, real_distance
               dest(i,j) = source(s1 - real_distance + 1, j)
            end do
         end do
         
      end if

      return
      end
      


      subroutine cmax_cshift_1w_3d_axis2(dest,source,distance, s1,s2,s3)
      integer distance, s1,s2,s3
      integer dest(s1,s2,s3), source(s1,s2,s3)

      integer i,j,k, real_distance

      real_distance = mod(s2 + mod(distance, s2), s2)

      if (real_distance .ge. 0) then

         do k = 1,s3
            do j = 1, s2 - real_distance
               do i = 1, s1
                  dest(i,j,k) = source(i, j + real_distance, k)
               end do
            end do
            do j = s2 - real_distance + 1, s2
               do i = 1, s1
                  dest(i,j,k) = source(i, j - (s2 - real_distance), k)
               end do
            end do
         end do
         
      else

         do k = 1,s3
            do j = 1 + real_distance, s2
               do i = 1, s1
                  dest(i,j,k) = source(i, j - real_distance, k)
               end do
            end do
            do j = 1, real_distance
               do i = 1, s1
                  dest(i,j,k) = source(i, s2 - real_distance + 1, k)
               end do
            end do
         end do
         
      end if

      return
      end


cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc


      subroutine cmax_cshift_2w_2d_axis1(dest,source,distance, s1, s2)
      integer distance, s1,s2
      complex dest(s1,s2), source(s1,s2)

      integer i,j, real_distance

      real_distance = mod(s1 + mod(distance, s1), s1)

      if (real_distance .ge. 0) then

         do j = 1,s2
            do i = 1, s1 - real_distance
               dest(i,j) = source(i + real_distance, j)
            end do
            do i = s1 - real_distance + 1, s1
               dest(i,j) = source(i - (s1 - real_distance), j)
            end do
         end do
         
      else

         do j = 1,s2
            do i = 1 + real_distance, s1
               dest(i,j) = source(i - real_distance, j)
            end do
            do i = 1, real_distance
               dest(i,j) = source(s1 - real_distance + 1, j)
            end do
         end do
         
      end if

      return
      end
      


      subroutine cmax_cshift_2w_3d_axis2(dest,source,distance, s1,s2,s3)
      integer distance, s1,s2,s3
      complex dest(s1,s2,s3), source(s1,s2,s3)

      integer i,j,k, real_distance

      real_distance = mod(s2 + mod(distance, s2), s2)

      if (real_distance .ge. 0) then

         do k = 1,s3
            do j = 1, s2 - real_distance
               do i = 1, s1
                  dest(i,j,k) = source(i, j + real_distance, k)
               end do
            end do
            do j = s2 - real_distance + 1, s2
               do i = 1, s1
                  dest(i,j,k) = source(i, j - (s2 - real_distance), k)
               end do
            end do
         end do
         
      else

         do k = 1,s3
            do j = 1 + real_distance, s2
               do i = 1, s1
                  dest(i,j,k) = source(i, j - real_distance, k)
               end do
            end do
            do j = 1, real_distance
               do i = 1, s1
                  dest(i,j,k) = source(i, s2 - real_distance + 1, k)
               end do
            end do
         end do
         
      end if

      return
      end
      
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc


      subroutine cmax_cshift_4w_2d_axis1(dest,source,distance, s1, s2)
      integer distance, s1,s2
      double complex dest(s1,s2), source(s1,s2)

      integer i,j, real_distance

      real_distance = mod(s1 + mod(distance, s1), s1)

      if (real_distance .ge. 0) then

         do j = 1,s2
            do i = 1, s1 - real_distance
               dest(i,j) = source(i + real_distance, j)
            end do
            do i = s1 - real_distance + 1, s1
               dest(i,j) = source(i - (s1 - real_distance), j)
            end do
         end do
         
      else

         do j = 1,s2
            do i = 1 + real_distance, s1
               dest(i,j) = source(i - real_distance, j)
            end do
            do i = 1, real_distance
               dest(i,j) = source(s1 - real_distance + 1, j)
            end do
         end do
         
      end if

      return
      end
      


      subroutine cmax_cshift_4w_3d_axis2(dest,source,distance, s1,s2,s3)
      integer distance, s1,s2,s3
      double complex dest(s1,s2,s3), source(s1,s2,s3)

      integer i,j,k, real_distance

      real_distance = mod(s2 + mod(distance, s2), s2)

      if (real_distance .ge. 0) then

         do k = 1,s3
            do j = 1, s2 - real_distance
               do i = 1, s1
                  dest(i,j,k) = source(i, j + real_distance, k)
               end do
            end do
            do j = s2 - real_distance + 1, s2
               do i = 1, s1
                  dest(i,j,k) = source(i, j - (s2 - real_distance), k)
               end do
            end do
         end do
         
      else

         do k = 1,s3
            do j = 1 + real_distance, s2
               do i = 1, s1
                  dest(i,j,k) = source(i, j - real_distance, k)
               end do
            end do
            do j = 1, real_distance
               do i = 1, s1
                  dest(i,j,k) = source(i, s2 - real_distance + 1, k)
               end do
            end do
         end do
         
      end if

      return
      end
      








