module OW_parms_areal

use sep
use OW_type

implicit none

logical,public, save				:: adj,dot,icwind,impulse,dso,resmig,resmig_lin,targwe
logical,public, save				:: greencomp, tgtmig,tgthess,greensynth
type (axis) ,public, save				:: kxm, h, w, xd, xm, z, sh, kz, ksh
type (dat_grid)	 ,public, save			:: rec, sou
type (mod_grid)	 ,public, save			:: img
type (velo) ,public, save				:: v
integer	 ,public, save				:: nref,iwb,tapertr,padtraces,nz,nzmin,nzmax
integer	 ,public, save				:: isp,nwbott,node,niter,n_wind,n_sampt,nshots
real ,public, save					:: ymin,ymax,zmin,zmax,pi,dwbott,xmax,xmin
real, public, save					:: eps,vsal,rho, maxang,x0tgt,z0tgt
real, public, save					:: depthdat,xmindat,xmaxdat,xintdat
integer, public, save				:: numxdat,numzdat,nxtgt,nztgt
contains

subroutine owparam()

real						:: wbott,dd1,dd2,od1,od2,dv1,dv2,ov1,ov2
real						:: deltat, d_sampt,dzref,ozref
integer						:: iw,nt, i, m,nd1,nd2,nv1,nv2,nzref,jh
logical						:: inversion,migration

 call from_aux("sou","n1",sou%h%n);	call from_aux("sou","d1",sou%h%d);	call from_aux("sou","o1",sou%h%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("sou","n4",sou%xd%n);	call from_aux("sou","d4",sou%xd%d);	call from_aux("sou","o4",sou%xd%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)

 call from_param("greencomp",greencomp,.false.)
! call from_param("tgtmig",tgtmig,.false.) 
! call from_param("tgthess",tgthess,.false.)
 call from_param("greensynth",greensynth,.false.)

 call from_param("node",node,9999)
 call from_param("wbott",wbott,0.)
 call from_param("maxang",maxang,90.)
 call from_param("traces_in_taper",tapertr,0)
 call from_param("padtraces",padtraces,tapertr)

 if (greencomp) then
   call from_param("depthdat",depthdat)
   call from_param("xmindat",xmindat)
   call from_param("xmaxdat",xmaxdat)
   call from_param("xintdat",xintdat)
   call from_param("ymin",ymin,0.)
   call from_param("ymax",ymax,0.)
   call from_param("z0tgt",z0tgt)
   call from_param("nztgt",nztgt)
   call from_param("zmin",zmin,0.)
   call from_param("zmax",zmax,z0tgt+(nztgt-1)*v%z%d)
   numzdat=int((depthdat-v%z%o)/v%z%d)+1.5
   numxdat=int((xmaxdat-xmindat)/xintdat)+1.5
   nz=max(int((z0tgt)/v%z%d)+1.5+nztgt,int(zmax/v%z%d)+1.5)
   call from_aux("recv","n1",rec%h%n);	call from_aux("recv","d1",rec%h%d);	call from_aux("recv","o1",rec%h%o)
   call from_aux("recv","n3",rec%w%n);	call from_aux("recv","d3",rec%w%d);	call from_aux("recv","o3",rec%w%o)
   call from_aux("recv","n4",rec%xd%n);	call from_aux("recv","d4",rec%xd%d);	call from_aux("recv","o4",rec%xd%o)  
   xmin=min(rec%xd%o,rec%xd%o+rec%h%o)
   xmax=max(rec%xd%o,(rec%xd%n-1)*rec%xd%d+rec%xd%o+(rec%h%n-1)*rec%h%d+rec%h%o)

   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 (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=ceiling((xmax-xmin)/v%xm%d)
   img%xm%d=v%xm%d
   img%xm%o=xmin

   img%z%n=nz
   img%z%o=v%z%o
   img%z%d=v%z%d

   if (img%xm%o < v%xm%o ) call seperr("Min.x-image space < Min.x-velocity")
   if ((img%xm%n-1)*img%xm%d+img%xm%o > v%xm%o+(v%xm%n-1)*v%xm%d ) call seperr("Max.x-image space > Max.x-velocity")
 
   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")

   call to_history("n1",img%xm%n,"greenup"); call to_history("d1",img%xm%d,"greenup"); call to_history("o1",img%xm%o,"greenup")  
   call to_history("n2",nz,"greenup") ; call to_history("d2",img%z%d,"greenup") ; call to_history("o2",img%z%o,"greenup")
   call to_history("n3",rec%w%n,"greenup"); call to_history("d3",rec%w%d,"greenup"); call to_history("o3",rec%w%o,"greenup")
   call to_history("n4",1,"greenup"); call to_history("d4",1.,"greenup"); call to_history("o4",1.,"greenup")  
   call to_history("n5",numxdat,"greenup"); call to_history("d5",xintdat,"greenup"); call to_history("o5",xmindat,"greenup")
   call to_history("esize",8,"greenup")

   call to_history("n1",img%xm%n,"greendown"); call to_history("d1",img%xm%d,"greendown"); call to_history("o1",img%xm%o,"greendown")  
   call to_history("n2",nz,"greendown") ; call to_history("d2",img%z%d,"greendown") ; call to_history("o2",img%z%o,"greendown")
   call to_history("n3",rec%w%n,"greendown"); call to_history("d3",rec%w%d,"greendown"); call to_history("o3",rec%w%o,"greendown")
   call to_history("n4",1,"greendown"); call to_history("d4",1.,"greendown"); call to_history("o4",1.,"greendown")  
   call to_history("n5",numxdat,"greendown"); call to_history("d5",xintdat,"greendown"); call to_history("o5",xmindat,"greendown")
   call to_history("esize",8,"greendown")
 else if (greensynth) then
   call from_param("depthdat",depthdat)
   call from_param("x0tgt",x0tgt)
   call from_param("nxtgt",nxtgt)
   call from_param("z0tgt",z0tgt)
   call from_param("nztgt",nztgt)
   call from_param("ymin",ymin,0.)
   call from_param("ymax",ymax,0.)
   call from_param("zmin",zmin,0.)
   call from_param("zmax",zmax,z0tgt+(nztgt-1)*v%z%d)
   nz=max(int((z0tgt)/v%z%d)+1.5+nztgt,int(zmax/v%z%d)+1.5)
   numzdat=int((depthdat-v%z%o)/v%z%d)+1.5

   call from_aux("greenup","n1",nshots); call from_aux("greenup","d1",img%xm%d); call from_aux("greenup","o1",img%xm%o)  
   call from_aux("greenup","n2",img%z%n) ; call from_aux("greenup","d2",img%z%d) ; call from_aux("greenup","o2",img%z%o)
   call from_aux("greenup","n3",numxdat); call from_aux("greenup","d3",xintdat); call from_aux("greenup","o3",xmindat)
   call from_aux("greenup","n5",rec%w%n); call from_aux("greenup","d5",rec%w%d); call from_aux("greenup","o5",rec%w%o)

   call from_aux("greendown","n1",img%xm%n); call from_aux("greendown","d1",img%xm%d); call from_aux("greendown","o1",img%xm%o)  
   call from_aux("greendown","n2",img%z%n) ; call from_aux("greendown","d2",img%z%d) ; call from_aux("greendown","o2",img%z%o)
   call from_aux("greendown","n3",numxdat); call from_aux("greendown","d3",xintdat); call from_aux("greendown","o3",xmindat)
   call from_aux("greendown","n5",rec%w%n); call from_aux("greendown","d5",rec%w%d); call from_aux("greendown","o5",rec%w%o)

   call from_aux("recv","n1",rec%h%n);	call from_aux("recv","d1",rec%h%d);	call from_aux("recv","o1",rec%h%o)
   call from_aux("recv","n4",rec%xd%n);	call from_aux("recv","d4",rec%xd%d);	call from_aux("recv","o4",rec%xd%o)  

   xmin=min(rec%xd%o,rec%xd%o+rec%h%o)
   xmax=max(rec%xd%o,(rec%xd%n-1)*rec%xd%d+rec%xd%o+(rec%h%n-1)*rec%h%d+rec%h%o)

   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 (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=ceiling((xmax-xmin)/v%xm%d)
!   img%xm%d=v%xm%d
!   img%xm%o=xmin

   img%z%n=nz
   img%z%o=v%z%o
   img%z%d=v%z%d

   if (img%xm%o < v%xm%o ) call seperr("Min.x-image space < Min.x-velocity")
   if ((img%xm%n-1)*img%xm%d+img%xm%o > v%xm%o+(v%xm%n-1)*v%xm%d ) call seperr("Max.x-image space > Max.x-velocity")
 
   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")

   call to_history("n1",nxtgt,"greensyn"); call to_history("d1",img%xm%d,"greensyn"); call to_history("o1",x0tgt,"greensyn")
   call to_history("n2",nztgt,"greensyn") ; call to_history("d2",img%z%d,"greensyn") ; call to_history("o2",z0tgt,"greensyn")
   call to_history("n3",nshots,"greensyn"); call to_history("d3",img%xm%d,"greensyn"); call to_history("o3",img%xm%o,"greensyn")  
   call to_history("n4",rec%w%n,"greensyn"); call to_history("d4",rec%w%d,"greensyn"); call to_history("o4",rec%w%o,"greensyn")
   call to_history("esize",8,"greensyn")
 end if

 write(0,*) " depthdat "," xmindat  "," xmaxdat  ","  xintdat ","  numxdat  ","  numzdat  "
 write(0,1001) depthdat,xmindat,xmaxdat,xintdat,numxdat,numzdat
1001 format (4F8.2,2I8)

 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 (nzref .gt. v%z%n) then
     call seperr("# depth steps vref .gt. from vel file")
  end if
  if (dzref .ne. v%z%d) then
     call seperr("depth interval vref different from vel file")
  end if
  if (ozref .ne. v%z%o) then
     call seperr("depth origin vref different from vel file")
  end if
 else
  call from_param("vsal",vsal)
  call from_param("nrefvel",nref) 
 end if

 pi=4*atan(1.)
 isp=abs(floor(rec%h%o/rec%h%d)) + 1
!--------------------------------------------------
! Depth vector
!
 allocate(img%z%all(img%z%n))
 do iwb = 1,nz
    img%z%all(iwb) = img%z%o + (iwb - 1) * v%z%d
 end do

!--------------------------------------------------
! Frequency vector
! no zero frequency component
!
 rec%w%o   = 2*pi * rec%w%o
 rec%w%d   = 2*pi * rec%w%d
 allocate(rec%w%all(rec%w%n))
 do iwb = 1,rec%w%n
    rec%w%all(iwb) = rec%w%o + (iwb - 1) * rec%w%d
 end do

!--------------------------------------------------
! Wavenumber vectors
!
 kxm%n = img%xm%n
 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*img%xm%d)      !! 1/length
 kxm%all = 2 * PI * kxm%all     !! radians

end subroutine

end module

!
!!----------------------------------------------------
!write(0,*) "   PARM   "
!write(0,*) "=========="
!write(0,*) "PARAMETERS"
!write(0,*) "=========="
!write(0,*) //
!write(0,*) "no.ref.velocities   = ",nref
!write(0,*) "min.x-coord         = ",img%xm%o
!write(0,*) "max.x-coord         = ",(img%xm%n-1)*img%xm%d+img%xm%o
!write(0,*) "min.y-coord         = ",ymin
!write(0,*) "max.y-coord         = ",ymax
!write(0,*) "min.depth           = ",zmin
!write(0,*) "max.depth           = ",zmax
!write(0,*) "traces in taper     = ",tapertr
!write(0,*) "padded traces       = ",padtraces
!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/(2*pi)
!write(0,*) "minimum freq    = ", rec%w%o/(2*pi)
!write(0,*) "number of shots = ", rec%xd%n
!write(0,*) "shot spacing    = ", rec%xd%d
!write(0,*) "min.shot-x      = ", rec%xd%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%xd%n
!write(0,*) "s.fnct spacing  = ", sou%xd%d
!write(0,*) "min.s.fnct-x    = ", sou%xd%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.of offsets    = ", img%sh%n
!write(0,*) "offset spacing   = ", img%sh%d
!write(0,*) "offset origin     = ", img%sh%o
!write(0,*) "no.depth samples= ", img%z%n
!write(0,*) "depth interval  = ", v%z%d
!write(0,*) "minimum depth   = ", img%z%o
!
!write(0,*) //
!write(0,*) "WAVENUMBER"
!write(0,*) "=========="
!write(0,*) "no.wavenumbers  = ", kxm%n
!write(0,*) "waveno.spacing  = ", kxm%d
!write(0,*) "waveno.origin   = ", kxm%o
!write(0,*) //
!!write(0,*) kxm%all

! else if (tgtmig) then
!    call from_param("depthdat",depthdat)
!    call from_param("xintdat",xintdat)
!    numzdat=int((depthdat-v%z%o)/v%z%d)+1.5
!    call from_aux("green","n5",numxdat)
!    call from_aux("green","o5",xmindat)
! else if (tgthess) then
!   call from_param("depthdat",depthdat)
!   numzdat=int((depthdat-v%z%o)/v%z%d)+1.5
!   call from_aux("green","n1",img%xm%n); call from_aux("green","d1",img%xm%d); call from_aux("green","o1",img%xm%o)  
!   call from_aux("green","n2",nz) ; call from_aux("green","d2",img%z%d) ; call from_aux("green","o2",zmin)
!   call from_aux("green","n5",rec%w%n); call from_aux("green","d5",rec%w%d); call from_aux("green","o5",rec%w%o)
!   call from_aux("green","n4",numxdat); call from_aux("green","d4",xintdat); call from_aux("green","o4",xmindat)

!  if (exist_file("image")) then
!   call to_history("n1",nz,"image") ; call to_history("d1",img%z%d,"image") ; call to_history("o1",zmin,"image")
!   call to_history("n2",img%sh%n,"image") ; call to_history("d2",img%sh%d,"image") ; call to_history("o2",img%sh%o,"image")
!   call to_history("n3",numxdat,"image"); call to_history("d3",rec%h%d,"image"); call to_history("o3",xmindat,"image")  
!   call to_history("n4",1,"image"); call to_history("d4",1,"image"); call to_history("o4",0,"image")
!   call to_history("n5",1,"image"); call to_history("d5",1,"image"); call to_history("o5",0,"image")  
!   call to_history("esize",4,"image")
!  end if

! if(exist_file("recdat")) then
!   call to_history("n1",rec%h%n,"recdat");call to_history("d1",rec%h%d,"recdat");call to_history("o1",rec%h%o,"recdat")
!   call to_history("n2",1,"recdat");call to_history("d2",1.,"recdat");call to_history("o2",0.,"recdat")
!   call to_history("n3",rec%w%n,"recdat");call to_history("d3",rec%w%d,"recdat");call to_history("o3",rec%w%o,"recdat")
!   call to_history("n4",rec%xd%n,"recdat");call to_history("d4",rec%xd%d,"recdat");call to_history("o4",rec%xd%o,"recdat")  
!   call to_history("esize",8,"recdat")
! end if

! else if (tgtmig) then
!    call from_param("depthdat",depthdat)
!    call from_param("xintdat",xintdat)
!    numzdat=int((depthdat-v%z%o)/v%z%d)+1.5
!    call from_aux("green","n5",numxdat)
!    call from_aux("green","o5",xmindat)
! else if (tgthess) then
!   call from_param("depthdat",depthdat)
!   numzdat=int((depthdat-v%z%o)/v%z%d)+1.5
!   call from_aux("green","n1",img%xm%n); call from_aux("green","d1",img%xm%d); call from_aux("green","o1",img%xm%o)  
!   call from_aux("green","n2",nz) ; call from_aux("green","d2",img%z%d) ; call from_aux("green","o2",zmin)
!   call from_aux("green","n5",rec%w%n); call from_aux("green","d5",rec%w%d); call from_aux("green","o5",rec%w%o)
!   call from_aux("green","n4",numxdat); call from_aux("green","d4",xintdat); call from_aux("green","o4",xmindat)

! write(0,*) "*************************"
! write(0,*) "* ",nz,nzmax,nzmin," *"
! write(0,*) "*************************"

! if(exist_file("green").and.greencomp) then
!  call to_history("n1",img%xm%n,"green"); call to_history("d1",img%xm%d,"green"); call to_history("o1",img%xm%o,"green")  
!  call to_history("n2",nz,"green") ; call to_history("d2",img%z%d,"green") ; call to_history("o2",img%z%o,"green")
!  call to_history("n3",rec%w%n,"green"); call to_history("d3",rec%w%d,"green"); call to_history("o3",rec%w%o,"green")
!  call to_history("n4",2,"green"); call to_history("d4",1.,"green"); call to_history("o4",1.,"green")  
!  call to_history("n5",numxdat,"green"); call to_history("d5",xintdat,"green"); call to_history("o5",xmindat,"green")
!  call to_history("esize",8,"green")
! else if(exist_file("green").and.tgthess) then
!  call to_history("n1",img%xm%n,"green"); call to_history("d1",img%xm%d,"green"); call to_history("o1",img%xm%o,"green")  
!  call to_history("n2",nz,"green") ; call to_history("d2",img%z%d,"green") ; call to_history("o2",img%z%o,"green")
!  call to_history("n5",rec%w%n,"green"); call to_history("d5",rec%w%d,"green"); call to_history("o5",rec%w%o,"green")
!  call to_history("n3",2,"green"); call to_history("d3",1.,"green"); call to_history("o3",1.,"green")  
!  call to_history("n4",numxdat,"green"); call to_history("d4",xintdat,"green"); call to_history("o4",xmindat,"green")
!  call to_history("esize",8,"green")
! end if

