program RWE2D_source
  use sep
  use util_
  use RWE2D_source_wemig
  use C2R2D 

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

  integer :: ii,nt
  real    :: maxang,minang,maxt,dt,pi,dr
  real,    allocatable,dimension(:,:) :: Vel     !! Velocity model  
  complex, allocatable,dimension(:,:) :: swfin   !! Starting WF
  complex, allocatable,dimension(:,:) :: swfout  !! Output WF 
  real   , allocatable,dimension(:  ) :: rline
  logical                               :: adj,add
  character(len=128) :: name

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

  call from_param("maxt",maxt,-2.) 
  call from_param("minang",minang, 10.)
  call from_param("maxang",maxang,170.)

  !! IN/OUT header info for output files
  call readin( ax,az,aw,as,at,ar,rr )

  !! Set tau axis
  at%o = -abs(maxt); at%d = abs(2.*maxt)/dble(at%n)

  !! Set receiver axis (RWE)
  pi = acos(-1.)
  ar%n = rr%n
  ar%d = (maxang-minang)/180.*pi/dble(ar%n-1.)
  ar%o = pi*minang/180.

  call writeout( ax,az,aw,as,at,ar,rr )

  write(0,*) 'Vz   ',az%n,az%o,az%d
  write(0,*) 'Vx   ',ax%n,ax%o,ax%d
  write(0,*) 'SHOTS',as%n,as%o,as%d
  write(0,*) 'RECS ',ar%n,ar%o,ar%d
  write(0,*) 'FREQ ',aw%n,aw%o,aw%d
  write(0,*) 'Tau  ',at%n,at%o,at%d

  allocate(swfin(at%n,aw%n),Vel(az%n,ax%n),swfout(ar%n,aw%n),rline(ar%n) )

  !! Read in Velocity
  call sep_read(Vel   ,"vel")
  write(0,*) 'VELOCITY MIN/MAX', minval(Vel),maxval(Vel)

  !! . . Initialize module and image space
  call wemig_init(ax,az,aw,as,at,ar,rr,Vel)
  write(0,*) 'INITIALIZED Vel'

  !! Initialize logical variables
  adj=.false.; add=.false.

  !! . . Loop over shots
  do ii=1,as%n  

     !! Read in SWF
     swfin=0.; swfout=0.
     call sep_read(swfin ,"swf ")
     write(0,*) 'SWF MIN/MAX', minval(abs(swfin)),maxval(abs(swfin))

     !! Calculate wavefield
     call wemig(adj,add,swfin,swfout )

     !! Cart WF
     call sep_write(swfout,"data")

     !! Update coordinates
     call coord_init( as%d )

  end do !! . . End Shot-point loop

  call sep_end_prog()
  call exit()
end program RWE2D_source
