
      SUBROUTINE cmax_pack_1d(dest,source,mask,axis,pad,type,
     &                        dsize, s1)
      integer dest(*), source(*)
      logical mask(*)
      integer axis, pad, type
      integer dsize, s1
      if (axis .gt. 1) then
         pause "axis must be <= 1 for a 1d pack"
      endif
      call cmax_pack_nd(dest,source,mask,axis,pad,type,
     &                  dsize, s1, 1,1,1,1,1,1)
      end

      subroutine cmax_pack_2d(dest,source,mask,axis,pad,type, 
     &                        dsize, s1,s2)
      integer dest(*), source(*)
      logical mask(*)
      integer axis, pad, type
      integer dsize, s1,s2
      if (axis .gt. 2) then
         pause "axis must be <= 2 for a 2d pack"
      endif
      call cmax_pack_nd(dest,source,mask,axis,pad,type,
     &                  dsize, s1, s2, 1,1,1,1,1)
      end

      subroutine cmax_pack_3d(dest,source,mask,axis,pad,type,
     &                        dsize, s1,s2,s3)
      integer dest(*), source(*)
      logical mask(*)
      integer axis, pad, type
      integer dsize, s1,s2,s3
      if (axis .gt. 3) then
         pause "axis must be <= 3 for a 3d pack"
      endif
      call cmax_pack_nd(dest,source,mask,axis,pad,type,
     &                  dsize, s1, s2, s3, 1,1,1,1)
      end

      subroutine cmax_pack_4d(dest,source,mask,axis,pad,type,
     &                        dsize, s1,s2,s3,s4)
      integer dest(*), source(*)
      logical mask(*)
      integer axis, pad, type
      integer dsize, s1,s2,s3,s4
      if (axis .gt. 4) then
         pause "axis must be <= 4 for a 4d pack"
      endif
      call cmax_pack_nd(dest,source,mask,axis,pad,type,
     &                  dsize, s1, s2, s3, s4, 1,1,1)
      end

      subroutine cmax_pack_5d(dest,source,mask,axis,pad,type,
     &                        dsize, s1,s2,s3,s4,s5)
      integer dest(*), source(*)
      logical mask(*)
      integer axis, pad, type
      integer dsize, s1,s2,s3,s4,s5
      if (axis .gt. 5) then
         pause "axis must be <= 5 for a 5d pack"
      endif
      call cmax_pack_nd(dest,source,mask,axis,pad,type,
     &                  dsize, s1, s2, s3, s4, s5, 1,1)
      end

      subroutine cmax_pack_6d(dest,source,mask,axis,pad,type,
     &                        dsize, s1,s2,s3,s4,s5,s6)
      integer dest(*), source(*)
      logical mask(*)
      integer axis, pad, type
      integer dsize, s1,s2,s3,s4,s5,s6
      if (axis .gt. 6) then
         pause "axis must be <= 6 for a 6d pack"
      endif
      call cmax_pack_nd(dest,source,mask,axis,pad,type,
     &                  dsize, s1, s2, s3, s4, s5, s6, 1)
      end

      subroutine cmax_pack_7d(dest,source,mask,axis,pad,type,
     &                        dsize, s1,s2,s3,s4,s5,s6,s7)
      integer dest(*), source(*)
      logical mask(*)
      integer axis, pad, type
      integer dsize, s1,s2,s3,s4,s5,s6,s7
      if (axis .gt. 7) then
         pause "axis must be <= 7 for a 7d pack"
      endif
      call cmax_pack_nd(dest,source,mask,axis,pad,type,
     &                  dsize, s1, s2, s3, s4, s5, s6, s7)
      end


      subroutine cmax_pack_nd(dest,source,mask,axis,pad,type, 
     &                        dsize, s1,s2,s3,s4,s5,s6,s7)
      integer dest(*), source(*)
      logical mask(*)
      integer axis, pad, type
      integer dsize, s1,s2,s3,s4,s5,s6,s7

      integer sizes(7)
      integer lower, middle, upper
      integer 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_PACK was not valid"
      end if

      if (lower .eq. 1) then
         call cmax_pack_2d_axis1(dest,source,mask,pad,elt_size,
     &                           dsize, middle, upper)
      else
         call cmax_pack_3d_axis2(dest,source,mask,pad,elt_size,
     &                           dsize, lower, middle, upper)
      end if
      
      end


      subroutine cmax_pack_2d_axis1(dest,source,mask,pad,
     &                              elt_size, dsize, s1, s2)
      integer pad, elt_size, dsize, s1,s2
      integer dest(dsize,s2), source(s1,s2), mask(s1,s2)
      if (elt_size .eq. 1) then
         call cmax_pack_1w_2d_axis1(dest,source,mask,pad,
     &                                dsize, s1, s2)
      end if
      if (elt_size .eq. 2) then
         call cmax_pack_2w_2d_axis1(dest,source,mask,pad,
     &                                dsize, s1, s2)
      end if
      if (elt_size .eq. 4) then
         call cmax_pack_4w_2d_axis1(dest,source,mask,pad,
     &                                dsize, s1, s2)
      end if

      end



      subroutine cmax_pack_3d_axis2(dest,source,mask,pad,elt_size,
     &                              dsize, s1, s2, s3)
      integer pad, elt_size, dsize, s1,s2,s3
      integer dest(s1,dsize,s3), source(s1,s2,s3), mask(s1,s2,s3)
      if (elt_size .eq. 1) then
         call cmax_pack_1w_3d_axis2(dest,source,mask,pad, 
     &                              dsize, s1, s2, s3)
      end if
      if (elt_size .eq. 2) then
         call cmax_pack_2w_3d_axis2(dest,source,mask,pad, 
     &                              dsize, s1, s2, s3)
      end if
      if (elt_size .eq. 4) then
         call cmax_pack_4w_3d_axis2(dest,source,mask,pad, 
     &                              dsize, s1, s2, s3)
      end if

      end


cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      subroutine cmax_pack_1w_2d_axis1(dest,source,mask,pad,dsize,s1,s2)
      integer dsize, s1,s2
      integer dest(dsize,s2), source(s1,s2)
      logical mask(s1,s2)
      integer pad

      integer i,j, count

      do j=1,s2
         count = 1
         do i=1,s1
            if (mask(i,j)) then
               if (count .gt. dsize) then
                  pause "Error in CMAX_PACK: DEST not big enough."
               end if
               dest(count, j) = source(i,j)
               count = count + 1
            end if
         end do
         do i=count,dsize
            dest(i,j) = pad
         end do
      end do

      return
      end
      


      subroutine cmax_pack_1w_3d_axis2(dest,source,mask,pad,
     &                                 dsize, s1,s2,s3)
      integer dsize, s1,s2,s3
      integer dest(s1,dsize,s3), source(s1,s2,s3)
      logical mask(s1,s2,s3)
      integer pad

      integer i,j,k, count

      do k=1,s3
         do i=1,s1
            count = 1
            do j=1,s2
               if (mask(i,j,k)) then
                  if (count .gt. dsize) then
                     pause "Error in CMAX_PACK: DEST not big enough."
                  end if
                  dest(i, count, k) = source(i,j,k)
                  count = count + 1
               end if
            end do
            do j=count,dsize
               dest(i,j,k) = pad
            end do
         end do
      end do

      return
      end


cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc


      subroutine cmax_pack_2w_2d_axis1(dest,source,mask,pad,dsize,s1,s2)
      integer dsize, s1,s2
      complex dest(dsize,s2), source(s1,s2)
      logical mask(s1,s2)
      complex pad

      integer i,j, count

      do j=1,s2
         count = 1
         do i=1,s1
            if (mask(i,j)) then
               if (count .gt. dsize) then
                  pause "Error in CMAX_PACK: DEST not big enough."
               end if
               dest(count, j) = source(i,j)
               count = count + 1
            end if
         end do
         do i=count,dsize
            dest(i,j) = pad
         end do
      end do

      return
      end
      


      subroutine cmax_pack_2w_3d_axis2(dest,source,mask,pad,
     &                                 dsize, s1,s2,s3)
      integer dsize, s1,s2,s3
      complex dest(s1,dsize,s3), source(s1,s2,s3)
      logical mask(s1,s2,s3)
      complex pad

      integer i,j,k, count

      do k=1,s3
         do i=1,s1
            count = 1
            do j=1,s2
               if (mask(i,j,k)) then
                  if (count .gt. dsize) then
                     pause "Error in CMAX_PACK: DEST not big enough."
                  end if
                  dest(i, count, k) = source(i,j,k)
                  count = count + 1
               end if
            end do
            do j=count,dsize
               dest(i,j,k) = pad
            end do
         end do
      end do

      return
      end
      
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc


      subroutine cmax_pack_4w_2d_axis1(dest,source,mask,pad,dsize,s1,s2)
      integer dsize, s1,s2
      double complex dest(dsize,s2), source(s1,s2)
      logical mask(s1,s2)
      double complex pad

      integer i,j, count

      do j=1,s2
         count = 1
         do i=1,s1
            if (mask(i,j)) then
               if (count .gt. dsize) then
                  pause "Error in CMAX_PACK: DEST not big enough."
               end if
               dest(count, j) = source(i,j)
               count = count + 1
            end if
         end do
         do i=count,dsize
            dest(i,j) = pad
         end do
      end do

      return
      end
      


      subroutine cmax_pack_4w_3d_axis2(dest,source,mask,pad,
     &                                 dsize, s1,s2,s3)
      integer dsize, s1,s2,s3
      double complex dest(s1,dsize,s3), source(s1,s2,s3)
      logical mask(s1,s2,s3)
      double complex pad

      integer i,j,k, count

      do k=1,s3
         do i=1,s1
            count = 1
            do j=1,s2
               if (mask(i,j,k)) then
                  if (count .gt. dsize) then
                     pause "Error in CMAX_PACK: DEST not big enough."
                  end if
                  dest(i, count, k) = source(i,j,k)
                  count = count + 1
               end if
            end do
            do j=count,dsize
               dest(i,j,k) = pad
            end do
         end do
      end do

      return
      end
      
