program SourceLocate
  use sep
  use sourcemod
  implicit none

  integer:: n1,n2,n3,nx,ny,nz,ns,nh,ntr,verbose,n
  integer:: ix,iy,is,iz,its,update,st
  real::z,x,y,s,v,shx,shy,shz,ox,dx,oy,dy,oz,dz,os,ds
  real:: zout,xout,yout,vout,sout,eps,lt,mint
  real,allocatable,dimension(:,:):: head,model,models
  real,allocatable,dimension(:)  :: time,timeout

  call exh_search_init(shx,shy,shz,n1,ox,dx,oy,dy,oz,dz,os,ds,nx,ny,nz,ns,n,verbose)
!  write(0,*) '       ************asdfasdfasdf*************          '
!  write(0,*)' sx,sy,sz',shx,shy,shz
!  write(0,'(a,3f10.2,f10.7,f10.2)')' ox,oy,oz,os,ov',ox,oy,oz,os,1/os
!  write(0,'(a,3f10.2,f10.7)')' dx,dy,dz,ds',dx,dy,dz,ds
!  write(0,'(a,4i4)')' nx,ny,nz,ns',nx,ny,nz,ns
!  write(0,*) '       ************asdfasdfasdf*************          '
  allocate(time(n1),timeout(n1),model(4,1),models(size(model,1)+1,n))
  time=0.;timeout=0.;model=0.

  mint=100000.
  models(5,:)=mint

  call sep_read(time)

  its=0
  update=0
  slo:do is=1,ns
     s=(os+ (is-1)*ds)
     v=1/(os+ (is-1)*ds)
     write(0,*) 'velocity',v
     dep:do iz=1,nz
        z=oz+ (iz-1)*dz
        xpo:do ix=1,nx
           x=ox+ (ix-1)*dx
           ypo:do iy=1,ny
              its=its+1
              y=oy+ (iy-1)*dy
              model(1,1)=x
              model(2,1)=y
              model(3,1)=z
              model(4,1)=s
              st=sourcemod_op(model,1)
              call getpicks(timeout)
              call normmeasure(time,timeout,lt)

              if(lt.lt.mint) then
                 if(verbose.gt.5)write(0,*)' Residual',lt,' (previous residual:',mint,')'
                 xout=x
                 yout=y
                 zout=z
                 vout=v
                 sout=s
                 mint=lt
                 update=update+1
                 call rankupdates(model,models,lt,update)
                 if(verbose.gt.9.and.lt.lt.eps) then
                    write(0,*)'Possible winner:'
                    write(0,*)'  error',lt,'update number:',update
                    write(0,'(a,5f10.2)')' x,y,z,v',xout,yout,zout,vout
                    write(0,*) ' -        -        -        -        -        -'
                 end if
              end if
           end do ypo
        end do xpo
     end do dep
  end do slo

  if(update.gt.1.or.mint.le.eps) then
     write(0,*) 'Final residual :',mint
     write(0,*) 'Updated :',update,'times'
     write(0,*)' model space loops',its
  else
     write(0,*)' failed to update'
  end if
  
  model(1,1)=xout
  model(2,1)=yout
  model(3,1)=zout
  model(4,1)=sout
  st=sourcemod_op(model,1)
  call getpicks(timeout)
  call sep_write(timeout)
  write(0,*)' * ** * ** '
  write(0,*)' Best',n, 'models'
  do ix=1,n
     write(0,*)ix,':',models(1:5,ix)
  end do
  write(0,*) '**************************************************' 
  write(0,*) ' Model space input parameter ranges '
  write(0,*)' X :',ox,' to ',(nx-1)*dx+ox,' answer:',xout
  write(0,*)'   X :',ox-shx,' to ',(nx-1)*dx+ox-shx,' answer:',xout-shx
  write(0,*)' Y :',oy,' to ',(ny-1)*dy+oy,' answer:',yout
  write(0,*)'   Y :',oy-shy,' to ',(ny-1)*dy+oy-shy,' answer:',yout-shy
  write(0,*)' Z :',oz,' to ',(nz-1)*dz+oz,' answer:',zout
  write(0,*)'   Z :',oz-shz,' to ',(nz-1)*dz+oz-shz,' answer:',zout-shz
  write(0,*)' S :',os,' to ',(ns-1)*ds+os,' answer:',sout
  write(0,*)'   V :',1/os,' to ',1/((ns-1)*ds+os),' answer:',1/sout


end program SourceLocate
