module OW_parms_areal

use sep
use OW_type

implicit none

character*6                                                           :: extrap
logical,public, save					:: shaper
type (axis) ,public, save					:: kxm, h, w, x, xm, z, sh
type (dat_grid)	 ,public, save				:: rec, sou
type (mod_grid)	 ,public, save				:: sog, recev
type (velo) ,public, save					:: v
integer	 ,public, save					:: nref,iwb,ikxa,tapertr
integer	 ,public, save					:: padtraces,dref,isp,rndcte,subf,nreal,dipop
integer, public, save					:: imgxn, nphi, nx, ngap, nwbott, shift, node
integer, public, save					:: nz,nzmax,nzmin,nhorz
real ,public, save						:: xmin,xmax,ymin,ymax,zmin,zmax,pi,dwbott,trick,white
real, public, save						:: vsal,dphi,ophi,hmax,zmx,t0,eps,fac,maxang
!complex,allocatable,dimension(:),public,save			:: wavelet
! using lloyd
logical,public, save				:: lloyd,perc_start
real,public,save					:: min_r_pct,min_s_pct,slow_own
integer,public,save					:: niter_over



contains

subroutine owparam()

real							:: t1,wbott
integer							:: i,m,n,stat
integer							:: nzref,nhorz1
real							:: dzref,ozref

 call from_aux("cig","n1",sog%xm%n);call from_aux("cig","d1",sog%xm%d);call from_aux("cig","o1",sog%xm%o)
 call from_aux("cig","n2",sog%sh%n);call from_aux("cig","d2",sog%sh%d);call from_aux("cig","o2",sog%sh%o)
 call from_aux("cig","n3",sog%z%n);call from_aux("cig","d3",sog%z%d);call from_aux("cig","o3",sog%z%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%xm%n);	call from_aux("vel","d1",v%xm%d);	call from_aux("vel","o1",v%xm%o)
 call from_aux("vel","n3",v%z%n);	call from_aux("vel","d3",v%z%d);	call from_aux("vel","o3",v%z%o)
 call from_param("extrapolator",extrap,"ffd")
if (extrap(1:4).eq."pspi") then
   call from_param("lloyd",lloyd,.true.)
   call from_param("nrefvel",nref,1) 
   if (lloyd) then
      call from_param("min_region_pct",min_r_pct,1.)
      min_r_pct=min_r_pct/100.
     !MINIMUM ALLOWED DEVIATION BETWEEN CENTROIDS
      call from_param("min_slow_dev",min_s_pct,9.)
      min_s_pct=min_s_pct/100.
      call from_param("perc_start",perc_start,.false.)
      call from_param("vel_own",slow_own,-1.)
      slow_own=1./slow_own
     !NUMBER OF ITERATIONS
      call from_param("niter_lloyd",niter_over,-1)
   else
      call from_param("vsal",vsal)
   end if
else
   call from_param("trick",trick,0.14)
   call from_param("dipop",dipop,45)
end if
 if (sog%z%d .ne. v%z%d) then
    call seperr("source/receiv depth interval .ne. velocity depth interval")
 end if
   call from_param("white",white,0.1)

pi=4*atan(1.)

 !!  get parameters
  call from_param("shaper",shaper,.true.)
  call from_param("padtraces",padtraces,0)
  call from_param("traces_in_taper",tapertr,padtraces/4)
  call from_param("wbott",wbott,0.)
  wbott=int(wbott/sog%z%d)*sog%z%d
  nwbott=int(wbott/sog%z%d)+1
  dwbott=wbott
 write(0,*) "wbott ",wbott,"dwbott ",dwbott,"nwbott ",nwbott
  call from_param("node",node,9999)
  call from_param("maxang",maxang,80.)

 xmin=sog%xm%o-(padtraces+tapertr)*sog%xm%d
 xmax=((sog%xm%n-1)+(padtraces+tapertr))*sog%xm%d+sog%xm%o
 if (xmin < v%xm%o ) call seperr("Min.x-image < Min.x-velocity")
 if (xmax > v%xm%o+(v%xm%n-1)*v%xm%d ) call seperr("Max.x-image > Max.x-velocity")

 call from_param("zmin",zmin,sog%z%o)
 call from_param("zmax",zmax,(sog%z%n-1)*sog%z%d+sog%z%o)

 if (zmin < v%z%o ) call seperr("Min.z-image < Min.z-velocity")
 if (zmax > v%z%o+(v%z%n-1)*v%z%d ) call seperr("Max.z-image > Max.z-velocity")

 nx=(xmax-xmin)/sog%xm%d+1.5
 nz=(zmax-zmin)/sog%z%d+1.5
 nzmax=(zmax-v%z%o)/sog%z%d+1.5 
 nzmin=int((zmin-v%z%o)/v%z%d)+1.5
 write(0,*) "*************************"
 write(0,*) "* ",nz,nzmax,nzmin," *"
 write(0,*) "*************************"

!
!
!------------------------------------------------

!----------------------------------------------
!
! Wavefields
!

 call to_history("n1",(nx-2*(padtraces+tapertr)),"rec_out");call to_history("d1",sog%sh%d,"rec_out");call to_history("o1",xmin+(padtraces+tapertr)*sog%sh%d,"rec_out")  
 call to_history("n2",1,"rec_out");call to_history("d2",1,"rec_out");call to_history("o2",0.,"rec_out")
 call to_history("n3",sou%w%n,"rec_out");call to_history("d3",sou%w%d,"rec_out");call to_history("o3",sou%w%o,"rec_out")
  call to_history("n4",1,"rec_out");call to_history("d4",1.,"rec_out");call to_history("o4",1.,"rec_out")
 call to_history("n5",1,"rec_out");call to_history("d5",1.,"rec_out");call to_history("o5",1.,"rec_out") 
 call to_history("n6",1,"rec_out");call to_history("d6",1.,"rec_out");call to_history("o6",zmin,"rec_out") 
 call to_history("esize",8,"rec_out")

!--------------------------------------------------
! Frequency vector
! no zero frequency component
!
sou%w%o   = 2*pi * sou%w%o
sou%w%d   = 2*pi * sou%w%d
allocate(sou%w%all(sou%w%n))
do iwb = 1,sou%w%n
   sou%w%all(iwb) = sou%w%o + (iwb - 1) * sou%w%d
end do
!--------------------------------------------------
! Wavenumber vector
!
kxm%n = nx
allocate(kxm%all(kxm%n))
kxm%all=0.
if(kxm%n/=1) then
  m = kxm%n/2
  do i=1,m+1
     kxm%all(i)=i-1
  end do
  do i=m+2,kxm%n
     kxm%all(i)=i-kxm%n-1
  end do
else
  kxm%all=0. !! make sure k=0 if n=1
end if

kxm%all = kxm%all / (kxm%n*sog%xm%d)      !! 1/length
kxm%all = 2 * PI * kxm%all     !! radians

write(0,*) "   PARM   "
write(0,*) "=========="
write(0,*) "PARAMETERS"
write(0,*) "=========="
write(0,*) //
write(0,*) "no.ref.velocities   = ",nref
write(0,*) "max.limit velocity  = ",vsal
write(0,*) "traces in taper     = ",tapertr
write(0,*) "padded traces       = ",padtraces
write(0,*) "no.traces in gap    = ",ngap
write(0,*) //
write(0,*) "============"
write(0,*) "HEADER WORDS"
write(0,*) "============"
write(0,*) //
write(0,*) "Rotated CIG input"
write(0,*) "================="
write(0,*) "no.offsets      = ", sog%sh%n
write(0,*) "offset spacing  = ", sog%sh%d
write(0,*) "min.offset      = ", sog%sh%o
write(0,*) "no.rotated CIGs = ", sog%xm%n
write(0,*) "CIG spacing     = ", sog%xm%d
write(0,*) "min.CIG x-coord = ", sog%xm%o
write(0,*) "no.depth steps  = ", sog%z%n
write(0,*) "depth interval  = ", sog%z%d
write(0,*) "depth origin    = ", sog%z%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,*) "AREAL SPACE"
write(0,*) "==========="
write(0,*) "no.of traces    = ", nx
write(0,*) "trace spacing   = ", sog%sh%d
write(0,*) "min.trace-x     = ", xmin
write(0,*) "no.frequencies  = ", sou%w%n
write(0,*) "freq.interval   = ", sou%w%d/(2*pi)
write(0,*) "min.frequency   = ", sou%w%o/(2*pi)
write(0,*) //
write(0,*) "WAVENUMBER"
write(0,*) "=========="
write(0,*) "no.wavenumbers  = ", kxm%n

end subroutine

end module
