module OW_parms

use sep
use sep3d_struct_mod
use OW_type

implicit none

type (axis) 					:: kxm, h, w, x, xm, z, sh
type (dat_grid)					:: rec, sou
type (mod_grid)					:: img
type (velo)					:: v
integer,public					:: nref,ngap,nreal,subf,node,padtraces,tapertr,isp,ixp
integer,public					:: rndcte,shift,nshots,npath,jshift,nzmin,nzmax,nblock
real,public					:: xmin,xmax,ymin,ymax,zmin,zmax,pi,wbott,vsal,eps,wpath,vrange,fac
real,public,allocatable				:: weight(:)
logical,public					:: compute
type(sep3d)					:: axgr

contains

subroutine owparam()

integer						:: iw,ikx,i
integer						:: nzref
real						:: dzref,ozref

 call from_history("n1",rec%h%n);	call from_history("d1",rec%h%d);	call from_history("o1",rec%h%o)
 call from_history("n4",rec%x%n);	call from_history("d4",rec%x%d);	call from_history("o4",rec%x%o)  

 call from_aux("sou","n3",sou%w%n);	call from_aux("sou","d3",sou%w%d);	call from_aux("sou","o3",sou%w%o)

 call from_aux("vel","n1",v%z%n);	call from_aux("vel","d1",v%z%d);	call from_aux("vel","o1",v%z%o)
 call from_aux("vel","n2",v%xm%n);	call from_aux("vel","d2",v%xm%d);	call from_aux("vel","o2",v%xm%o)

if (exist_file("vref")) then
   call from_aux("vref","n1",nref)
   call from_aux("vref","n3",nzref)
   call from_aux("vref","d3",dzref)
   call from_aux("vref","o3",ozref)
   if (dzref .ne. v%z%d) call seperr("depth interval vref different from vel file")
   if (ozref .ne. v%z%o) call seperr("depth origin vref different from vel file")
else
   call from_param("vsal",vsal)
   call from_param("nrefvel",nref) 
end if

 !!  get parameters
  call from_param("compute",compute,.true.)
  call from_param("nblock",nblock,1) 
  call from_param("xmin",xmin) 
  call from_param("xmax",xmax)
  call from_param("ymin",ymin,0.)
  call from_param("ymax",ymax,0.)
  call from_param("zmin",zmin,0.)
  call from_param("zmax",zmax)
  call from_param("wbott",wbott,0.)
  call from_param("node",node,9999)
  call from_param("traces_in_taper",tapertr,101)
  call from_param("padtraces",padtraces,tapertr)
 xmin=int(xmin/rec%h%d)*rec%h%d
 xmax=int(xmax/rec%h%d)*rec%h%d
 if (xmin < v%xm%o ) call seperr("Min.x-image space < Min.x-velocity")
 if (xmax > v%xm%o+(v%xm%n-1)*v%xm%d ) call seperr("Max.x-image space > Max.x-velocity")

 if (xmin > rec%x%o + rec%h%o - (padtraces+tapertr)*v%xm%d) call seperr("Min.x-output space > Min.1st GF")
 if (xmax < rec%h%o+(rec%h%n-1)*rec%h%d + rec%x%o+(rec%x%n-1)*rec%x%d + (padtraces+tapertr)*v%xm%d) &
    call seperr("Max.x-output space < Max.Last GF")

 if (zmin < v%xm%o ) call seperr("Min.z-image space < Min.z-velocity")
 if (zmax > v%z%o+(v%z%n-1)*v%z%d ) call seperr("Max.z-image space > Max.z-velocity")
 if (rec%h%d .ne. v%xm%d) call seperr("x-offset interval .ne. velocity x-interval")

 img%xm%n=(xmax-xmin)/v%xm%d+1.5
 img%xm%d=v%xm%d
 img%xm%o=xmin

 img%z%n=(zmax-zmin)/v%z%d+1.5
 img%z%o=zmin
 img%z%d=v%z%d
 nzmin=zmin/v%z%d+1.5
 nzmax=zmax/v%z%d+1.5

!--------------------------------------------------
! shot position in the acq.geom.
ixp=abs(floor(rec%h%o/rec%h%d))+1
isp=ixp+tapertr+padtraces
write(0,*) "======================="
write(0,*) isp, isp-(tapertr+padtraces),abs(floor(rec%h%o/rec%h%d)),rec%h%n
write(0,*) "======================="

pi=4*atan(1.)

!--------------------------------------------------
! Wavenumber vector
!
kxm%n = rec%h%n+2*(tapertr+padtraces)
kxm%d = 1./(kxm%n*rec%h%d)
kxm%o = 1./(2*rec%h%d)-kxm%d
allocate(kxm%all(kxm%n))
do ikx=1,kxm%n
   kxm%all(ikx) = mod(kxm%o+(ikx-1)*kxm%d,1./rec%h%d)-kxm%o
end do
kxm%all = 2*pi * kxm%all

write(0,*) kxm%all
write(0,*) "======================================"

 call to_history("n1",rec%h%n,"shots");	call to_history("d1",rec%h%d,"shots");	call to_history("o1",rec%h%o,"shots")
 call to_history("n2",1,"shots");	call to_history("d2",1.,"shots");	call to_history("o2",0.,"shots")
 call to_history("n3",sou%w%n,"shots");	call to_history("d3",sou%w%d,"shots");	call to_history("o3",sou%w%o,"shots")
 call to_history("n4",rec%x%n+3,"shots");	call to_history("d4",rec%x%d,"shots");	call to_history("o4",rec%x%o,"shots")
 call to_history("n5",1,"shots");	call to_history("d5",1.,"shots");	call to_history("o5",0.,"shots")
 call to_history("esize",8,"shots")

 call init_sep3d(axgr, "OUTPUT","COMPLEX", "REGULAR", ctag="ws")
 call axis_allocate(axgr,4)
 axgr%o(1)=img%xm%o; axgr%n(1)=img%xm%n; axgr%d(1)=img%xm%d
 axgr%o(2)=sou%w%o; axgr%n(2)=sou%w%n; axgr%d(2)=sou%w%d
 axgr%o(3)=img%z%o; axgr%n(3)=img%z%n; axgr%d(3)=img%z%d
 axgr%o(4)=rec%x%o-(ixp-1)*rec%h%d; axgr%n(4)=rec%x%n+2*(ixp-1); axgr%d(4)=rec%x%d
 call sep3d_set_sep3d(axgr)
if (compute) then
 call sep3d_write_description("ws",axgr)
end if
!--------------------------------------------------
! Frequency vector
! 
sou%w%o   = 2*pi * sou%w%o
sou%w%d   = 2*pi * sou%w%d
allocate(sou%w%all(sou%w%n))
do iw = 1,sou%w%n
   sou%w%all(iw) = sou%w%o + (iw - 1) * sou%w%d
end do

write(0,*) "   PARM   "
write(0,*) "=========="
write(0,*) "PARAMETERS"
write(0,*) "=========="
write(0,*) //
write(0,*) "no.ref.velocities   = ",nref
write(0,*) "min.x-coord         = ",xmin
write(0,*) "max.x-coord         = ",xmax
write(0,*) "min.y-coord         = ",ymin
write(0,*) "max.y-coord         = ",ymax
write(0,*) "min.depth           = ",zmin
write(0,*) "max.depth           = ",zmax
write(0,*) "no.subsurf.offset   = ",img%sh%n
write(0,*) "min.subsurf.offset  = ",img%sh%o
write(0,*) "subsurf.offset int. = ",img%sh%d
write(0,*) //
write(0,*) "============"
write(0,*) "HEADER WORDS"
write(0,*) "============"
write(0,*) //
write(0,*) "SHOTS"
write(0,*) "====="
write(0,*) "number of recv  = ", rec%h%n
write(0,*) "recv spacing    = ", rec%h%d
write(0,*) "min.recv offset = ", rec%h%o
write(0,*) "number of freq  = ", rec%w%n
write(0,*) "freq spacing    = ", rec%w%d
write(0,*) "minimum freq    = ", rec%w%o
write(0,*) "number of shots = ", rec%x%n
write(0,*) "shot spacing    = ", rec%x%d
write(0,*) "min.shot-x      = ", rec%x%o
write(0,*) //
write(0,*) "SOURCE FUNCTION"
write(0,*) "==============="
write(0,*) "no.of traces    = ", sou%h%n
write(0,*) "trace spacing   = ", sou%h%d
write(0,*) "min.trace-x     = ", sou%h%o
write(0,*) "number of freq  = ", sou%w%n
write(0,*) "freq spacing    = ", sou%w%d
write(0,*) "minimum freq    = ", sou%w%o
write(0,*) "no.of s.fnct    = ", sou%x%n
write(0,*) "s.fnct spacing  = ", sou%x%d
write(0,*) "min.s.fnct-x    = ", sou%x%o
write(0,*) //
write(0,*) "VELOCITY MODEL"
write(0,*) "=============="
write(0,*) "no.of traces    = ", v%xm%n
write(0,*) "trace spacing   = ", v%xm%d
write(0,*) "min.trace-x     = ", v%xm%o
write(0,*) "no.depth samples= ", v%z%n
write(0,*) "depth interval  = ", v%z%d
write(0,*) "minimum depth   = ", v%z%o
write(0,*) //
write(0,*) "IMAGE SPACE"
write(0,*) "==========="
write(0,*) "no.of traces    = ", img%xm%n
write(0,*) "trace spacing   = ", img%xm%d
write(0,*) "min.trace-x     = ", img%xm%o
write(0,*) "no.depth samples= ", img%z%n
write(0,*) "depth interval  = ", img%z%d
write(0,*) "minimum depth   = ", img%z%o
write(0,*) //



end subroutine

end module
