subroutine trxpl_ascii_write(apath,kx,kr)
 
  !  write ASCII file of accumulated scalar and profile data...
 
  implicit NONE
 
  character*(*), intent(in) :: apath   ! filename
  integer, intent(in) :: kx            ! grid option for flux coordinate
  integer, intent(in) :: kr            ! grid option for major radius
 
  include 'trxpl.blk'
 
  !------------------------------------------------------
  real*8, dimension(:), allocatable :: zxtmp,zx,zr,zfx,zfr
  integer :: inumx,inumr,intmp,idum,ierr,i,j,k,ios,ilf
  integer :: id_rho=0,id_rhozc=0,id_rmajm=0,id_rmjsym=0,inumpx,inumpr
  !------------------------------------------------------
 
  call eq_ganum('__RHO',id_rho)
  call eq_ganum('RHO_ZC',id_rhozc)
  call eq_ganum('RMAJM',id_rmajm)
  call eq_ganum('RMJSYM',id_rmjsym)
 
  inumpx=0
  inumpr=0
  do i=1,numpro
     if((id_xaxis(i).eq.id_rho).or.(id_xaxis(i).eq.id_rhozc)) then
        inumpx=inumpx+1
     else if((id_xaxis(i).eq.id_rmajm).or.(id_xaxis(i).eq.id_rmjsym)) then
        inumpr=inumpr+1
     else
        write(6,*) 'profile skipped: x axis not recognized: ',prolabs(1:3,i)
     endif
  enddo
 
 
  !  setup user requested flux coord grid
 
  call eq_ngrid(id_rho,intmp)
  allocate(zxtmp(intmp))
  call eq_grid(id_rho,zxtmp,intmp,idum,ierr)
  if(ierr.ne.0) call errmsg_exit(' ?trxpl: code error, trxpl_ascii_write')
 
  if(kx.gt.0) then
     inumx=1+kx*(intmp-1)
     allocate(zx(inumx))
     zx(1)=zxtmp(1)
     do i=2,inumx
        j=1+(i-1)/kx    ! if kx=3: 1 1 2 2 2 3 3 3 ...
        k=i-(j-1)*kx-1  ! if kx=3: 1 2 0 1 2 0 1 2 ...
        if(j.eq.intmp) then
           j=intmp-1
           k=k+kx
        endif
        zx(i)=(zxtmp(j)*(kx-k)+zxtmp(j+1)*k)/kx
     enddo
  else
     inumx=-kx
     allocate(zx(inumx))
     do i=1,inumx
        zx(i)=(zxtmp(1)*(inumx-i)+zxtmp(intmp)*(i-1))/(inumx-1)
     enddo
  endif
  deallocate(zxtmp)
 
  allocate(zfx(inumx))  ! data buffer
 
  !  setup user requested major radius coord grid
 
  call eq_ngrid(id_rmajm,intmp)
  allocate(zxtmp(intmp))
  call eq_grid(id_rmajm,zxtmp,intmp,idum,ierr)
  if(ierr.ne.0) call errmsg_exit(' ?trxpl: code error, trxpl_ascii_write')
 
  if(kr.gt.0) then
     inumr=1+kr*(intmp-1)
     allocate(zr(inumr))
     zr(1)=zxtmp(1)
     do i=2,inumr
        j=1+(i-1)/kr    ! if kr=3: 1 1 2 2 2 3 3 3 ...
        k=i-(j-1)*kr-1  ! if kr=3: 1 2 0 1 2 0 1 2 ...
        if(j.eq.intmp) then
           j=intmp-1
           k=k+kr
        endif
        zr(i)=(zxtmp(j)*(kr-k)+zxtmp(j+1)*k)/kr
     enddo
  else
     inumr=-kr
     allocate(zr(inumr))
     do i=1,inumr
        zr(i)=(zxtmp(1)*(inumr-i)+zxtmp(intmp)*(i-1))/(inumr-1)
     enddo
  endif
  deallocate(zxtmp)
 
  allocate(zfr(inumr))  ! data buffer
 
  !---------------------------------------
  !  open file... move existing one out of way if necessary
 
  ilf=len_trim(apath)
 
  open(unit=99,file=apath,status='old',iostat=ios)
  if(ios.eq.0) then
     close(unit=99)
     write(6,*) ' %trxpl warning: file exists: ',apath(1:ilf)
     write(6,*) '  renamed to: ',apath(1:ilf)//'~'
     call frename(apath(1:ilf),apath(1:ilf)//'~',idum)
  endif
 
  open(unit=99,file=apath,status='new',iostat=ios)
  if(ios.ne.0) then
     write(6,*) ' ?trxpl: open failure: ',apath(1:ilf)
     write(6,*) '  data not written.'
     deallocate(zx,zfx,zr,zfr)
     return
  endif
 
  !---------------------------------------
  !  write scalars
 
  write(99,'(" ---> scalar_info: numsc=",i4)') numsc
  do i=1,numsc
     write(99,1001) sclabs(1,i),sclabs(2,i),sclabs(3,i),scvals(i)
1001 format(1x,a,1x,a,a,1x,' = ',1pd13.6)
  enddo
 
1009 format(1x,1pd13.6)
 
  !---------------------------------------
  !  write profiles vs. x
 
  write(99,'(" ---> profiles_vs_x: numpro=",i4," inumx=",i4," x follows:")') &
       inumpx,inumx
  do i=1,inumx
     write(99,1009) zx(i)
  enddo
  do j=1,numpro
     if((id_xaxis(j).eq.id_rho).or.(id_xaxis(j).eq.id_rhozc)) then
        call eq_rgetf(inumx,zx,id_pros(j),0,zfx,ierr)
        if(ierr.ne.0) call errmsg_exit(' ?trxpl: internal error 1.')
        write(99,1010) prolabs(1,j),prolabs(2,j),prolabs(3,j)
        do i=1,inumx
           write(99,1009) zfx(i)
        enddo
     endif
  enddo
 
  !---------------------------------------
  !  write profiles vs. R
 
  write(99,'(" ---> profiles_vs_R: numpro=",i4," inumR=",i4," R (m) follows:")') &
       inumpr,inumr
  do i=1,inumR
     write(99,1009) zr(i)
  enddo
  do j=1,numpro
     if((id_xaxis(j).eq.id_rmajm).or.(id_xaxis(j).eq.id_rmjsym)) then
        call eq_xgetf(inumr,zr,id_pros(j),0,zfr,ierr)
        if(ierr.ne.0) call errmsg_exit(' ?trxpl: internal error 1.')
        write(99,1010) prolabs(1,j),prolabs(2,j),prolabs(3,j)
        do i=1,inumr
           write(99,1009) zfr(i)
        enddo
     endif
  enddo
 
1010 format(1x,'name: ',a,a,1x,'units: ',a,' data:')
 
  close(unit=99)
 
  deallocate(zx,zfx,zr,zfr)
  write(6,*) ' --------------------------------------- '
  write(6,*) ' succesfully wrote: ',apath(1:ilf)
  write(6,*) ' --------------------------------------- '
 
  return
 
end subroutine trxpl_ascii_write
