subroutine rf_wavecode_call(ilprep,ilexec,ilread,ilserv,zcp_time,ierr)

  !  generate a single wavecode call.
  !  rfxqlo has been loaded to supply necessary input data

  use logmod
  use rfxqlo_interface
!MG  use trmpi
  implicit NONE

  !  DMC Mar 2006 -- add capability for "partial calls"; we will support
  !  i/o and execution separately, to enable coupling of MPI-parallel TORIC5
  !  RF wave code server.  CAUTION: other codes won't support this!

  logical, intent(in) :: ilprep ! .TRUE. to prep input for wave code
  logical, intent(in) :: ilexec ! .TRUE. to exectue wave code
  logical, intent(in) :: ilread ! .TRUE. to read output of wave code

  logical, intent(in) :: ilserv ! .TRUE. if separate server process was used
  real*8, intent(out) :: zcp_time  ! cpu time in server (if applicable)

  integer, intent(out) :: ierr  ! status: 0=OK 
  !------------------------------------
  type (rfxqlo_system) :: zsys
  type (rfxqlo_rfgrid) :: zgrid
  integer :: lunmsg = 6

  logical :: iall
  integer :: iexec
  character*200 zcwd
  !------------------------------------
  zcp_time = 0

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

  iexec=1

  !  see also rfprep.for

  iall = ilprep.and.ilexec.and.ilread
  if((.not.iall).and.(zsys%wavecode.ne.'TORIC5')) then
     write(lunmsg,*) ' ?rf_wavecode_call: wavecode: ',trim(zsys%wavecode)
     write(lunmsg,*) '  this option does not support segmented execution!'
     write(lunmsg,*) '  call has: ilprep,ilexec,ilread =',ilprep,ilexec,ilread
     ierr=88
     return
  endif

  if (zsys%wavecode.eq.'TORIC5') then

     ! Brambilla's TORIC4 (2003 updated TORIC) (now TORIC5-- Mar 2006);
     !   lshoot=7 -- simplified equilibrium geometry
     !   lshoot=8 -- true numerical equilibrium geometry

     ! Execute in SUBdirectory -- make sure it exists!

     ierr = 0
!!! (driver must take care of this)         call gmkdir(' ',zsys%filepath,ierr)

     if(ierr.ne.0) then
        write(lunmsg,*) &
             ' ?rf_wavecode_call: icrf work subdirectory creation failed!'
        write(lunmsg,*) '  subdirectory: ',trim(zsys%filepath)
     else

        !  save current working directory
        call sget_cwd(zcwd)
        call rfxqlo_rfgrid_get(zgrid,ierr)
        if(ierr.ne.0) then
           write(lunmsg,*) ' ?rf_wavecode_call: rfxqlo_system_get failure zgrid.'
           return
        endif

        !  go to ICRF work directory
        if(ilexec.or.ilprep) then
           ! 03/19/2013 CLF: Mark if serial
           if (zgrid%ncpu .gt. 1) then
              call trmpi_requestor('CHANGE_DIRECTORY',trim(zsys%filepath))
           else
              call trmpi_requestor('!CHANGE_DIRECTORY',trim(zsys%filepath))
           endif
        else
           call sset_cwd(trim(zsys%filepath),ierr)
           if(ierr.ne.0) call errmsg_exit('?sset_cwd in rf_wavecode_call')
        endif

        ! NOTE: when a server is used, this call is only dealing with
        ! input and output, not actually executing the wave code.  See
        ! server launch in caller code (rf_wavecode_exec(...)).

        call toric4_fpdrive(ilprep,ilexec,ilread, &
             ilserv, zcp_time)

        ! if a server was used, the zcp_time is set to a non-zero value.

        ! return to original working directory
        if(ilexec.or.ilprep) then
           ! 03/19/2013 CLF: Mark if serial
           if (zgrid%ncpu .gt. 1) then
              call trmpi_requestor('CHANGE_DIRECTORY',trim(zcwd))
           else   
              call trmpi_requestor('!CHANGE_DIRECTORY',trim(zcwd))
           endif
        else
           call sset_cwd(trim(zcwd),ierr)
           if(ierr.ne.0) call errmsg_exit('?sset_cwd in rf_wavecode_call')
        endif

     endif

  else
     ! error check
     write(lunmsg,*) ' ?rf_wavecode_call: unknown wave code: '// &
          trim(zsys%wavecode)
     iexec=0

  endif
  !
  if(iexec.eq.0) then
     ierr=1
  endif

end subroutine rf_wavecode_call
