# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "<stdin>"
module srm_io_mod
  use sep3d_struct_mod
  use srm_mod
  use off2ang_mod
  use temp_mod
  implicit none
  logical, private ,save :: write_gathers
  integer, private, external :: sep_num_thread,sep_thread_num
  contains
  logical function init_srm_io(in,intag,out,outtag,gathers,gathertag&
    &,writegathers)
    type(sep3d) :: in,out,gathers
    character(len=*) :: intag,outtag,gathertag
    integer, external :: getch
    integer :: ndim,ngam,nang(2)
    real :: ogam,dgam,oang(2),dang(2)
    logical :: writegathers,master_data
    init_srm_io=.false.
    call from_param("master_data",master_data,.true.)
    ndim=sep3d_ndims(in)
!if(ndim==3) call sep3d_reshape(in,(/1,2,2,3,3/)) !2-D
    if (ndim.eq.4) then
      call sep3d_reshape(in,(/1,2,3,4,4/)) !CAM
    else if (ndim.ne.5) then
      write(0,*) "only know how to deal with 4 and 5 dimensional&
        & input"
      return
    end if
    if (in%n(2).ne.1 .and. mod(in%n(2),2).ne.0) then
      write(0,*) "offsetx axis must be even"
      return
    end if
    if (in%n(3).ne.1 .and. mod(in%n(3),2).ne.0) then
      write(0,*) "offsety axis must be even"
      return
    end if
    call sep3d_set_sep3d(in)
    if (.not. init_srm_params(ngam,ogam,dgam)) then
      write(0,*) "trouble grabbing srm parameters"
      return
    end if
    if (.not. init_off2ang_params(in,nang,oang,dang)) then
      write(0,*) "trouble grabbing srm parameters"
      return
    end if
    call init_sep3d(out,"OUTPUT","FLOAT","REGULAR",(/in%n(1),in%n(4)&
      &,in%n(5),ngam/),(/in%o(1),in%o(4),in%o(5),ogam/),(/in%d(1),in%d&
      &(4),in%d(5),dgam/))
    if (sep_num_thread()>1) then
      if (.not.sep3d_section_tag(out,4,"BLOCK",nsect=sep_num_thread()&
        &,master_data=master_data)) then
        call seperr("trouble sectioning tag")
      end if
    end if
!if(sep_thread_num()==0)
    call sep3d_write_description(outtag,out)
    write_gathers=.false.
    if (1.eq.getch(gathertag,"d",ndim)) then
      write_gathers=.true.
      call init_sep3d(gathers,"OUTPUT","FLOAT","REGULAR",(/in%n(1),nang&
        &(2),in%n(3),in%n(4),in%n(5),ngam/),(/in%o(1),oang(2),in%o(3&
        &),in%o(4),in%o(5),ogam/),(/in%d(1),dang(2),in%d(3),in%d(4),in%d(5),dgam/))
      if (sep_num_thread()>1) then
        if (.not.sep3d_section_tag(gathers,6,"BLOCK",nsect=sep_num_thread&
          &(),master_data=master_data)) then
          call seperr("trouble gather tag")
        end if
      end if
      call sep3d_write_description(gathertag,gathers)
    end if
! call init_sep3d(temp1,"OUTPUT","COMPLEX","REGULAR",
! (/in%n(1),nang(2),in%n(3),in%n(4),in%n(5),ngam/),
! (/in%o(1),oang(2),in%o(3),in%o(4),in%o(5),ogam/),
! (/in%d(1),dang(2),in%d(3),in%d(4),in%d(5),dgam/))
! if(.not.sep3d_section_tag(temp1,6,"BLOCK",nsect=sep_num_thread(),
! master_data=master_data))
! call seperr("trouble gather tag")
! call sep3d_write_description("temp1.H",temp1)
! call init_sep3d(temp2,"OUTPUT","COMPLEX","REGULAR",
! (/in%n(1),nang(2),in%n(3),in%n(4),in%n(5),ngam/),
! (/in%o(1),oang(2),in%o(3),in%o(4),in%o(5),ogam/),
! (/in%d(1),dang(2),in%d(3),in%d(4),in%d(5),dgam/))
! if(.not.sep3d_section_tag(temp2,6,"BLOCK",nsect=sep_num_thread(),
! master_data=master_data))
! call seperr("trouble gather tag")
! call sep3d_write_description("temp2.H",temp2)
!
!
! call init_sep3d(temp3,"OUTPUT","COMPLEX","REGULAR",
! (/in%n(1),nang(2),in%n(3),in%n(4),in%n(5),ngam/),
! (/in%o(1),oang(2),in%o(3),in%o(4),in%o(5),ogam/),
! (/in%d(1),dang(2),in%d(3),in%d(4),in%d(5),dgam/))
! if(.not.sep3d_section_tag(temp3,6,"BLOCK",nsect=sep_num_thread(),
! master_data=master_data))
! call seperr("trouble gather tag")
! call sep3d_write_description("temp3.H",temp3)
!
! call init_sep3d(temp4,"OUTPUT","COMPLEX","REGULAR",
! (/in%n(1),nang(2),in%n(3),in%n(4),in%n(5),ngam/),
! (/in%o(1),oang(2),in%o(3),in%o(4),in%o(5),ogam/),
! (/in%d(1),dang(2),in%d(3),in%d(4),in%d(5),dgam/))
! if(.not.sep3d_section_tag(temp4,6,"BLOCK",nsect=sep_num_thread(),
! master_data=master_data))
! call seperr("trouble gather tag")
! call sep3d_write_description("temp4.H",temp4)
!
    writegathers= write_gathers
    init_srm_io=.true.
  end function
end module
