      subroutine kdsaw_deln_r4(zpre,zdel,ierr)
!
      use kdsaw_module
      implicit NONE
!
      real,intent(in) :: zpre(nzones)   ! input density profile
      real,intent(out) :: zdel(nzones)  ! output ideal Kadomtsev density change
!
      integer, intent(out) :: ierr      ! completion code; 0=OK
!
!  REAL*4 interface to KDSAW_DELN
!
      real(fp) :: zpre_r8(nzones),zdel_r8(nzones)
!
      if(.not.kdsaw_flag) then
         write(abs(nonlin),*) ' ?kdsaw_deln: cannot return delta(n) profile.'
         write(abs(nonlin),*) '  --> call kdsaw_init first.'
         ierr=1
         zdel=0.0
         return
      endif

      zpre_r8=zpre
!
      call kdsaw_deln(zpre_r8,zdel_r8,ierr)
!
      zdel=zdel_r8
!
      return
      end
!---------------------------------------------------------------------
!
subroutine kdsaw_deln(zpre,zdel,ierr)
!
  use kdsaw_module
#ifdef __DEBUG
  use ezcdf
#endif

  implicit NONE
!
!  USING THE MIXING FACTORS CALCULATED IN THE SAWMIX ROUTINE,
!  EVALUATE THE EXPECTED CHANGE ZDEL IN PRE-SAWTOOTH DENSITY
!  PROFILE ZPRE DUE TO THE SAWTOOTH (ideal Kadomtsev mixing).
!
!  THE VOLUME INTEGRAL ACROSS THE WHOLE PLASMA OF ZDEL WILL COME
!  OUT TO ZERO
!
!  Note: the dimensioning of the arrays is determined by the argument
!  "izones" in a prior call to kdsaw_init.
!
  real(fp), intent(in) :: zpre(nzones) ! input density profile
  real(fp), intent(out) :: zdel(nzones) ! ideal Kadomtsev change profile
!
  integer, intent(out) :: ierr      ! completion code, 0=OK
!
!----------------------------
  integer i,ii,inm,ipre,ip,ioff,icdf,idims(3),ier_cdf
  real(fp) :: zsum
  character*6 zrhoname,ztype
!-------------------------------------------------------------------

#ifdef __DEBUG
  if(nonlin.ge.0) then
     call cdf_open(icdf,'kdsaw_debug.cdf','a')
     ii=0
     do ii=ii+1
        write(zrhoname,'("RHO_",i2.2)') ii
        call cdf_inquire(icdf,zrhoname,idims,ztype,ier_cdf)
        if(ier_cdf.ne.0) exit
     enddo
     call cdf_define(icdf,zrhoname,zpre)
     call cdf_write(icdf,zrhoname,zpre)
     call cdf_close(icdf)
  endif
#endif
!
!  INITIALIZE THE CHANGE PROFILE
!
  zdel=zero
!
  if(.not.kdsaw_flag) then
     write(abs(nonlin),*) ' ?kdsaw_deln: cannot return delta(n) profile.'
     write(abs(nonlin),*) '  --> call kdsaw_init first.'
     ierr=1
     return
  else
     ierr=0
  endif
!
!  CALCULATE SUMS FOR EACH ZONE IN THE MIXING REGION
!
  ioff=lcentr-1
!
  DO I=LCENTR,IMIX
     INM=INZMIX(I)
     ZSUM=zero
     DO IP=1,INM
        IPRE=IZMIX(IP,I)
        ZSUM=ZSUM+ZZMIX(IP,I)*ZPRE(IPRE-ioff)*DVOL(IPRE,2)
     enddo    
     ZDEL(I-ioff)=ZSUM/DVOL(I,2) - ZPRE(I-ioff)
  enddo    
!
RETURN
end subroutine kdsaw_deln
