program main
  use killwave_mod
  use killwave_ns
  implicit none

    integer             :: i    
    call initpar()
    dip_='dip'; A_='A'; ATA_='ATA';  FILT_='FILT'; infile_='infile'; outfile_='outfile'
        
    if (exist_file(infile_))then
        call from_aux(infile_,"n1",n1); call from_aux(infile_,"d1",d1); call from_aux(infile_,"o1",o1)
        call from_aux(infile_,"n2",n2); call from_aux(infile_,"d2",d2); call from_aux(infile_,"o2",o2)
        call from_aux(infile_,"n3",nrho); call from_aux(infile_,"d3",drho); call from_aux(infile_,"o3",orho)
        allocate( infile(n1*n2*nrho)); 
        allocate( inslice(n1*n2)); 
        allocate(outslice(n1*n2)); 
        call sep_read(infile, infile_)
        if(exist_file(dip_))then
                allocate(   dips(n1*n2*nrho)); 
                call sep_read(dips,dip_)
        endif
        allocate (ptch(n1*n2*nrho),n_nfilt(1))
        all=.false.
    else
        call from_param('n1',n1, 64); call from_param('d1',d1, 10.); call from_param('o1',o1, 0.); 
        call from_param('n2',n2, 32); call from_param('d2',d2, 10.); call from_param('o1',o1, 0.); 
        call from_param('n3',nrho, 1 ); call from_param('d3',d3, 10.); call from_param('o1',o1, 0.); 
        allocate(   dips(n1*n2*nrho)); 
        allocate( infile(n1*n2*nrho) ); 
        allocate(outfile(n1*n2*nrho) ); 
        allocate (ptch(n1*n2*nrho),n_nfilt(1))
        call random_number(infile)
   endif        

   call params()
   if (rand_in)call random_number(infile)
   do iang=1,nang
        dip=oang+(iang-1)*dang
        call ns_init(dip,eps)
        if (verb>3) write(0,'(A30,I4,A3,I4)')'Working on angle', iang, ' of', nang 
        do irho=1,nrho
                inslice=infile(1+n1*n2*(irho-1):n1*n2*irho)
                call ns_filt_lop(.false.,.false.,inslice,outslice)
                if (verb>4) write(0,'(A30,I4,A3,I4,A8,I4,A3,I4)')'Writing rho',irho,' of', nrho, ' angle', iang, ' of', nang 
                call srite("outfile", outslice, 4*n1*n2)
	enddo
   enddo
end program
