module wei_mig_shared_mod
  use wei_wavespace_mod
  use wei_file_mod
  use sep

  implicit none


  contains


  subroutine check_setup_mig(wave,image,zero)
   type(wfile) :: image,zero
   type(wspace),pointer ::  wave
   real         :: o
   logical      :: adj,image_real,ADD
   integer      :: esize
   integer       :: nwind(5),fwind(5),jwind(5)
   character(len=10) :: aa
   
#ifdef  DEBUG
   write(0,*) "IN CHECK SETUP MIG"
#endif
   o=-wave%o(3)/wave%d(3)+1.
   if(abs(o-nint(o)) >.01) then
     call seperr("Must include zero offset index")
   end if
   if(nint(o) < 1 .or. nint(o) > wave%n(3) ) then
     call seperr("Must include zero offset index")
   end if
   wave%adj=.true. !migration
   wave%opsign=-1  !migration
   call from_param("nzb",wave%nzb,1) !Depth block

   call from_param("add",add,.false.)
   if(add) then
     call from_aux("R","esize",esize,4)
     image_real=.false.
     if(esize==4) image_real=.true.
     call init_file_tag(image,wave,"R",image_real,.false.,image_axes)
     call from_param("Zero",aa,"none")
     if(aa(1:4)/="none") call init_file_tag(zero,wave,"Zero",image_real,.false.,image_axes)
     image%add=.true.
   else
     call from_param("image_real",image_real,.true.)
     nwind=-1;fwind=-1; jwind=-1
     call from_param("nwind_gath",nwind,nwind)
     call from_param("fwind_gath",fwind,fwind)
     call from_param("jwind_gath",jwind,jwind)
     call init_file_struct(image,wave,"R",image_real,.false.,image_axes,nwind,fwind,jwind)
     call from_param("Zero",aa,"none")
     if(aa(1:4)/="none") then
       fwind=0;
       jwind=1;
       nwind(1:4)=wave%n(1:4)
       nwind(3)=1
       nwind(5)=size(wave%dz)
       fwind(3)=min(wave%n(3)-1,max(0,nint((-wave%o(3)/wave%d(3)))))
       
       fwind(4)=max(wave%n(4)-1,min(0,nint((-wave%o(4)/wave%d(4)))))
       call init_file_struct(zero,wave,"Zero",image_real,.false.,image_axes,nwind,fwind,jwind)

     else 
       zero%doit=.false.
     end if
     image%add=.false.
   end if

#ifdef  DEBUG
   write(0,*) "OUT CHECK SETUP MIG"
#endif

  end subroutine



end module
