!
!
! Thu Jan  9 21:34:42 PST 2003
!
!
module tomo_bulk_mod 
  use sep3d_struct_mod
  use param_check_mod
  use back_project_mod
  use ray_database_mod
  use tomo_full_mod
  use error_mod
  implicit none
  logical, private, save :: verb,tau
  character(len=128), private, save :: ray0_tag,ang_tag
  real, private, save :: oang,dang
  integer, private, save ::  nang
  character(len=256),private,save   :: data_err_method
  contains
  logical function calc_bulk_tomo()
    type(sep3d)    :: err_s
    integer        :: npts
    real, pointer  :: x(:),y(:),z(:),dip1(:),dip2(:),loc(:,:),dips(:,:&
      &),descriptor(:)
    character(len=128) :: iloc,fract,scale,nlen,data
    logical :: tau
    calc_bulk_tomo=.false.
    call init_sep3d("err",err_s,"INPUT")
    call sep3d_grab_headers("err",err_s,npts)
    allocate(x(npts),y(npts),z(npts),dip1(npts),dip2(npts))
    allocate(loc(3,npts),dips(2,npts),descriptor(npts))
    call sep3d_grab_key_vals(err_s,"s_x",x)
    call sep3d_grab_key_vals(err_s,"s_y",y)
    call sep3d_grab_key_vals(err_s,"s_z",z)
    call sep3d_grab_key_vals(err_s,"dip_xz",dip1)
    call sep3d_grab_key_vals(err_s,"dip_yz",dip2)
    call sep3d_grab_key_vals(err_s,"descriptor",descriptor)
    call sep3df_delete(err_s,"err")
    loc(1,:)=x
    loc(2,:)=y
    loc(3,:)=z
    dips(1,:)=dip1
    dips(2,:)=dip2
    descriptor(:)=descriptor
    if (.not. create_ray_database(ang_tag,oang,dang,nang,.true.,loc,dips&
      &,descriptor)) then
      write(0,*) "trouble creating ray database for tag ",trim(ang_tag&
        &)
      return
    end if
    if (.not. create_ray_database(ray0_tag,0.,1.,1,.false.,loc,dips&
      &,descriptor)) then
      write(0,*) "trouble creating ray database for tag ",trim(ray0_tag&
        &)
      return
    end if
    if (.not. init_err_map(ang_tag)) then
      write(0,*) "trouble creatting error map"
      return
    end if
    if (.not. new_traceback(1,iloc,fract,scale,nlen,data)) then
      write(0,*) "trouble create new back project"
      return
    end if
    write(0,*) "BEFORE DATE ERRT",trim(data_err_method)
    if (.not. back_project_bulk(ang_tag,ray0_tag,data_err_method))&
      & then
      write(0,*) "trouble back projecting ray"
      return
    end if
    call to_history("iloc",trim(iloc),"tomo_oper")
    call to_history("fract",trim(fract),"tomo_oper")
    call to_history("scale",trim(scale),"tomo_oper")
    call to_history("data",trim(data),"tomo_oper")
    call to_history("nlen",trim(nlen),"tomo_oper")
    call to_history("traceback_type","bulk","tomo_oper")
    if (.not. finish_cur_trace_back()) then
      write(0,*) "trouble finishing back project"
      return
    end if
    deallocate(x,y,z,dip1,dip2,loc,dips)
    calc_bulk_tomo=.true.
  end function 
  logical function load_bulk_tomo_oper(dat)
    real, pointer :: dat(:)
    load_bulk_tomo_oper=.false.
    if (.not. load_calc_traceback(1,"bulk",dat)) then
      write(0,*) "trouble loading traceback 0"
      return
    end if
    load_bulk_tomo_oper=.true.
  end function 
  integer function tomo_bulk_op(adj,add,model,data)
    logical, intent (in) :: adj,add
    real :: model(:),data(:)
    call set_current(1)
    call adjnull(adj,add,model,data)
    tomo_bulk_op=0
    call tb_standard_op(adj,model,data)
    tomo_bulk_op=0
  end function 
  logical function back_project_bulk(ang_tag,zero_tag,err_method)
    character(len=*) :: err_method,ang_tag,zero_tag
    back_project_bulk=back_project_full(ang_tag,zero_tag,err_method&
      &,trace_back_bulk,.true.)
  end function 
  logical function check_par_tomo_bulk(struct,tag,ok)
    type(sep3d)        :: struct
    character(len=*)   :: tag
    logical        :: stat_ok(20),ok,stat_rerun(20)
    check_par_tomo_bulk=.false.
    stat_ok=.true.
    stat_rerun=.true.
    call from_param('tau',tau,.false.)
    if (tau) then
      write(0,*) "normal ray tomography won't work in tau domain"
      return
    end if
    if (model_space(1:2).ne."v2") then
      write(0,*) "bulk tomo mode requires model_space=ds"
      return
    end if
    if (sep3d_ndims(struct) .eq.2) then
      stat_ok(1)=read_check_ray_params_2d(tag,stat_rerun(1))
    else
      stat_ok(1)=read_check_ray_params_3d(tag,stat_rerun(1))
    end if 
    stat_rerun(2)=check_par_file("ray0_tag",ray0_tag,tag,ray0_tag)
    stat_rerun(3)=check_par_file("ang_tag",ang_tag,tag,ang_tag)
    stat_rerun(4)=check_par_file("oang",oang,tag,2.)
    stat_rerun(5)=check_par_file("dang",dang,tag,2.)
    stat_rerun(6)=check_par_file("nang",nang,tag,20)
    stat_rerun(7)=check_par_file("data_err_method",data_err_method,tag&
      &,"bulk_pts")
    stat_rerun(8)=data_errors_build_ok
    stat_ok(9)=check_pars_back_project(tag,stat_rerun(10))
    ray0_tag="ray0_tag"
    ang_tag="ang_tag"
    if (all(stat_ok)) then
      check_par_tomo_bulk=.true.
    end if
    if (all(stat_rerun)) then
      ok=.true.
    else
      ok=.false.
    end if
  end function 
end module 
