!-----subroutine---------------------------------------------g.guidati--
! 
     subroutine DETFIELD
! 
!-----------------------------------------------------------------------
!               
!     franco guidati    IAG
!                       Uni Stuttgart
!
! 
!     scope             determine sigma distribution
! 
!.......................................................................
!               declarations
!.......................................................................
      USE TINoiseGeneric
      USE TINoiseInput
      USE TINoiseDDD
      USE TINoiseRHSin
      USE TIPrecision                                                 
                                                                      
IMPLICIT                        NONE                                  
                                                                     
   ! Local variables.                                                 
                                                                      
INTEGER(4)                   :: klo,khi,i,n_in,nvar,nj,j1,j2,ivar,nok,nbad                                                                    
REAL(DbKi)                   :: kk2, Isumwell, Isum, Itot,eps,h1,hmin
REAL(DbKi)                   :: ys1,ys2,poverall,phipot
REAL(DbKi)                   :: bigben(2),bigben2(2),dtime,etime
REAL(DbKi)                   :: phif(10)

COMPLEX(DbKi)                :: value,dval1,dval2,dval11,dval12,dval22 
COMPLEX(DbKi)                :: btrans,btrans1,btrans2,btrans11,btrans12,btrans22
COMPLEX(DbKi)                :: bbb,bbb1,bbb2

EXTERNAL         :: SIGSUM,rkqs , RHSI

      nvar    =  10
      eps     =  1.0D-04
      h1      =  0.1d0
      hmin    =  0.0d0
      lderiv = .false.
     
!.......................................................................
!              
!.......................................................................

!	write(79,*) 'ZONE I=
 !     write(*,*) etime(bigben2),dtime(bigben)
      ys1 = -200.0
      ys2 = 100.0

      poverall = 0.0
      nj = 101
!      write(722,*) 'variables = x1,x2,b,b1,b2,b11,b12,b22'
!      write(722,*) 'ZONE I=',nj,', J=',nj
      
      
      do j1=1,nj
	write(*,*) j1
        do j2=1,nj

	  x1 = (  -1.0 + 3.0 * dble(j1) / dble(nj)  ) 
	  x2 = -1.500324 + 3.0 * dble(j2) / dble(nj)
	  
	  do ivar=1,nvar
	    phif(ivar) = 0.0
	  end do

 	  do ipath=1,npath
!            if(ipath.eq.npath/2+1) then
            call RHSINT(value,dval1,dval2,dval11,dval12,dval22)

            phif( 1) = phif( 1) + dble(value)
            phif( 2) = phif( 2) + dble(-imag*value)
            phif( 3) = phif( 3) + dble(dval1)
            phif( 4) = phif( 4) + dble(-imag*dval1)
            phif( 5) = phif( 5) + dble(dval2)
            phif( 6) = phif( 6) + dble(-imag*dval2)
            phif( 7) = phif( 7) + dble(dval11)
            phif( 8) = phif( 8) + dble(-imag*dval11)
            phif( 9) = phif( 9) + dble(dval12)
            phif(10) = phif(10) + dble(-imag*dval12)
!            endif
	  end do
          s1 = 0.0d0
          s2 = sworkat(na+1)
          call odeint(phif,nvar,s1,s2,eps,h1,hmin,nok,nbad,SIGSUM,rkqs)

          btrans   = dcmplx(phif(1),phif(2))
          btrans1  = dcmplx(phif(3),phif(4))
          btrans2  = dcmplx(phif(5),phif(6))
          btrans11 = dcmplx(phif(7),phif(8))
          btrans12 = dcmplx(phif(9),phif(10))
	  btrans22 = -btrans11 - kwave2 * btrans
	  phipot = x1 
	  bbb = btrans * exp(-imag*kwave*mach_ti*phipot)
          bbb1 = (btrans1-btrans*imag*kwave*mach_ti)* exp(-imag*kwave*mach_ti*x1)
          bbb2 = btrans2 * exp(-imag*kwave*mach_ti*x1)
!	  write(722,*) real(x1),real(x2),real(bbb),real(-imag*bbb)
!     >                 ,real(btrans1),real(btrans2),
!     >                 real(btrans11),real(btrans12),real(btrans22)
	end do
      end do
!      call flush(722)

    !  write(*,*) etime(bigben2),dtime(bigben)

!.......................................................................
!               end of subroutine
!.......................................................................
      return
!***********************************************************************
      end 
