program main
  use wei_global_defi_mod
  use wei_global_calc_mod
  use wei_shotmig_kernel_mod
  use wei_multi_ref_kernel_mod
  use wei_io_mod
  use wei_vel_mod
  use wei_ssf_mod
  use wei_int_mod
  use wei_cig_mod
  use wei_type_mod
  use wei_report_mod
  implicit none

  integer    :: ish, iwb, izb, stat
  integer    :: nws_bak, nzs_bak
  integer    :: indx_dept
  type(axis) :: aws
  type(axis) :: azs

  !!.. input history
  call wei_input_history()
  call wei_image_history()
  call wei_io_imag_init()
  call wei_radians_calc()
  call wei_block_calc()

  if (dgrid%hdr_rec) call wei_io_head_init("hrec")
  if (dgrid%hdr_sou) call wei_io_head_init("hsou")

  do ish=1, dgrid%nsh


     if (dgrid%hdr_rec) call wei_io_head_oper("hrec")
     if (dgrid%hdr_sou) call wei_io_head_oper("hsou")

     call wei_counter_init()
     call wei_global_calc(ish)

     call wei_grids_report(ish, dgrid%nsh)

     if(igrid%ax%n>1.and.ogrid%ax%n == 0) cycle
     if(igrid%ay%n>1.and.ogrid%ay%n == 0) cycle 

     nws_bak = wblock%ns
     nzs_bak = zblock%ns

     call wei_wfld_alloc(wblock%ns)
     call wei_modl_alloc(zblock%ns)
     call wei_memory_report()
    
     do iwb=1, wblock%nb

        aws%o = wblock%os(iwb); aws%d = wblock%ds; aws%n = wblock%blen(iwb)

        if (aws%n /= nws_bak) then
           call wei_wfld_dealloc()
           call wei_wfld_alloc(aws%n)
           nws_bak = aws%n
        end if

        call wei_io_crec(wblock%indx(iwb), aws%n, ish)
        call wei_io_csou(wblock%indx(iwb), aws%n)

        call wei_wfld_init(aws%n)
 
        do izb=1, zblock%nb

           azs%o = zblock%os(izb); azs%d = zblock%ds; azs%n = zblock%blen(izb)

           if (azs%n /= nzs_bak) then
              call wei_modl_dealloc()
              call wei_modl_alloc(azs%n)
              nzs_bak = azs%n
           end if

           call wei_io_velo(zblock%indx(izb), azs%n)
           call wei_io_imag("reed", zblock%indx(izb), azs%n)

           stat = wei_shotmig_kernel_init(aws_in = aws         , &
                                          azs_in = azs         , &
                                          VLin   = wei_vel_init, &
                                          FXin   = wei_ssf_init, &
                                          WIin   = wei_int_init, &
                                          EPin   = wei_multi_ref_kernel_init, &
                                          IGin   = wei_cig_init)

           stat = wei_shotmig_kernel_oper(VLop = wei_vel_oper, &
                                          FXop = wei_ssf_oper, &
                                          WIop = wei_int_oper, &
                                          EPop = wei_multi_ref_kernel_oper, &
                                          IGop = wei_cig_oper)

           stat = wei_shotmig_kernel_clos(VLnd = wei_vel_clos, &
                                          FXnd = wei_ssf_clos, &
                                          WInd = wei_int_clos, &
                                          EPnd = wei_multi_ref_kernel_clos, &
                                          IGnd = wei_cig_clos)

           call wei_io_imag("rite", zblock%indx(izb), azs%n)
        end do

     end do

     call wei_modl_dealloc()
     call wei_wfld_dealloc()

  end do

end program
