module OW_parms_areal

use sep
use mpi_sep
use OW_type
use OW_mpi 
use base_blkcal_mod

implicit none
character*12,public, save                                   :: sufx
character*6,public, save                                    :: extrap,cgsolv
character*7,public, save                                    :: imgcon
character*80,public, save                                   :: hor1, listagem
logical,public, save				:: adj,dot,icwind,impulse,dso,resmig,migration
logical,public, save				:: light,moddso,smooth,imgnorm,inversion
logical,public, save                                        :: lapl,geomspread,zweight,resmig_lin,areal
logical,public, save				:: timeshift,l1norm,envelope,born2,renorm
logical,public, save                                        :: bounds,cbounds
type (axis) ,public, save				:: kxm, hx, hy, kym, w, xd
type (axis) ,public, save				:: xm, ym, z, shx, shy, kz, khx, khy
type (dat_grid)	 ,public, save			:: rec, sou
type (mod_grid)	 ,public, save			:: img
type (velo) ,public, save				:: v
type (block) ,public, save				:: wblock,zblock
integer	 ,public, save				:: max_iters, line_search,iters
integer	 ,public, save				:: windx,windh,windz,frame,tc1,tc2
integer	 ,public, save				:: freq,jhx,jhy,nx,ny,nref,iwb
integer	 ,public, save				:: tapertrx,padtracesx,tapertry,padtracesy,nreal
integer	 ,public, save				:: nz,nzmin,nzmax,nsecdimy,nsecdimx,nitercg
integer	 ,public, save				:: isp,nwbott,node,niter,n_wind,n_sampt,nxfilt,nh0,dipop
integer, public, save                                       :: ninx,nfnx,niny,nfny
real ,public, save					:: xmin,xmax,ymin,ymax,zmin,zmax,pi,dwbott,yline_
real ,public, save					:: osecdimx,dsecdimx,osecdimy,dsecdimy,thresh,maxvchg
real, public, save					:: vsal,rho,maxang,damp,beta
real, public, save					:: xv_min,xv_max,yv_min,yv_max,trick,expdso,zwgt
real,public, save                                           :: vmin,vmax,p_up,p_lo
real,public, save                                           :: err,err1,epsteep,factor,lambda_min,tola,tol(2)
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
integer, public, save                                       :: shtblk,shtres,shtblm

contains

subroutine owparam()

real						:: wbott,dd1,dd2,dd3,od1,od2,od3,dv1,dv2,ov1,ov2
real						:: deltat, d_sampt,dzref,ozref,zmin1
integer						:: iw,nt, i, m,nd1,nd2,nd3,nv1,nv2,nzref,jh,nreal1

if (wpar%i_mpi==0) then
 call from_param("dipflt",dipflt,.false.)
 if (dipflt) call from_param('frame',frame,50)
call from_param("epscg",epscg,1.)
call from_param("nitercg",nitercg,50)
call from_param("cgsolv",cgsolv,"      ")
if (cgsolv(1:3).eq."reg") call from_param("lapl",lapl,.true.)
!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("geomspread",geomspread,.true.)
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.14)
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.)
call from_param("renorm",renorm,.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")
call from_param("imgnorm",imgnorm,.false.)
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("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_x",tapertrx,0)
call from_param("padtraces_x",padtracesx,tapertrx)
call from_param("traces_in_taper_y",tapertry,0)
call from_param("padtraces_y",padtracesy,tapertry)
call from_param("maxang",maxang,89.)
call from_param("damp", damp, 25000.)
call from_param("zweight",zweight,.false.)

 call from_aux("rec","n1",rec%hx%n);	call from_aux("rec","d1",rec%hx%d);     call from_aux("rec","o1",rec%hx%o)
 call from_aux("rec","n2",rec%hy%n);	call from_aux("rec","d2",rec%hy%d);     call from_aux("rec","o2",rec%hy%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","n5",nreal1)
 call from_aux("rec","o6",zmin1)

 call from_aux("sou","n1",sou%hx%n);	call from_aux("sou","d1",sou%hx%d);	call from_aux("sou","o1",sou%hx%o)
 call from_aux("sou","n2",sou%hy%n);	call from_aux("sou","d2",sou%hy%d);	call from_aux("sou","o2",sou%hy%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","n5",nreal)
 call from_aux("sou","o6",zmin)

 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","n2",v%ym%n);	call from_aux("vel","d2",v%ym%d);	call from_aux("vel","o2",v%ym%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 (zmin.ne.zmin1) call seperr("source and receiver recorded at different depth levels")
 call from_param("zmax",zmax,(v%z%n-1)*v%z%d+zmin)

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 (nreal1 .ne. nreal) then
    call seperr("# source realizations .ne. # receiver realizations")
 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. rec%xd%n) then
    call seperr("# source planes. .ne. # receiver planes")
 end if
 if (sou%hx%n .ne. rec%hx%n) then
    call seperr("# source x-traces. .ne. # receiver x-traces")
 end if
 if (sou%hy%n .ne. rec%hy%n) then
    call seperr("# source y-traces. .ne. # receiver y-traces")
 end if
 if (sou%hx%d .ne. rec%hx%d) then
    call seperr("source x-trace interval .ne. areal shot x-trace interval")
 end if
 if (sou%hy%d .ne. rec%hy%d) then
    call seperr("source y-trace interval .ne. areal shot y-trace interval")
 end if
 if (sou%hx%o .ne. rec%hx%o) then
    call seperr("source x-trace origin .ne. areal shot x-trace origin")
 end if
 if (sou%hy%o .ne. rec%hy%o) then
    call seperr("source y-trace origin .ne. areal shot y-trace origin")
 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

   img%xm%n=rec%hx%n+2*padtracesx+2*tapertrx
   img%xm%d=rec%hx%d
   img%xm%o=rec%hx%o-(padtracesx+tapertrx)*img%xm%d
   img%ym%n=rec%hy%n+2*padtracesy+2*tapertry
   img%ym%d=rec%hy%d
   img%ym%o=rec%hy%o-(padtracesy+tapertry)*img%ym%d

 xmin=img%xm%o
 xmax=((img%xm%n-1)+(padtracesx+tapertrx))*img%xm%d+img%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")

 ymin=img%ym%o
 ymax=((img%ym%n-1)+(padtracesy+tapertry))*img%ym%d+img%ym%o
 if (ymin < v%ym%o ) call seperr("Min.y-image < Min.y-velocity")
 if (ymax > v%ym%o+(v%ym%n-1)*v%ym%d ) call seperr("Max.y-image > Max.y-velocity")

 nx=(xmax-xmin)/img%xm%d+1.5
 ny=(ymax-ymin)/img%ym%d+1.5

if (inversion) 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)
   call from_param("yv_min",yv_min,img%ym%o+10*img%ym%d)
   call from_param("yv_max",yv_max,(img%ym%n-1)*img%ym%d+img%ym%o-10*img%ym%d)
end if

  call from_param("yline_",yline_,((img%ym%n-1)*img%ym%d+img%ym%o)/2)

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

   call from_param("nhx",img%shx%n,1)
   call from_param("jhx",jhx,1)
   call from_param("dhx",img%shx%d,jhx*img%xm%d)
   call from_param("ohx",img%shx%o,-int(img%shx%n/2)*img%shx%d)
   call from_param("nhy",img%shy%n,1)
   call from_param("jhy",jhy,1)
   call from_param("dhy",img%shy%d,jhy*img%ym%d)
   call from_param("ohy",img%shy%o,-int(img%shy%n/2)*img%shy%d)

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

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

pi=4*atan(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)

!!! khy
   khy%n = img%shy%n
   allocate(khy%all(khy%n))
   khy%all=0.
   if(khy%n/=1) then
     m = khy%n/2
     do i=1,m+1
        khy%all(i)=i-1
     end do
     do i=m+2,khy%n
        khy%all(i)=i-khy%n-1
     end do
   else
     khy%all=0. !! make sure k=0 if n=1
   end if
   khy%all = khy%all / (khy%n*img%shy%d)      !! 1/length
   khy%all = 2 * PI * khy%all     !! radians
   if (khy%n/=1) then
      khy%d = khy%all(2)-khy%all(1)
   else
      khy%d=1/img%shy%d
   end if

!!! khx
   khx%n = img%shx%n
   allocate(khx%all(khx%n))
   khx%all=0.
   if(khx%n/=1) then
     m = khx%n/2
     do i=1,m+1
        khx%all(i)=i-1
     end do
     do i=m+2,khx%n
        khx%all(i)=i-khx%n-1
     end do
   else
     khx%all=0. !! make sure k=0 if n=1
   end if
   khx%all = khx%all / (khx%n*img%shx%d)      !! 1/length
   khx%all = 2 * PI * khx%all     !! radians
   if (khx%n/=1) then
      khx%d = khx%all(2)-khx%all(1)
   else
      khx%d=1/img%shx%d
   end if

kxm%n = rec%hx%n+2*(tapertrx+padtracesx)
kxm%d = 1./(kxm%n*rec%hx%d)
kxm%o = 1./(2*rec%hx%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%hx%d)-kxm%o
end do
kxm%all = 2*pi * kxm%all

kym%n = rec%hy%n+2*(tapertry+padtracesy)
kym%d = 1./(kym%n*rec%hy%d)
kym%o = 1./(2*rec%hy%d)-kym%d
allocate(kym%all(kym%n))
do i=1,kym%n
   kym%all(i) = mod(kym%o+(i-1)*kym%d,1./rec%hy%d)-kym%o
end do
kym%all = 2*pi * kym%all

   nsecdimx=img%shx%n
   dsecdimx=img%shx%d
   osecdimx=img%shx%o

   nsecdimy=img%shy%n
   dsecdimy=img%shy%d
   osecdimy=img%shy%o

!----------------------------------------------
!
! Migration output
!
if (migration) then
   call auxscr("bkgimg")
   call to_history("n1",img%xm%n,"bkgimg"); call to_history("d1",img%xm%d,"bkgimg"); call to_history("o1",img%xm%o,"bkgimg")  
   call to_history("n2",img%shx%n,"bkgimg") ; call to_history("d2",img%shx%d,"bkgimg") ; call to_history("o2",img%shx%o,"bkgimg")
   call to_history("n3",img%ym%n,"bkgimg"); call to_history("d3",img%ym%d,"bkgimg"); call to_history("o3",img%ym%o,"bkgimg")  
   call to_history("n4",img%shy%n,"bkgimg") ; call to_history("d4",img%shy%d,"bkgimg") ; call to_history("o4",img%shy%o,"bkgimg")
   call to_history("n5",nz,"bkgimg") ; call to_history("d5",img%z%d,"bkgimg") ; call to_history("o5",zmin,"bkgimg")
   call to_history("esize",4,"bkgimg")
end if

if (inversion) then
      call auxscr("grad")
      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",img%ym%n,"grad"); call to_history("d2",img%ym%d,"grad"); call to_history("o2",img%ym%o,"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")

      call auxscr("Velout")
      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",img%ym%n,"Velout"); call to_history("d2",img%ym%d,"Velout"); call to_history("o2",img%ym%o,"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")

      call auxscr("Dimage")
      call to_history("n1",img%xm%n,"Dimage"); call to_history("d1",img%xm%d,"Dimage"); call to_history("o1",img%xm%o,"Dimage")  
      call to_history("n2",img%shx%n,"Dimage") ; call to_history("d2",img%shx%d,"Dimage") ; call to_history("o2",img%shx%o,"Dimage")
      call to_history("n3",img%ym%n,"Dimage"); call to_history("d3",img%ym%d,"Dimage"); call to_history("o3",img%ym%o,"Dimage")  
      call to_history("n4",img%shy%n,"Dimage") ; call to_history("d4",img%shy%d,"Dimage") ; call to_history("o4",img%shy%o,"Dimage")
      call to_history("n5",nz,"Dimage") ; call to_history("d5",img%z%d,"Dimage") ; call to_history("o5",zmin,"Dimage")
      call to_history("esize",4,"Dimage")

      call auxscr("bkgimg")
      call to_history("n1",img%xm%n,"bkgimg"); call to_history("d1",img%xm%d,"bkgimg"); call to_history("o1",img%xm%o,"bkgimg")  
      call to_history("n2",img%shx%n,"bkgimg") ; call to_history("d2",img%shx%d,"bkgimg") ; call to_history("o2",img%shx%o,"bkgimg")
      call to_history("n3",img%ym%n,"bkgimg"); call to_history("d3",img%ym%d,"bkgimg"); call to_history("o3",img%ym%o,"bkgimg")  
      call to_history("n4",img%shy%n,"bkgimg") ; call to_history("d4",img%shy%d,"bkgimg") ; call to_history("o4",img%shy%o,"bkgimg")
      call to_history("n5",nz,"bkgimg") ; call to_history("d5",img%z%d,"bkgimg") ; call to_history("o5",zmin,"bkgimg")
      call to_history("esize",4,"bkgimg")

   call auxscr("yline")
   call to_history("n1",img%xm%n,"yline"); call to_history("d1",img%xm%d,"yline"); call to_history("o1",img%xm%o,"yline")  
   call to_history("n2",img%shx%n,"yline") ; call to_history("d2",img%shx%d,"yline") ; call to_history("o2",img%shx%o,"yline")
   call to_history("n3",2,"yline"); call to_history("d3",img%ym%d,"yline"); call to_history("o3",yline_,"yline")  
   call to_history("n4",img%shy%n,"yline") ; call to_history("d4",img%shy%d,"yline") ; call to_history("o4",img%shy%o,"yline")
   call to_history("n5",nz,"yline") ; call to_history("d5",img%z%d,"yline") ; call to_history("o5",zmin,"yline")
   call to_history("n6",1,"yline") ; call to_history("d6",1.,"yline") ; call to_history("o6",1.,"yline")
   call to_history("esize",4,"yline")

   call auxscr("bkws")
   call to_history("n1",kxm%n,"bkws"); call to_history("d1",img%xm%d,"bkws"); call to_history("o1",img%xm%o,"bkws")  
   call to_history("n2",kym%n,"bkws"); call to_history("d2",img%ym%d,"bkws"); call to_history("o2",img%ym%o,"bkws")
   call to_history("n3",img%z%n,"bkws");  call to_history("d3",img%z%d,"bkws");  call to_history("o3",img%z%o,"bkws")  
   call to_history("n4",sou%w%n,"bkws");  call to_history("d4",sou%w%d,"bkws");  call to_history("o4",sou%w%o,"bkws")
   call to_history("n5",1,"bkws");        call to_history("d5",1.,"bkws");       call to_history("o5",0.,"bkws")
   call to_history("esize",8,"bkws")

   call auxscr("bkwr")
   call to_history("n1",kxm%n,"bkwr"); call to_history("d1",img%xm%d,"bkwr"); call to_history("o1",img%xm%o,"bkwr")  
   call to_history("n2",kym%n,"bkwr"); call to_history("d2",img%ym%d,"bkwr"); call to_history("o2",img%ym%o,"bkwr")
   call to_history("n3",img%z%n,"bkwr");  call to_history("d3",img%z%d,"bkwr");  call to_history("o3",img%z%o,"bkwr")  
   call to_history("n4",sou%w%n,"bkwr");  call to_history("d4",sou%w%d,"bkwr");  call to_history("o4",sou%w%o,"bkwr")
   call to_history("n5",1,"bkwr");        call to_history("d5",1.,"bkwr");       call to_history("o5",0.,"bkwr")
   call to_history("esize",8,"bkwr")

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

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

!=====================
! Defining blocks
! w and z
call from_param("nwblk",wblock%ns,node)
call from_param("nzblk",zblock%ns,int(nz/3))
call base_blkcal(wblock, sou%w)
call base_blkcal(zblock, img%z)

!-------->
write(0,*) "Frequency blocking"
write(0,*) wblock%bres,wblock%ds,wblock%nb,wblock%nb_res
write(0,*) wblock%blen(:)
write(0,*) wblock%os(:)
write(0,*) wblock%indx(:)
!-------->
write(0,*) "Depth blocking"
write(0,*) zblock%bres,zblock%ds,zblock%nb,zblock%nb_res
write(0,*) zblock%blen(:)
write(0,*) zblock%os(:)
write(0,*) zblock%indx(:)
!-------->
 call from_param("list",listagem,"listagem")
 open(unit=3,file=listagem)
 call from_param("line_search",line_search,3)
 call from_param("max_iters",max_iters,10)
!----------------------------------------------
! Set bounds
 call from_param("bounds",bounds,.false.)
 if (bounds) then
    call from_param("cbounds",cbounds,.false.)
    if (cbounds) then
       call from_param("vmin",vmin)
       call from_param("vmax",vmax)
    else
       call from_param("p_up",p_up)
       call from_param("p_lo",p_lo)
       if (p_up >= 1.) p_up=1+p_up/100
       if (p_lo >= 1.) p_lo=1-p_lo/100
    end if
 end if 
!----------------------------------------------
!!!!!!!!!!!!!!!!!!
   ! Horizon with no velocity update
   if (exist_file("horv0")) then
      call from_param('horv0',hor1)
   end if

   ! ********************************************** 
   !  Initialization of code variables 
    iters=1
    err=1
    epsteep=1e-8 
    tol(1)=1e-8
    tol(2)=1e-5

   ! Line search variables 
    factor=0.5
    lambda_min=0.00001
   ! ********************************************** 
   !   Set tolerance for stopping criterium 
    err1=tiny(1.)
   ! ********************************************** 
    tola=0; 
   ! ********************************************** 

!<--------
   ninx=tapertrx+padtracesx+1
   nfnx=rec%hx%n+padtracesx+tapertrx
   niny=tapertry+padtracesy+1
   nfny=rec%hy%n+padtracesy+tapertry
!<--------

 shtblm = wpar%n_mpi-1
 shtblk=int(rec%xd%n/shtblm)
 shtres=mod(rec%xd%n,shtblm)
 if (shtres.ne.0) shtblk=shtblk+1

 write(0,*) shtblk,shtres,shtblm,wpar%i_mpi

!----------------------------------------------------
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,*) "traces in taper x   = ",tapertrx
write(0,*) "padded traces   x   = ",padtracesx
write(0,*) "traces in taper y   = ",tapertry
write(0,*) "padded traces   y   = ",padtracesy
write(0,*) //
write(0,*) "============"
write(0,*) "HEADER WORDS"
write(0,*) "============"
write(0,*) //
write(0,*) "SHOTS"
write(0,*) "====="
write(0,*) " # xlines  = ", rec%hx%n
write(0,*) "xline intv = ", rec%hx%d
write(0,*) "xline orig = ", rec%hx%o
write(0,*) " # ilines  = ", rec%hy%n
write(0,*) "iline intv = ", rec%hy%d
write(0,*) "iline orig = ", rec%hy%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,*) " # xlines  = ", sou%hx%n
write(0,*) "xline intv = ", sou%hx%d
write(0,*) "xline orig = ", sou%hx%o
write(0,*) " # ilines  = ", sou%hy%n
write(0,*) "iline intv = ", sou%hy%d
write(0,*) "iline orig = ", sou%hy%o
write(0,*) "number of freq  = ", sou%w%n
write(0,*) "freq spacing    = ", sou%w%d/(2*pi)
write(0,*) "minimum freq    = ", sou%w%o/(2*pi)
write(0,*) "number of shots = ", sou%xd%n
write(0,*) "shot spacing    = ", sou%xd%d
write(0,*) "min.shot-x      = ", sou%xd%o
write(0,*) //
write(0,*) "VELOCITY MODEL"
write(0,*) "=============="
write(0,*) " # xlines   = ", v%xm%n
write(0,*) " xline intv = ", v%xm%d
write(0,*) " xline orig = ", v%xm%o
write(0,*) " # ilines   = ", v%ym%n
write(0,*) " iline intv = ", v%ym%d
write(0,*) " iline orig = ", v%ym%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,*) " # xlines   = ", img%xm%n
write(0,*) " xline intv = ", img%xm%d
write(0,*) " xline orig = ", img%xm%o
write(0,*) " # ilines   = ", img%ym%n
write(0,*) " iline intv = ", img%ym%d
write(0,*) " iline orig = ", img%ym%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,*) "no.of x-offsets    = ", img%shx%n
write(0,*) "x-offset spacing   = ", img%shx%d
write(0,*) "x-offset origin     = ", img%shx%o
write(0,*) "no.of y-offsets    = ", img%shy%n
write(0,*) "y-offset spacing   = ", img%shy%d
write(0,*) "y-offset origin     = ", img%shy%o

write(0,*) //
write(0,*) "WAVENUMBER"
write(0,*) "=========="
write(0,*) "no.wavenumbers x = ", kxm%n
write(0,*) "waveno.spacing x = ", kxm%d
write(0,*) "waveno.origin  x = ", kxm%o
write(0,*) "no.wavenumbers y = ", kym%n
write(0,*) "waveno.spacing y = ", kym%d
write(0,*) "waveno.origin  y = ", kym%o
write(0,*) //

write(0,*) "*****************************************************"
end if
end subroutine
!----------------------------------------------------------------
  subroutine owparm_bcast()
    integer :: mpierr

write(0,*) "BCAST = ",1
    call MPI_BCAST_(axk%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(ayk%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(azk%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(axk%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(ayk%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(azk%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(axk%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(ayk%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(azk%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

write(0,*) "BCAST = ",2
    call MPI_BCAST_(extrap,1,MPI_LOGICAL,0,MPI_COMM_WORLD,mpierr) 
    if (extrap(1:3).eq."ffd") then
       call MPI_BCAST_(extrap,6,MPI_CHARACTER ,0,MPI_COMM_WORLD,mpierr) 
       call MPI_BCAST_(dipop,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
       call MPI_BCAST_(trick,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    else if (extrap(1:4).eq."pspi") then
       call MPI_BCAST_(nref,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
       call MPI_BCAST_(lloyd,1,MPI_LOGICAL,0,MPI_COMM_WORLD,mpierr) 
       if (not(lloyd)) then
          call MPI_BCAST_(vsal,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
       end if
    end if

    if (dso) then
       call MPI_BCAST_(maxvchg,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
       call MPI_BCAST_(moddso,1,MPI_LOGICAL,0,MPI_COMM_WORLD,mpierr) 
       call MPI_BCAST_(expdso,1,MPI_LOGICAL,0,MPI_COMM_WORLD,mpierr) 
       call MPI_BCAST_(envelope,1,MPI_LOGICAL,0,MPI_COMM_WORLD,mpierr) 
       if (envelope) then
          call MPI_BCAST_(tc1,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
          call MPI_BCAST_(tc2,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
          call MPI_BCAST_(freq,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
       end if
       if (moddso) then
          call MPI_BCAST_(beta,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
       end if
    end if

write(0,*) "BCAST = ",3
    call MPI_BCAST_(timeshift,1,MPI_LOGICAL,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(imgcon,7,MPI_CHARACTER ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(imgnorm,1,MPI_LOGICAL,0,MPI_COMM_WORLD,mpierr) 

    call MPI_BCAST_(ymin,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(ymax,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(zmin,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(zmax,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(node,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(niter,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(nxfilt,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(tapertrx,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(padtracesx,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(tapertry,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(padtracesy,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(maxang,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

write(0,*) "BCAST = ",4
    call MPI_BCAST_(rec%hx%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(rec%hx%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(rec%hx%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(rec%hy%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(rec%hy%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(rec%hy%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(rec%w%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(rec%w%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(rec%w%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(rec%xd%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(rec%xd%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(rec%xd%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

    call MPI_BCAST_(sou%hx%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(sou%hx%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(sou%hx%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(sou%hy%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(sou%hy%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(sou%hy%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(sou%w%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(sou%w%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(sou%w%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(sou%xd%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(sou%xd%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(sou%xd%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(nreal,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 

write(0,*) "BCAST = ",5
    call MPI_BCAST_(v%xm%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(v%xm%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(v%xm%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(v%ym%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(v%ym%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(v%ym%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(v%z%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(v%z%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(v%z%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

    call MPI_BCAST_(img%xm%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(img%xm%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(img%xm%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(img%ym%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(img%ym%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(img%ym%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

    call MPI_BCAST_(xmin,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(xmax,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(ymin,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(ymax,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

write(0,*) "BCAST = ",6
    call MPI_BCAST_(xv_min,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(xv_max,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(yv_min,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(yv_max,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

    call MPI_BCAST_(nx,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(ny,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(nz,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(nzmax,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(nzmin,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(img%z%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

    call MPI_BCAST_(img%shx%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(jhx,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(img%shx%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(img%shx%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(img%shy%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(jhy,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(img%shy%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(img%shy%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

write(0,*) "BCAST = ",7
    call MPI_BCAST_(img%z%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(img%z%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(img%z%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

!    call MPI_BCAST_(wbott,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
!    call MPI_BCAST_(nwbott,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
!    call MPI_BCAST_(dwbott,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

    call MPI_BCAST_(pi,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

    call MPI_BCAST_(kxm%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(kxm%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(kxm%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(kxm%all,kxm%n,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

    call MPI_BCAST_(kym%n,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(kym%d,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(kym%o,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(kym%all,kym%n,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 

write(0,*) "BCAST = ",8
    call MPI_BCAST_(nsecdimx,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(dsecdimx,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(osecdimx,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
write(0,*) "BCAST = ",8.1
    call MPI_BCAST_(nsecdimy,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(dsecdimy,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(osecdimy,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
write(0,*) "BCAST = ",8.2
    call MPI_BCAST_(rec%w%all,rec%w%n,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(sou%w%all,sou%w%n,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
write(0,*) "BCAST = ",8.3
    call MPI_BCAST_(wblock%ns,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(zblock%ns,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
write(0,*) "BCAST = ",8.4
    call MPI_BCAST_(wblock%bres,1,MPI_LOGICAL,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(wblock%ds,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(wblock%nb,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(wblock%nb_res,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
write(0,*) "BCAST = ",8.41,wblock%blen
    call MPI_BCAST_(wblock%blen,wblock%nb,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
write(0,*) "BCAST = ",8.42
    call MPI_BCAST_(wblock%os,wblock%nb,MPI_REAL    ,0,MPI_COMM_WORLD,mpierr) 
write(0,*) "BCAST = ",8.43
    call MPI_BCAST_(wblock%indx,wblock%nb,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 

write(0,*) "BCAST = ",9
    call MPI_BCAST_(zblock%bres,1,MPI_LOGICAL,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(zblock%ds,1,MPI_REAL   ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(zblock%nb,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(zblock%nb_res,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(zblock%blen,zblock%nb,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(zblock%os,zblock%nb,MPI_REAL    ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(zblock%indx,zblock%nb,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr) 

    call MPI_BCAST_(listagem, 80, MPI_CHARACTER,0,MPI_COMM_WORLD,mpierr)
    call MPI_BCAST_(line_search,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr)
    call MPI_BCAST_(max_iters,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr)

    call MPI_BCAST_(bounds,1,MPI_LOGICAL,0,MPI_COMM_WORLD,mpierr) 
 if (bounds) then
    call MPI_BCAST_(cbounds,1,MPI_LOGICAL,0,MPI_COMM_WORLD,mpierr) 
    if (cbounds) then
       call MPI_BCAST_(vmin,1,MPI_REAL    ,0,MPI_COMM_WORLD,mpierr) 
       call MPI_BCAST_(vmax,1,MPI_REAL    ,0,MPI_COMM_WORLD,mpierr) 
    else
       call MPI_BCAST_(p_up,1,MPI_REAL    ,0,MPI_COMM_WORLD,mpierr) 
       call MPI_BCAST_(p_lo,1,MPI_REAL    ,0,MPI_COMM_WORLD,mpierr) 
    end if
 end if 
!----------------------------------------------
!!!!!!!!!!!!!!!!!!
   ! Horizon with no velocity update
   if (exist_file("horv0")) then
    call MPI_BCAST_(hor1, 80, MPI_CHARACTER,0,MPI_COMM_WORLD,mpierr)
   end if

   ! ********************************************** 
   !  Initialization of code variables 
    call MPI_BCAST_(iters,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr)
    call MPI_BCAST_(err,1,MPI_REAL    ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(epsteep,1,MPI_REAL    ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(tol,2,MPI_REAL    ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(factor,1,MPI_REAL    ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(lambda_min,1,MPI_REAL    ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(err1,1,MPI_REAL    ,0,MPI_COMM_WORLD,mpierr) 
    call MPI_BCAST_(tola,1,MPI_REAL    ,0,MPI_COMM_WORLD,mpierr) 

    call MPI_BCAST_(ninx,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr)
    call MPI_BCAST_(nfnx,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr)
    call MPI_BCAST_(niny,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr)
    call MPI_BCAST_(nfny,1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr)

write(0,*) "BCAST = ",10
 end subroutine
!----------------------------------------------------------------
  subroutine owparm_bcast1()
    integer :: mpierr,i_r,status

if (wpar%i_mpi==0) then
  do i_r=1,wpar%n_mpi-1
    call MPI_SEND_(axk%d,1,MPI_REAL   ,i_r,1001,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(ayk%d,1,MPI_REAL   ,i_r,1002,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(azk%d,1,MPI_REAL   ,i_r,1003,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(axk%n,1,MPI_INTEGER,i_r,1004,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(ayk%n,1,MPI_INTEGER,i_r,1005,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(azk%n,1,MPI_INTEGER,i_r,1006,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(axk%o,1,MPI_REAL   ,i_r,1007,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(ayk%o,1,MPI_REAL   ,i_r,1008,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(azk%o,1,MPI_REAL   ,i_r,1009,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(extrap,1,MPI_LOGICAL,i_r,1010,MPI_COMM_WORLD,mpierr) 
    if (extrap(1:3).eq."ffd") then
       call MPI_SEND_(extrap,6,MPI_CHARACTER ,i_r,1011,MPI_COMM_WORLD,mpierr) 
       call MPI_SEND_(dipop,1,MPI_REAL   ,i_r,1012,MPI_COMM_WORLD,mpierr) 
       call MPI_SEND_(trick,1,MPI_REAL   ,i_r,1013,MPI_COMM_WORLD,mpierr) 
    else if (extrap(1:4).eq."pspi") then
       call MPI_SEND_(nref,1,MPI_INTEGER,i_r,1014,MPI_COMM_WORLD,mpierr) 
       call MPI_SEND_(lloyd,1,MPI_LOGICAL,i_r,1015,MPI_COMM_WORLD,mpierr) 
       if (not(lloyd)) then
          call MPI_SEND_(vsal,1,MPI_REAL   ,i_r,1016,MPI_COMM_WORLD,mpierr) 
       end if
    end if

    if (dso) then
       call MPI_SEND_(maxvchg,1,MPI_REAL   ,i_r,1017,MPI_COMM_WORLD,mpierr) 
       call MPI_SEND_(moddso,1,MPI_LOGICAL,i_r,1018,MPI_COMM_WORLD,mpierr) 
       call MPI_SEND_(expdso,1,MPI_LOGICAL,i_r,1019,MPI_COMM_WORLD,mpierr) 
       call MPI_SEND_(envelope,1,MPI_LOGICAL,i_r,1020,MPI_COMM_WORLD,mpierr) 
       if (envelope) then
          call MPI_SEND_(tc1,1,MPI_INTEGER,i_r,1021,MPI_COMM_WORLD,mpierr) 
          call MPI_SEND_(tc2,1,MPI_INTEGER,i_r,1022,MPI_COMM_WORLD,mpierr) 
          call MPI_SEND_(freq,1,MPI_REAL   ,i_r,1023,MPI_COMM_WORLD,mpierr) 
       end if
       if (moddso) then
          call MPI_SEND_(beta,1,MPI_REAL   ,i_r,1024,MPI_COMM_WORLD,mpierr) 
       end if
    end if

    call MPI_SEND_(timeshift,1,MPI_LOGICAL,i_r,1025,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(imgcon,7,MPI_CHARACTER ,i_r,1026,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(imgnorm,1,MPI_LOGICAL,i_r,1027,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(ymin,1,MPI_REAL   ,i_r,1028,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(ymax,1,MPI_REAL   ,i_r,1029,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(zmin,1,MPI_REAL   ,i_r,1030,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(zmax,1,MPI_REAL   ,i_r,1031,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(node,1,MPI_INTEGER,i_r,1032,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(niter,1,MPI_INTEGER,i_r,1033,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(nxfilt,1,MPI_INTEGER,i_r,1034,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(tapertrx,1,MPI_INTEGER,i_r,1035,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(padtracesx,1,MPI_INTEGER,i_r,1036,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(tapertry,1,MPI_INTEGER,i_r,1037,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(padtracesy,1,MPI_INTEGER,i_r,1038,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(maxang,1,MPI_REAL   ,i_r,1039,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(rec%hx%n,1,MPI_INTEGER,i_r,1040,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(rec%hx%d,1,MPI_REAL   ,i_r,1041,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(rec%hx%o,1,MPI_REAL   ,i_r,1042,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(rec%hy%n,1,MPI_INTEGER,i_r,1043,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(rec%hy%d,1,MPI_REAL   ,i_r,1044,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(rec%hy%o,1,MPI_REAL   ,i_r,1045,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(rec%w%n,1,MPI_INTEGER,i_r,1046,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(rec%w%d,1,MPI_REAL   ,i_r,1047,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(rec%w%o,1,MPI_REAL   ,i_r,1048,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(rec%xd%n,1,MPI_INTEGER,i_r,1049,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(rec%xd%d,1,MPI_REAL   ,i_r,1050,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(rec%xd%o,1,MPI_REAL   ,i_r,1051,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(sou%hx%n,1,MPI_INTEGER,i_r,1052,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(sou%hx%d,1,MPI_REAL   ,i_r,1053,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(sou%hx%o,1,MPI_REAL   ,i_r,1054,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(sou%hy%n,1,MPI_INTEGER,i_r,1055,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(sou%hy%d,1,MPI_REAL   ,i_r,1056,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(sou%hy%o,1,MPI_REAL   ,i_r,1057,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(sou%w%n,1,MPI_INTEGER,i_r,1058,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(sou%w%d,1,MPI_REAL   ,i_r,1059,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(sou%w%o,1,MPI_REAL   ,i_r,1060,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(sou%xd%n,1,MPI_INTEGER,i_r,1061,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(sou%xd%d,1,MPI_REAL   ,i_r,1062,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(sou%xd%o,1,MPI_REAL   ,i_r,1063,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(nreal,1,MPI_INTEGER,i_r,1064,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(v%xm%n,1,MPI_INTEGER,i_r,1065,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(v%xm%d,1,MPI_REAL   ,i_r,1066,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(v%xm%o,1,MPI_REAL   ,i_r,1067,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(v%ym%n,1,MPI_INTEGER,i_r,1068,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(v%ym%d,1,MPI_REAL   ,i_r,1069,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(v%ym%o,1,MPI_REAL   ,i_r,1070,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(v%z%n,1,MPI_INTEGER,i_r,1071,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(v%z%d,1,MPI_REAL   ,i_r,1072,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(v%z%o,1,MPI_REAL   ,i_r,1073,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(img%xm%n,1,MPI_INTEGER,i_r,1074,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(img%xm%d,1,MPI_REAL   ,i_r,1075,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(img%xm%o,1,MPI_REAL   ,i_r,1076,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(img%ym%n,1,MPI_INTEGER,i_r,1077,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(img%ym%d,1,MPI_REAL   ,i_r,1078,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(img%ym%o,1,MPI_REAL   ,i_r,1079,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(xmin,1,MPI_REAL   ,i_r,1080,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(xmax,1,MPI_REAL   ,i_r,1081,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(ymin,1,MPI_REAL   ,i_r,1082,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(ymax,1,MPI_REAL   ,i_r,1083,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(xv_min,1,MPI_REAL   ,i_r,1084,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(xv_max,1,MPI_REAL   ,i_r,1085,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(yv_min,1,MPI_REAL   ,i_r,1086,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(yv_max,1,MPI_REAL   ,i_r,1087,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(nx,1,MPI_INTEGER,i_r,1088,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(ny,1,MPI_INTEGER,i_r,1089,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(nz,1,MPI_INTEGER,i_r,1090,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(nzmax,1,MPI_INTEGER,i_r,1091,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(nzmin,1,MPI_INTEGER,i_r,1092,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(img%z%o,1,MPI_REAL   ,i_r,1093,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(img%shx%n,1,MPI_INTEGER,i_r,1094,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(jhx,1,MPI_INTEGER,i_r,1095,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(img%shx%d,1,MPI_REAL   ,i_r,1096,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(img%shx%o,1,MPI_REAL   ,i_r,1097,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(img%shy%n,1,MPI_INTEGER,i_r,1098,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(jhy,1,MPI_INTEGER,i_r,1099,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(img%shy%d,1,MPI_REAL   ,i_r,1100,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(img%shy%o,1,MPI_REAL   ,i_r,1101,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(img%z%n,1,MPI_INTEGER,i_r,1102,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(img%z%o,1,MPI_REAL   ,i_r,1103,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(img%z%d,1,MPI_REAL   ,i_r,1104,MPI_COMM_WORLD,mpierr) 

!    call MPI_SEND_(wbott,1,MPI_REAL   ,i_r,1105,MPI_COMM_WORLD,mpierr) 
!    call MPI_SEND_(nwbott,1,MPI_INTEGER,i_r,1106,MPI_COMM_WORLD,mpierr) 
!    call MPI_SEND_(dwbott,1,MPI_REAL   ,i_r,1107,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(pi,1,MPI_REAL   ,i_r,1108,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(kxm%n,1,MPI_INTEGER,i_r,1109,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(kxm%d,1,MPI_REAL   ,i_r,1110,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(kxm%o,1,MPI_REAL   ,i_r,1111,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(kxm%all,kxm%n,MPI_REAL   ,i_r,1112,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(kym%n,1,MPI_INTEGER,i_r,1113,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(kym%d,1,MPI_REAL   ,i_r,1114,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(kym%o,1,MPI_REAL   ,i_r,1115,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(kym%all,kym%n,MPI_REAL   ,i_r,1116,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(nsecdimx,1,MPI_INTEGER,i_r,1117,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(dsecdimx,1,MPI_REAL   ,i_r,1118,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(osecdimx,1,MPI_REAL   ,i_r,1119,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(nsecdimy,1,MPI_INTEGER,i_r,1210,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(dsecdimy,1,MPI_REAL   ,i_r,1211,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(osecdimy,1,MPI_REAL   ,i_r,1120,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(rec%w%all,rec%w%n,MPI_REAL   ,i_r,1121,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(sou%w%all,sou%w%n,MPI_REAL   ,i_r,1122,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(wblock%ns,1,MPI_INTEGER,i_r,1123,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(zblock%ns,1,MPI_INTEGER,i_r,1124,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(wblock%bres,1,MPI_LOGICAL,i_r,1125,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(wblock%ds,1,MPI_REAL   ,i_r,1126,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(wblock%nb,1,MPI_INTEGER,i_r,1127,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(wblock%nb_res,1,MPI_INTEGER,i_r,1128,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(wblock%blen,wblock%nb,MPI_INTEGER,i_r,1129,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(wblock%os,wblock%nb,MPI_REAL    ,i_r,1130,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(wblock%indx,wblock%nb,MPI_INTEGER,i_r,1131,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(zblock%bres,1,MPI_LOGICAL,i_r,1132,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(zblock%ds,1,MPI_REAL   ,i_r,1133,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(zblock%nb,1,MPI_INTEGER,i_r,1134,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(zblock%nb_res,1,MPI_INTEGER,i_r,1135,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(zblock%blen,zblock%nb,MPI_INTEGER,i_r,1136,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(zblock%os,zblock%nb,MPI_REAL    ,i_r,1137,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(zblock%indx,zblock%nb,MPI_INTEGER,i_r,1138,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(listagem, 80, MPI_CHARACTER,i_r,1139,MPI_COMM_WORLD,mpierr)
    call MPI_SEND_(line_search,1,MPI_INTEGER,i_r,1140,MPI_COMM_WORLD,mpierr)
    call MPI_SEND_(max_iters,1,MPI_INTEGER,i_r,1141,MPI_COMM_WORLD,mpierr)

    call MPI_SEND_(bounds,1,MPI_LOGICAL,i_r,1142,MPI_COMM_WORLD,mpierr) 
 if (bounds) then
    call MPI_SEND_(cbounds,1,MPI_LOGICAL,i_r,1143,MPI_COMM_WORLD,mpierr) 
    if (cbounds) then
       call MPI_SEND_(vmin,1,MPI_REAL    ,i_r,1144,MPI_COMM_WORLD,mpierr) 
       call MPI_SEND_(vmax,1,MPI_REAL    ,i_r,1145,MPI_COMM_WORLD,mpierr) 
    else
       call MPI_SEND_(p_up,1,MPI_REAL    ,i_r,1146,MPI_COMM_WORLD,mpierr) 
       call MPI_SEND_(p_lo,1,MPI_REAL    ,i_r,1147,MPI_COMM_WORLD,mpierr) 
    end if
 end if 
!----------------------------------------------
!!!!!!!!!!!!!!!!!!
   ! Horizon with no velocity update
   call MPI_SEND_(hor1, 80, MPI_CHARACTER,i_r,1148,MPI_COMM_WORLD,mpierr)

   ! ********************************************** 
   !  Initialization of code variables 
    call MPI_SEND_(iters,1,MPI_INTEGER,i_r,1149,MPI_COMM_WORLD,mpierr)
    call MPI_SEND_(err,1,MPI_REAL    ,i_r,1150,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(epsteep,1,MPI_REAL    ,i_r,1151,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(tol,2,MPI_REAL    ,i_r,1152,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(factor,1,MPI_REAL    ,i_r,1153,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(lambda_min,1,MPI_REAL    ,i_r,1154,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(err1,1,MPI_REAL    ,i_r,1155,MPI_COMM_WORLD,mpierr) 
    call MPI_SEND_(tola,1,MPI_REAL    ,i_r,1156,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(ninx,1,MPI_INTEGER,i_r,1157,MPI_COMM_WORLD,mpierr)
    call MPI_SEND_(nfnx,1,MPI_INTEGER,i_r,1158,MPI_COMM_WORLD,mpierr)
    call MPI_SEND_(niny,1,MPI_INTEGER,i_r,1159,MPI_COMM_WORLD,mpierr)
    call MPI_SEND_(nfny,1,MPI_INTEGER,i_r,1160,MPI_COMM_WORLD,mpierr)

!    write(0,*) wpar%i_mpi,"inversion = ",inversion
    call MPI_SEND_(inversion,1,MPI_LOGICAL,i_r,1063,MPI_COMM_WORLD,mpierr)
    call MPI_SEND_(migration,1,MPI_LOGICAL,i_r,1062,MPI_COMM_WORLD,mpierr) 

    call MPI_SEND_(shtblk,1,MPI_INTEGER,i_r,2157,MPI_COMM_WORLD,mpierr)
    call MPI_SEND_(shtres,1,MPI_INTEGER,i_r,2158,MPI_COMM_WORLD,mpierr)
    call MPI_SEND_(shtblm,1,MPI_INTEGER,i_r,2159,MPI_COMM_WORLD,mpierr)

   end do
else
    call MPI_RECV_(axk%d,1,MPI_REAL   ,0,1001,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(ayk%d,1,MPI_REAL   ,0,1002,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(azk%d,1,MPI_REAL   ,0,1003,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(axk%n,1,MPI_INTEGER,0,1004,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(ayk%n,1,MPI_INTEGER,0,1005,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(azk%n,1,MPI_INTEGER,0,1006,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(axk%o,1,MPI_REAL   ,0,1007,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(ayk%o,1,MPI_REAL   ,0,1008,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(azk%o,1,MPI_REAL   ,0,1009,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(extrap,1,MPI_LOGICAL,0,1010,MPI_COMM_WORLD,status,mpierr) 
    if (extrap(1:3).eq."ffd") then
       call MPI_RECV_(extrap,6,MPI_CHARACTER ,0,1011,MPI_COMM_WORLD,status,mpierr) 
       call MPI_RECV_(dipop,1,MPI_REAL   ,0,1012,MPI_COMM_WORLD,status,mpierr) 
       call MPI_RECV_(trick,1,MPI_REAL   ,0,1013,MPI_COMM_WORLD,status,mpierr) 
    else if (extrap(1:4).eq."pspi") then
       call MPI_RECV_(nref,1,MPI_INTEGER,0,1014,MPI_COMM_WORLD,status,mpierr) 
       call MPI_RECV_(lloyd,1,MPI_LOGICAL,0,1015,MPI_COMM_WORLD,status,mpierr) 
       if (not(lloyd)) then
          call MPI_RECV_(vsal,1,MPI_REAL   ,0,1016,MPI_COMM_WORLD,status,mpierr) 
       end if
    end if

    if (dso) then
       call MPI_RECV_(maxvchg,1,MPI_REAL   ,0,1017,MPI_COMM_WORLD,status,mpierr) 
       call MPI_RECV_(moddso,1,MPI_LOGICAL,0,1018,MPI_COMM_WORLD,status,mpierr) 
       call MPI_RECV_(expdso,1,MPI_LOGICAL,0,1019,MPI_COMM_WORLD,status,mpierr) 
       call MPI_RECV_(envelope,1,MPI_LOGICAL,0,1020,MPI_COMM_WORLD,status,mpierr) 
       if (envelope) then
          call MPI_RECV_(tc1,1,MPI_INTEGER,0,1021,MPI_COMM_WORLD,status,mpierr) 
          call MPI_RECV_(tc2,1,MPI_INTEGER,0,1022,MPI_COMM_WORLD,status,mpierr) 
          call MPI_RECV_(freq,1,MPI_REAL   ,0,1023,MPI_COMM_WORLD,status,mpierr) 
       end if
       if (moddso) then
          call MPI_RECV_(beta,1,MPI_REAL   ,0,1024,MPI_COMM_WORLD,status,mpierr) 
       end if
    end if

    call MPI_RECV_(timeshift,1,MPI_LOGICAL,0,1025,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(imgcon,7,MPI_CHARACTER ,0,1026,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(imgnorm,1,MPI_LOGICAL,0,1027,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(ymin,1,MPI_REAL   ,0,1028,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(ymax,1,MPI_REAL   ,0,1029,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(zmin,1,MPI_REAL   ,0,1030,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(zmax,1,MPI_REAL   ,0,1031,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(node,1,MPI_INTEGER,0,1032,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(niter,1,MPI_INTEGER,0,1033,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(nxfilt,1,MPI_INTEGER,0,1034,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(tapertrx,1,MPI_INTEGER,0,1035,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(padtracesx,1,MPI_INTEGER,0,1036,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(tapertry,1,MPI_INTEGER,0,1037,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(padtracesy,1,MPI_INTEGER,0,1038,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(maxang,1,MPI_REAL   ,0,1039,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(rec%hx%n,1,MPI_INTEGER,0,1040,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(rec%hx%d,1,MPI_REAL   ,0,1041,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(rec%hx%o,1,MPI_REAL   ,0,1042,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(rec%hy%n,1,MPI_INTEGER,0,1043,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(rec%hy%d,1,MPI_REAL   ,0,1044,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(rec%hy%o,1,MPI_REAL   ,0,1045,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(rec%w%n,1,MPI_INTEGER,0,1046,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(rec%w%d,1,MPI_REAL   ,0,1047,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(rec%w%o,1,MPI_REAL   ,0,1048,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(rec%xd%n,1,MPI_INTEGER,0,1049,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(rec%xd%d,1,MPI_REAL   ,0,1050,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(rec%xd%o,1,MPI_REAL   ,0,1051,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(sou%hx%n,1,MPI_INTEGER,0,1052,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(sou%hx%d,1,MPI_REAL   ,0,1053,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(sou%hx%o,1,MPI_REAL   ,0,1054,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(sou%hy%n,1,MPI_INTEGER,0,1055,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(sou%hy%d,1,MPI_REAL   ,0,1056,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(sou%hy%o,1,MPI_REAL   ,0,1057,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(sou%w%n,1,MPI_INTEGER,0,1058,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(sou%w%d,1,MPI_REAL   ,0,1059,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(sou%w%o,1,MPI_REAL   ,0,1060,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(sou%xd%n,1,MPI_INTEGER,0,1061,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(sou%xd%d,1,MPI_REAL   ,0,1062,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(sou%xd%o,1,MPI_REAL   ,0,1063,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(nreal,1,MPI_INTEGER,0,1064,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(v%xm%n,1,MPI_INTEGER,0,1065,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(v%xm%d,1,MPI_REAL   ,0,1066,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(v%xm%o,1,MPI_REAL   ,0,1067,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(v%ym%n,1,MPI_INTEGER,0,1068,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(v%ym%d,1,MPI_REAL   ,0,1069,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(v%ym%o,1,MPI_REAL   ,0,1070,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(v%z%n,1,MPI_INTEGER,0,1071,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(v%z%d,1,MPI_REAL   ,0,1072,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(v%z%o,1,MPI_REAL   ,0,1073,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(img%xm%n,1,MPI_INTEGER,0,1074,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(img%xm%d,1,MPI_REAL   ,0,1075,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(img%xm%o,1,MPI_REAL   ,0,1076,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(img%ym%n,1,MPI_INTEGER,0,1077,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(img%ym%d,1,MPI_REAL   ,0,1078,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(img%ym%o,1,MPI_REAL   ,0,1079,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(xmin,1,MPI_REAL   ,0,1080,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(xmax,1,MPI_REAL   ,0,1081,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(ymin,1,MPI_REAL   ,0,1082,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(ymax,1,MPI_REAL   ,0,1083,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(xv_min,1,MPI_REAL   ,0,1084,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(xv_max,1,MPI_REAL   ,0,1085,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(yv_min,1,MPI_REAL   ,0,1086,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(yv_max,1,MPI_REAL   ,0,1087,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(nx,1,MPI_INTEGER,0,1088,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(ny,1,MPI_INTEGER,0,1089,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(nz,1,MPI_INTEGER,0,1090,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(nzmax,1,MPI_INTEGER,0,1091,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(nzmin,1,MPI_INTEGER,0,1092,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(img%z%o,1,MPI_REAL   ,0,1093,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(img%shx%n,1,MPI_INTEGER,0,1094,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(jhx,1,MPI_INTEGER,0,1095,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(img%shx%d,1,MPI_REAL   ,0,1096,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(img%shx%o,1,MPI_REAL   ,0,1097,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(img%shy%n,1,MPI_INTEGER,0,1098,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(jhy,1,MPI_INTEGER,0,1099,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(img%shy%d,1,MPI_REAL   ,0,1100,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(img%shy%o,1,MPI_REAL   ,0,1101,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(img%z%n,1,MPI_INTEGER,0,1102,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(img%z%o,1,MPI_REAL   ,0,1103,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(img%z%d,1,MPI_REAL   ,0,1104,MPI_COMM_WORLD,status,mpierr) 

!    call MPI_RECV_(wbott,1,MPI_REAL   ,0,1105,MPI_COMM_WORLD,status,mpierr) 
!    call MPI_RECV_(nwbott,1,MPI_INTEGER,0,1106,MPI_COMM_WORLD,status,mpierr) 
!    call MPI_RECV_(dwbott,1,MPI_REAL   ,0,1107,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(pi,1,MPI_REAL   ,0,1108,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(kxm%n,1,MPI_INTEGER,0,1109,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(kxm%d,1,MPI_REAL   ,0,1110,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(kxm%o,1,MPI_REAL   ,0,1111,MPI_COMM_WORLD,status,mpierr) 
    allocate(kxm%all(kxm%n))
    call MPI_RECV_(kxm%all,kxm%n,MPI_REAL   ,0,1112,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(kym%n,1,MPI_INTEGER,0,1113,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(kym%d,1,MPI_REAL   ,0,1114,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(kym%o,1,MPI_REAL   ,0,1115,MPI_COMM_WORLD,status,mpierr) 
    allocate(kym%all(kym%n))
    call MPI_RECV_(kym%all,kym%n,MPI_REAL   ,0,1116,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(nsecdimx,1,MPI_INTEGER,0,1117,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(dsecdimx,1,MPI_REAL   ,0,1118,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(osecdimx,1,MPI_REAL   ,0,1119,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(nsecdimy,1,MPI_INTEGER,0,1210,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(dsecdimy,1,MPI_REAL   ,0,1211,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(osecdimy,1,MPI_REAL   ,0,1120,MPI_COMM_WORLD,status,mpierr) 
    allocate(rec%w%all(rec%w%n))
    allocate(sou%w%all(sou%w%n))
    call MPI_RECV_(rec%w%all,rec%w%n,MPI_REAL   ,0,1121,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(sou%w%all,sou%w%n,MPI_REAL   ,0,1122,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(wblock%ns,1,MPI_INTEGER,0,1123,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(zblock%ns,1,MPI_INTEGER,0,1124,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(wblock%bres,1,MPI_LOGICAL,0,1125,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(wblock%ds,1,MPI_REAL   ,0,1126,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(wblock%nb,1,MPI_INTEGER,0,1127,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(wblock%nb_res,1,MPI_INTEGER,0,1128,MPI_COMM_WORLD,status,mpierr) 
    allocate(wblock%blen(wblock%nb))
    allocate(wblock%os(wblock%nb))
    allocate(wblock%indx(wblock%nb))
    call MPI_RECV_(wblock%blen,wblock%nb,MPI_INTEGER,0,1129,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(wblock%os,wblock%nb,MPI_REAL    ,0,1130,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(wblock%indx,wblock%nb,MPI_INTEGER,0,1131,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(zblock%bres,1,MPI_LOGICAL,0,1132,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(zblock%ds,1,MPI_REAL   ,0,1133,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(zblock%nb,1,MPI_INTEGER,0,1134,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(zblock%nb_res,1,MPI_INTEGER,0,1135,MPI_COMM_WORLD,status,mpierr) 
    allocate(zblock%blen(zblock%nb))
    allocate(zblock%os(zblock%nb))
    allocate(zblock%indx(zblock%nb))
    call MPI_RECV_(zblock%blen,zblock%nb,MPI_INTEGER,0,1136,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(zblock%os,zblock%nb,MPI_REAL    ,0,1137,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(zblock%indx,zblock%nb,MPI_INTEGER,0,1138,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(listagem, 80, MPI_CHARACTER,0,1139,MPI_COMM_WORLD,status,mpierr)
    call MPI_RECV_(line_search,1,MPI_INTEGER,0,1140,MPI_COMM_WORLD,status,mpierr)
    call MPI_RECV_(max_iters,1,MPI_INTEGER,0,1141,MPI_COMM_WORLD,status,mpierr)

    call MPI_RECV_(bounds,1,MPI_LOGICAL,0,1142,MPI_COMM_WORLD,status,mpierr) 
 if (bounds) then
    call MPI_RECV_(cbounds,1,MPI_LOGICAL,0,1143,MPI_COMM_WORLD,status,mpierr) 
    if (cbounds) then
       call MPI_RECV_(vmin,1,MPI_REAL    ,0,1144,MPI_COMM_WORLD,status,mpierr) 
       call MPI_RECV_(vmax,1,MPI_REAL    ,0,1145,MPI_COMM_WORLD,status,mpierr) 
    else
       call MPI_RECV_(p_up,1,MPI_REAL    ,0,1146,MPI_COMM_WORLD,status,mpierr) 
       call MPI_RECV_(p_lo,1,MPI_REAL    ,0,1147,MPI_COMM_WORLD,status,mpierr) 
    end if
 end if 
!----------------------------------------------
!!!!!!!!!!!!!!!!!!
   ! Horizon with no velocity update
   call MPI_RECV_(hor1, 80, MPI_CHARACTER,0,1148,MPI_COMM_WORLD,status,mpierr)
   ! ********************************************** 
   !  Initialization of code variables 
    call MPI_RECV_(iters,1,MPI_INTEGER,0,1149,MPI_COMM_WORLD,status,mpierr)
    call MPI_RECV_(err,1,MPI_REAL    ,0,1150,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(epsteep,1,MPI_REAL    ,0,1151,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(tol,2,MPI_REAL    ,0,1152,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(factor,1,MPI_REAL    ,0,1153,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(lambda_min,1,MPI_REAL    ,0,1154,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(err1,1,MPI_REAL    ,0,1155,MPI_COMM_WORLD,status,mpierr) 
    call MPI_RECV_(tola,1,MPI_REAL    ,0,1156,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(ninx,1,MPI_INTEGER,0,1157,MPI_COMM_WORLD,status,mpierr)
    call MPI_RECV_(nfnx,1,MPI_INTEGER,0,1158,MPI_COMM_WORLD,status,mpierr)
    call MPI_RECV_(niny,1,MPI_INTEGER,0,1159,MPI_COMM_WORLD,status,mpierr)
    call MPI_RECV_(nfny,1,MPI_INTEGER,0,1160,MPI_COMM_WORLD,status,mpierr)

    call MPI_RECV_(inversion,1,MPI_LOGICAL,0,1063,MPI_COMM_WORLD,status,mpierr)
    call MPI_RECV_(migration,1,MPI_LOGICAL,0,1062,MPI_COMM_WORLD,status,mpierr) 

    call MPI_RECV_(shtblk,1,MPI_INTEGER,0,2157,MPI_COMM_WORLD,status,mpierr)
    call MPI_RECV_(shtres,1,MPI_INTEGER,0,2158,MPI_COMM_WORLD,status,mpierr)
    call MPI_RECV_(shtblm,1,MPI_INTEGER,0,2159,MPI_COMM_WORLD,status,mpierr)

if (migration) then
   call auxscr("bkgimg")
   call to_history("n1",img%xm%n,"bkgimg"); call to_history("d1",img%xm%d,"bkgimg"); call to_history("o1",img%xm%o,"bkgimg")  
   call to_history("n2",img%shx%n,"bkgimg") ; call to_history("d2",img%shx%d,"bkgimg") ; call to_history("o2",img%shx%o,"bkgimg")
   call to_history("n3",img%ym%n,"bkgimg"); call to_history("d3",img%ym%d,"bkgimg"); call to_history("o3",img%ym%o,"bkgimg")  
   call to_history("n4",img%shy%n,"bkgimg") ; call to_history("d4",img%shy%d,"bkgimg") ; call to_history("o4",img%shy%o,"bkgimg")
   call to_history("n5",nz,"bkgimg") ; call to_history("d5",img%z%d,"bkgimg") ; call to_history("o5",zmin,"bkgimg")
   call to_history("esize",4,"bkgimg")
end if

if (inversion) then
      call auxscr("grad")
      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",img%ym%n,"grad"); call to_history("d2",img%ym%d,"grad"); call to_history("o2",img%ym%o,"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")

      call auxscr("Velout")
      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",img%ym%n,"Velout"); call to_history("d2",img%ym%d,"Velout"); call to_history("o2",img%ym%o,"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")

      call auxscr("Dimage")
      call to_history("n1",img%xm%n,"Dimage"); call to_history("d1",img%xm%d,"Dimage"); call to_history("o1",img%xm%o,"Dimage")  
      call to_history("n2",img%shx%n,"Dimage") ; call to_history("d2",img%shx%d,"Dimage") ; call to_history("o2",img%shx%o,"Dimage")
      call to_history("n3",img%ym%n,"Dimage"); call to_history("d3",img%ym%d,"Dimage"); call to_history("o3",img%ym%o,"Dimage")  
      call to_history("n4",img%shy%n,"Dimage") ; call to_history("d4",img%shy%d,"Dimage") ; call to_history("o4",img%shy%o,"Dimage")
      call to_history("n5",nz,"Dimage") ; call to_history("d5",img%z%d,"Dimage") ; call to_history("o5",zmin,"Dimage")
      call to_history("esize",4,"Dimage")

      call auxscr("bkgimg")
      call to_history("n1",img%xm%n,"bkgimg"); call to_history("d1",img%xm%d,"bkgimg"); call to_history("o1",img%xm%o,"bkgimg")  
      call to_history("n2",img%shx%n,"bkgimg") ; call to_history("d2",img%shx%d,"bkgimg") ; call to_history("o2",img%shx%o,"bkgimg")
      call to_history("n3",img%ym%n,"bkgimg"); call to_history("d3",img%ym%d,"bkgimg"); call to_history("o3",img%ym%o,"bkgimg")  
      call to_history("n4",img%shy%n,"bkgimg") ; call to_history("d4",img%shy%d,"bkgimg") ; call to_history("o4",img%shy%o,"bkgimg")
      call to_history("n5",nz,"bkgimg") ; call to_history("d5",img%z%d,"bkgimg") ; call to_history("o5",zmin,"bkgimg")
      call to_history("esize",4,"bkgimg")

   call auxscr("bkws")
   call to_history("n1",kxm%n,"bkws"); call to_history("d1",img%xm%d,"bkws"); call to_history("o1",img%xm%o,"bkws")  
   call to_history("n2",kym%n,"bkws"); call to_history("d2",img%ym%d,"bkws"); call to_history("o2",img%ym%o,"bkws")
   call to_history("n3",img%z%n,"bkws");  call to_history("d3",img%z%d,"bkws");  call to_history("o3",img%z%o,"bkws")  
   call to_history("n4",sou%w%n,"bkws");  call to_history("d4",sou%w%d,"bkws");  call to_history("o4",sou%w%o,"bkws")
   call to_history("n5",1,"bkws");        call to_history("d5",1.,"bkws");       call to_history("o5",0.,"bkws")
   call to_history("esize",8,"bkws")

   call auxscr("bkwr")
   call to_history("n1",kxm%n,"bkwr"); call to_history("d1",img%xm%d,"bkwr"); call to_history("o1",img%xm%o,"bkwr")  
   call to_history("n2",kym%n,"bkwr"); call to_history("d2",img%ym%d,"bkwr"); call to_history("o2",img%ym%o,"bkwr")
   call to_history("n3",img%z%n,"bkwr");  call to_history("d3",img%z%d,"bkwr");  call to_history("o3",img%z%o,"bkwr")  
   call to_history("n4",sou%w%n,"bkwr");  call to_history("d4",sou%w%d,"bkwr");  call to_history("o4",sou%w%o,"bkwr")
   call to_history("n5",1,"bkwr");        call to_history("d5",1.,"bkwr");       call to_history("o5",0.,"bkwr")
   call to_history("esize",8,"bkwr")


end if





end if
 end subroutine
!----------------------------------------------------------------
end module
