program RWE3D_source
  use sep
  use util_
  use RWE3D_source_wemig
  use C2R

  implicit none
  type(myaxis) :: ax,ay,az,aw,ar,as

  integer :: ix,iy,iz,ii,iw
  integer :: nref,nsx,nsy,nsz,norm,cnx,cny,cnz
  real    :: forward,xmin,ymin,zmin,xmax,ymax,zmax,dxx,dyy,dzz
  real,    allocatable,dimension(:,:,:,:):: raysin      !! Input Rayfield
  real,    allocatable,dimension(:,:,:)  :: Vel,gnorm         !! Velocity model  
  complex, pointer,    dimension(:,:,:)  :: swf     !! S&R wfld
  real,    allocatable,dimension(:,:,:)  :: Pimg,CCimg  !! Single shot-image 
  logical                                :: verbose,kin
  character(len=128) :: name

  !! Geometry
  integer, allocatable :: rmask(:,:,:)
  real,    allocatable :: refs(:,:,:), fields(:,:,:,:)

  call sep_init(SOURCE)
  call getch_add_string("head=/dev/null")
  call getch_add_string("noheader=y")
  call set_no_putch()
  call sep_begin_prog()

  call from_param("forward",forward,0.) !! Forward scattering option
  call from_param("nref",nref,1)        !! # reference velocities
  call from_param("verbose",verbose,.false.)
  call from_param("kinematic",  kin,.true.)
  call from_param("norm",norm,1)
  name="rays"

  !! Read in input dimensions
  call from_aux("swf","n1",ax%n)
  call from_aux("swf","n2",ay%n)
  call from_aux("swf","n3",aw%n)
  call from_aux("swf","n4",as%n)
  call from_aux("swf","d1",ax%d)
  call from_aux("swf","d2",ay%d)
  call from_aux("swf","d3",aw%d)
  call from_aux("swf","d4",as%d)
  call from_aux("swf","o1",ax%o)
  call from_aux("swf","o2",ay%o)
  call from_aux("swf","o3",aw%o)
  call from_aux("swf","o4",as%o)
  call from_aux("rays","n1",az%n)
  call from_aux("rays","d1",az%d)
  call from_aux("rays","o1",az%o)

  write(0,*) 'DEPTH',az%n,az%o,az%d
  write(0,*) 'X1   ',ax%n,ax%o,ax%d
  write(0,*) 'X2   ',ay%n,ay%o,ay%d
  write(0,*) 'SHOTS',as%n,as%o,as%d
  write(0,*) 'FREQ ',aw%n,aw%o,aw%d


  call from_param("xmin",xmin,ax%o)
  call from_param("ymin",ymin,ay%o)
  call from_param("zmin",zmin,az%o)
  call from_param("xmax",xmax,(ax%n-1)*ax%d+ax%o)
  call from_param("ymax",ymax,(ay%n-1)*ay%d+ay%o)
  call from_param("zmax",zmax,(az%n-1)*az%d+az%o)
  call from_param("dxx",dxx,ax%d)
  call from_param("dyy",dyy,ay%d)
  call from_param("dzz",dzz,az%d)
  call from_param("nsx",nsx,3)
  call from_param("nsy",nsy,3)
  call from_param("nsz",nsz,3)

  cnx =       floor((xmax - xmin)/dxx)+1
  cny = max(1,floor((ymax - ymin)/dyy))
  cnz =       floor((zmax - zmin)/dzz)+1

  !! . . Create offset axis
  call auxputch("n1","i",cnz,"image")
  call auxputch("n2","i",cnx,"image")
  call auxputch("n3","i",cny,"image")
  call auxputch("n4","i",as%n,"image")
  call auxputch("d1","r",dzz,"image")
  call auxputch("d2","r",dxx,"image")
  call auxputch("d3","r",dyy,"image")
  call auxputch("d4","r",as%d,"image")
  call auxputch("o1","r",zmin,"image")
  call auxputch("o2","r",xmin,"image")
  call auxputch("o3","r",ymin,"image")
  call auxputch("o4","r",as%o,"image")
  call auxputch("esize","i",4,"image")

  allocate(swf(ax%n,ay%n,aw%n),Pimg(az%n,ax%n,ay%n) )
  allocate(raysin(az%n,ax%n,ay%n,3) )
  allocate( Vel(az%n,ax%n,ay%n),CCimg(cnz,cnx,cny) ,gnorm(az%n,ax%n,ay%n))
  allocate(rmask(ax%n,ay%n,az%n),refs(nref,10,az%n),fields(ax%n,ay%n,10,az%n) )

  call sep_read(raysin,"rays")
  call sep_read(Vel,"vel")

  write(0,*) 'VELOCITY MIN/MAX', minval(Vel),maxval(Vel)

  !! . . Calculate Geometry
  call geometry_init(az%n,ax%n,ay%n,az%d,ax%d,ay%d,kin,nref,verbose)
  call geometry_calc(raysin,Vel,fields,refs,rmask,gnorm)
  call geometry_close()
  deallocate( Vel )
  write(0,*) 'GNORM',minval(gnorm),maxval(gnorm)

  !! . . Initialize module and image space
  call wemig_src_init(ax,ay,az,aw,ar,forward,kin,nref,verbose)
  call C2R_init(nsx,nsy,nsz,norm,dxx,dyy,dzz,cnx,cny,cnz,ax%n,az%n,ay%n,xmin,ymin,zmin)

  do ii=1,as%n   !! . . Loop over shot points
     swf=0.;  Pimg=0.;

     !! Input Source Wavefield
     call sep_read(swf,"swf")
     call wemig_src(swf,Pimg,ii,fields,refs,rmask)

     !! Read in for output
     call C2R_run(Pimg,raysin,CCimg,gnorm)
     call sep_write(CCimg,"image")

     !! Restart values for Propagation
  end do !! . . End Shot-point loop

  call sep_end_prog()
  call exit()
end program RWE3D_source
