program z2t_
  use sep
  use ztconv_

  implicit none

  integer :: esize
  integer :: n1_out, nv(2), nf(3), n_t(3)
  real    :: o1_out, ov(2), of(3), o_t(3)
  real    :: d1_out, dv(2), df(3), d_t(3)

  integer :: i3
  integer :: tempi

  logical :: inverse
 
  real,   dimension(:,:), allocatable :: vel_z, sig_z, img_z
  real,   dimension(:,:), allocatable :: vel_t, sig_t, img_t

  real,   dimension(:,:), allocatable :: map
  logical :: field
  character(len=128) :: temp_ch

  integer :: nz,nt,nx
  real    :: oz,ot,ox
  real    :: dz,dt,dx

  integer  :: auxin

  call initpar()
  call from_param("inverse",inverse,.false.)
  call from_history("esize",esize)

  call from_history("n1",nv(1))
  call from_history("o1",ov(1))
  call from_history("d1",dv(1))

  call from_history("n2",nv(2))
  call from_history("o2",ov(2))
  call from_history("d2",dv(2))

  call from_param("n1_out",n1_out)
  call from_param("o1_out",o1_out)
  call from_param("d1_out",d1_out)

  call to_history("n1",n1_out)
  call to_history("o1",o1_out)
  call to_history("d1",d1_out)
  
  if (-1.ne.auxin("field")) then
     field=.true.
     call copy_history("field","field_out")

     call to_history("d1",d1_out,'field_out')
     call to_history("n1",n1_out,'field_out')

     do i3=1,3 
        call sep_get_data_axis_par("field",i3,nf(i3),of(i3),df(i3),temp_ch)
     end do
     
     if(nv(1) /= nf(1)) call seperr("field_n1 /= vel_n1")
     if(ov(2)                > of(2)                ) call seperr("velocity too small for tt")
     if(ov(2)+dv(2)*(nv(2)-1)< of(2)+(nf(2)-1)*df(2)) call seperr("velocity too big for field 2")
  end if

  if(.not. inverse) then
     call init_ztconv(nv,ov,dv,&
     (/n1_out,nv(2)/),(/o1_out,ov(2)/),(/d1_out,dv(2)/))
  else
     call init_ztconv((/n1_out,nv(2)/),(/o1_out,ov(2)/),(/d1_out,dv(2)/),&
     nv,ov,dv)
  end if

  allocate(map(nv(1),nv(2)))
  
  if(.not. inverse) then
     !! TRANSFORMATION FROM DEPTH TO TIME
     allocate(vel_z( nv(1),nv(2)))
     allocate(vel_t(n1_out,nv(2)))

     call sreed("in",vel_z,size(vel_z)*4)  ! read velocity in z
     call createmap_z(vel_z,map)           ! z to t map
     call convert_z2t(vel_z,vel_t,map)     ! convert from z to t
     call srite("out",vel_t,size(vel_t)*4) ! write velocity in t

     if (field) then
        allocate(img_z(nf(1),nf(2)),img_t(n1_out, nf(2)))
        tempi=(of(2)- ov(2))/dv(2)+1
        do i3=1,nf(3)
           call sreed("field",    img_z,size(img_z)*4)
           call convert_z2t(img_z,img_t,map(:,tempi:tempi+nf(2)-1))
           call srite("field_out",img_t,size(img_t)*4)
        end do
        deallocate(img_z)
     end if

  else
     !! TRANSFORMATION FROM TIME TO DEPTH
     allocate(vel_z(n1_out,nv(2)))
     allocate(vel_t( nv(1),nv(2)))

     call sreed("in",vel_t,size(vel_t)*4)  ! read velocity in t
     call createmap_t(vel_t,map)           ! t to z map
     call convert_t2z(vel_t,vel_z,map)     ! convert from t to z
     call srite("out",vel_z,size(vel_z)*4) ! write velocity in z        

     if (field) then
        allocate(img_t(nf(1),nf(2)),img_z(n1_out, nf(2)))
        tempi=(of(2)- ov(2))/dv(2)+1
        do i3=1,nf(3)
           call sreed("field",    img_t,size(img_t)*4)
           call convert_t2z(img_t,img_z,map(:,tempi:tempi+nf(2)-1))
           call srite("field_out",img_z,size(img_z)*4)
        end do
        deallocate(img_t)
     end if

  end if

end program z2t_





