module wem_fileio_mod
  use  sep
  use  base_struct_mod
  use  wem_struct_mod
  implicit none

  integer, external, private :: sreed_window, srite_window, getch

contains
!--------------------------------------------------------------------------------------
  subroutine wem_fio_hess_init(filetag, hgrid)
    character(len=*)                :: filetag
    type(hes_grid)                  :: hgrid
    real, allocatable, dimension(:) :: tmp
    integer                         :: ioffd, nmodl, noffd
    nmodl = hgrid%ax_modl%n*hgrid%ay_modl%n*hgrid%az_modl%n
    noffd = hgrid%ax_offd%n*hgrid%ay_offd%n*hgrid%az_offd%n
    allocate(tmp(nmodl))
    tmp = 0.
    do ioffd=1, noffd
       call sep_write(tmp, filetag, esize=4)
    end do
    deallocate(tmp)
    return
  end subroutine
!-------------------------------------------------------------------------------------
  subroutine wem_fio_imag_init(igrid, filetag)
    type(img_grid)                  :: igrid
    character(len=*)                :: filetag
    real, allocatable, dimension(:) :: tmp
    integer                         :: ioffs, nmodl, noffs
    nmodl = igrid%ax%n*igrid%ay%n*igrid%az%n
    noffs = igrid%ahx%n*igrid%ahy%n*igrid%ahz%n
    allocate(tmp(nmodl))
    tmp = 0.
    do ioffs=1, noffs
       call sep_write(tmp, filetag, esize=4)
    end do
    call sseek(filetag, 0, 0)
    deallocate(tmp)
    return
  end subroutine
!-------------------------------------------------------------------------------------
  subroutine wem_fio_crec_init(dgrid, filetag)
    type(dat_grid)                     :: dgrid
    character(len=*)                   :: filetag
    complex, allocatable, dimension(:) :: tmp
    integer                            :: ish
    allocate(tmp(dgrid%agx%n*dgrid%agy%n*dgrid%aw%n))
    tmp = 0.
    do ish=1, dgrid%nshg
       call sep_write(tmp, filetag, esize=8) 
    end do
    call sseek(filetag, 0, 0)
    deallocate(tmp)
    return
  end subroutine
!=====================================================================================
! read or write one depth block image
!-------------------------------------------------------------------------------------
  subroutine wem_fio_imag(modl, igrid, ogrid, indz, filetag, flag)
    real, dimension(:,:,:,:) :: modl
    type(img_grid)           :: igrid
    type(out_grid)           :: ogrid
    integer                  :: indz
    character(len=*)         :: filetag
    character(len=4)         :: flag
            
    integer, dimension(5)    :: n_cube, j_wind, n_wind, f_wind
    integer                  :: n_dim_cube, err

    n_dim_cube = 5
    n_cube = (/igrid%ahx%n, igrid%ahy%n, igrid%ax%n, igrid%ay%n, igrid%az%n /)
    j_wind = (/1, 1, 1, 1, 1/)
    n_wind = (/ogrid%ahx%n, ogrid%ahy%n, ogrid%ax%n, ogrid%ay%n, 1/)
    f_wind = (/0,0,ogrid%index_outimg_xmin-1,ogrid%index_outimg_ymin-1,indz-1/)

    if (flag == "rite") then
       err = srite_window(filetag,n_dim_cube,n_cube,n_wind,f_wind,j_wind,4,modl)
       if (err .lt. 0) call seperr("wem_io_imag: trouble writing image file")
    else if(flag == "reed") then
       modl = 0.
       err = sreed_window(filetag,n_dim_cube,n_cube,n_wind,f_wind,j_wind,4,modl)
        if (err .lt. 0) call seperr("wem_io_imag: trouble reading image file")
     else
        call seperr("you have to specify the flag for wem_io_imag()")
     end if
     return
   end subroutine

!=============================================================================
! read one frequency block receiver wavefield
!-----------------------------------------------------------------------------
   subroutine wem_fio_crec(crec, dgrid, indw, nws, ish, filetag, flag)
     complex, dimension(:,:) :: crec
     type(dat_grid)          :: dgrid 
     integer                 :: indw, nws, ish
     character(len=*)        :: filetag
     character(len=4)        :: flag
     integer, dimension(3)   :: n_cube, j_wind, n_wind, f_wind
     integer                 :: n_dim_cube, err  

     n_dim_cube = 3
     n_cube = (/dgrid%ntrg, dgrid%aw%n, dgrid%nshg/)
     j_wind = (/1, 1, 1/)
     n_wind = (/dgrid%ntrg, nws, 1/)
     f_wind = (/0, indw-1, ish-1/)

     if (flag == "rite") then
        err = srite_window(filetag,n_dim_cube,n_cube,n_wind,f_wind,j_wind,8,crec)
        if (err.lt.0) call seperr("wem_fio_crec: trouble writing receiver wavefield")
     else if(flag == "reed") then
        crec = 0.
        err = sreed_window(filetag,n_dim_cube,n_cube,n_wind,f_wind,j_wind,8,crec)
        if (err.lt.0) call seperr("wem_fio_crec: trouble reading receiver wavefield")
     else
        call seperr("you have to specify the flag for wem_fio_crec()")
     end if
     return
   end subroutine

!=============================================================================
! read one frequency block receiver wavefield
!-----------------------------------------------------------------------------
   subroutine wem_fio_csou(csou, dgrid, indw, nws, ish, filetag, flag)
     complex, dimension(:,:) :: csou
     type(dat_grid)          :: dgrid
     integer                 :: indw, nws, ish
     character(len=*)        :: filetag
     character(len=4)        :: flag
     integer, dimension(3)   :: n_cube, j_wind, n_wind, f_wind
     integer                 :: n_dim_cube, err

     n_dim_cube = 3
     n_cube = (/dgrid%ntrs, dgrid%aw%n, dgrid%nshs/)
     j_wind = (/1, 1, 1/)
     n_wind = (/dgrid%ntrs, nws, 1/)
     if (dgrid%comsou) then 
        f_wind = (/0, indw-1, ish-1/)
     else
        f_wind = (/0, indw-1, 0/)
     end if   
     if (flag == "rite") then
        err = srite_window(filetag,n_dim_cube,n_cube,n_wind,f_wind,j_wind,8,csou)
        if (err.lt.0) call seperr("wem_fio_csou: trouble writing receiver wavefield")
     else if(flag == "reed") then
        csou = 0.
        err = sreed_window(filetag,n_dim_cube,n_cube,n_wind,f_wind,j_wind,8,csou)
        if (err.lt.0) call seperr("wem_fio_csou: trouble reading receiver wavefield")
     else
        call seperr("you have to specify the flag for wem_fio_csou()")
     end if
     return
   end subroutine

  subroutine wem_fio_velo(vmod, vgrid, pgrid, indz, nzs, filetag, flag)
    real, dimension(:,:,:) :: vmod
    type(vel_grid)         :: vgrid
    type(pro_grid)         :: pgrid
    integer                :: indz
    integer                :: nzs
    character(len=*)       :: filetag
    character(len=4)       :: flag
    integer, dimension(3)  :: n_cube, j_wind, n_wind, f_wind
    integer                :: n_dim_cube, err

    n_dim_cube = 3
    n_cube = (/vgrid%ax%n, vgrid%ay%n, vgrid%az%n /)
    j_wind = (/1, 1, 1/)
    n_wind = (/pgrid%ax%n, pgrid%ay%n, nzs/)
    f_wind = (/vgrid%index_provel_xmin-1, vgrid%index_provel_ymin-1, indz-1/)
    if (flag == "rite") then
       err = srite_window(filetag,n_dim_cube,n_cube,n_wind,f_wind,j_wind,4,vmod)
       vmod = 0.
    else if (flag == "reed") then
       vmod = 0.
       err = sreed_window(filetag,n_dim_cube,n_cube,n_wind,f_wind,j_wind,4,vmod)
    end if
    return
  end subroutine



end module
