!-------------------------------------------------------------------
!  sawnbi_prof - apply effect of kadomtsev sawtooth on fast ion
!  population that was previously computed by pre_sawnbi -- here
!  applied to the profiles of fast ion density, etc.
!
subroutine sawnbi_prof
  !
  !-------------------------------------------------------------------
  !  f90 modules...
  !
  use nbi_com
  !
  !-------------------------------------------------------------------
  !  see pre_sawnbi subroutine...
  !
  !
  !  in addition the following summary profiles are copied.
  !
  !   bdenssw(:,isb) -- postsawtooth #/zone, each fast specie
  !   udensplw(:,isb)-- postsawtooth upll, jles/zone, each fast specie
  !   udensppw(:,isb)-- postsawtooth uperp, jles/zone, each fast specie
  !   ebapl2sw(:,isb)-- postsawtooth <epll>, ev, each specie
  !   ebapp2sw(:,isb)-- postsawtooth <epll>, ev, each specie
  !   omegabsw(:,isb)-- postsawtooth angular velocity, 1/sec, each specie
  !
  !
  !---------------------------------------------------------------
  !  note: pre_sawnbi zeroes the bdenssw of species which are not
  !  to be affected by the sawtooth.  hence the maxval(bdenssw) test...
  !
  !---------------------------------------------------------------

  implicit none
  integer :: isb,j,iptcl,imc
  real(fp) :: zmax

  !---------------------------------------------------------------
  !
  if(nsfast.eq.0) return
  !
  if(sawflag.or.sawflag2) then
     zmax=0.0_fp
     do isb=1,nsfast
        zmax=max(zmax,maxval(bdenssw(lcentr:ledge,isb)))
     end do
     if(zmax.gt.0.0_fp) then
        sawflag=.true.
        sawflag2=.true.
     else
        write(nonlin,*) ' %sawnbi_prof: no sawtooth data found!'
        sawflag=.false.
        sawflag2=.false.
     end if
  end if
  !
  if(.not.sawflag) return
  !
  if(lcentr.eq.0) then
     lcentr=2
     ledge=lcentr+nzones-1
  end if
  !
  do isb=1,nsfast  ! loop over species
     !
     !  sawtooth applied...
     !
     do j=lcentr,ledge
        bdenss(j,isb)=bdenssw(j,isb)
        udenspl(j,isb)=udensplw(j,isb)
        udenspp(j,isb)=udensppw(j,isb)
        ebapp2s(j,isb)=ebapp2sw(j,isb)
        ebapl2s(j,isb)=ebapl2sw(j,isb)
        omegabs(j,isb)=omegabsw(j,isb)
     end do
     !
  end do                             ! loop over species
  !
  return
end subroutine sawnbi_prof
