# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "<stdin>"
!
!
!
! Thu Jan 9 22:45:45 PST 2003
!
!
module ray_database_mod
  use velocity_mod
  use handle_rays_3d_mod
  use handle_rays_2d_mod
  use gsmooth_mod
  use sep
  implicit none
  contains
  logical function create_ray_database(tag,oang,dang,nang,match,loc,dips&
    &,extra)
    type(sep3d) :: vel_s
    real,pointer :: vel3d(:,:,:),vel2d(:,:)
    character(len=*) :: tag
    real :: oang,dang
    integer :: nang
    logical :: match
    real :: loc(:,:),dips(:,:),extra(:)
    integer :: n(3),nwind1,nwind2,nwind3
    real :: o(3),d(3)
    create_ray_database=.false.
    if (.not. vel_structure(vel_s)) then
      write(0,*) "trouble grabbing velocity structure"
      return
    end if
    call set_n_o_d_3d(vel_s,n,o,d)
    allocate(vel3d(n(1),n(2),n(3)))
    call from_param("ray_vsm_nwind1",nwind1,5)
    call from_param("ray_vsm_nwind2",nwind2,5)
    call from_param("ray_vsm_nwind3",nwind3,min(5,n(3)))
    if (n(3).eq.1) then
      allocate(vel2d(n(1),n(2)))
      if (.not. vel_2d(vel2d)) then
        write(0,*) "trouble grabbing 2-D velocity function"
        return
      end if
      vel3d(:,:,1)=vel2d
      call gauss_smooth(vel3d,nwind1,nwind2,nwind3,1.)
      vel2d=vel3d(:,:,1)
      if (.not. init_2d_ray_tracer(vel_s,vel2d,nang,oang,dang)) then
        write(0,*) "      ERROR: initializing 2-D ray tracer"
        return
      end if
      if (.not. shoot_rays_2d(tag,loc,dips,extra,match)) then
        write(0,*) "      ERROR: shooting matched rays"
        return
      end if
      deallocate(vel3d)
      deallocate(vel2d)
    else
      if (.not. vel_3d(vel3d)) then
        write(0,*) "trouble grabbing 3-D velocity function"
        return
      end if
      call gauss_smooth(vel3d,nwind1,nwind2,nwind3,1.)
      if (.not. init_3d_ray_tracer(vel_s,vel3d,nang,oang,dang)) then
        write(0,*) "      ERROR: initializing 3-D ray tracer"
        return
      end if
      if (.not. shoot_rays_3d(tag,loc,dips,extra,match)) then
        write(0,*) "      ERROR: shooting matched rays"
        return
      end if
      deallocate(vel3d)
    end if
    create_ray_database=.true.
  end function
end module
