subroutine rf_wavecode_exec(rf_wavecode_call,npserve,zcp_serve,ierr)

  !  call the RF wavecode, looping over all frequencies and Nphi values

  use rfxqlo_interface
  use execsystem

  implicit NONE

  external rf_wavecode_call       ! actual call in here

  integer, intent(in) :: npserve  ! >0: MPI TORIC call in memory
                                  ! <0: MPI TORIC subprocess
                                  ! =0: serial call in memory

  real*8, intent(out) :: zcp_serve  ! if npserve<0: cpu time used by subprocess
  integer, intent(out) :: ierr    ! -- call rf_wavecode_call(ierr)

  !--------------------------------------
  type (rfxqlo_system) :: zsys
  type (rfxqlo_dims) :: zdims
  type (rfxqlo_antenna) :: zant

  integer :: lunmsg = 6

  integer :: iant
  integer :: inphi

  logical :: ilprep,ilexec,ilread
  character*20 sname,runid,zlbl
  character*200 workpath

  !  this should probably be user controllable e.g. via rfx_sys...
  character*4 :: ztime_out = '7200' ! *temporary* -- 2 hour timeout

  !  abs(nPserve) = #cpus (nominally at least) encoded as ascii:
  !  (only used if nPserve < 0):
  character*6 :: in_cpu
  character*120 :: zzfile,filelog
  character*200 :: env_tor,env_xe,env_mpi_cmd
  character*300 :: zcmd
  character(120) :: zdir1,zdir2,zdir3
  integer :: ntor,itor_nprocs
  integer :: atoi
  integer :: n_antenna

  real*8 :: zcpu

  !--------------------------------------
  zcp_serve = 0
  filelog='/dev/null'

  call rfxqlo_system_get(zsys,ierr)
  if(ierr.ne.0) then
     write(lunmsg,*) ' ?rf_wavecode_exec: rfxqlo_system_get failure.'
     return
  endif

  call rfxqlo_dim_get(zdims,ierr)
  if(ierr.ne.0) then
     write(lunmsg,*) ' ?rf_wavecode_exec: rfxqlo_dim_get failure.'
     return
  endif

  n_antenna = zdims%n_antenna

  runid = zsys%run_id
  workpath = zsys%filepath

  !  preliminary cleanup

  ilprep = .FALSE.
  ilexec = .FALSE.
  ilread = .FALSE.

  call antloop

  if(npserve.eq.0) then

     !  do all in one call...

     ilprep = .TRUE.
     ilexec = .TRUE.
     ilread = .TRUE.

     call antloop

  else

     !  do all the prep in this loop...

     ilprep = .TRUE.
     ilexec = .FALSE.
     ilread = .FALSE.

     call antloop

     if(npserve.gt.0) then

        ilprep = .FALSE.
        ilexec = .TRUE.
        ilread = .FALSE.

        call antloop

     else

        call mpi_sget_env('XE',env_xe,ierr)
        call mpi_sget_env('TOR_NPROCS',env_tor,ierr)
        call mpi_sget_env('MPI_CMD',env_mpi_cmd,ierr)
        call sget_cwd(zdir1)   ! save $cwd
        call sset_cwd(trim(workpath),ierr)
        call sget_cwd(zdir2)
        write(lunmsg,*)'TORIC5 Top: Directory changed to= ',trim(zdir2)

        itor_nprocs=atoi(env_tor,1)
        ntor=itor_nprocs
        do iant=1,n_antenna
           call rfxqlo_select_antenna(iant,ierr)
           if(ierr.ne.0) write(lunmsg,*) ' ...error on antenna #',iant
           if(ierr.ne.0) exit

           call rfxqlo_antenna_get(zant,ierr)

           do inphi = 1,zant%num_nphi
              call rfxqlo_select_nphi(inphi,ierr)
              if(ierr.ne.0) write(lunmsg,*) ' ...error on Nphi #',inphi
              if(ierr.ne.0) exit
              if(zant%power* &
                   zant%wnphi_vac(inphi).gt. &
                   0.0d0) then
                 call rfxqlo_rfbasic_lbl(zlbl)
                 zzfile = trim(runid)//'_'// &
                      trim(zlbl)//'_torica.inp'

                  zcmd=trim(adjustl(env_mpi_cmd))//' -np '//trim(adjustl(env_tor))//' '&
                       //trim(adjustl(env_xe))//'/mpi_toric_main '//trim(zzfile)//' > '//trim(adjustl(filelog))
                  write(lunmsg,*)'zcmd= ', trim(zcmd)
                  ierr=jsystem(trim(adjustl(zcmd)))
                  write(lunmsg,*)' mpi_toric_main status =',  ierr
              endif
           enddo
        enddo
!     cd back to the TRANSP working directory

      call sset_cwd(trim(zdir1),ierr)
      call sget_cwd(zdir3)
      write(lunmsg,*)'TORIC5 Bottom: Current directory= ',trim(zdir3)
        
        !  execute wave code calculations in server process...
     
 !MG       sname = zsys%wavecode
 !MG         call ulower(sname)

 !MG         in_cpu = ' '
 !MG         write(in_cpu,'(i6)') min(999999,max(1,abs(npserve)))

 !MG        zcmd = 'launch_service  '//trim(sname)//'  '//trim(runid)//'  '// &
 !MG            trim(workpath)//'  '//in_cpu//'  default  '//ztime_out

 !MG        ierr = jsystem(trim(zcmd))

     endif

     if(ierr.eq.0) then

        !  read the results in this loop

        ilprep = .FALSE.
        ilexec = .FALSE.
        ilread = .TRUE.

        call antloop
     endif

  endif

  !-------------> check/renorm fields due to power absorption vs. nphi

  call rfxqlo_all_renorm_check(ierr)

  !-------------

  contains

    subroutine antloop

      character*120 zzfile

      ! loop over antenna and N_phi...

      do iant=1,n_antenna
         call rfxqlo_select_antenna(iant,ierr)
         if(ierr.ne.0) write(lunmsg,*) ' ...error on antenna #',iant
         if(ierr.ne.0) exit

         call rfxqlo_antenna_get(zant,ierr)

         do inphi = 1,zant%num_nphi
            call rfxqlo_select_nphi(inphi,ierr)
            if(ierr.ne.0) write(lunmsg,*) ' ...error on Nphi #',inphi
            if(ierr.ne.0) exit

            if(ilprep.or.ilexec.or.ilread) then
               if(zant%power* &
                    zant%wnphi_vac(inphi).gt. &
                    0.0d0) then

                  call rf_wavecode_call(ilprep,ilexec,ilread, &
                       ilread.AND.(npserve.lt.0),zcpu, ierr)
                  if(ierr.ne.0) exit
                  zcp_serve = zcp_serve + zcpu

               endif

            else
               !  cleanup; ignore errors...

               call rfxqlo_rfbasic_lbl(zlbl)
               zzfile = trim(workpath)//'/'//trim(runid)//'_'// &
                    trim(zlbl)//'_torica.inp'
               open(unit=zsys%lunut(1),file=trim(zzfile),status='old', &
                    iostat=ierr)

               if(ierr.eq.0) then
                  !  clean up...
                  close(unit=zsys%lunut(1))
                  call fwc_delete(trim(workpath), &
                       trim(runid)//'_'//trim(zlbl)//'*',ierr)
               endif

               ierr=0
            endif

         enddo
         if(ierr.ne.0) exit

      enddo

    end subroutine antloop

end subroutine rf_wavecode_exec
