module hes_unfold_mod
  use base_codtrs_mod
  use base_struct_mod
  use base_ioutil_mod
  use sep
  implicit none


  type(axis)                         :: ax_modl, ay_modl, az_modl
  type(axis)                         :: ax_offd, ay_offd, az_offd
  real   , dimension(:,:,:), pointer :: hessin
  real   , dimension(:)    , pointer :: hessou
  integer                            :: cart(3), ndim(3)
  integer                            :: ix_modl, iy_modl, iz_modl
  integer                            :: ix_offd, iy_offd, iz_offd
  integer                            :: jx_offd, jy_offd, jz_offd
  integer                            :: index_x_modl, index_y_modl, index_z_modl
  integer                            :: index_x_offd, index_y_offd, index_z_offd
  integer                            :: nmodl, index_offd
  real                               :: x_offd, y_offd, z_offd

  integer, dimension(6)              :: n_cube, j_wind, n_wind, f_wind
  integer                            :: n_dim_cube, err
  integer, external                  :: sreed_window, srite_window
  character(len=256)                 :: hessin_, hessou_


contains


  integer function hes_unfold_init() result(stat)

  end function


  integer function hes_unfold_clos() result(stat)
  end function



  integer function hes_unfold_oper() result(stat)


    do iz_offd=1, hgrid%az_offd%n; z_offd = hgrid%az_offd%o+(iz_offd-1)*hgrid%az_offd%d; jz_offd = nint(z_offd/hgrid%az_modl%d)
    do iy_offd=1, hgrid%ay_offd%n; y_offd = hgrid%ay_offd%o+(iy_offd-1)*hgrid%ay_offd%d; jy_offd = nint(y_offd/hgrid%ay_modl%d)
    do ix_offd=1, hgrid%ax_offd%n; x_offd = hgrid%ax_offd%o+(ix_offd-1)*hgrid%ax_offd%d; jx_offd = nint(x_offd/hgrid%ax_modl%d)

       call convol_fileio(hess, hgrid, ix_offd, iy_offd, iz_offd, filetag, "reed")

       do iz_modl=1, hgrid%az_modl%n; index_z_modl = max(1, min(iz_modl-jz_offd,hgrid%az_modl%n))
       do iy_modl=1, hgrid%ay_modl%n; index_y_modl = max(1, min(iy_modl-jy_offd,hgrid%ay_modl%n))
       do ix_modl=1, hgrid%ax_modl%n; index_x_modl = max(1, min(ix_modl-jx_offd,hgrid%ax_modl%n))
          mtrx(index_modl,index_offd) = hess(ix_modl,iy_modl,iz_modl)*modl(index_x_modl,index_y_modl,index_z_modl)
       end do
       end do
       end do

    end do
    end do
    end do

  end function

end module
