module dip_prog 

  use sep
  use OW_type
  use OW_parms_areal
  use find_xz_dip_mod

  implicit none

  subroutine ow_dips(dat,dip)
  real, pointer 				:: dat(:),dat3(img%xm%n,img%z%n,1),dip3(img%xm%n,img%z%n,1),dip(:),coh(img%xm%n,img%z%n,1)
  integer 				:: n(3),ndim,n123
  character(len=20) 			:: method
  integer 				:: nwind(3),niter_dip,nl_iter,ierr,n3,i3
  real 					:: eps,eps2,icycle
  integer 				:: maxsize,i,j,k
  logical 				:: verb,verb2,v3d
  logical 				:: dave

  n=1
  maxsize=maxsize*250000
  n(1)=img%xm%n;  n(2)=img%z%n;  n(3)=1
  verb2=.false.

  if (.not. v3d) then
     n3=n(3); n(3)=1 
     if (n3>1 .and. verb) then
        verb=.false.; verb2=.true.
     end if
  else
     n3=1
  end if 
  n123=product(n)

  if (method(1:8) .eq. "sergey_f") then
     if (n123>maxsize/4) then; write(0,*) "need max_size to be at least ",n123*16/1000000+1; call seperr("maxsize too small "); end if
  else if (method(1:8) .eq. "sergey_s") then
     if (n123>maxsize/7) then; write(0,*) "need max_size to be at least ",n123*28/1000000+1; call seperr("maxsize too small "); end if
  else if (method(1:4) .eq. "puck") then 
     nwind(1)=min(n(1),5); nwind(2)=min(n(2),5); nwind(3)=min(n(3),5);       
     if (n123>maxsize/4) then; write(0,*) "need max_size to be at least ",n123*16/1000000+1; call seperr("maxsize too small "); end if
     do i=1,img%z%n; do j=1,img%xm%n;
        dat3(i,j,1)=dat((i-1)*img%xm%n+j)
     end do;end do
  end if
  
  icycle=.0001
  do i3=1,n3 !xline loop 
        if (verb2 .and. real(i3)/real(n3) > icycle ) then
           write(0,*) "working on ",nint(real(i3)/real(n3)*100.),"%"; icycle=icycle+.05
        end if !3d
        if (method(1:8) .eq. "sergey_f") then
           if (.not. find_sergey_fast(n,dat,nl_iter,eps,eps2,dip,verb,dave)) call seperr("trouble finding Sergey dip using fast method")
        else if (method(1:8) .eq. "sergey_s") then
           if (.not. find_sergey_slow(n,dat,niter_dip,nl_iter,eps,dip,verb))call seperr("trouble finding Sergey dip using slow method")
!        else if (method(1:6) .eq. "puck_f") then
!           if (.not. find_puck_dip_fast(dat3,nwind,dip3,coh)) call seperr("trouble finding Puch dip using fast method")
        else if (method(1:6) .eq. "puck_s") then
           if (.not. find_puck_dip_slow(dat3,nwind,dip3,coh,verb)) call seperr("trouble finding Puch dip using fast method")
        end if
  end do 
  if (method(1:4) .eq. "puck") then 
     do i=1,img%z%n; do j=1,img%xm%n;
        dip((i-1)*img%xm%n+j)=dip3(i,j,1)
     end do;end do
  end if
  end subroutine
end module
