      subroutine sf_box_complex(iuo,nla,nsfcomp,ndimsf,nsfbox,
     &                      nxs,field_real,field_imag,sfarray)
c sfbox for complex field. bazavov/berg jan 17 2006.
c nla(ndimsf) -- dimensions of the sublattice were sf is calculated
c nsfcomp -- array with components of \vec{n} with 
c            nsfcomp(...,0)=n_1^2+n_2^2+...+n_ndimsf^2
c field_real/imag -- real and imaginary part of the complex field.
c returns: sfarray -- values of sf for modes from 1 to nsfbox.
      include '../../libs/fortran/implicit.sta'
      include '../../libs/fortran/constants.par'
      dimension nla(ndimsf),ix(ndimsf),nsfcomp(nsfbox,0:ndimsf)
      dimension field_real(nxs),field_imag(nxs),sfarray(nsfbox)
      ltest=.true.
      ltest=.false.
      if(ltest) then
        write(iuo,'(/,2x,"sf_box_complex:")') 
        write(iuo,'(2x,"nsfbox,ndimsf:",2i9)') nsfbox,ndimsf
        write(iuo,'(2x,"nxs,nla:",6i9)') nxs,nla
        write(iuo,'(2x,"nsfcomp:",6i9)') nsfcomp
      endif

      do in=1,nsfbox ! loop over sf components.
        sfre=zero
        sfim=zero
        do is=1,nxs ! loop over sites.
          call ixcor(ix,nla,is,ndimsf) ! coordinates from site index.
          prod=zero ! calculate value in the exponent:
          do i=1,ndimsf ! vec{k}\cdot\vec{x} product.
            prod=prod+(nsfcomp(in,i)*ix(i)*one)/nla(i)
          end do
          prod=prod*tpi
          expcos=cos(prod)
          expsin=sin(prod)
          sfre=sfre+field_real(is)*expcos-field_imag(is)*expsin
          sfim=sfim+field_real(is)*expsin+field_imag(is)*expcos
        end do
        sfre=sfre/nxs
        sfim=sfim/nxs
        sfarray(in)=sfre*sfre+sfim*sfim
      end do

      if(ltest) write(iuo,'("sfarray:",4g15.6)') sfarray
      if(ltest) stop "sf_box_complex: ltest."
      return
      end
