    
   	program correlate

	integer np,npnts,ntrace,it,ip,id,ii,i
	complex esoil(200,4,42),c(200,4,21)
	complex cc(4,21),norm(200,4)
	character targetfile*12,outfile*12


	np=4
	npnts=21

	read *, ntrace
	read (*,'(a12)') targetfile
	read (*,'(a12)') outfile   
	open(unit=14,file=targetfile)
	open(unit=15,file=outfile)


      do 50 it=1,ntrace                
      do 51 ip=1,np
        read(14,100)
      do 52 id=1,npnts
         read(14,*) esoil(it,ip,id)
	esoil(it,ip,43-id)=esoil(it,ip,id)
 52	continue 
 51	continue 
 50	continue  
 100	format(1x) 		

	do 67 ip=1,np
	do 68 id=1,npnts
	cc(ip,id)=cmplx(0.0,0.0)
	do 69 it=1,ntrace
	c(it,ip,id)=cmplx(0.0,0.0)
	do 70 ii=1,npnts
	c(it,ip,id)=c(it,ip,id)+esoil(it,ip,ii)
     *   *conjg(esoil(it,ip,ii+(id-1)))
   70	continue

	if (id .eq. 1) then
	norm(it,ip)=c(it,ip,1)
	else
	   continue
	   endif

	c(it,ip,id)=c(it,ip,id)/norm(it,ip)
	cc(ip,id)=cc(ip,id)+c(it,ip,id)/(ntrace*1.0)

   69	continue	
 68	continue
 67	continue
	
	do 80 i=1,npnts
	

	write(15,*) i,real(cc(1,i)),aimag(cc(1,i)),cabs(cc(1,i)),
     *	              real(cc(4,i)),aimag(cc(4,i)),cabs(cc(4,i)),
     *	              real(cc(2,i)),aimag(cc(2,i)),cabs(cc(2,i))	

  80    continue 
	

	close(14)
	close(15)



	stop

	end
