module ray_io_mod

use process_types
use ray_types

implicit none

integer, private :: n_bytes_io,ierr
integer, external :: sreed,srite,auxin

contains

subroutine read_ray (  param , ray ) 

type (tomo_param_type) param
type (ray_obj_type) ray
integer tempi

!write(0,*)'READ  ray'

n_bytes_io=ray%ndim_ray*ray%nt_ray*ray%n_ray*ray%nref_ray*4

if(auxin('R_traj') .lt. 0) call seperr('R_traj tag not found')
if(sreed('R_traj',ray%traj_ray,n_bytes_io) .ne. n_bytes_io) then
  call seperr("Problems in reading tag R_traj ")
end if
if(auxin('R_rpar') .lt. 0) call seperr('R_rpar tag not found')
tempi=sreed('R_rpar',ray%rpar_ray,ray%n_ray*ray%nref_ray*16)
if(tempi .ne. ray%n_ray*16*ray%nref_ray) then
		write(0,*) tempi,ray%n_ray*ray%nref_ray
  call seperr("Problems in reading tag a_rpar ")
end if
write(0,*) "ray par----- ",tempi,size(ray%rpar_ray,1),size(ray%rpar_ray,2),size(ray%rpar_ray,3)
if(auxin('R_mod') .lt. 0) call seperr('R_mod tag not found')
if(sreed('R_mod',ray%mod_ray,n_bytes_io) .ne. n_bytes_io) then
  call seperr("Problems in reading tag R_mod ")
end if

if(sreed("zero_off",ray%rays_up,size(ray%rays_up)*4) .ne. ray%n3_up*ray%n1_up*ray%n2_up*8) then
 call seperr("Trouble reading zero offset rays")
end if

if(auxin('zero_off_mod') .lt. 0) call seperr('zero_off_mod tag not found')
if(sreed("zero_off_mod",ray%mod_up,size(ray%mod_up)*4) .ne. ray%n1_up*ray%n2_up*ray%n3_up*8) then
 call seperr("Trouble reading zero offset rays")
end if

n_bytes_io=ray%n_ray*ray%nref_ray*4
if(auxin('R_time_tot') .lt. 0) call seperr('R_time_tot tag not found')
if(sreed('R_time_tot',ray%ttot_ray,n_bytes_io) .ne. n_bytes_io) then
  call seperr("Problems in reading tag R_time_tot ")
end if
if(auxin('R_time_ref') .lt. 0) call seperr('R_time_ref tag not found')
if(sreed('R_time_ref',ray%tref_ray,n_bytes_io) .ne. n_bytes_io) then
  call seperr("Problems in reading tag R_time_ref ")
end if

return
end subroutine read_ray

subroutine read_time_ray ( ray, time_ray ) 
type (ray_obj_type) ray
real, dimension (:,:) :: time_ray

n_bytes_io=ray%n_ray*ray%nref_ray*4

write(0,*) "error",n_bytes_io
if(auxin('R_time') .lt. 0) call seperr('R_time tag not found')
if(sreed('R_time',time_ray,n_bytes_io) .ne. n_bytes_io) then
  call seperr("Problems in reading tag R_time ")
end if

return
end subroutine read_time_ray

subroutine read_time_0_ray ( ray, time_ray ) 
type (ray_obj_type) ray
real, dimension (:,:) :: time_ray

n_bytes_io=ray%n_ray*ray%nref_ray*4


if(auxin('R_time_0') .lt. 0) call seperr('R_time_0 tag not found')
if(sreed('R_time_0',time_ray,n_bytes_io) .ne. n_bytes_io) then
  call seperr("Problems in reading tag R_time_0 ")
end if

return
end subroutine read_time_0_ray

subroutine write_time_ray (ray, time_ray ) 
type (ray_obj_type) ray
real, dimension (:,:) :: time_ray

!write(0,*)'write  time_ray'
n_bytes_io=ray%n_ray*ray%nref_ray*4

if(srite('R_time_mod',time_ray,n_bytes_io) .ne. n_bytes_io) then
  call seperr("Problems in reading tag R_time_mod ")
end if

return
end subroutine write_time_ray


subroutine alloc_ray (param, ray) 

type (tomo_param_type) param
type (ray_obj_type) ray

write(0,*) "check",ray%nt_ray,ray%n_ray,ray%nref_ray

allocate (ray%traj_ray(ray%ndim_ray,ray%nt_ray,ray%n_ray, &
  ray%nref_ray),stat=ierr)  
if(ierr .ne. 0) call seperr("Problems with allocation of traj_ray")
allocate (ray%rpar_ray(ray%ndim_ray,2,ray%n_ray,&
  ray%nref_ray),stat=ierr)  
if(ierr .ne. 0) call seperr("Problems with allocation of rpar_ray")

allocate (ray%mod_ray(ray%ndim_ray,ray%nt_ray,ray%n_ray, &
  ray%nref_ray),stat=ierr)  
if(ierr .ne. 0) call seperr("Problems with allocation of mod_ray")

allocate (ray%ttot_ray(ray%n_ray,ray%nref_ray),stat=ierr)  
if(ierr .ne. 0) call seperr("Problems with allocation of ttot_ray")
allocate (ray%tref_ray(ray%n_ray,ray%nref_ray),stat=ierr)  
if(ierr .ne. 0) call seperr("Problems with allocation of tref_ray")
write(0,*) "i see size",ray%n1_up,ray%n2_up,ray%n3_up
allocate (ray%rays_up(ray%ndim_ray,ray%n1_up,ray%n2_up,ray%n3_up),stat=ierr)  
if(ierr .ne. 0) call seperr("Problems allocate in zero offset rays")
allocate (ray%mod_up(ray%ndim_ray,ray%n1_up,ray%n2_up,ray%n3_up),stat=ierr)  
if(ierr .ne. 0) call seperr("Problems allocate in zero offset  mod rays")

return
end subroutine alloc_ray

subroutine dealloc_ray (param,ray) 
type (tomo_param_type) param
type (ray_obj_type) ray

deallocate (ray%traj_ray,stat=ierr)  
if(ierr .ne. 0) call seperr("Problems with deallocation traj_ray")
deallocate (ray%rpar_ray,stat=ierr)  
if(ierr .ne. 0) call seperr("Problems with deallocation rpar_ray")
deallocate (ray%mod_ray,stat=ierr)  
if(ierr .ne. 0) call seperr("Problems with deallocation mod_ray")
deallocate (ray%ttot_ray,stat=ierr)  
if(ierr .ne. 0) call seperr("Problems with deallocation ttot_ray")
deallocate (ray%tref_ray,stat=ierr)  
if(ierr .ne. 0) call seperr("Problems with deallocation tref_ray")

return
end subroutine dealloc_ray

end module ray_io_mod
