subroutine rfxqlo_global_renorm(ierr)

  ! After all wavefields have been computed with vacuum power normalization,
  ! renormalize taking coupling efficiency (or equivalently the antenna load)
  ! spectrum.  The RF wavecode sets the mode coupling efficiency or antenna
  ! load on a per n_phi per antenna basis with calls to "rfxqlo_set_coupling";
  ! see rfxqlo_wavefields.f90.

  ! the operation applies to wavefields and power profiles for all nphi for
  ! the currently selected antenna.

  ! the wavefield objects should all be in nstate=0 (no field) or nstate=1
  ! (vacuum field normalization).  If any are in nstate=2 ierr=1 is returned
  ! and no action is taken.

  use rfxqlo_private
  implicit NONE

  integer, intent(out) :: ierr

  !-----------------------------------
  integer :: iant,iphi,ii,inum,inum_active,iflag
  real*8, dimension(:), allocatable :: vac_powers,coupled_powers,renorm
  real*8 :: vacsum,cpl,cplsum,apower
  !-----------------------------------

  iant = selected_antenna
  inum = antenna_array(iant)%num_nphi

  ierr = 0
  if(inum.le.1) return

  apower = antenna_array(iant)%power
  if(apower.le.0.0d0) return

  inum_active = 0

  do iphi=1,inum
     if(.not.calculated(iphi,iant)) then
        write(lunmsg,*) ' ?rfxqlo_global_renorm: wavefield not calculated:'
        write(lunmsg,*) '  antenna# ',iant,' Nphi index ',iphi,'.'
        ierr=1
        exit
     else if(wavefields(iphi,iant)%nstate.eq.2) then
        ierr=1
        write(lunmsg,*) ' ?rfxqlo_global_renorm: wavefield nstate=2 already!'
        exit
     else if(wavefields(iphi,iant)%nstate.eq.1) then
        inum_active=inum_active + 1
     endif
  enddo
  if(ierr.ne.0) return

  if(inum_active.eq.1) then
     do iphi=1,inum
        if(wavefields(iphi,iant)%nstate.eq.1) then
           wavefields(iphi,iant)%nstate=2
        endif
     enddo
     ierr = 0
     return  ! no renorm necessary, if only one mode has power.
  endif

  ! multiple modes have power, so...

  allocate(vac_powers(inum),coupled_powers(inum),renorm(inum))

  vacsum=0.0d0
  cplsum=0.0d0
  iflag=0

  do iphi=1,inum
     if(wavefields(iphi,iant)%nstate.eq.1) then
        vac_powers(iphi) = wavefields(iphi,iant)%mode_power
        if(vac_powers(iphi).lt.0.0d0) vac_powers(iphi)=0.0d0
        cpl = wavefields(iphi,iant)%mode_coupling
        coupled_powers(iphi) = vac_powers(iphi)*cpl
        vacsum = vacsum + vac_powers(iphi)
        cplsum = cplsum + coupled_powers(iphi)
        if((vac_powers(iphi).gt.0.0d0).and.(cpl.le.0.0)) iflag=iphi
     else
        vac_powers(iphi)=0.0d0
        coupled_powers(iphi)=0.0d0
     endif
  enddo

  if(iflag.gt.0) then
     ierr=2
     write(lunmsg,*) ' ?rfxqlo_global_renorm: antenna#',iant,' nphi index#', &
          iflag,' nphi=',antenna_array(iant)%nphi(iflag)
     write(lunmsg,*) '  vacuum power .gt. 0: ',vac_powers(iflag),' ...but...'
     write(lunmsg,*) '  RF code specified coupling .le. 0: ', &
          wavefields(iflag,iant)%mode_coupling
     deallocate(vac_powers,coupled_powers,renorm)
     return
  endif

  if(abs(vacsum-apower)/apower .gt. 1.0d-4) then
     ierr=3
     write(lunmsg,*) ' ?rfxqlo_global_renorm: wavefield power inconsistency:'
     write(lunmsg,*) '  antenna power: ',apower
     write(lunmsg,*) '  summed wavefield mode power: ',vacsum
     deallocate(vac_powers,coupled_powers,renorm)
     return
  endif

  coupled_powers = coupled_powers * (apower/cplsum)
  do iphi=1,inum
     if(vac_powers(iphi).eq.0.0d0) then
        renorm(iphi) = 1.0d0
     else
        renorm(iphi) = coupled_powers(iphi)/vac_powers(iphi)
     endif
  enddo

  do iphi=1,inum
     if(abs(renorm(iphi)-1.0d0).gt.(1.0d-10)) then
        wavefields(iphi,iant)%eplus = wavefields(iphi,iant)%eplus * &
             sqrt(renorm(iphi))
        outpros(iphi,iant)%ext_pwrs = outpros(iphi,iant)%ext_pwrs * &
             renorm(iphi)

        call rfxqlo_prof_rescale(outpros(iphi,iant)%id_jrfe, renorm(iphi),ierr)
        if(ierr.ne.0) then
           write(lunmsg,*) ' ?rfxqlo_global_renorm: renormalization error.'
           exit
        endif

        do ii=1,rfxqlo_maxspc+2
           call rfxqlo_prof_rescale(outpros(iphi,iant)%id_pwrs(ii), &
                renorm(iphi),ierr)
           if(ierr.ne.0) exit
        enddo
        if(ierr.ne.0) then
           write(lunmsg,*) ' ?rfxqlo_global_renorm: renormalization error.'
           exit
        endif
     endif
     if(wavefields(iphi,iant)%nstate.eq.1) then
        wavefields(iphi,iant)%nstate = 2
        wavefields(iphi,iant)%mode_power = coupled_powers(iphi)
     endif
  enddo

  deallocate(vac_powers,coupled_powers,renorm)

end subroutine rfxqlo_global_renorm

subroutine rfxqlo_global_renorm_check(ierr)

  ! for selected_antenna: check fields for renormalization: execute if needed.

  use rfxqlo_private
  implicit NONE

  integer, intent(out) :: ierr

  !------------------------
  integer :: iant,inphi,isumn
  integer :: istate,ismin,ismax
  !------------------------

  ierr = 0

  isumn = 0
  ismin = 99
  ismax = 0

  iant = selected_antenna
  do inphi=1,antenna_array(iant)%num_nphi
     if(antenna_array(iant)%power* &
          antenna_array(iant)%wnphi_vac(inphi).gt.0.0d0) then
        isumn = isumn + 1
        istate = wavefields(inphi,iant)%nstate
        if(istate.eq.0) then
           write(lunmsg,*) ' ?rfxqlo_global_renorm_check: (inphi,iant) = ', &
                inphi,iant
           write(lunmsg,*) '  nstate = 0 -- wavefield calculation not found,'
           write(lunmsg,*) '  but input power is not zero.'
           ierr = ierr + 1
        else
           ismin=min(ismin,istate)
           ismax=max(ismax,istate)
        endif
     endif
  enddo
  if(ierr.ne.0) return

  if((isumn.gt.0).and.(ismin.ne.ismax)) then
     write(lunmsg,*) ' ?rfxqlo_global_renorm_check: field states inconsistent.'
     ! this means it looks like some nphi modes normalized and some are not...
     ierr=99
     return
  endif

  istate=ismin

  if(istate.eq.1) then
     if(.not.antenna_array(iant)%wnphi_renorm) return  ! if renorm not wanted
  endif

  if(istate.eq.2) return  ! if renorm already done

  if(isumn.eq.0) then
     ! no power on this antenna
     continue

  else if(isumn.eq.1) then
     ! if just one mode, no renorm needed; just mark as renormalized.

     do inphi=1,antenna_array(iant)%num_nphi
        if(antenna_array(iant)%power* &
             antenna_array(iant)%wnphi_vac(inphi).gt.0.0d0) then
        
           wavefields(inphi,iant)%nstate = 2  ! the only mode w/ non-zero power

        endif
     enddo

  else
     ! more than one nphi mode; global renorm IS needed

     call rfxqlo_global_renorm(ierr)

  endif

end subroutine rfxqlo_global_renorm_check

subroutine rfxqlo_all_renorm_check(ierr)

  ! for ALL antennas check renorm

  use rfxqlo_private
  implicit NONE

  integer, intent(out) :: ierr

  !------------------------
  integer :: iant,isel_save
  !------------------------

  isel_save = selected_antenna

  do iant=1,n_antenna

     selected_antenna=iant
     call rfxqlo_global_renorm_check(ierr)
     if(ierr.ne.0) exit

  enddo

  selected_antenna = isel_save

end subroutine rfxqlo_all_renorm_check
