module killwave_io
use sep
use killwave_utils
use killwave_params


implicit none

!integer, private,save:: nang, nfilt, n1

contains 
!==================================
subroutine killwave_io_init(nang_in,nfilt_in) 
	integer :: nang_in, nfilt_in, i 
	nang=nang_in
	nfilt=nfilt_in

	A=0.
	ATA=0.
	FILT=0.
        do i=1,nang
                call sep_write(   A,    A_, esize=4); 
                call sep_write( ATA,  ATA_, esize=4); 
                call sep_write(FILT, FILT_, esize=4); 
        enddo
end subroutine
!#####################################################
subroutine A_io(flag, A, iang)
    real, dimension(:,:),   allocatable :: A
    character(len=4)                    :: flag
    integer                             :: iang,dim1, dim2
    integer, dimension(3)               :: n_cube, j_wind, n_wind, f_wind
    integer                             :: n_dim_cube, err

    n_dim_cube = 3
    n_cube = (/2   , 2   , nang	 /)
    j_wind = (/1   , 1   , 1     /)
    n_wind = (/2   , 2   , 1     /)
    f_wind = (/0   , 0   , iang-1/)
    if (flag == "rite") then
       err = srite_window(A_,n_dim_cube,n_cube,n_wind,f_wind,j_wind,4,A)
    else if (flag == "reed") then
       A = 0.
       err = sreed_window(A_,n_dim_cube,n_cube,n_wind,f_wind,j_wind,4,A)
    else
       call seperr("you have to specify the flag for data_io_data()")
    end if
    if (err /= 0) call seperr("data_io_data: error reading data")
end subroutine
!#####################################################
subroutine ATA_io(flag, A, iang)
    real, dimension(:,:),   allocatable :: A
    character(len=4)                    :: flag
    integer                             :: iang,dim1, dim2
    integer, dimension(3)               :: n_cube, j_wind, n_wind, f_wind
    integer                             :: n_dim_cube, err

    n_dim_cube = 3
    n_cube = (/3   , 3   , nang	 /)
    j_wind = (/1   , 1   , 1     /)
    n_wind = (/2   , 2   , 1     /)
    f_wind = (/0   , 0   , iang-1/)
    if (flag == "rite") then
       err = srite_window(ATA_,n_dim_cube,n_cube,n_wind,f_wind,j_wind,4,A)
    else if (flag == "reed") then
       A = 0.
       err = sreed_window(ATA_,n_dim_cube,n_cube,n_wind,f_wind,j_wind,4,A)
    else
       call seperr("you have to specify the flag for data_io_data()")
    end if
    if (err /= 0) call seperr("data_io_data: error reading data")
end subroutine!#####################################################
subroutine filt_io(flag, F, iang)
    real, dimension(:,:),   allocatable :: F
    character(len=4)                    :: flag
    integer                             :: iang
    integer, dimension(3)               :: n_cube, j_wind, n_wind, f_wind
    integer                             :: n_dim_cube, err
    character :: y

    n_dim_cube = 3
    n_cube = (/3*nfilt, 2, nang  /)
    j_wind = (/1      , 1, 1     /)
    n_wind = (/3*nfilt, 2, 1     /)
    f_wind = (/0      , 0, iang-1/)

    if (flag == "rite") then
       err = srite_window(FILT_,n_dim_cube,n_cube,n_wind,f_wind,j_wind,4,F)
    else if (flag == "reed") then
       F = 0.
       err = sreed_window(FILT_,n_dim_cube,n_cube,n_wind,f_wind,j_wind,4,F)
    else
       call seperr("you have to specify the flag for data_io_data()")
    end if
    if (err /= 0) call seperr("data_io_data: error reading data")
  end subroutine
!#####################################################
end module