program main
  use sep
  use wem_imgcon_mod




  call auxin(crec_)
  call auxin(csou_)


  call hinaxis(crec_, dgrid%agx, 1)
  call hinaxis(crec_, dgrid%agy, 2)
  call hinaxis(crec_, dgrid%aw , 3)
  call hinaxis(crec_, dgrid%asx, 4)
  call hinaxis(crec_, dgrid%asy, 5)


  if (dgrid%comsou) then
     call hinaxis(csou_, agxcs, 1)
     call hinaxis(csou_, agycs, 2)
     call hinaxis(csou_, asxcs, 4)
     call hinaxis(csou_, asycs, 5)
     dgrid%ntrs = agxcs%n * agycs%n
     dgrid%nshs = asxcs%n * asycs%n
     if (dgrid%ntrs /= dgrid%ntrg) call seperr("ERROR: dgrid%ntrs /= dgrid%ntrg")
     if (dgrid%nshs /= dgrid%nshg) call seperr("ERROR: dgrid%nshs /= dgrid%nshg")
  else
     dgrid%ntrs = 1
     dgrid%nshs = 1
  end if

  if (dgrid%offset) then
     dgrid%gxmin_save = dgrid%agx%o
     dgrid%gymin_save = dgrid%agy%o
  end if


  do ish=1, nsh

     call wem_glocal(pgrid, ogrid, vgrid, dgrid, igrid, ish)

     do iwb=1, wblock%nb
        aws%o = wblock%os(iwb); aws%d = wblock%ds; aws%n = wblock%blen(iwb)
        call wem_fio_csou(csou, dgrid, wblock%indx(iwb), aws%n, ish, csou_, "reed")
        call wem_fio_crec(crec, dgrid, wblock%indx(iwb), aws%n, ish, crec_, "reed")
        call wem_wfs_init(wfds, csou, dgrid, pgrid)
        call wem_wfr_init(wfdr, crec, dgrid, pgrid)

        do iws=1, aws%n
           do ihy=1, nhy
           do ihx=1, nhx
           do imy=1, pgrid%ay%n
           do imx=1, pgrid%ax%n
              data(imx,imy,ihx,ihy,iws) = data(imx,imy,ihx,ihy,iws) + conjg()
           end do
           end do
           end do
           end do
        end do
     end do
  end do


end program
