module mva_fileio_mod
  implicit none

contains

  subroutine mva_fio_wfld()
    real, dimension(:,:,:,:,:) :: modl
    type(img_grid)             :: igrid
    type(out_grid)             :: ogrid
    integer                    :: indz
    integer                    :: nzs
    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%ax%n, igrid%ay%n, igrid%ahx%n, igrid%ahy%n, igrid%az%n /)
    j_wind = (/1, 1, 1, 1, 1/)
    n_wind = (/ogrid%ax%n, ogrid%ay%n, ogrid%ahx%n, ogrid%ahy%n, nzs/)
    f_wind = (/ogrid%index_outimg_xmin-1, ogrid%index_outimg_ymin-1, 0, 0, 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
end module
