module OW_parms_areal

use sep
use OW_type

implicit none

character*12,public, save                                   :: sufx
character*6,public, save                                    :: extrap,cgsolv
character*7,public, save                                    :: imgcon
logical,public, save				:: adj,dot,icwind,impulse,dso,resmig,light,moddso,smooth,migration
logical,public, save				:: zweight,resmig_lin,areal,timeshift,l1norm,envelope,born2
type (axis) ,public, save				:: kxm, h, w, xd, xm, z, sh, kz, ksh, th
type (dat_grid)	 ,public, save			:: rec, sou
type (mod_grid)	 ,public, save			:: img
type (velo) ,public, save				:: v
integer	 ,public, save				:: windx,windh,windz,frame,tc1,tc2,freq
integer	 ,public, save				:: nref,iwb,tapertr,padtraces,nz,nzmin,nzmax,nsecdim
integer	 ,public, save				:: isp,nwbott,node,niter,n_wind,n_sampt,nxfilt,nh0,dipop
real ,public, save					:: ymin,ymax,zmin,zmax,pi,dwbott,osecdim,dsecdim,thresh,maxvchg
real, public, save					:: vsal,rho,maxang,damp,beta,xv_min,xv_max,trick,expdso,zwgt
integer, public, save				:: nxw, nah,nax
real, public, save					:: dah,oah,dax,oax
type(axis), public, save               		          :: axk, ayk, azk
real, public, save                               		:: axkd, aykd, azkd, axko, ayko, azko
integer, public, save                            		:: axkn, aykn, azkn
! using lloyd
logical,public, save				:: lloyd,perc_start
real,public,save					:: min_r_pct,min_s_pct,slow_own
integer,public,save					:: niter_over
! DIPS
logical,public,save					:: dipflt
!logical,public,save				:: dips,verb,v3d,dave
!character*20,public,save				:: method
!integer,public,save				:: maxsize,nl_iter,niter_dip,nwind
!real, public,save					:: eps,eps2
real, public, save                                          :: epscg
!
integer, public, save                                       :: serg_na, serg_niter, dip_niter
real, public, save                                          :: serg_eps1, serg_eps2, serg_epsh, dip_eps
contains

subroutine owparam()

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

if (exist_file("vspl")) then
 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("node",node,9999)
 call from_param("oxk", axko, 0.); call from_param("dxk", axkd, 1.); call from_param("nxk", axkn, 99999 )
 call from_param("oyk", ayko, 0.); call from_param("dyk", aykd, 1.); call from_param("nyk", aykn, 99999 )
 call from_param("ozk", azko, 0.); call from_param("dzk", azkd, 1.); call from_param("nzk", azkn, 99999 )
 call from_param("xv_min",xv_min,v%xm%o); call from_param("xv_max",xv_max,(v%xm%n-1)*v%xm%d+v%xm%o)
 if (axkn.ne.99999 .or. azkn.ne.99999) then
    axk%d=axkd
    ayk%d=aykd
    azk%d=azkd
    axk%n=axkn
    ayk%n=aykn
    azk%n=azkn
    axk%o=axko
    ayk%o=ayko
    azk%o=azko
 end if
else
 call from_param("dipflt",dipflt,.false.)
 if (dipflt) call from_param('frame',frame,50)
call from_param("epscg",epscg,1.)
call from_param("cgsolv",cgsolv,"      ")
!if (dipflt) then
!  call from_param('max_size',maxsize,100)
!  call from_param("verb",verb,.false.)
!  call from_param("v3d",v3d,.false.)
!  call from_param("dave",dave,.false.)
!  call from_param("method",method,"sergey_f")
!  if (method(1:8) .eq. "sergey_f") then
!     call from_param("eps",eps,.0001)
!     call from_param("eps2",eps2,.0001)
!     call from_param("nl_iter",nl_iter,10)
!  else if (method(1:8) .eq. "sergey_s") then
!     call from_param("eps",eps,.04)
!     call from_param("niter_dip",niter_dip,20)
!     call from_param("nl_iter",nl_iter,10)
!  else if (method(1:6) .eq. "puck_f") then
!     call from_param("nwind",nwind,nwind)
!  else if (method(1:6) .eq. "puck_s") then
!     call from_param("nwind",nwind,nwind)
!  else
!     write(0,*) "method=",trim(method); call seperr( "Unknown method")
!  end if
!else
     call from_param("oxk", axko, 0.); call from_param("dxk", axkd, 1.); call from_param("nxk", axkn, 99999 )
     call from_param("oyk", ayko, 0.); call from_param("dyk", aykd, 1.); call from_param("nyk", aykn, 99999 )
     call from_param("ozk", azko, 0.); call from_param("dzk", azkd, 1.); call from_param("nzk", azkn, 99999 )
     if (axkn.ne.99999 .or. azkn.ne.99999) then
       axk%d=axkd
       ayk%d=aykd
       azk%d=azkd
       axk%n=axkn
       ayk%n=aykn
       azk%n=azkn
       axk%o=axko
       ayk%o=ayko
       azk%o=azko
     end if
!!!!!!!!!!!!!!!!!!
!
! for smoothing along dips
  call from_param("smooth",smooth,.true.)
  if (smooth) then
     call from_param("serg_na"    ,serg_na,8)
     call from_param("serg_epsh"  ,serg_epsh,0.01)
  ! Set smoothing factors for SERGEY
     call from_param("serg_niter",serg_niter,30) 
     call from_param("serg_eps1",serg_eps1,0.002)
     call from_param("serg_eps2",serg_eps2,0.002)
     call from_param("dip_niter",dip_niter,100)
     call from_param("dip_eps",dip_eps,10.)
!!!!!!!!!!!!!!!!!!
  end if
call from_param("sufx",sufx,"_")
call from_param("dot",dot,.false.)
call from_param("adj",adj,.true.)
call from_param("inversion",inversion,.false.)
call from_param("dso",dso,.false.)
call from_param("l1norm",l1norm,.false.)
call from_param("windx",windx,11)
call from_param("windh",windh,3)
call from_param("windz",windz,11)
call from_param("extrapolator",extrap,"ffd")
call from_param("zwgt",zwgt,99999.)
if (extrap(1:3).eq."ffd") then
   call from_param("dipop",dipop,45)
   call from_param("trick",trick,0.1666)
end if
if (dso) then
   call from_param("maxvchg",maxvchg,0.15)
   call from_param("moddso",moddso,.false.)
   call from_param("expdso",expdso,1.)
   call from_param("envelope",envelope,.false.)
   if (envelope) then
     call from_param("tc1",tc1,25)
     call from_param("tc2",tc2,5)
     call from_param("freq",freq,0)
   end if
end if
call from_param("born2",born2,.false.)
if (moddso) call from_param("beta",beta,0.1)
call from_param("migration",migration,.false.)
call from_param("areal",areal,.true.)
call from_param("timeshift",timeshift,.false.)
call from_param("imgcon",imgcon,"imgprim")

if (imgcon(1:7).ne."imgprim".or.imgcon(1:7).ne."imgmult") then
   write(0,*) imgcon
!   call seperr("Which img.condition to use? It must be imgprim(primaries) or imgmult(multiples)")
end if
if (inversion) then
   if (not(dso)) then
      call from_param("resmig",resmig,.false.)
      call from_param("rho",rho,1.)
      if (not(resmig)) then
         call from_param("resmig_lin",resmig_lin,.false.)
         if (not(resmig_lin)) then
            call seperr("How to compute the perturbed image?")
         end if
      end if
   end if
end if
call from_param("impulse",impulse,.false.)
call from_param("ymin",ymin,0.)
call from_param("ymax",ymax,0.)
call from_param("zmax",zmax,(v%z%n-1)*v%z%d+v%z%o)
call from_param("node",node,9999)
call from_param("niter",niter,5) ; write(0,*) "****** NITER  = ", niter
call from_param("nxfilt",nxfilt,99999)
call from_param("wbott",wbott,0.)
call from_param("traces_in_taper",tapertr,0)
call from_param("padtraces",padtraces,tapertr)
call from_param("maxang",maxang,89.)
call from_param("damp", damp, 25000.)
call from_param("zweight",zweight,.false.)

if (timeshift) then
   call from_param("thresh",thresh,.33)
   call from_param("nxw",nxw,31)
   call from_param("nah",nah,41);call from_param("dah",dah,2.);call from_param("oah",oah,-40.)
   call from_param("nax",nax,21);call from_param("dax",dax,2.);call from_param("oax",oax,-10.)
end if

 call from_aux("rec","n1",rec%h%n);	call from_aux("rec","d1",rec%h%d);call from_aux("rec","o1",rec%h%o)
 call from_aux("rec","n3",rec%w%n);	call from_aux("rec","d3",rec%w%d);call from_aux("rec","o3",rec%w%o)
 call from_aux("rec","n4",rec%xd%n);call from_aux("rec","d4",rec%xd%d);call from_aux("rec","o4",rec%xd%o)  
 call from_aux("rec","o6",zminr)  
 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("sou","o6",zmins)  
 if (zmins.ne.zminr) call seperr("z0 source .ne. z0 receiver")
 call from_param("zmin",zmin,zminr)
 
 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)

if (not(inversion.or.dot.or.migration)) then
   if (adj) then
      call from_aux("deltaI","n1",img%xm%n);call from_aux("deltaI","d1",img%z%d);call from_aux("deltaI","o1",img%xm%o)
      if (timeshift) then
         call from_aux("deltaI","n2",img%th%n);call from_aux("deltaI","d2",img%th%d);call from_aux("deltaI","o2",img%th%o)
      else
         call from_aux("deltaI","n2",img%sh%n);call from_aux("deltaI","d2",img%sh%d);call from_aux("deltaI","o2",img%sh%o)
      end if
      call from_aux("deltaI","n3",img%z%n);call from_aux("deltaI","d3",img%z%d);call from_aux("deltaI","o3",img%z%o)  
      call from_param("xv_min",xv_min,v%xm%o)
      call from_param("xv_max",xv_max,(v%xm%n-1)*v%xm%d+v%xm%o)
   else
       call from_aux("deltaS","n3",nd1);	call from_aux("deltaS","d3",dd1);	call from_aux("deltaS","o3",od1)
       call from_aux("deltaS","n1",nd2);	call from_aux("deltaS","d1",dd2);	call from_aux("deltaS","o1",od2)
   end if
end if
if (extrap(1:4).eq."pspi") then
   call from_param("nrefvel",nref,1) 
   call from_param("lloyd",lloyd,.false.)
   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
end if
 if (sou%w%n .ne. rec%w%n) then
    call seperr("# source freq. .ne. # receiver freq.")
 end if
 if (sou%xd%n.ne.1) then
    if (sou%xd%n .ne. rec%xd%n) then
       call seperr("# source planes. .ne. # receiver planes")
    end if
    if (sou%h%n .ne. rec%h%n) then
       call seperr("# source traces. .ne. # receiver traces")
    end if
    if (sou%h%d .ne. rec%h%d) then
       call seperr("source trace interval .ne. areal shot trace interval")
    end if
 end if
 if (sou%w%d .ne. rec%w%d) then
    call seperr("source freq.interval .ne. receiver freq.interval")
 end if

 if (sou%w%o .ne. rec%w%o) then
    call seperr("source freq.origin .ne. receiver freq.origin")
 end if

call from_param("icwind",icwind,.false.)
if (icwind) then
   call from_param("deltat",deltat,0.048)
   n_sampt=1;
   do while (rec%w%n > n_sampt)
      n_sampt=n_sampt*2
   end do 
   n_sampt=n_sampt*2
   d_sampt=1./(n_sampt*rec%w%d)
   n_wind=int(deltat/d_sampt)+1
   write(0,*) "n_sampt = ",n_sampt,"deltat = ",deltat," n_wind = ",n_wind
   if (mod(n_wind,2) == 0) n_wind=n_wind+1
   deltat=n_wind*d_sampt 
   write(0,*) "n_sampt = ",n_sampt,"deltat = ",deltat," n_wind = ",n_wind
end if
if (areal) then
   img%xm%n=rec%h%n+2*padtraces+2*tapertr
   img%xm%d=rec%h%d
   img%xm%o=rec%h%o-(padtraces+tapertr)*img%xm%d
else
   img%xm%n=rec%h%n+2*padtraces+2*tapertr+(rec%xd%n-1)*rec%xd%d/rec%h%d
   img%xm%d=rec%h%d
   img%xm%o=rec%h%o-(padtraces+tapertr)*img%xm%d+rec%xd%o
end if

if (inversion.or.exist_file("deltaI")) then
   call from_param("xv_min",xv_min,img%xm%o+10*img%xm%d)
   call from_param("xv_max",xv_max,(img%xm%n-1)*img%xm%d+img%xm%o-10*img%xm%d)
end if
! to be changed for real 3D
  img%ym%n=1
  img%ym%d=1.
  img%ym%o=0.

if (not(inversion)) then
   if (not(adj)) then
      if (nd1.ne.v%z%n) then
write(0,*) nd1,v%z%n
         call seperr("deltaS # depth samples .ne. velocity  ")
      end if
      if (nd2.ne.v%xm%n) then
         call seperr("deltaS # traces .ne. velocity interval ")
      end if
      if (dd1.ne.v%z%d) then
         call seperr("deltaS depth interval .ne. velocity interval ")
      end if
      if (dd2.ne.v%xm%d) then
         call seperr("deltaS trace interval .ne. velocity interval ")
      end if
      if (od1.ne.v%z%o) then
         call seperr("deltaS depth origin .ne. velocity interval ")
      end if
      if (od2.ne.v%xm%o) then
         call seperr("deltaS x origin .ne. velocity interval ")
      end if
   end if
end if

  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")

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

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


  call from_param("wbott",wbott,0.)

  if(.not.adj.or.dot.or.inversion.or.migration) then
     if (timeshift) then
        call from_param("nth",img%th%n,121)
        call from_param("oth",img%th%o,-0.24)
        call from_param("dth",img%th%d,0.004)
     else
        call from_param("nh",img%sh%n,1)
        call from_param("jh",jh,1)
        call from_param("dh",img%sh%d,jh*img%xm%d)
        call from_param("oh",img%sh%o,-int(img%sh%n/2)*img%sh%d)
!!!!!!!!!!!!!!!!!
!
! works for symmetric offset
        nh0=int(img%sh%n/2.)+1
     end if
  end if
!----------------------------------------------
!
! Migration output
!
if (.not.adj.or.dot.or.inversion.or.migration) then
  img%z%n=nz
  img%z%o=zmin
  img%z%d=v%z%d
end if

if (migration) then
   call to_history("n1",img%xm%n,"bkgimg"); call to_history("d1",rec%h%d,"bkgimg"); call to_history("o1",img%xm%o,"bkgimg")  
   if (timeshift) then
      call to_history("n2",img%th%n,"bkgimg") ; call to_history("d2",img%th%d,"bkgimg") ; call to_history("o2",img%th%o,"bkgimg")
   else
      call to_history("n2",img%sh%n,"bkgimg") ; call to_history("d2",img%sh%d,"bkgimg") ; call to_history("o2",img%sh%o,"bkgimg")
   end if
   call to_history("n3",nz,"bkgimg") ; call to_history("d3",img%z%d,"bkgimg") ; call to_history("o3",zmin,"bkgimg")
   call to_history("n4",1,"bkgimg"); call to_history("d4",1,"bkgimg"); call to_history("o4",0,"bkgimg")
   call to_history("n5",1,"bkgimg"); call to_history("d5",1,"bkgimg"); call to_history("o5",0,"bkgimg")  
   call to_history("esize",4,"bkgimg")
end if

if (not(inversion)) then
   if (adj) then
      call to_history("n1",img%xm%n) ; call to_history("d1",img%xm%d) ; call to_history("o1",img%xm%o)
      call to_history("n2",1) ; call to_history("d2",1.) ; call to_history("o2",0.)
      call to_history("n3",nz); call to_history("d3",img%z%d); call to_history("o3",zmin)  
      call to_history("n4",1); call to_history("d4",1); call to_history("o4",0)
      call to_history("n5",1); call to_history("d5",1); call to_history("o5",0)  
      call to_history("esize",4)
   if (exist_file("ddws")) then
      call to_history("n1",kxm%n,"ddws"); call to_history("d1",img%xm%d,"ddws"); call to_history("o1",img%xm%o,"ddws")  
      call to_history("n2",nz,"ddws") ; call to_history("d2",img%z%d,"ddws") ; call to_history("o2",zmin,"ddws")
      call to_history("n3",1,"ddws"); call to_history("d3",1,"ddws"); call to_history("o3",0,"ddws")  
      call to_history("n4",1,"ddws"); call to_history("d4",1,"ddws"); call to_history("o4",0,"ddws")
      call to_history("n5",rec%w%n,"ddws") ; call to_history("d5",rec%w%d,"ddws") ; call to_history("o5",rec%w%o,"ddws")
      call to_history("esize",8,"ddws")
   end if

   if (exist_file("ddwr")) then
      call to_history("n1",kxm%n,"ddwr"); call to_history("d1",img%xm%d,"ddwr"); call to_history("o1",img%xm%o,"ddwr")  
      call to_history("n2",nz,"ddwr") ; call to_history("d2",img%z%d,"ddwr") ; call to_history("o2",zmin,"ddwr")
      call to_history("n3",1,"ddwr"); call to_history("d3",1,"ddwr"); call to_history("o3",0,"ddwr")  
      call to_history("n4",1,"ddwr"); call to_history("d4",1,"ddwr"); call to_history("o4",0,"ddwr")
      call to_history("n5",rec%w%n,"ddwr") ; call to_history("d5",rec%w%d,"ddwr") ; call to_history("o5",rec%w%o,"ddwr")
      call to_history("esize",8,"ddwr")
   end if

   if (exist_file("ddwsz")) then
      call to_history("n1",kxm%n,"ddwsz"); call to_history("d1",img%xm%d,"ddwsz"); call to_history("o1",img%xm%o,"ddwsz")  
      call to_history("n2",nz,"ddwsz") ; call to_history("d2",img%z%d,"ddwsz") ; call to_history("o2",zmin,"ddwsz")
      call to_history("n3",1,"ddwsz"); call to_history("d3",1,"ddwsz"); call to_history("o3",0,"ddwsz")  
      call to_history("n4",1,"ddwsz"); call to_history("d4",1,"ddwsz"); call to_history("o4",0,"ddwsz")
      call to_history("n5",rec%w%n,"ddwsz") ; call to_history("d5",rec%w%d,"ddwsz") ; call to_history("o5",rec%w%o,"ddwsz")
      call to_history("esize",8,"ddwsz")
   end if

   if (exist_file("ddwrz")) then
      call to_history("n1",kxm%n,"ddwrz"); call to_history("d1",img%xm%d,"ddwrz"); call to_history("o1",img%xm%o,"ddwrz")  
      call to_history("n2",nz,"ddwrz") ; call to_history("d2",img%z%d,"ddwrz") ; call to_history("o2",zmin,"ddwrz")
      call to_history("n3",1,"ddwrz"); call to_history("d3",1,"ddwrz"); call to_history("o3",0,"ddwrz")  
      call to_history("n4",1,"ddwrz"); call to_history("d4",1,"ddwrz"); call to_history("o4",0,"ddwrz")
      call to_history("n5",rec%w%n,"ddwrz") ; call to_history("d5",rec%w%d,"ddwrz") ; call to_history("o5",rec%w%o,"ddwrz")
      call to_history("esize",8,"ddwrz")
   end if

   else
      call to_history("n1",img%xm%n); call to_history("d1",img%xm%d); call to_history("o1",img%xm%o)  
      if (timeshift) then
         call to_history("n2",img%th%n) ; call to_history("d2",img%th%d) ; call to_history("o2",img%th%o)
      else
         call to_history("n2",img%sh%n) ; call to_history("d2",img%sh%d) ; call to_history("o2",img%sh%o)
      end if
      call to_history("n3",nz) ; call to_history("d3",img%z%d) ; call to_history("o3",zmin)
      call to_history("n4",1); call to_history("d4",1); call to_history("o4",0)
      call to_history("n5",1); call to_history("d5",1); call to_history("o5",0)  
      call to_history("esize",4)

      if (light.and.not(timeshift)) then
         call to_history("n1",img%xm%n,"illum"); call to_history("d1",img%xm%d,"illum"); call to_history("o1",img%xm%o,"illum")  
         call to_history("n2",1,"illum") ; call to_history("d2",1.,"illum") ; call to_history("o2",0.,"illum")
         call to_history("n3",nz,"illum") ; call to_history("d3",img%z%d,"illum") ; call to_history("o3",img%z%o,"illum")
         call to_history("n4",1,"illum"); call to_history("d4",1,"illum"); call to_history("o4",0,"illum")
         call to_history("n5",1,"illum"); call to_history("d5",1,"illum"); call to_history("o5",0,"illum")  
         call to_history("esize",4,"illum")
      end if
   end if
else
   if (light.and.not(timeshift)) then
      call to_history("n1",img%xm%n,"illum"); call to_history("d1",img%xm%d,"illum"); call to_history("o1",img%xm%o,"illum")  
      call to_history("n2",1,"illum") ; call to_history("d2",1.,"illum") ; call to_history("o2",0.,"illum")
      call to_history("n3",nz,"illum") ; call to_history("d3",img%z%d,"illum") ; call to_history("o3",img%z%o,"illum")
      call to_history("n4",1,"illum"); call to_history("d4",1,"illum"); call to_history("o4",0,"illum")
      call to_history("n5",1,"illum"); call to_history("d5",1,"illum"); call to_history("o5",0,"illum")  
      call to_history("esize",4,"illum")
   end if
   if (exist_file("grad")) then
      call to_history("n1",img%xm%n,"grad"); call to_history("d1",img%xm%d,"grad"); call to_history("o1",img%xm%o,"grad")  
      call to_history("n2",1,"grad") ; call to_history("d2",1.,"grad") ; call to_history("o2",0.,"grad")
      call to_history("n3",nz,"grad") ; call to_history("d3",img%z%d,"grad") ; call to_history("o3",img%z%o,"grad")
      call to_history("n4",1,"grad"); call to_history("d4",1,"grad"); call to_history("o4",0,"grad")
      call to_history("n5",1,"grad"); call to_history("d5",1,"grad"); call to_history("o5",0,"grad")  
      call to_history("esize",4,"grad")
   end if
   if (exist_file("Velout")) then
      call to_history("n1",img%xm%n,"Velout"); call to_history("d1",img%xm%d,"Velout"); call to_history("o1",img%xm%o,"Velout")  
      call to_history("n2",1,"Velout") ; call to_history("d2",1.,"Velout") ; call to_history("o2",0.,"Velout")
      call to_history("n3",img%z%n,"Velout") ; call to_history("d3",img%z%d,"Velout") ; call to_history("o3",img%z%o,"Velout")
      call to_history("n4",1,"Velout"); call to_history("d4",1,"Velout"); call to_history("o4",0,"Velout")
      call to_history("n5",1,"Velout"); call to_history("d5",1,"Velout"); call to_history("o5",0,"Velout")  
      call to_history("esize",4,"Velout")
   end if
   if (exist_file("Dimage")) then
      call to_history("n1",img%xm%n,"Dimage"); call to_history("d1",rec%h%d,"Dimage"); call to_history("o1",img%xm%o,"Dimage")  
      if (timeshift) then
         call to_history("n2",img%th%n,"Dimage") ; call to_history("d2",img%th%d,"Dimage") ; call to_history("o2",img%th%o,"Dimage")
      else
         call to_history("n2",img%sh%n,"Dimage") ; call to_history("d2",img%sh%d,"Dimage") ; call to_history("o2",img%sh%o,"Dimage")
      end if
      call to_history("n3",nz,"Dimage") ; call to_history("d3",img%z%d,"Dimage") ; call to_history("o3",zmin,"Dimage")
      call to_history("n4",1,"Dimage"); call to_history("d4",1,"Dimage"); call to_history("o4",0,"Dimage")
      call to_history("n5",1,"Dimage"); call to_history("d5",1,"Dimage"); call to_history("o5",0,"Dimage")  
      call to_history("esize",4,"Dimage")
   end if
   if (exist_file("bkgimg")) then
      call to_history("n1",img%xm%n,"bkgimg"); call to_history("d1",rec%h%d,"bkgimg"); call to_history("o1",img%xm%o,"bkgimg")  
      if (timeshift) then
         call to_history("n2",img%th%n,"bkgimg") ; call to_history("d2",img%th%d,"bkgimg") ; call to_history("o2",img%th%o,"bkgimg")
      else
         call to_history("n2",img%sh%n,"bkgimg") ; call to_history("d2",img%sh%d,"bkgimg") ; call to_history("o2",img%sh%o,"bkgimg")
      end if
      call to_history("n3",nz,"bkgimg") ; call to_history("d3",img%z%d,"bkgimg") ; call to_history("o3",zmin,"bkgimg")
      call to_history("n4",1,"bkgimg"); call to_history("d4",1,"bkgimg"); call to_history("o4",0,"bkgimg")
      call to_history("n5",1,"bkgimg"); call to_history("d5",1,"bkgimg"); call to_history("o5",0,"bkgimg")  
      call to_history("esize",4,"bkgimg")
   end if

   call to_history("n1",img%xm%n); call to_history("d1",img%xm%d); call to_history("o1",img%xm%o)  
   call to_history("n2",1) ; call to_history("d2",1.) ; call to_history("o2",0.)
   call to_history("n3",nz) ; call to_history("d3",img%z%d) ; call to_history("o3",zmin)
   call to_history("n4",1); call to_history("d4",1); call to_history("o4",0)
   call to_history("n5",1); call to_history("d5",1); call to_history("o5",0)  
   call to_history("esize",4)
end if

if (migration) then
   call to_history("n1",img%xm%n); call to_history("d1",img%xm%d); call to_history("o1",img%xm%o)  
   if (timeshift) then
      call to_history("n2",img%th%n) ; call to_history("d2",img%th%d) ; call to_history("o2",img%th%o)
   else
      call to_history("n2",img%sh%n) ; call to_history("d2",img%sh%d) ; call to_history("o2",img%sh%o)
   end if
   call to_history("n3",nz) ; call to_history("d3",img%z%d) ; call to_history("o3",zmin)
   call to_history("n4",1); call to_history("d4",1); call to_history("o4",0)
   call to_history("n5",1); call to_history("d5",1); call to_history("o5",0)  
   call to_history("esize",4)
end if


  wbott=int(wbott/v%z%d)*v%z%d
  nwbott=int(wbott/v%z%d)+1
  dwbott=wbott/2.

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

!--------------------------------------------------
! Wavenumber vectors
!
! BUG DANGER: img%z%n ==> nz !!!!!
!
!
!if (resmig.or.resmig_lin) then
!!!kz
   kz%n = img%z%n
   allocate(kz%all(kz%n))
   kz%all=0.
   if(kz%n/=1) then
     m = kz%n/2
     do i=1,m+1
        kz%all(i)=i-1
     end do
     do i=m+2,kz%n
        kz%all(i)=i-kz%n-1
     end do
   else
     kz%all=0. !! make sure k=0 if n=1
   end if
   kz%all = kz%all / (kz%n*img%z%d)      !! 1/length
   kz%all = 2 * PI * kz%all     !! radians
   kz%d = kz%all(2)-kz%all(1)
!!! ksh
if (not(timeshift)) then
   ksh%n = img%sh%n
   allocate(ksh%all(ksh%n))
   ksh%all=0.
   if(ksh%n/=1) then
     m = ksh%n/2
     do i=1,m+1
        ksh%all(i)=i-1
     end do
     do i=m+2,ksh%n
        ksh%all(i)=i-ksh%n-1
     end do
   else
     ksh%all=0. !! make sure k=0 if n=1
   end if
   ksh%all = ksh%all / (ksh%n*img%sh%d)      !! 1/length
   ksh%all = 2 * PI * ksh%all     !! radians
   if (ksh%n/=1) then
      ksh%d = ksh%all(2)-ksh%all(1)
   else
      ksh%d=1/img%sh%d
   end if
end if
!end if
!kxm%n = rec%h%n+2*padtraces+2*tapertr
!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)
!kxm%all = 2*pi*kxm%all

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 i=1,kxm%n
   kxm%all(i) = mod(kxm%o+(i-1)*kxm%d,1./rec%h%d)-kxm%o
end do
kxm%all = 2*pi * kxm%all

if (timeshift) then
   nsecdim=img%th%n
   dsecdim=img%th%d
   osecdim=img%th%o
else
   nsecdim=img%sh%n
   dsecdim=img%sh%d
   osecdim=img%sh%o
end if


   if (exist_file("ddws")) then
      call to_history("n1",kxm%n,"ddws"); call to_history("d1",img%xm%d,"ddws"); call to_history("o1",img%xm%o,"ddws")  
      call to_history("n2",nz,"ddws") ; call to_history("d2",img%z%d,"ddws") ; call to_history("o2",zmin,"ddws")
      call to_history("n3",1,"ddws"); call to_history("d3",1,"ddws"); call to_history("o3",0,"ddws")  
      call to_history("n4",1,"ddws"); call to_history("d4",1,"ddws"); call to_history("o4",0,"ddws")
      call to_history("n5",rec%w%n,"ddws") ; call to_history("d5",rec%w%d,"ddws") ; call to_history("o5",rec%w%o,"ddws")
      call to_history("esize",8,"ddws")
   end if

   if (exist_file("ddwr")) then
      call to_history("n1",kxm%n,"ddwr"); call to_history("d1",img%xm%d,"ddwr"); call to_history("o1",img%xm%o,"ddwr")  
      call to_history("n2",nz,"ddwr") ; call to_history("d2",img%z%d,"ddwr") ; call to_history("o2",zmin,"ddwr")
      call to_history("n3",1,"ddwr"); call to_history("d3",1,"ddwr"); call to_history("o3",0,"ddwr")  
      call to_history("n4",1,"ddwr"); call to_history("d4",1,"ddwr"); call to_history("o4",0,"ddwr")
      call to_history("n5",rec%w%n,"ddwr") ; call to_history("d5",rec%w%d,"ddwr") ; call to_history("o5",rec%w%o,"ddwr")
      call to_history("esize",8,"ddwr")
   end if

   if (exist_file("dws")) then
      call to_history("n1",kxm%n,"dws"); call to_history("d1",img%xm%d,"dws"); call to_history("o1",img%xm%o,"dws")  
      call to_history("n2",nz,"dws") ; call to_history("d2",img%z%d,"dws") ; call to_history("o2",zmin,"dws")
      call to_history("n3",1,"dws"); call to_history("d3",1,"dws"); call to_history("o3",0,"dws")  
      call to_history("n4",1,"dws"); call to_history("d4",1,"dws"); call to_history("o4",0,"dws")
      call to_history("n5",rec%w%n,"dws") ; call to_history("d5",rec%w%d,"dws") ; call to_history("o5",rec%w%o,"dws")
      call to_history("esize",8,"dws")
   end if

   if (exist_file("dwr")) then
      call to_history("n1",kxm%n,"dwr"); call to_history("d1",img%xm%d,"dwr"); call to_history("o1",img%xm%o,"dwr")  
      call to_history("n2",nz,"dwr") ; call to_history("d2",img%z%d,"dwr") ; call to_history("o2",zmin,"dwr")
      call to_history("n3",1,"dwr"); call to_history("d3",1,"dwr"); call to_history("o3",0,"dwr")  
      call to_history("n4",1,"dwr"); call to_history("d4",1,"dwr"); call to_history("o4",0,"dwr")
      call to_history("n5",rec%w%n,"dwr") ; call to_history("d5",rec%w%d,"dwr") ; call to_history("o5",rec%w%o,"dwr")
      call to_history("esize",8,"dwr")
   end if

   if (exist_file("ddwsz")) then
      call to_history("n1",kxm%n,"ddwsz"); call to_history("d1",img%xm%d,"ddwsz"); call to_history("o1",img%xm%o,"ddwsz")  
      call to_history("n2",nz,"ddwsz") ; call to_history("d2",img%z%d,"ddwsz") ; call to_history("o2",zmin,"ddwsz")
      call to_history("n3",1,"ddwsz"); call to_history("d3",1,"ddwsz"); call to_history("o3",0,"ddwsz")  
      call to_history("n4",1,"ddwsz"); call to_history("d4",1,"ddwsz"); call to_history("o4",0,"ddwsz")
      call to_history("n5",rec%w%n,"ddwsz") ; call to_history("d5",rec%w%d,"ddwsz") ; call to_history("o5",rec%w%o,"ddwsz")
      call to_history("esize",8,"ddwsz")
   end if

   if (exist_file("ddwrz")) then
      call to_history("n1",kxm%n,"ddwrz"); call to_history("d1",img%xm%d,"ddwrz"); call to_history("o1",img%xm%o,"ddwrz")  
      call to_history("n2",nz,"ddwrz") ; call to_history("d2",img%z%d,"ddwrz") ; call to_history("o2",zmin,"ddwrz")
      call to_history("n3",1,"ddwrz"); call to_history("d3",1,"ddwrz"); call to_history("o3",0,"ddwrz")  
      call to_history("n4",1,"ddwrz"); call to_history("d4",1,"ddwrz"); call to_history("o4",0,"ddwrz")
      call to_history("n5",rec%w%n,"ddwrz") ; call to_history("d5",rec%w%d,"ddwrz") ; call to_history("o5",rec%w%o,"ddwrz")
      call to_history("esize",8,"ddwrz")
   end if

!--------------------------------------------------
! 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

!----------------------------------------------------
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
if (timeshift) then
   write(0,*) "no.timeshifts   = ", img%th%n
   write(0,*) "time interval   = ", img%th%d
   write(0,*) "time origin     = ", img%th%o
else
   write(0,*) "no.of offsets    = ", img%sh%n
   write(0,*) "offset spacing   = ", img%sh%d
   write(0,*) "offset origin     = ", img%sh%o
end if
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,*) "*****************************************************"
end if
end subroutine

end module
